2019-07-17 19:01:50 +00:00
|
|
|
// Package vultr implements a DNS provider for solving the DNS-01 challenge using the Vultr DNS.
|
2016-03-20 23:23:37 +00:00
|
|
|
// See https://www.vultr.com/api/#dns
|
2019-03-11 16:56:48 +00:00
|
|
|
package vultr
|
2016-03-20 23:23:37 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-17 19:01:50 +00:00
|
|
|
"context"
|
2018-09-15 17:07:24 +00:00
|
|
|
"errors"
|
2016-03-20 23:23:37 +00:00
|
|
|
"fmt"
|
2018-09-15 17:07:24 +00:00
|
|
|
"net/http"
|
2016-03-20 23:23:37 +00:00
|
|
|
"strings"
|
2018-09-15 17:07:24 +00:00
|
|
|
"time"
|
2016-03-20 23:23:37 +00:00
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2020-11-28 13:53:02 +00:00
|
|
|
"github.com/vultr/govultr/v2"
|
|
|
|
"golang.org/x/oauth2"
|
2016-03-20 23:23:37 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "VULTR_"
|
|
|
|
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-15 17:07:24 +00:00
|
|
|
type Config struct {
|
|
|
|
APIKey string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
2020-11-28 13:53:02 +00:00
|
|
|
HTTPTimeout time.Duration
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 17:07:24 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-11 22:51:10 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2020-11-28 13:53:02 +00:00
|
|
|
HTTPTimeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30),
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2016-03-20 23:23:37 +00:00
|
|
|
type DNSProvider struct {
|
2018-09-15 17:07:24 +00:00
|
|
|
config *Config
|
2019-07-17 19:01:50 +00:00
|
|
|
client *govultr.Client
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance with a configured Vultr client.
|
|
|
|
// Authentication uses the VULTR_API_KEY environment variable.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvAPIKey)
|
2018-06-11 15:32:50 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("vultr: %w", err)
|
2018-06-11 15:32:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.APIKey = values[EnvAPIKey]
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00: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-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if config.APIKey == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("vultr: credentials missing")
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 13:53:02 +00:00
|
|
|
httpClient := config.HTTPClient
|
|
|
|
if httpClient == nil {
|
|
|
|
httpClient = &http.Client{
|
|
|
|
Timeout: config.HTTPTimeout,
|
|
|
|
Transport: &oauth2.Transport{
|
|
|
|
Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: config.APIKey}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client := govultr.NewClient(httpClient)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
return &DNSProvider{client: client, config: config}, nil
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
// Present creates a TXT record to fulfill the DNS-01 challenge.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2019-07-22 21:29:32 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2016-03-20 23:23:37 +00:00
|
|
|
|
2019-07-22 21:29:32 +00:00
|
|
|
zoneDomain, err := d.getHostedZone(ctx, domain)
|
2016-03-20 23:23:37 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("vultr: %w", err)
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-27 12:28:59 +00:00
|
|
|
name := extractRecordName(fqdn, zoneDomain)
|
2016-03-20 23:23:37 +00:00
|
|
|
|
2020-11-28 13:53:02 +00:00
|
|
|
req := govultr.DomainRecordReq{
|
|
|
|
Name: name,
|
|
|
|
Type: "TXT",
|
|
|
|
Data: `"` + value + `"`,
|
|
|
|
TTL: d.config.TTL,
|
|
|
|
Priority: func(v int) *int { return &v }(0),
|
|
|
|
}
|
|
|
|
_, err = d.client.DomainRecord.Create(ctx, zoneDomain, &req)
|
2016-03-20 23:23:37 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("vultr: API call failed: %w", err)
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2019-07-22 21:29:32 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
2016-03-20 23:23:37 +00:00
|
|
|
|
2019-07-22 21:29:32 +00:00
|
|
|
zoneDomain, records, err := d.findTxtRecords(ctx, domain, fqdn)
|
2016-03-20 23:23:37 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("vultr: %w", err)
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
var allErr []string
|
2016-03-20 23:23:37 +00:00
|
|
|
for _, rec := range records {
|
2020-11-28 13:53:02 +00:00
|
|
|
err := d.client.DomainRecord.Delete(ctx, zoneDomain, rec.ID)
|
2016-03-20 23:23:37 +00:00
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
allErr = append(allErr, err.Error())
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
if len(allErr) > 0 {
|
|
|
|
return errors.New(strings.Join(allErr, ": "))
|
|
|
|
}
|
|
|
|
|
2016-03-20 23:23:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +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
|
|
|
|
}
|
|
|
|
|
2019-07-22 21:29:32 +00:00
|
|
|
func (d *DNSProvider) getHostedZone(ctx context.Context, domain string) (string, error) {
|
2020-11-28 13:53:02 +00:00
|
|
|
listOptions := &govultr.ListOptions{PerPage: 25}
|
|
|
|
|
|
|
|
var hostedDomain govultr.Domain
|
2016-03-20 23:23:37 +00:00
|
|
|
|
2020-11-28 13:53:02 +00:00
|
|
|
for {
|
|
|
|
domains, meta, err := d.client.Domain.List(ctx, listOptions)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("API call failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dom := range domains {
|
|
|
|
if strings.HasSuffix(domain, dom.Domain) && len(dom.Domain) > len(hostedDomain.Domain) {
|
2018-06-21 17:06:16 +00:00
|
|
|
hostedDomain = dom
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-28 13:53:02 +00:00
|
|
|
|
|
|
|
if domain == hostedDomain.Domain {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta.Links.Next == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
listOptions.Cursor = meta.Links.Next
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
2020-11-28 13:53:02 +00:00
|
|
|
|
2016-03-20 23:23:37 +00:00
|
|
|
if hostedDomain.Domain == "" {
|
2020-11-28 13:53:02 +00:00
|
|
|
return "", fmt.Errorf("no matching domain found for domain %s", domain)
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hostedDomain.Domain, nil
|
|
|
|
}
|
|
|
|
|
2020-11-28 13:53:02 +00:00
|
|
|
func (d *DNSProvider) findTxtRecords(ctx context.Context, domain, fqdn string) (string, []govultr.DomainRecord, error) {
|
2019-07-22 21:29:32 +00:00
|
|
|
zoneDomain, err := d.getHostedZone(ctx, domain)
|
2016-03-20 23:23:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-28 13:53:02 +00:00
|
|
|
listOptions := &govultr.ListOptions{PerPage: 25}
|
2016-03-20 23:23:37 +00:00
|
|
|
|
2020-11-28 13:53:02 +00:00
|
|
|
var records []govultr.DomainRecord
|
|
|
|
for {
|
|
|
|
result, meta, err := d.client.DomainRecord.List(ctx, zoneDomain, listOptions)
|
|
|
|
if err != nil {
|
|
|
|
return "", records, fmt.Errorf("API call has failed: %w", err)
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
2020-11-28 13:53:02 +00:00
|
|
|
|
|
|
|
recordName := extractRecordName(fqdn, zoneDomain)
|
|
|
|
for _, record := range result {
|
|
|
|
if record.Type == "TXT" && record.Name == recordName {
|
|
|
|
records = append(records, record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta.Links.Next == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
listOptions.Cursor = meta.Links.Next
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return zoneDomain, records, nil
|
|
|
|
}
|
|
|
|
|
2020-06-27 12:28:59 +00:00
|
|
|
func extractRecordName(fqdn, zone string) string {
|
2018-12-06 21:50:17 +00:00
|
|
|
name := dns01.UnFqdn(fqdn)
|
2020-06-27 12:28:59 +00:00
|
|
|
if idx := strings.Index(name, "."+zone); idx != -1 {
|
2016-03-20 23:23:37 +00:00
|
|
|
return name[:idx]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|