2018-12-06 21:50:17 +00:00
|
|
|
// 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 (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2018-09-15 17:07:24 +00:00
|
|
|
"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"
|
2023-05-05 07:49:38 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/pdns/internal"
|
2016-08-19 07:07:18 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "PDNS_"
|
|
|
|
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
EnvAPIURL = envNamespace + "API_URL"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
2024-01-27 22:23:10 +00:00
|
|
|
EnvAPIVersion = envNamespace + "API_VERSION"
|
2020-03-11 22:51:10 +00:00
|
|
|
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-03-11 22:51:10 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
Host *url.URL
|
2021-03-17 08:53:38 +00:00
|
|
|
ServerName string
|
2024-01-27 22:23:10 +00:00
|
|
|
APIVersion int
|
2018-09-15 17:07:24 +00:00
|
|
|
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.
|
2018-09-15 17:07:24 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2024-01-27 22:23:10 +00:00
|
|
|
ServerName: env.GetOrDefaultString(EnvServerName, "localhost"),
|
|
|
|
APIVersion: env.GetOrDefaultInt(EnvAPIVersion, 0),
|
2020-03-11 22:51:10 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
|
2018-09-15 17:07:24 +00:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-11 22:51:10 +00:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
2018-09-15 17:07:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-05-05 07:49:38 +00:00
|
|
|
config *Config
|
|
|
|
client *internal.Client
|
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) {
|
2020-03-11 22:51:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
hostURL, err := url.Parse(values[EnvAPIURL])
|
2018-06-11 15:32:50 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("pdns: %w", err)
|
2018-06-11 15:32:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Host = hostURL
|
2020-03-11 22:51:10 +00:00
|
|
|
config.APIKey = values[EnvAPIKey]
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-08-19 07:07:18 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +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
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +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
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +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
|
|
|
}
|
|
|
|
|
2024-01-27 22:23:10 +00:00
|
|
|
client := internal.NewClient(config.Host, config.ServerName, config.APIVersion, config.APIKey)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2024-01-27 22:23:10 +00:00
|
|
|
if config.APIVersion <= 0 {
|
|
|
|
err := client.SetAPIVersion(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("pdns: failed to get API version %v", err)
|
|
|
|
}
|
2018-06-21 17:06:16 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return &DNSProvider{config: config, client: client}, nil
|
2016-08-19 07:07:18 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 18:42:28 +00:00
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Adjusting here to cope with spikes in propagation times.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2018-09-15 17:07:24 +00:00
|
|
|
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.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("pdns: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
zone, err := d.client.GetHostedZone(ctx, authZone)
|
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
|
|
|
}
|
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
name := info.EffectiveFQDN
|
2023-05-05 07:49:38 +00:00
|
|
|
if d.client.APIVersion() == 0 {
|
|
|
|
// pre-v1 API wants non-fqdn
|
2023-03-07 08:39:05 +00:00
|
|
|
name = dns01.UnFqdn(info.EffectiveFQDN)
|
2016-08-19 07:07:18 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
// Look for existing records.
|
|
|
|
existingRRSet := findTxtRecord(zone, info.EffectiveFQDN)
|
|
|
|
|
|
|
|
// merge the existing and new records
|
|
|
|
var records []internal.Record
|
|
|
|
if existingRRSet != nil {
|
|
|
|
records = existingRRSet.Records
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := internal.Record{
|
2023-06-06 15:49:59 +00:00
|
|
|
Content: "\"" + info.Value + "\"",
|
2016-08-19 07:07:18 +00:00
|
|
|
Disabled: false,
|
|
|
|
|
|
|
|
// pre-v1 API
|
|
|
|
Type: "TXT",
|
|
|
|
Name: name,
|
2018-09-15 17:07:24 +00:00
|
|
|
TTL: d.config.TTL,
|
2016-08-19 07:07:18 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
rrSets := internal.RRSets{
|
|
|
|
RRSets: []internal.RRSet{
|
2018-05-30 22:03:55 +00:00
|
|
|
{
|
2016-08-19 07:07:18 +00:00
|
|
|
Name: name,
|
|
|
|
ChangeType: "REPLACE",
|
|
|
|
Type: "TXT",
|
|
|
|
Kind: "Master",
|
2018-09-15 17:07:24 +00:00
|
|
|
TTL: d.config.TTL,
|
2023-05-05 07:49:38 +00:00
|
|
|
Records: append(records, rec),
|
2016-08-19 07:07:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = d.client.UpdateRecords(ctx, zone, rrSets)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("pdns: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
2022-09-17 16:20:41 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return d.client.Notify(ctx, 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.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2016-08-19 07:07:18 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2016-08-19 07:07:18 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("pdns: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
|
2016-08-19 07:07:18 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
zone, err := d.client.GetHostedZone(ctx, authZone)
|
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
|
|
|
}
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
set := findTxtRecord(zone, info.EffectiveFQDN)
|
|
|
|
|
2019-03-21 14:46:21 +00:00
|
|
|
if set == nil {
|
2023-03-07 08:39:05 +00:00
|
|
|
return fmt.Errorf("pdns: no existing record found for %s", info.EffectiveFQDN)
|
2019-03-21 14:46:21 +00:00
|
|
|
}
|
2016-08-19 07:07:18 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
rrSets := internal.RRSets{
|
|
|
|
RRSets: []internal.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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
err = d.client.UpdateRecords(ctx, zone, rrSets)
|
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
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return d.client.Notify(ctx, zone)
|
|
|
|
}
|
|
|
|
|
|
|
|
func findTxtRecord(zone *internal.HostedZone, fqdn string) *internal.RRSet {
|
|
|
|
for _, set := range zone.RRSets {
|
|
|
|
if set.Type == "TXT" && (set.Name == dns01.UnFqdn(fqdn) || set.Name == fqdn) {
|
|
|
|
return &set
|
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
2022-09-17 16:20:41 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil
|
2016-08-19 07:07:18 +00:00
|
|
|
}
|