2019-07-17 21:01:50 +02:00
|
|
|
// Package vultr implements a DNS provider for solving the DNS-01 challenge using the Vultr DNS.
|
2016-03-21 01:23:37 +02:00
|
|
|
// See https://www.vultr.com/api/#dns
|
2019-03-11 18:56:48 +02:00
|
|
|
package vultr
|
2016-03-21 01:23:37 +02:00
|
|
|
|
|
|
|
import (
|
2019-07-17 21:01:50 +02:00
|
|
|
"context"
|
2018-09-15 19:07:24 +02:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2016-03-21 01:23:37 +02:00
|
|
|
"fmt"
|
2018-09-15 19:07:24 +02:00
|
|
|
"net/http"
|
2019-07-17 21:01:50 +02:00
|
|
|
"strconv"
|
2016-03-21 01:23:37 +02:00
|
|
|
"strings"
|
2018-09-15 19:07:24 +02:00
|
|
|
"time"
|
2016-03-21 01:23:37 +02:00
|
|
|
|
2019-03-11 18:56:48 +02:00
|
|
|
"github.com/go-acme/lego/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/platform/config/env"
|
2019-07-17 21:01:50 +02:00
|
|
|
"github.com/vultr/govultr"
|
2016-03-21 01:23:37 +02:00
|
|
|
)
|
|
|
|
|
2018-09-15 19:07:24 +02: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{
|
2018-12-06 23:50:17 +02:00
|
|
|
TTL: env.GetOrDefaultInt("VULTR_TTL", dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("VULTR_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("VULTR_POLLING_INTERVAL", dns01.DefaultPollingInterval),
|
2018-09-15 19:07:24 +02:00
|
|
|
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),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 01:23:37 +02:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
|
|
|
|
type DNSProvider struct {
|
2018-09-15 19:07:24 +02:00
|
|
|
config *Config
|
2019-07-17 21:01:50 +02:00
|
|
|
client *govultr.Client
|
2016-03-21 01:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance with a configured Vultr client.
|
|
|
|
// Authentication uses the VULTR_API_KEY environment variable.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-06-11 17:32:50 +02:00
|
|
|
values, err := env.Get("VULTR_API_KEY")
|
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, fmt.Errorf("vultr: %v", err)
|
2018-06-11 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = values["VULTR_API_KEY"]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-03-21 01:23:37 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// 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")
|
2016-03-21 01:23:37 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
if config.APIKey == "" {
|
|
|
|
return nil, fmt.Errorf("vultr: credentials missing")
|
|
|
|
}
|
|
|
|
|
2019-07-17 21:01:50 +02:00
|
|
|
client := govultr.NewClient(config.HTTPClient, config.APIKey)
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
return &DNSProvider{client: client, config: config}, nil
|
2016-03-21 01:23:37 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 21:07:20 +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 {
|
2019-07-22 23:29:32 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2016-03-21 01:23:37 +02:00
|
|
|
|
2019-07-22 23:29:32 +02:00
|
|
|
zoneDomain, err := d.getHostedZone(ctx, domain)
|
2016-03-21 01:23:37 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("vultr: %v", err)
|
2016-03-21 01:23:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
name := d.extractRecordName(fqdn, zoneDomain)
|
2016-03-21 01:23:37 +02:00
|
|
|
|
2019-07-22 23:29:32 +02:00
|
|
|
err = d.client.DNSRecord.Create(ctx, zoneDomain, "TXT", name, value, d.config.TTL, 0)
|
2016-03-21 01:23:37 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("vultr: API call failed: %v", err)
|
2016-03-21 01:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-07-22 23:29:32 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
2016-03-21 01:23:37 +02:00
|
|
|
|
2019-07-22 23:29:32 +02:00
|
|
|
zoneDomain, records, err := d.findTxtRecords(ctx, domain, fqdn)
|
2016-03-21 01:23:37 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("vultr: %v", err)
|
2016-03-21 01:23:37 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
var allErr []string
|
2016-03-21 01:23:37 +02:00
|
|
|
for _, rec := range records {
|
2019-07-22 23:29:32 +02:00
|
|
|
err := d.client.DNSRecord.Delete(ctx, zoneDomain, strconv.Itoa(rec.RecordID))
|
2016-03-21 01:23:37 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
allErr = append(allErr, err.Error())
|
2016-03-21 01:23:37 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
if len(allErr) > 0 {
|
|
|
|
return errors.New(strings.Join(allErr, ": "))
|
|
|
|
}
|
|
|
|
|
2016-03-21 01:23:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02: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
|
|
|
|
}
|
|
|
|
|
2019-07-22 23:29:32 +02:00
|
|
|
func (d *DNSProvider) getHostedZone(ctx context.Context, domain string) (string, error) {
|
|
|
|
domains, err := d.client.DNSDomain.List(ctx)
|
2016-03-21 01:23:37 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return "", fmt.Errorf("API call failed: %v", err)
|
2016-03-21 01:23:37 +02:00
|
|
|
}
|
|
|
|
|
2019-07-17 21:01:50 +02:00
|
|
|
var hostedDomain govultr.DNSDomain
|
2018-06-21 19:06:16 +02:00
|
|
|
for _, dom := range domains {
|
|
|
|
if strings.HasSuffix(domain, dom.Domain) {
|
|
|
|
if len(dom.Domain) > len(hostedDomain.Domain) {
|
|
|
|
hostedDomain = dom
|
2016-03-21 01:23:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hostedDomain.Domain == "" {
|
2018-09-15 19:07:24 +02:00
|
|
|
return "", fmt.Errorf("no matching Vultr domain found for domain %s", domain)
|
2016-03-21 01:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return hostedDomain.Domain, nil
|
|
|
|
}
|
|
|
|
|
2019-07-22 23:29:32 +02:00
|
|
|
func (d *DNSProvider) findTxtRecords(ctx context.Context, domain, fqdn string) (string, []govultr.DNSRecord, error) {
|
|
|
|
zoneDomain, err := d.getHostedZone(ctx, domain)
|
2016-03-21 01:23:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-17 21:01:50 +02:00
|
|
|
var records []govultr.DNSRecord
|
2019-07-22 23:29:32 +02:00
|
|
|
result, err := d.client.DNSRecord.List(ctx, zoneDomain)
|
2016-03-21 01:23:37 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return "", records, fmt.Errorf("API call has failed: %v", err)
|
2016-03-21 01:23:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
recordName := d.extractRecordName(fqdn, zoneDomain)
|
2016-03-21 01:23:37 +02:00
|
|
|
for _, record := range result {
|
|
|
|
if record.Type == "TXT" && record.Name == recordName {
|
|
|
|
records = append(records, record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return zoneDomain, records, nil
|
|
|
|
}
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) extractRecordName(fqdn, domain string) string {
|
2018-12-06 23:50:17 +02:00
|
|
|
name := dns01.UnFqdn(fqdn)
|
2016-03-21 01:23:37 +02:00
|
|
|
if idx := strings.Index(name, "."+domain); idx != -1 {
|
|
|
|
return name[:idx]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|