2018-12-06 23:50:17 +02:00
|
|
|
// Package rackspace implements a DNS provider for solving the DNS-01 challenge using rackspace DNS.
|
2019-03-11 18:56:48 +02:00
|
|
|
package rackspace
|
2016-11-04 11:29:14 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-09-15 19:07:24 +02:00
|
|
|
"errors"
|
2016-11-04 11:29:14 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2016-11-04 11:29:14 +02:00
|
|
|
)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// defaultBaseURL represents the Identity API endpoint to call.
|
2018-09-15 19:07:24 +02:00
|
|
|
const defaultBaseURL = "https://identity.api.rackspacecloud.com/v2.0/tokens"
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "RACKSPACE_"
|
|
|
|
|
|
|
|
EnvUser = envNamespace + "USER"
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
type Config struct {
|
|
|
|
BaseURL string
|
|
|
|
APIUser string
|
|
|
|
APIKey string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
BaseURL: defaultBaseURL,
|
2020-03-12 00:51:10 +02:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, 300),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2018-09-15 19:07:24 +02:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-12 00:51:10 +02:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
2018-09-15 19:07:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2016-11-04 11:29:14 +02:00
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2016-11-04 11:29:14 +02:00
|
|
|
type DNSProvider struct {
|
2018-09-15 19:07:24 +02:00
|
|
|
config *Config
|
2016-11-04 11:29:14 +02:00
|
|
|
token string
|
|
|
|
cloudDNSEndpoint string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Rackspace.
|
2018-09-15 19:07:24 +02:00
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// RACKSPACE_USER and RACKSPACE_API_KEY.
|
2016-11-04 11:29:14 +02:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-12 00:51:10 +02:00
|
|
|
values, err := env.Get(EnvUser, EnvAPIKey)
|
2018-06-11 17:32:50 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("rackspace: %w", err)
|
2018-06-11 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
2020-03-12 00:51:10 +02:00
|
|
|
config.APIUser = values[EnvUser]
|
|
|
|
config.APIKey = values[EnvAPIKey]
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-11-04 11:29:14 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Rackspace.
|
|
|
|
// It authenticates against the API, also grabbing the DNS Endpoint.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("rackspace: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.APIUser == "" || config.APIKey == "" {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("rackspace: credentials missing")
|
2016-11-04 11:29:14 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
identity, err := login(config)
|
2016-11-04 11:29:14 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("rackspace: %w", err)
|
2016-11-04 11:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through the Service Catalog to get the DNS Endpoint
|
|
|
|
var dnsEndpoint string
|
2018-09-15 19:07:24 +02:00
|
|
|
for _, service := range identity.Access.ServiceCatalog {
|
2016-11-04 11:29:14 +02:00
|
|
|
if service.Name == "cloudDNS" {
|
|
|
|
dnsEndpoint = service.Endpoints[0].PublicURL
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 19:29:18 +02:00
|
|
|
|
2016-11-04 11:29:14 +02:00
|
|
|
if dnsEndpoint == "" {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("rackspace: failed to populate DNS endpoint, check Rackspace API for changes")
|
2016-11-04 11:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{
|
2018-09-15 19:07:24 +02:00
|
|
|
config: config,
|
|
|
|
token: identity.Access.Token.ID,
|
2016-11-04 11:29:14 +02:00
|
|
|
cloudDNSEndpoint: dnsEndpoint,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-12-06 23:50:17 +02:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2018-10-12 19:29:18 +02:00
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
zoneID, err := d.getHostedZoneID(fqdn)
|
2016-11-04 11:29:14 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("rackspace: %w", err)
|
2016-11-04 11:29:14 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 19:53:04 +02:00
|
|
|
rec := Records{
|
|
|
|
Record: []Record{{
|
2018-12-06 23:50:17 +02:00
|
|
|
Name: dns01.UnFqdn(fqdn),
|
2016-11-04 11:29:14 +02:00
|
|
|
Type: "TXT",
|
|
|
|
Data: value,
|
2018-09-15 19:07:24 +02:00
|
|
|
TTL: d.config.TTL,
|
2016-11-04 11:29:14 +02:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(rec)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("rackspace: %w", err)
|
2016-11-04 11:29:14 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:11:07 +02:00
|
|
|
_, err = d.makeRequest(http.MethodPost, fmt.Sprintf("/domains/%s/records", zoneID), bytes.NewReader(body))
|
2018-09-15 19:07:24 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("rackspace: %w", err)
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
|
|
|
return nil
|
2016-11-04 11:29:14 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2018-12-06 23:50:17 +02:00
|
|
|
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
2018-10-12 19:29:18 +02:00
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
zoneID, err := d.getHostedZoneID(fqdn)
|
2016-11-04 11:29:14 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("rackspace: %w", err)
|
2016-11-04 11:29:14 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
record, err := d.findTxtRecord(fqdn, zoneID)
|
2016-11-04 11:29:14 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("rackspace: %w", err)
|
2016-11-04 11:29:14 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:11:07 +02:00
|
|
|
_, err = d.makeRequest(http.MethodDelete, fmt.Sprintf("/domains/%s/records?id=%s", zoneID, record.ID), nil)
|
2018-09-15 19:07:24 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("rackspace: %w", err)
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Adjusting here to cope with spikes in propagation times.
|
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2016-11-04 11:29:14 +02:00
|
|
|
}
|