1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-23 09:15:11 +02:00
lego/providers/dns/vultr/vultr.go

247 lines
6.3 KiB
Go
Raw Normal View History

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"
"errors"
2016-03-21 01:23:37 +02:00
"fmt"
"net/http"
2016-03-21 01:23:37 +02:00
"strings"
"time"
2016-03-21 01:23:37 +02:00
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"
"github.com/vultr/govultr/v3"
"golang.org/x/oauth2"
2016-03-21 01:23:37 +02: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 19:35:25 +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
HTTPTimeout time.Duration
}
2020-05-08 19:35:25 +02:00
// NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config {
return &Config{
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
HTTPTimeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
}
}
2020-05-08 19:35:25 +02:00
// DNSProvider implements the challenge.Provider interface.
2016-03-21 01:23:37 +02:00
type DNSProvider struct {
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) {
values, err := env.Get(EnvAPIKey)
if err != nil {
2020-02-27 20:14:46 +02:00
return nil, fmt.Errorf("vultr: %w", err)
}
config := NewDefaultConfig()
config.APIKey = values[EnvAPIKey]
return NewDNSProviderConfig(config)
2016-03-21 01:23:37 +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
}
if config.APIKey == "" {
2020-02-27 20:14:46 +02:00
return nil, errors.New("vultr: credentials missing")
}
2023-05-05 09:49:38 +02:00
authClient := OAuthStaticAccessToken(config.HTTPClient, config.APIKey)
authClient.Timeout = config.HTTPTimeout
2023-05-05 09:49:38 +02:00
client := govultr.NewClient(authClient)
return &DNSProvider{client: client, config: config}, nil
2016-03-21 01:23:37 +02:00
}
// Present creates a TXT record to fulfill the DNS-01 challenge.
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
2019-07-22 23:29:32 +02:00
ctx := context.Background()
info := dns01.GetChallengeInfo(domain, keyAuth)
2016-03-21 01:23:37 +02:00
2022-11-25 19:12:21 +02:00
// TODO(ldez) replace domain by FQDN to follow CNAME.
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 {
2020-02-27 20:14:46 +02:00
return fmt.Errorf("vultr: %w", err)
2016-03-21 01:23:37 +02:00
}
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, zoneDomain)
if err != nil {
return fmt.Errorf("vultr: %w", err)
}
2016-03-21 01:23:37 +02:00
req := govultr.DomainRecordReq{
Name: subDomain,
Type: "TXT",
Data: `"` + info.Value + `"`,
TTL: d.config.TTL,
Priority: func(v int) *int { return &v }(0),
}
_, resp, err := d.client.DomainRecord.Create(ctx, zoneDomain, &req)
2016-03-21 01:23:37 +02:00
if err != nil {
return fmt.Errorf("vultr: %w", extendError(resp, err))
2016-03-21 01:23:37 +02:00
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
2019-07-22 23:29:32 +02:00
ctx := context.Background()
info := dns01.GetChallengeInfo(domain, keyAuth)
2016-03-21 01:23:37 +02:00
2022-11-25 19:12:21 +02:00
// TODO(ldez) replace domain by FQDN to follow CNAME.
zoneDomain, records, err := d.findTxtRecords(ctx, domain, info.EffectiveFQDN)
2016-03-21 01:23:37 +02:00
if err != nil {
2020-02-27 20:14:46 +02:00
return fmt.Errorf("vultr: %w", err)
2016-03-21 01:23:37 +02:00
}
var allErr []string
2016-03-21 01:23:37 +02:00
for _, rec := range records {
err := d.client.DomainRecord.Delete(ctx, zoneDomain, rec.ID)
2016-03-21 01:23:37 +02:00
if err != nil {
allErr = append(allErr, err.Error())
2016-03-21 01:23:37 +02:00
}
}
if len(allErr) > 0 {
return errors.New(strings.Join(allErr, ": "))
}
2016-03-21 01:23:37 +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
}
2019-07-22 23:29:32 +02:00
func (d *DNSProvider) getHostedZone(ctx context.Context, domain string) (string, error) {
listOptions := &govultr.ListOptions{PerPage: 25}
var hostedDomain govultr.Domain
2016-03-21 01:23:37 +02:00
for {
domains, meta, resp, err := d.client.Domain.List(ctx, listOptions)
if err != nil {
return "", extendError(resp, err)
}
for _, dom := range domains {
if strings.HasSuffix(domain, dom.Domain) && len(dom.Domain) > len(hostedDomain.Domain) {
hostedDomain = dom
2016-03-21 01:23:37 +02:00
}
}
if domain == hostedDomain.Domain {
break
}
if meta.Links.Next == "" {
break
}
listOptions.Cursor = meta.Links.Next
2016-03-21 01:23:37 +02:00
}
2016-03-21 01:23:37 +02:00
if hostedDomain.Domain == "" {
return "", fmt.Errorf("no matching domain found for domain %s", domain)
2016-03-21 01:23:37 +02:00
}
return hostedDomain.Domain, nil
}
func (d *DNSProvider) findTxtRecords(ctx context.Context, domain, fqdn string) (string, []govultr.DomainRecord, error) {
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 {
return "", nil, err
}
subDomain, err := dns01.ExtractSubDomain(fqdn, zoneDomain)
if err != nil {
return "", nil, err
}
listOptions := &govultr.ListOptions{PerPage: 25}
2016-03-21 01:23:37 +02:00
var records []govultr.DomainRecord
for {
result, meta, resp, err := d.client.DomainRecord.List(ctx, zoneDomain, listOptions)
if err != nil {
return "", records, extendError(resp, err)
2016-03-21 01:23:37 +02:00
}
for _, record := range result {
if record.Type == "TXT" && record.Name == subDomain {
records = append(records, record)
}
}
if meta.Links.Next == "" {
break
}
listOptions.Cursor = meta.Links.Next
2016-03-21 01:23:37 +02:00
}
return zoneDomain, records, nil
}
2023-05-05 09:49:38 +02:00
func OAuthStaticAccessToken(client *http.Client, accessToken string) *http.Client {
if client == nil {
client = &http.Client{Timeout: 5 * time.Second}
}
client.Transport = &oauth2.Transport{
Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken}),
Base: client.Transport,
}
return client
}
func extendError(resp *http.Response, err error) error {
msg := "API call failed"
if resp != nil {
msg += fmt.Sprintf(" (%d)", resp.StatusCode)
}
return fmt.Errorf("%s: %w", msg, err)
}