active24.go raw

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