ipv64.go raw

   1  // Package ipv64 implements a DNS provider for solving the DNS-01 challenge using IPv64.
   2  // See https://ipv64.net/healthcheck_updater_api for more info on updating TXT records.
   3  package ipv64
   4  
   5  import (
   6  	"context"
   7  	"errors"
   8  	"fmt"
   9  	"net/http"
  10  	"time"
  11  
  12  	"github.com/go-acme/lego/v4/challenge"
  13  	"github.com/go-acme/lego/v4/challenge/dns01"
  14  	"github.com/go-acme/lego/v4/platform/config/env"
  15  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  16  	"github.com/go-acme/lego/v4/providers/dns/ipv64/internal"
  17  	"github.com/miekg/dns"
  18  )
  19  
  20  // Environment variables names.
  21  const (
  22  	envNamespace = "IPV64_"
  23  
  24  	EnvAPIKey = envNamespace + "API_KEY"
  25  
  26  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  27  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  28  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  29  )
  30  
  31  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  32  
  33  // Config is used to configure the creation of the DNSProvider.
  34  type Config struct {
  35  	APIKey             string
  36  	PropagationTimeout time.Duration
  37  	PollingInterval    time.Duration
  38  	HTTPClient         *http.Client
  39  	SequenceInterval   time.Duration // Deprecated: unused, will be removed in v5.
  40  }
  41  
  42  // NewDefaultConfig returns a default configuration for the DNSProvider.
  43  func NewDefaultConfig() *Config {
  44  	return &Config{
  45  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
  46  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  47  		HTTPClient: &http.Client{
  48  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  49  		},
  50  	}
  51  }
  52  
  53  // DNSProvider implements the challenge.Provider interface.
  54  type DNSProvider struct {
  55  	config *Config
  56  	client *internal.Client
  57  }
  58  
  59  // NewDNSProvider returns a new DNS provider using
  60  // environment variable IPV64_TOKEN for adding and removing the DNS record.
  61  func NewDNSProvider() (*DNSProvider, error) {
  62  	values, err := env.Get(EnvAPIKey)
  63  	if err != nil {
  64  		return nil, fmt.Errorf("ipv64: %w", err)
  65  	}
  66  
  67  	config := NewDefaultConfig()
  68  	config.APIKey = values[EnvAPIKey]
  69  
  70  	return NewDNSProviderConfig(config)
  71  }
  72  
  73  // NewDNSProviderConfig return a DNSProvider instance configured for IPv64.
  74  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  75  	if config == nil {
  76  		return nil, errors.New("ipv64: the configuration of the DNS provider is nil")
  77  	}
  78  
  79  	if config.APIKey == "" {
  80  		return nil, errors.New("ipv64: credentials missing")
  81  	}
  82  
  83  	client := internal.NewClient(internal.OAuthStaticAccessToken(config.HTTPClient, config.APIKey))
  84  
  85  	if config.HTTPClient != nil {
  86  		client.HTTPClient = config.HTTPClient
  87  	}
  88  
  89  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
  90  
  91  	return &DNSProvider{config: config, client: client}, nil
  92  }
  93  
  94  // Present creates a TXT record to fulfill the dns-01 challenge.
  95  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
  96  	info := dns01.GetChallengeInfo(domain, keyAuth)
  97  
  98  	sub, root, err := splitDomain(dns01.UnFqdn(info.EffectiveFQDN))
  99  	if err != nil {
 100  		return fmt.Errorf("ipv64: %w", err)
 101  	}
 102  
 103  	err = d.client.AddRecord(context.Background(), root, sub, "TXT", info.Value)
 104  	if err != nil {
 105  		return fmt.Errorf("ipv64: %w", err)
 106  	}
 107  
 108  	return nil
 109  }
 110  
 111  // CleanUp clears IPv64 TXT record.
 112  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 113  	info := dns01.GetChallengeInfo(domain, keyAuth)
 114  
 115  	sub, root, err := splitDomain(dns01.UnFqdn(info.EffectiveFQDN))
 116  	if err != nil {
 117  		return fmt.Errorf("ipv64: %w", err)
 118  	}
 119  
 120  	err = d.client.DeleteRecord(context.Background(), root, sub, "TXT", info.Value)
 121  	if err != nil {
 122  		return fmt.Errorf("ipv64: %w", err)
 123  	}
 124  
 125  	return nil
 126  }
 127  
 128  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 129  // Adjusting here to cope with spikes in propagation times.
 130  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 131  	return d.config.PropagationTimeout, d.config.PollingInterval
 132  }
 133  
 134  func splitDomain(full string) (string, string, error) {
 135  	split := dns.Split(full)
 136  	if len(split) < 3 {
 137  		return "", "", fmt.Errorf("unsupported domain: %s", full)
 138  	}
 139  
 140  	if len(split) == 3 {
 141  		return "", full, nil
 142  	}
 143  
 144  	domain := full[split[len(split)-3]:]
 145  	subDomain := full[:split[len(split)-3]-1]
 146  
 147  	return subDomain, domain, nil
 148  }
 149