2023-02-27 02:54:39 +02:00
|
|
|
// Package bunny implements a DNS provider for solving the DNS-01 challenge using Bunny DNS.
|
|
|
|
package bunny
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-12-05 15:24:23 +02:00
|
|
|
"slices"
|
2023-02-27 02:54:39 +02:00
|
|
|
"time"
|
|
|
|
|
2024-11-16 00:21:21 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge"
|
2023-02-27 02:54:39 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2024-12-05 20:49:13 +02:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
|
2024-12-05 15:24:23 +02:00
|
|
|
"github.com/miekg/dns"
|
2023-09-03 16:27:27 +02:00
|
|
|
"github.com/nrdcg/bunny-go"
|
2024-12-05 15:24:23 +02:00
|
|
|
"golang.org/x/net/publicsuffix"
|
2023-02-27 02:54:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "BUNNY_"
|
|
|
|
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
)
|
|
|
|
|
2024-11-16 00:21:21 +02:00
|
|
|
const minTTL = 60
|
|
|
|
|
|
|
|
var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
|
|
|
|
|
2023-02-27 02:54:39 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, minTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
|
|
|
client *bunny.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for bunny.
|
|
|
|
// Credentials must be passed in the environment variable: BUNNY_API_KEY.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
values, err := env.Get(EnvAPIKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("bunny: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = values[EnvAPIKey]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for bunny.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("bunny: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.APIKey == "" {
|
|
|
|
return nil, errors.New("bunny: credentials missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.TTL < minTTL {
|
|
|
|
return nil, fmt.Errorf("bunny: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := bunny.NewClient(config.APIKey)
|
|
|
|
|
|
|
|
return &DNSProvider{config: config, client: client}, nil
|
|
|
|
}
|
|
|
|
|
2023-06-30 20:42:28 +02:00
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Adjusting here to cope with spikes in propagation times.
|
2023-02-27 02:54:39 +02:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2023-02-27 02:54:39 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-12-05 15:24:23 +02:00
|
|
|
zone, err := d.findZone(ctx, dns01.UnFqdn(info.EffectiveFQDN))
|
2023-02-27 02:54:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("bunny: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-12-05 20:49:13 +02:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, ptr.Deref(zone.Domain))
|
2023-02-27 02:54:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("bunny: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
record := &bunny.AddOrUpdateDNSRecordOptions{
|
2024-12-05 20:49:13 +02:00
|
|
|
Type: ptr.Pointer(bunny.DNSRecordTypeTXT),
|
|
|
|
Name: ptr.Pointer(subDomain),
|
|
|
|
Value: ptr.Pointer(info.Value),
|
|
|
|
TTL: ptr.Pointer(int32(d.config.TTL)),
|
2023-02-27 02:54:39 +02:00
|
|
|
}
|
|
|
|
|
2024-12-05 20:49:13 +02:00
|
|
|
if _, err := d.client.DNSZone.AddDNSRecord(ctx, ptr.Deref(zone.ID), record); err != nil {
|
|
|
|
return fmt.Errorf("bunny: failed to add TXT record: fqdn=%s, zoneID=%d: %w", info.EffectiveFQDN, ptr.Deref(zone.ID), err)
|
2023-02-27 02:54:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2023-02-27 02:54:39 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-12-05 15:24:23 +02:00
|
|
|
zone, err := d.findZone(ctx, dns01.UnFqdn(info.EffectiveFQDN))
|
2023-02-27 02:54:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("bunny: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-12-05 20:49:13 +02:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, ptr.Deref(zone.Domain))
|
2023-02-27 02:54:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("bunny: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var record *bunny.DNSRecord
|
|
|
|
for _, r := range zone.Records {
|
2024-12-05 20:49:13 +02:00
|
|
|
if ptr.Deref(r.Name) == subDomain && ptr.Deref(r.Type) == bunny.DNSRecordTypeTXT {
|
2023-02-27 02:54:39 +02:00
|
|
|
r := r
|
|
|
|
record = &r
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if record == nil {
|
2024-12-05 20:49:13 +02:00
|
|
|
return fmt.Errorf("bunny: could not find TXT record zone=%d, subdomain=%s", ptr.Deref(zone.ID), subDomain)
|
2023-02-27 02:54:39 +02:00
|
|
|
}
|
|
|
|
|
2024-12-05 20:49:13 +02:00
|
|
|
if err := d.client.DNSZone.DeleteDNSRecord(ctx, ptr.Deref(zone.ID), ptr.Deref(record.ID)); err != nil {
|
|
|
|
return fmt.Errorf("bunny: failed to delete TXT record: id=%d, name=%s: %w", ptr.Deref(record.ID), ptr.Deref(record.Name), err)
|
2023-02-27 02:54:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) findZone(ctx context.Context, authZone string) (*bunny.DNSZone, error) {
|
|
|
|
zones, err := d.client.DNSZone.List(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-12-05 15:24:23 +02:00
|
|
|
zone := findZone(zones, authZone)
|
|
|
|
if zone == nil {
|
|
|
|
return nil, fmt.Errorf("could not find DNSZone domain=%s", authZone)
|
|
|
|
}
|
|
|
|
|
|
|
|
return zone, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findZone(zones *bunny.DNSZones, domain string) *bunny.DNSZone {
|
|
|
|
domains := possibleDomains(domain)
|
|
|
|
|
|
|
|
var domainLength int
|
|
|
|
|
2023-02-27 02:54:39 +02:00
|
|
|
var zone *bunny.DNSZone
|
|
|
|
for _, item := range zones.Items {
|
2024-12-05 15:24:23 +02:00
|
|
|
if item == nil {
|
|
|
|
continue
|
2023-02-27 02:54:39 +02:00
|
|
|
}
|
|
|
|
|
2024-12-05 20:49:13 +02:00
|
|
|
curr := ptr.Deref(item.Domain)
|
2024-12-05 15:24:23 +02:00
|
|
|
|
|
|
|
if slices.Contains(domains, curr) && domainLength < len(curr) {
|
|
|
|
domainLength = len(curr)
|
|
|
|
|
|
|
|
zone = item
|
|
|
|
}
|
2023-02-27 02:54:39 +02:00
|
|
|
}
|
|
|
|
|
2024-12-05 15:24:23 +02:00
|
|
|
return zone
|
2023-02-27 02:54:39 +02:00
|
|
|
}
|
|
|
|
|
2024-12-05 15:24:23 +02:00
|
|
|
func possibleDomains(domain string) []string {
|
|
|
|
var domains []string
|
|
|
|
|
|
|
|
labelIndexes := dns.Split(domain)
|
|
|
|
|
|
|
|
for _, index := range labelIndexes {
|
|
|
|
tld, _ := publicsuffix.PublicSuffix(domain)
|
|
|
|
if tld == domain[index:] {
|
|
|
|
// skip the TLD
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
domains = append(domains, dns01.UnFqdn(domain[index:]))
|
2023-02-27 02:54:39 +02:00
|
|
|
}
|
|
|
|
|
2024-12-05 15:24:23 +02:00
|
|
|
return domains
|
2023-09-19 18:04:28 +02:00
|
|
|
}
|