sonic.go raw

   1  // Package sonic implements a DNS provider for solving the DNS-01 challenge using  Sonic.
   2  package sonic
   3  
   4  import (
   5  	"context"
   6  	"errors"
   7  	"fmt"
   8  	"net/http"
   9  	"time"
  10  
  11  	"github.com/go-acme/lego/v4/challenge"
  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/sonic/internal"
  16  )
  17  
  18  // Environment variables names.
  19  const (
  20  	envNamespace = "SONIC_"
  21  
  22  	EnvUserID = envNamespace + "USER_ID"
  23  	EnvAPIKey = envNamespace + "API_KEY"
  24  
  25  	EnvTTL                = envNamespace + "TTL"
  26  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  27  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  28  	EnvSequenceInterval   = envNamespace + "SEQUENCE_INTERVAL"
  29  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  30  )
  31  
  32  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  33  
  34  // Config is used to configure the creation of the DNSProvider.
  35  type Config struct {
  36  	UserID             string
  37  	APIKey             string
  38  	HTTPClient         *http.Client
  39  	PropagationTimeout time.Duration
  40  	PollingInterval    time.Duration
  41  	SequenceInterval   time.Duration
  42  	TTL                int
  43  }
  44  
  45  // NewDefaultConfig returns a default configuration for the DNSProvider.
  46  func NewDefaultConfig() *Config {
  47  	return &Config{
  48  		TTL:                env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
  49  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
  50  		SequenceInterval:   env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout),
  51  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  52  		HTTPClient: &http.Client{
  53  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second),
  54  		},
  55  	}
  56  }
  57  
  58  // DNSProvider implements the challenge.Provider interface.
  59  type DNSProvider struct {
  60  	config *Config
  61  	client *internal.Client
  62  }
  63  
  64  // NewDNSProvider returns a DNSProvider instance configured for Sonic.
  65  // Credentials must be passed in the environment variables:
  66  // SONIC_USERID and SONIC_APIKEY.
  67  func NewDNSProvider() (*DNSProvider, error) {
  68  	values, err := env.Get(EnvUserID, EnvAPIKey)
  69  	if err != nil {
  70  		return nil, fmt.Errorf("sonic: %w", err)
  71  	}
  72  
  73  	config := NewDefaultConfig()
  74  	config.UserID = values[EnvUserID]
  75  	config.APIKey = values[EnvAPIKey]
  76  
  77  	return NewDNSProviderConfig(config)
  78  }
  79  
  80  // NewDNSProviderConfig return a DNSProvider instance configured for Sonic.
  81  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  82  	if config == nil {
  83  		return nil, errors.New("sonic: the configuration of the DNS provider is nil")
  84  	}
  85  
  86  	client, err := internal.NewClient(config.UserID, config.APIKey)
  87  	if err != nil {
  88  		return nil, fmt.Errorf("sonic: %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{client: client, config: config}, nil
  98  }
  99  
 100  // Present creates a TXT record using the specified parameters.
 101  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
 102  	info := dns01.GetChallengeInfo(domain, keyAuth)
 103  
 104  	err := d.client.SetRecord(context.Background(), dns01.UnFqdn(info.EffectiveFQDN), info.Value, d.config.TTL)
 105  	if err != nil {
 106  		return fmt.Errorf("sonic: unable to create record for %s: %w", info.EffectiveFQDN, err)
 107  	}
 108  
 109  	return nil
 110  }
 111  
 112  // CleanUp removes the TXT records matching the specified parameters.
 113  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 114  	info := dns01.GetChallengeInfo(domain, keyAuth)
 115  
 116  	err := d.client.SetRecord(context.Background(), dns01.UnFqdn(info.EffectiveFQDN), "_", d.config.TTL)
 117  	if err != nil {
 118  		return fmt.Errorf("sonic: unable to clean record for %s: %w", info.EffectiveFQDN, err)
 119  	}
 120  
 121  	return nil
 122  }
 123  
 124  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 125  // Adjusting here to cope with spikes in propagation times.
 126  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 127  	return d.config.PropagationTimeout, d.config.PollingInterval
 128  }
 129  
 130  // Sequential All DNS challenges for this provider will be resolved sequentially.
 131  // Returns the interval between each iteration.
 132  func (d *DNSProvider) Sequential() time.Duration {
 133  	return d.config.SequenceInterval
 134  }
 135