traefik/vendor/github.com/xenolf/lego/providers/dns/vultr/vultr.go

192 lines
5.1 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
// Package vultr implements a DNS provider for solving the DNS-01 challenge using
// the vultr DNS.
// See https://www.vultr.com/api/#dns
package vultr
import (
2018-09-17 13:16:03 +00:00
"crypto/tls"
"errors"
2017-02-07 21:33:23 +00:00
"fmt"
2018-09-17 13:16:03 +00:00
"net/http"
2017-02-07 21:33:23 +00:00
"strings"
2018-09-17 13:16:03 +00:00
"time"
2017-02-07 21:33:23 +00:00
vultr "github.com/JamesClonk/vultr/lib"
2018-05-31 07:30:04 +00:00
"github.com/xenolf/lego/acme"
2018-07-03 10:44:04 +00:00
"github.com/xenolf/lego/platform/config/env"
2017-02-07 21:33:23 +00:00
)
2018-09-17 13:16:03 +00:00
// Config is used to configure the creation of the DNSProvider
type Config struct {
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{
TTL: env.GetOrDefaultInt("VULTR_TTL", 120),
PropagationTimeout: env.GetOrDefaultSecond("VULTR_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond("VULTR_POLLING_INTERVAL", acme.DefaultPollingInterval),
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond("VULTR_HTTP_TIMEOUT", 0),
// from Vultr Client
Transport: &http.Transport{
TLSNextProto: make(map[string]func(string, *tls.Conn) http.RoundTripper),
},
},
}
}
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
type DNSProvider struct {
2018-09-17 13:16:03 +00:00
config *Config
2017-02-07 21:33:23 +00:00
client *vultr.Client
}
// NewDNSProvider returns a DNSProvider instance with a configured Vultr client.
// Authentication uses the VULTR_API_KEY environment variable.
func NewDNSProvider() (*DNSProvider, error) {
2018-07-03 10:44:04 +00:00
values, err := env.Get("VULTR_API_KEY")
if err != nil {
2018-09-17 13:16:03 +00:00
return nil, fmt.Errorf("vultr: %v", err)
2018-07-03 10:44:04 +00:00
}
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
config.APIKey = values["VULTR_API_KEY"]
return NewDNSProviderConfig(config)
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
// NewDNSProviderCredentials uses the supplied credentials
// to return a DNSProvider instance configured for Vultr.
// Deprecated
2017-02-07 21:33:23 +00:00
func NewDNSProviderCredentials(apiKey string) (*DNSProvider, error) {
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
config.APIKey = apiKey
return NewDNSProviderConfig(config)
}
// NewDNSProviderConfig return a DNSProvider instance configured for Vultr.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("vultr: the configuration of the DNS provider is nil")
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
if config.APIKey == "" {
return nil, fmt.Errorf("vultr: credentials missing")
}
options := &vultr.Options{
HTTPClient: config.HTTPClient,
UserAgent: acme.UserAgent,
}
client := vultr.NewClient(config.APIKey, options)
return &DNSProvider{client: client, config: config}, nil
2017-02-07 21:33:23 +00:00
}
// Present creates a TXT record to fulfil the DNS-01 challenge.
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
2018-09-17 13:16:03 +00:00
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
2017-02-07 21:33:23 +00:00
2018-07-03 10:44:04 +00:00
zoneDomain, err := d.getHostedZone(domain)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("vultr: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-07-03 10:44:04 +00:00
name := d.extractRecordName(fqdn, zoneDomain)
2017-02-07 21:33:23 +00:00
2018-09-17 13:16:03 +00:00
err = d.client.CreateDNSRecord(zoneDomain, name, "TXT", `"`+value+`"`, 0, d.config.TTL)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("vultr: API call failed: %v", err)
2017-02-07 21:33:23 +00:00
}
return nil
}
// 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 {
2018-05-31 07:30:04 +00:00
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
2017-02-07 21:33:23 +00:00
2018-07-03 10:44:04 +00:00
zoneDomain, records, err := d.findTxtRecords(domain, fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("vultr: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
var allErr []string
2017-02-07 21:33:23 +00:00
for _, rec := range records {
2018-07-03 10:44:04 +00:00
err := d.client.DeleteDNSRecord(zoneDomain, rec.RecordID)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
allErr = append(allErr, err.Error())
2017-02-07 21:33:23 +00:00
}
}
2018-09-17 13:16:03 +00:00
if len(allErr) > 0 {
return errors.New(strings.Join(allErr, ": "))
}
2017-02-07 21:33:23 +00:00
return nil
}
2018-09-17 13:16:03 +00:00
// 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
}
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) getHostedZone(domain string) (string, error) {
domains, err := d.client.GetDNSDomains()
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return "", fmt.Errorf("API call failed: %v", err)
2017-02-07 21:33:23 +00:00
}
var hostedDomain vultr.DNSDomain
2018-07-03 10:44:04 +00:00
for _, dom := range domains {
if strings.HasSuffix(domain, dom.Domain) {
if len(dom.Domain) > len(hostedDomain.Domain) {
hostedDomain = dom
2017-02-07 21:33:23 +00:00
}
}
}
if hostedDomain.Domain == "" {
2018-09-17 13:16:03 +00:00
return "", fmt.Errorf("no matching Vultr domain found for domain %s", domain)
2017-02-07 21:33:23 +00:00
}
return hostedDomain.Domain, nil
}
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) findTxtRecords(domain, fqdn string) (string, []vultr.DNSRecord, error) {
zoneDomain, err := d.getHostedZone(domain)
2017-02-07 21:33:23 +00:00
if err != nil {
return "", nil, err
}
var records []vultr.DNSRecord
2018-07-03 10:44:04 +00:00
result, err := d.client.GetDNSRecords(zoneDomain)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return "", records, fmt.Errorf("API call has failed: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-07-03 10:44:04 +00:00
recordName := d.extractRecordName(fqdn, zoneDomain)
2017-02-07 21:33:23 +00:00
for _, record := range result {
if record.Type == "TXT" && record.Name == recordName {
records = append(records, record)
}
}
return zoneDomain, records, nil
}
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) extractRecordName(fqdn, domain string) string {
2018-05-31 07:30:04 +00:00
name := acme.UnFqdn(fqdn)
2017-02-07 21:33:23 +00:00
if idx := strings.Index(name, "."+domain); idx != -1 {
return name[:idx]
}
return name
}