traefik/vendor/github.com/go-acme/lego/providers/dns/rackspace/rackspace.go

160 lines
4.5 KiB
Go
Raw Normal View History

2019-01-07 17:30:06 +00:00
// Package rackspace implements a DNS provider for solving the DNS-01 challenge using rackspace DNS.
2017-02-07 21:33:23 +00:00
package rackspace
import (
"bytes"
"encoding/json"
2018-09-17 13:16:03 +00:00
"errors"
2017-02-07 21:33:23 +00:00
"fmt"
"net/http"
"time"
2019-03-14 10:04:04 +00:00
"github.com/go-acme/lego/challenge/dns01"
"github.com/go-acme/lego/platform/config/env"
2017-02-07 21:33:23 +00:00
)
2018-09-17 13:16:03 +00:00
// defaultBaseURL represents the Identity API endpoint to call
const defaultBaseURL = "https://identity.api.rackspacecloud.com/v2.0/tokens"
// Config is used to configure the creation of the DNSProvider
type Config struct {
BaseURL string
APIUser string
APIKey string
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int
HTTPClient *http.Client
}
// NewDefaultConfig returns a default configuration for the DNSProvider
func NewDefaultConfig() *Config {
return &Config{
BaseURL: defaultBaseURL,
TTL: env.GetOrDefaultInt("RACKSPACE_TTL", 300),
2019-01-07 17:30:06 +00:00
PropagationTimeout: env.GetOrDefaultSecond("RACKSPACE_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond("RACKSPACE_POLLING_INTERVAL", dns01.DefaultPollingInterval),
2018-09-17 13:16:03 +00:00
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond("RACKSPACE_HTTP_TIMEOUT", 30*time.Second),
},
}
}
2017-02-07 21:33:23 +00:00
2018-05-31 07:30:04 +00:00
// DNSProvider is an implementation of the acme.ChallengeProvider interface
2017-02-07 21:33:23 +00:00
// used to store the reusable token and DNS API endpoint
type DNSProvider struct {
2018-09-17 13:16:03 +00:00
config *Config
2017-02-07 21:33:23 +00:00
token string
cloudDNSEndpoint string
}
// NewDNSProvider returns a DNSProvider instance configured for Rackspace.
2018-09-17 13:16:03 +00:00
// Credentials must be passed in the environment variables:
// RACKSPACE_USER and RACKSPACE_API_KEY.
2017-02-07 21:33:23 +00:00
func NewDNSProvider() (*DNSProvider, error) {
2018-07-03 10:44:04 +00:00
values, err := env.Get("RACKSPACE_USER", "RACKSPACE_API_KEY")
if err != nil {
2018-09-17 13:16:03 +00:00
return nil, fmt.Errorf("rackspace: %v", err)
2018-07-03 10:44:04 +00:00
}
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
config.APIUser = values["RACKSPACE_USER"]
config.APIKey = values["RACKSPACE_API_KEY"]
return NewDNSProviderConfig(config)
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00: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 == "" {
return nil, fmt.Errorf("rackspace: credentials missing")
2017-02-07 21:33:23 +00:00
}
2018-10-15 07:40:02 +00:00
identity, err := login(config)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return nil, fmt.Errorf("rackspace: %v", err)
2017-02-07 21:33:23 +00:00
}
// Iterate through the Service Catalog to get the DNS Endpoint
var dnsEndpoint string
2018-09-17 13:16:03 +00:00
for _, service := range identity.Access.ServiceCatalog {
2017-02-07 21:33:23 +00:00
if service.Name == "cloudDNS" {
dnsEndpoint = service.Endpoints[0].PublicURL
break
}
}
2018-10-15 07:40:02 +00:00
2017-02-07 21:33:23 +00:00
if dnsEndpoint == "" {
2018-09-17 13:16:03 +00:00
return nil, fmt.Errorf("rackspace: failed to populate DNS endpoint, check Rackspace API for changes")
2017-02-07 21:33:23 +00:00
}
return &DNSProvider{
2018-09-17 13:16:03 +00:00
config: config,
token: identity.Access.Token.ID,
2017-02-07 21:33:23 +00:00
cloudDNSEndpoint: dnsEndpoint,
}, nil
2018-09-17 13:16:03 +00:00
2017-02-07 21:33:23 +00:00
}
2018-10-10 14:28:04 +00:00
// Present creates a TXT record to fulfill the dns-01 challenge
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
2019-01-07 17:30:06 +00:00
fqdn, value := dns01.GetRecord(domain, keyAuth)
2018-10-15 07:40:02 +00:00
2018-07-03 10:44:04 +00:00
zoneID, err := d.getHostedZoneID(fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("rackspace: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-05-31 07:30:04 +00:00
rec := Records{
Record: []Record{{
2019-01-07 17:30:06 +00:00
Name: dns01.UnFqdn(fqdn),
2017-02-07 21:33:23 +00:00
Type: "TXT",
Data: value,
2018-09-17 13:16:03 +00:00
TTL: d.config.TTL,
2017-02-07 21:33:23 +00:00
}},
}
body, err := json.Marshal(rec)
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("rackspace: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-07-03 10:44:04 +00:00
_, err = d.makeRequest(http.MethodPost, fmt.Sprintf("/domains/%d/records", zoneID), bytes.NewReader(body))
2018-09-17 13:16:03 +00:00
if err != nil {
return fmt.Errorf("rackspace: %v", err)
}
return nil
2017-02-07 21:33:23 +00:00
}
// CleanUp removes the TXT record matching the specified parameters
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
2019-01-07 17:30:06 +00:00
fqdn, _ := dns01.GetRecord(domain, keyAuth)
2018-10-15 07:40:02 +00:00
2018-07-03 10:44:04 +00:00
zoneID, err := d.getHostedZoneID(fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("rackspace: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-07-03 10:44:04 +00:00
record, err := d.findTxtRecord(fqdn, zoneID)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("rackspace: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-07-03 10:44:04 +00:00
_, err = d.makeRequest(http.MethodDelete, fmt.Sprintf("/domains/%d/records?id=%s", zoneID, record.ID), nil)
2018-09-17 13:16:03 +00:00
if err != nil {
return fmt.Errorf("rackspace: %v", err)
}
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
2017-02-07 21:33:23 +00:00
}