hostingde.go raw

   1  // Package hostingde implements a DNS provider for solving the DNS-01 challenge using hosting.de.
   2  package hostingde
   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/hostingde"
  14  )
  15  
  16  // Environment variables names.
  17  const (
  18  	envNamespace = "HOSTINGDE_"
  19  
  20  	EnvAPIKey   = envNamespace + "API_KEY"
  21  	EnvZoneName = envNamespace + "ZONE_NAME"
  22  
  23  	EnvTTL                = envNamespace + "TTL"
  24  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  25  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  26  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  27  )
  28  
  29  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  30  
  31  // Config is used to configure the creation of the DNSProvider.
  32  type Config = hostingde.Config
  33  
  34  // NewDefaultConfig returns a default configuration for the DNSProvider.
  35  func NewDefaultConfig() *Config {
  36  	return &Config{
  37  		ZoneName:           env.GetOrFile(EnvZoneName),
  38  		TTL:                env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
  39  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
  40  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  41  		HTTPClient: &http.Client{
  42  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  43  		},
  44  	}
  45  }
  46  
  47  // DNSProvider implements the challenge.Provider interface.
  48  type DNSProvider struct {
  49  	prv challenge.ProviderTimeout
  50  }
  51  
  52  // NewDNSProvider returns a DNSProvider instance configured for hosting.de.
  53  // Credentials must be passed in the environment variables:
  54  // HOSTINGDE_ZONE_NAME and HOSTINGDE_API_KEY.
  55  func NewDNSProvider() (*DNSProvider, error) {
  56  	values, err := env.Get(EnvAPIKey)
  57  	if err != nil {
  58  		return nil, fmt.Errorf("hostingde: %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 hosting.de.
  68  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  69  	if config == nil {
  70  		return nil, errors.New("hostingde: the configuration of the DNS provider is nil")
  71  	}
  72  
  73  	provider, err := hostingde.NewDNSProviderConfig(config, "")
  74  	if err != nil {
  75  		return nil, fmt.Errorf("hostingde: %w", err)
  76  	}
  77  
  78  	return &DNSProvider{prv: provider}, nil
  79  }
  80  
  81  // Present creates a TXT record using the specified parameters.
  82  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
  83  	err := d.prv.Present(domain, token, keyAuth)
  84  	if err != nil {
  85  		return fmt.Errorf("hostingde: %w", err)
  86  	}
  87  
  88  	return nil
  89  }
  90  
  91  // CleanUp removes the TXT record matching the specified parameters.
  92  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
  93  	err := d.prv.CleanUp(domain, token, keyAuth)
  94  	if err != nil {
  95  		return fmt.Errorf("hostingde: %w", err)
  96  	}
  97  
  98  	return nil
  99  }
 100  
 101  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 102  // Adjusting here to cope with spikes in propagation times.
 103  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 104  	return d.prv.Timeout()
 105  }
 106