myaddr.go raw

   1  // Package myaddr implements a DNS provider for solving the DNS-01 challenge using myaddr.{tools,dev,io}.
   2  package myaddr
   3  
   4  import (
   5  	"context"
   6  	"errors"
   7  	"fmt"
   8  	"net/http"
   9  	"strings"
  10  	"time"
  11  
  12  	"github.com/go-acme/lego/v4/challenge/dns01"
  13  	"github.com/go-acme/lego/v4/platform/config/env"
  14  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  15  	"github.com/go-acme/lego/v4/providers/dns/myaddr/internal"
  16  )
  17  
  18  // Environment variables names.
  19  const (
  20  	envNamespace = "MYADDR_"
  21  
  22  	EnvPrivateKeysMapping = envNamespace + "PRIVATE_KEYS_MAPPING"
  23  
  24  	EnvTTL                = envNamespace + "TTL"
  25  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  26  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  27  	EnvSequenceInterval   = envNamespace + "SEQUENCE_INTERVAL"
  28  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  29  )
  30  
  31  // Config is used to configure the creation of the DNSProvider.
  32  type Config struct {
  33  	Credentials map[string]string
  34  
  35  	PropagationTimeout time.Duration
  36  	PollingInterval    time.Duration
  37  	SequenceInterval   time.Duration
  38  	TTL                int
  39  	HTTPClient         *http.Client
  40  }
  41  
  42  // NewDefaultConfig returns a default configuration for the DNSProvider.
  43  func NewDefaultConfig() *Config {
  44  	return &Config{
  45  		TTL:                env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
  46  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
  47  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  48  		SequenceInterval:   env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPollingInterval),
  49  		HTTPClient: &http.Client{
  50  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  51  		},
  52  	}
  53  }
  54  
  55  // DNSProvider implements the challenge.Provider interface.
  56  type DNSProvider struct {
  57  	config *Config
  58  	client *internal.Client
  59  }
  60  
  61  // NewDNSProvider returns a DNSProvider instance configured for myaddr.{tools,dev,io}.
  62  func NewDNSProvider() (*DNSProvider, error) {
  63  	values, err := env.Get(EnvPrivateKeysMapping)
  64  	if err != nil {
  65  		return nil, fmt.Errorf("myaddr: %w", err)
  66  	}
  67  
  68  	config := NewDefaultConfig()
  69  
  70  	credentials, err := env.ParsePairs(values[EnvPrivateKeysMapping])
  71  	if err != nil {
  72  		return nil, fmt.Errorf("myaddr: %w", err)
  73  	}
  74  
  75  	config.Credentials = credentials
  76  
  77  	return NewDNSProviderConfig(config)
  78  }
  79  
  80  // NewDNSProviderConfig return a DNSProvider instance configured for myaddr.{tools,dev,io}.
  81  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  82  	if config == nil {
  83  		return nil, errors.New("myaddr: the configuration of the DNS provider is nil")
  84  	}
  85  
  86  	client, err := internal.NewClient(config.Credentials)
  87  	if err != nil {
  88  		return nil, fmt.Errorf("myaddr: %w", err)
  89  	}
  90  
  91  	if config.HTTPClient != nil {
  92  		client.HTTPClient = config.HTTPClient
  93  	}
  94  
  95  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
  96  
  97  	return &DNSProvider{
  98  		config: config,
  99  		client: client,
 100  	}, nil
 101  }
 102  
 103  // Present creates a TXT record using the specified parameters.
 104  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
 105  	info := dns01.GetChallengeInfo(domain, keyAuth)
 106  
 107  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
 108  	if err != nil {
 109  		return fmt.Errorf("myaddr: could not find zone for domain %q: %w", domain, err)
 110  	}
 111  
 112  	fullSubdomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
 113  	if err != nil {
 114  		return fmt.Errorf("myaddr: %w", err)
 115  	}
 116  
 117  	_, after, found := strings.Cut(fullSubdomain, ".")
 118  	if !found {
 119  		return fmt.Errorf("myaddr: subdomain not found in: %q (%s)", fullSubdomain, info.EffectiveFQDN)
 120  	}
 121  
 122  	err = d.client.AddTXTRecord(context.Background(), after, info.Value)
 123  	if err != nil {
 124  		return fmt.Errorf("myaddr: add TXT record: %w", err)
 125  	}
 126  
 127  	return nil
 128  }
 129  
 130  // CleanUp removes the TXT record matching the specified parameters.
 131  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 132  	// There is no API endpoint to delete a TXT record:
 133  	// TXT records are automatically removed after a few minutes.
 134  	return nil
 135  }
 136  
 137  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 138  // Adjusting here to cope with spikes in propagation times.
 139  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 140  	return d.config.PropagationTimeout, d.config.PollingInterval
 141  }
 142  
 143  // Sequential All DNS challenges for this provider will be resolved sequentially.
 144  // Returns the interval between each iteration.
 145  func (d *DNSProvider) Sequential() time.Duration {
 146  	return d.config.SequenceInterval
 147  }
 148