2020-03-10 13:31:33 +02:00
|
|
|
// Package dynu implements a DNS provider for solving the DNS-01 challenge using Dynu DNS.
|
|
|
|
package dynu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
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/go-acme/lego/v4/providers/dns/dynu/internal"
|
2020-03-10 13:31:33 +02:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "DYNU_"
|
|
|
|
|
|
|
|
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.
|
2020-03-10 13:31:33 +02:00
|
|
|
type Config struct {
|
|
|
|
APIKey string
|
|
|
|
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2020-03-10 13:31:33 +02:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, 300),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 3*time.Minute),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 10*time.Second),
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2020-03-10 13:31:33 +02:00
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
|
|
|
client *internal.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Dynu.
|
|
|
|
// Credentials must be passed in the environment variables.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
values, err := env.Get(EnvAPIKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("dynu: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = values[EnvAPIKey]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Dynu.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("dynu: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.APIKey == "" {
|
|
|
|
return nil, errors.New("dynu: incomplete credentials, missing API key")
|
|
|
|
}
|
|
|
|
|
|
|
|
tr, err := internal.NewTokenTransport(config.APIKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("dynu: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := internal.NewClient()
|
|
|
|
client.HTTPClient = tr.Wrap(config.HTTPClient)
|
|
|
|
|
|
|
|
return &DNSProvider{config: config, client: client}, 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
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Present creates a TXT record using the specified parameters.
|
2020-03-10 13:31:33 +02:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
|
|
|
|
2022-11-25 19:12:21 +02:00
|
|
|
// TODO(ldez) replace domain by FQDN to follow CNAME.
|
2020-03-10 13:31:33 +02:00
|
|
|
rootDomain, err := d.client.GetRootDomain(domain)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dynu: could not find root domain for %s: %w", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
records, err := d.client.GetRecords(dns01.UnFqdn(fqdn), "TXT")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dynu: failed to get records for %s: %w", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, record := range records {
|
|
|
|
// the record already exist
|
|
|
|
if record.Hostname == dns01.UnFqdn(fqdn) && record.TextData == value {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
record := internal.DNSRecord{
|
|
|
|
Type: "TXT",
|
|
|
|
DomainName: rootDomain.DomainName,
|
|
|
|
Hostname: dns01.UnFqdn(fqdn),
|
|
|
|
NodeName: dns01.UnFqdn(strings.TrimSuffix(fqdn, dns.Fqdn(domain))),
|
|
|
|
TextData: value,
|
|
|
|
State: true,
|
2020-03-12 00:51:10 +02:00
|
|
|
TTL: d.config.TTL,
|
2020-03-10 13:31:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = d.client.AddNewRecord(rootDomain.ID, record)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dynu: failed to add record to %s: %w", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2020-03-10 13:31:33 +02:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
|
|
|
|
2022-11-25 19:12:21 +02:00
|
|
|
// TODO(ldez) replace domain by FQDN to follow CNAME.
|
2020-03-10 13:31:33 +02:00
|
|
|
rootDomain, err := d.client.GetRootDomain(domain)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dynu: could not find root domain for %s: %w", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
records, err := d.client.GetRecords(dns01.UnFqdn(fqdn), "TXT")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dynu: failed to get records for %s: %w", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, record := range records {
|
|
|
|
if record.Hostname == dns01.UnFqdn(fqdn) && record.TextData == value {
|
|
|
|
err = d.client.DeleteRecord(rootDomain.ID, record.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dynu: failed to remove TXT record for %s: %w", domain, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|