uniteddomains.go raw

   1  // Package uniteddomains implements a DNS provider for solving the DNS-01 challenge using United-Domains.
   2  package uniteddomains
   3  
   4  import (
   5  	"errors"
   6  	"fmt"
   7  	"net/http"
   8  	"time"
   9  
  10  	"github.com/go-acme/lego/v4/challenge"
  11  	"github.com/go-acme/lego/v4/challenge/dns01"
  12  	"github.com/go-acme/lego/v4/platform/config/env"
  13  	"github.com/go-acme/lego/v4/providers/dns/internal/ionos"
  14  )
  15  
  16  // Environment variables names.
  17  const (
  18  	envNamespace = "UNITEDDOMAINS_"
  19  
  20  	EnvAPIKey = envNamespace + "API_KEY"
  21  
  22  	EnvTTL                = envNamespace + "TTL"
  23  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  24  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  25  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  26  )
  27  
  28  const defaultBaseURL = "https://dnsapi.united-domains.de/dns"
  29  
  30  const minTTL = 300
  31  
  32  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  33  
  34  // Config is used to configure the creation of the DNSProvider.
  35  type Config = ionos.Config
  36  
  37  // NewDefaultConfig returns a default configuration for the DNSProvider.
  38  func NewDefaultConfig() *Config {
  39  	return &Config{
  40  		TTL:                env.GetOrDefaultInt(EnvTTL, minTTL),
  41  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 15*time.Minute),
  42  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  43  		HTTPClient: &http.Client{
  44  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  45  		},
  46  	}
  47  }
  48  
  49  // DNSProvider implements the challenge.Provider interface.
  50  type DNSProvider struct {
  51  	prv challenge.ProviderTimeout
  52  }
  53  
  54  // NewDNSProvider returns a DNSProvider instance configured for United-Domains.
  55  func NewDNSProvider() (*DNSProvider, error) {
  56  	values, err := env.Get(EnvAPIKey)
  57  	if err != nil {
  58  		return nil, fmt.Errorf("uniteddomains: %w", err)
  59  	}
  60  
  61  	config := NewDefaultConfig()
  62  	config.APIKey = values[EnvAPIKey]
  63  
  64  	return NewDNSProviderConfig(config)
  65  }
  66  
  67  // NewDNSProviderConfig return a DNSProvider instance configured for United-Domains.
  68  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  69  	if config == nil {
  70  		return nil, errors.New("uniteddomains: the configuration of the DNS provider is nil")
  71  	}
  72  
  73  	provider, err := ionos.NewDNSProviderConfig(config, defaultBaseURL)
  74  	if err != nil {
  75  		return nil, fmt.Errorf("uniteddomains: %w", err)
  76  	}
  77  
  78  	return &DNSProvider{prv: provider}, nil
  79  }
  80  
  81  // Timeout returns the timeout and interval to use when checking for DNS propagation.
  82  // Adjusting here to cope with spikes in propagation times.
  83  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
  84  	return d.prv.Timeout()
  85  }
  86  
  87  // Present creates a TXT record using the specified parameters.
  88  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
  89  	err := d.prv.Present(domain, token, keyAuth)
  90  	if err != nil {
  91  		return fmt.Errorf("uniteddomains: %w", err)
  92  	}
  93  
  94  	return nil
  95  }
  96  
  97  // CleanUp removes the TXT record matching the specified parameters.
  98  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
  99  	err := d.prv.CleanUp(domain, token, keyAuth)
 100  	if err != nil {
 101  		return fmt.Errorf("uniteddomains: %w", err)
 102  	}
 103  
 104  	return nil
 105  }
 106