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

227 lines
5.7 KiB
Go
Raw Normal View History

// 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"
"time"
"github.com/go-acme/lego/v4/challenge"
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/platform/config/env"
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"
)
// Environment variables names.
const (
envNamespace = "BUNNY_"
EnvAPIKey = envNamespace + "API_KEY"
EnvTTL = envNamespace + "TTL"
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
)
const minTTL = 60
var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
// 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.
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 {
info := dns01.GetChallengeInfo(domain, keyAuth)
ctx := context.Background()
2024-12-05 15:24:23 +02:00
zone, err := d.findZone(ctx, dns01.UnFqdn(info.EffectiveFQDN))
if err != nil {
return fmt.Errorf("bunny: %w", err)
}
2024-12-05 15:24:23 +02:00
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, deref(zone.Domain))
if err != nil {
return fmt.Errorf("bunny: %w", err)
}
record := &bunny.AddOrUpdateDNSRecordOptions{
Type: pointer(bunny.DNSRecordTypeTXT),
Name: pointer(subDomain),
Value: pointer(info.Value),
TTL: pointer(int32(d.config.TTL)),
}
if _, err := d.client.DNSZone.AddDNSRecord(ctx, deref(zone.ID), record); err != nil {
return fmt.Errorf("bunny: failed to add TXT record: fqdn=%s, zoneID=%d: %w", info.EffectiveFQDN, deref(zone.ID), err)
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
ctx := context.Background()
2024-12-05 15:24:23 +02:00
zone, err := d.findZone(ctx, dns01.UnFqdn(info.EffectiveFQDN))
if err != nil {
return fmt.Errorf("bunny: %w", err)
}
2024-12-05 15:24:23 +02:00
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, deref(zone.Domain))
if err != nil {
return fmt.Errorf("bunny: %w", err)
}
var record *bunny.DNSRecord
for _, r := range zone.Records {
if deref(r.Name) == subDomain && deref(r.Type) == bunny.DNSRecordTypeTXT {
r := r
record = &r
break
}
}
if record == nil {
return fmt.Errorf("bunny: could not find TXT record zone=%d, subdomain=%s", deref(zone.ID), subDomain)
}
if err := d.client.DNSZone.DeleteDNSRecord(ctx, deref(zone.ID), deref(record.ID)); err != nil {
return fmt.Errorf("bunny: failed to delete TXT record: id=%d, name=%s: %w", deref(record.ID), deref(record.Name), err)
}
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
var zone *bunny.DNSZone
for _, item := range zones.Items {
2024-12-05 15:24:23 +02:00
if item == nil {
continue
}
2024-12-05 15:24:23 +02:00
curr := deref(item.Domain)
if slices.Contains(domains, curr) && domainLength < len(curr) {
domainLength = len(curr)
zone = item
}
}
2024-12-05 15:24:23 +02:00
return zone
}
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:]))
}
2024-12-05 15:24:23 +02:00
return domains
2023-09-19 18:04:28 +02:00
}
func pointer[T string | int | int32 | int64](v T) *T { return &v }
func deref[T string | int | int32 | int64](v *T) T {
if v == nil {
var zero T
return zero
}
return *v
}