lego/providers/dns/pdns/pdns.go

217 lines
5.2 KiB
Go
Raw Normal View History

// Package pdns implements a DNS provider for solving the DNS-01 challenge using PowerDNS nameserver.
2019-03-11 16:56:48 +00:00
package pdns
2016-08-19 07:07:18 +00:00
import (
"bytes"
"encoding/json"
"errors"
2016-08-19 07:07:18 +00:00
"fmt"
"net/http"
"net/url"
"time"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/log"
"github.com/go-acme/lego/v4/platform/config/env"
2016-08-19 07:07:18 +00:00
)
// Environment variables names.
const (
envNamespace = "PDNS_"
EnvAPIKey = envNamespace + "API_KEY"
EnvAPIURL = envNamespace + "API_URL"
EnvTTL = envNamespace + "TTL"
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
2021-03-17 08:53:38 +00:00
EnvServerName = envNamespace + "SERVER_NAME"
)
2020-05-08 17:35:25 +00:00
// Config is used to configure the creation of the DNSProvider.
type Config struct {
APIKey string
Host *url.URL
2021-03-17 08:53:38 +00:00
ServerName string
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int
HTTPClient *http.Client
}
2020-05-08 17:35:25 +00:00
// NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config {
return &Config{
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
2021-03-17 08:53:38 +00:00
ServerName: env.GetOrDefaultString(EnvServerName, "localhost"),
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
},
}
}
2020-05-08 17:35:25 +00:00
// DNSProvider implements the challenge.Provider interface.
2016-08-19 07:07:18 +00:00
type DNSProvider struct {
apiVersion int
config *Config
2016-08-19 07:07:18 +00:00
}
// NewDNSProvider returns a DNSProvider instance configured for pdns.
// Credentials must be passed in the environment variable:
// PDNS_API_URL and PDNS_API_KEY.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvAPIKey, EnvAPIURL)
2016-08-19 07:07:18 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return nil, fmt.Errorf("pdns: %w", err)
2016-08-19 07:07:18 +00:00
}
hostURL, err := url.Parse(values[EnvAPIURL])
if err != nil {
2020-02-27 18:14:46 +00:00
return nil, fmt.Errorf("pdns: %w", err)
}
config := NewDefaultConfig()
config.Host = hostURL
config.APIKey = values[EnvAPIKey]
return NewDNSProviderConfig(config)
2016-08-19 07:07:18 +00:00
}
// NewDNSProviderConfig return a DNSProvider instance configured for pdns.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("pdns: the configuration of the DNS provider is nil")
2016-08-19 07:07:18 +00:00
}
if config.APIKey == "" {
2020-02-27 18:14:46 +00:00
return nil, errors.New("pdns: API key missing")
2016-08-19 07:07:18 +00:00
}
if config.Host == nil || config.Host.Host == "" {
2020-02-27 18:14:46 +00:00
return nil, errors.New("pdns: API URL missing")
2016-08-19 07:07:18 +00:00
}
d := &DNSProvider{config: config}
apiVersion, err := d.getAPIVersion()
if err != nil {
log.Warnf("pdns: failed to get API version %v", err)
}
d.apiVersion = apiVersion
return d, nil
2016-08-19 07:07:18 +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
2016-08-19 07:07:18 +00:00
}
2020-05-08 17:35:25 +00:00
// Present creates a TXT record to fulfill the dns-01 challenge.
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
fqdn, value := dns01.GetRecord(domain, keyAuth)
zone, err := d.getHostedZone(fqdn)
2016-08-19 07:07:18 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("pdns: %w", err)
2016-08-19 07:07:18 +00:00
}
name := fqdn
// pre-v1 API wants non-fqdn
if d.apiVersion == 0 {
name = dns01.UnFqdn(fqdn)
2016-08-19 07:07:18 +00:00
}
rec := Record{
2016-08-19 07:07:18 +00:00
Content: "\"" + value + "\"",
Disabled: false,
// pre-v1 API
Type: "TXT",
Name: name,
TTL: d.config.TTL,
2016-08-19 07:07:18 +00:00
}
// Look for existing records.
existingRrSet, err := d.findTxtRecord(fqdn)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("pdns: %w", err)
}
// merge the existing and new records
var records []Record
if existingRrSet != nil {
records = existingRrSet.Records
}
records = append(records, rec)
2016-08-19 07:07:18 +00:00
rrsets := rrSets{
RRSets: []rrSet{
{
2016-08-19 07:07:18 +00:00
Name: name,
ChangeType: "REPLACE",
Type: "TXT",
Kind: "Master",
TTL: d.config.TTL,
Records: records,
2016-08-19 07:07:18 +00:00
},
},
}
body, err := json.Marshal(rrsets)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("pdns: %w", err)
2016-08-19 07:07:18 +00:00
}
_, err = d.sendRequest(http.MethodPatch, zone.URL, bytes.NewReader(body))
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("pdns: %w", err)
}
return d.notify(zone)
2016-08-19 07:07:18 +00:00
}
2020-05-08 17:35:25 +00:00
// CleanUp removes the TXT record matching the specified parameters.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
fqdn, _ := dns01.GetRecord(domain, keyAuth)
2016-08-19 07:07:18 +00:00
zone, err := d.getHostedZone(fqdn)
2016-08-19 07:07:18 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("pdns: %w", err)
2016-08-19 07:07:18 +00:00
}
set, err := d.findTxtRecord(fqdn)
2016-08-19 07:07:18 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("pdns: %w", err)
2016-08-19 07:07:18 +00:00
}
if set == nil {
return fmt.Errorf("pdns: no existing record found for %s", fqdn)
}
2016-08-19 07:07:18 +00:00
rrsets := rrSets{
RRSets: []rrSet{
2018-05-30 17:53:04 +00:00
{
2016-08-19 07:07:18 +00:00
Name: set.Name,
Type: set.Type,
ChangeType: "DELETE",
},
},
}
body, err := json.Marshal(rrsets)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("pdns: %w", err)
2016-08-19 07:07:18 +00:00
}
_, err = d.sendRequest(http.MethodPatch, zone.URL, bytes.NewReader(body))
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("pdns: %w", err)
}
return d.notify(zone)
2016-08-19 07:07:18 +00:00
}