technitium.go raw

   1  // Package technitium implements a DNS provider for solving the DNS-01 challenge using Technitium.
   2  package technitium
   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/technitium/internal"
  16  )
  17  
  18  // Environment variables names.
  19  const (
  20  	envNamespace = "TECHNITIUM_"
  21  
  22  	EnvServerBaseURL = envNamespace + "SERVER_BASE_URL"
  23  	EnvAPIToken      = envNamespace + "API_TOKEN"
  24  
  25  	EnvTTL                = envNamespace + "TTL"
  26  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  27  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  28  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  29  )
  30  
  31  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  32  
  33  // Config is used to configure the creation of the DNSProvider.
  34  type Config struct {
  35  	BaseURL  string
  36  	APIToken string
  37  
  38  	PropagationTimeout time.Duration
  39  	PollingInterval    time.Duration
  40  	TTL                int
  41  	HTTPClient         *http.Client
  42  }
  43  
  44  // NewDefaultConfig returns a default configuration for the DNSProvider.
  45  func NewDefaultConfig() *Config {
  46  	return &Config{
  47  		TTL:                env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
  48  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
  49  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  50  		HTTPClient: &http.Client{
  51  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  52  		},
  53  	}
  54  }
  55  
  56  // DNSProvider implements the challenge.Provider interface.
  57  type DNSProvider struct {
  58  	config *Config
  59  	client *internal.Client
  60  }
  61  
  62  // NewDNSProvider returns a DNSProvider instance configured for Technitium.
  63  func NewDNSProvider() (*DNSProvider, error) {
  64  	values, err := env.Get(EnvServerBaseURL, EnvAPIToken)
  65  	if err != nil {
  66  		return nil, fmt.Errorf("technitium: %w", err)
  67  	}
  68  
  69  	config := NewDefaultConfig()
  70  	config.BaseURL = values[EnvServerBaseURL]
  71  	config.APIToken = values[EnvAPIToken]
  72  
  73  	return NewDNSProviderConfig(config)
  74  }
  75  
  76  // NewDNSProviderConfig return a DNSProvider instance configured for Technitium.
  77  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  78  	if config == nil {
  79  		return nil, errors.New("technitium: the configuration of the DNS provider is nil")
  80  	}
  81  
  82  	client, err := internal.NewClient(config.BaseURL, config.APIToken)
  83  	if err != nil {
  84  		return nil, fmt.Errorf("technitium: %w", err)
  85  	}
  86  
  87  	if config.HTTPClient != nil {
  88  		client.HTTPClient = config.HTTPClient
  89  	}
  90  
  91  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
  92  
  93  	return &DNSProvider{
  94  		config: config,
  95  		client: client,
  96  	}, nil
  97  }
  98  
  99  // Present creates a TXT record using the specified parameters.
 100  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
 101  	info := dns01.GetChallengeInfo(domain, keyAuth)
 102  
 103  	record := internal.Record{
 104  		Domain: info.EffectiveFQDN,
 105  		Type:   "TXT",
 106  		Text:   info.Value,
 107  	}
 108  
 109  	_, err := d.client.AddRecord(context.Background(), record)
 110  	if err != nil {
 111  		return fmt.Errorf("technitium: add record: %w", err)
 112  	}
 113  
 114  	return nil
 115  }
 116  
 117  // CleanUp removes the TXT record matching the specified parameters.
 118  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 119  	info := dns01.GetChallengeInfo(domain, keyAuth)
 120  
 121  	record := internal.Record{
 122  		Domain: info.EffectiveFQDN,
 123  		Type:   "TXT",
 124  		Text:   info.Value,
 125  	}
 126  
 127  	err := d.client.DeleteRecord(context.Background(), record)
 128  	if err != nil {
 129  		return fmt.Errorf("technitium: delete record: %w", err)
 130  	}
 131  
 132  	return nil
 133  }
 134  
 135  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 136  // Adjusting here to cope with spikes in propagation times.
 137  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 138  	return d.config.PropagationTimeout, d.config.PollingInterval
 139  }
 140