regfish.go raw

   1  // Package regfish implements a DNS provider for solving the DNS-01 challenge using Regfish.
   2  package regfish
   3  
   4  import (
   5  	"errors"
   6  	"fmt"
   7  	"net/http"
   8  	"sync"
   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  	regfishapi "github.com/regfish/regfish-dnsapi-go"
  16  )
  17  
  18  // Environment variables names.
  19  const (
  20  	envNamespace = "REGFISH_"
  21  
  22  	EnvAPIKey = envNamespace + "API_KEY"
  23  
  24  	EnvTTL                = envNamespace + "TTL"
  25  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  26  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  27  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  28  )
  29  
  30  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  31  
  32  // Config is used to configure the creation of the DNSProvider.
  33  type Config struct {
  34  	APIKey string
  35  
  36  	PropagationTimeout time.Duration
  37  	PollingInterval    time.Duration
  38  	TTL                int
  39  	HTTPClient         *http.Client
  40  }
  41  
  42  // NewDefaultConfig returns a default configuration for the DNSProvider.
  43  func NewDefaultConfig() *Config {
  44  	return &Config{
  45  		TTL:                env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
  46  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
  47  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  48  		HTTPClient: &http.Client{
  49  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  50  		},
  51  	}
  52  }
  53  
  54  // DNSProvider implements the challenge.Provider interface.
  55  type DNSProvider struct {
  56  	config *Config
  57  	client *regfishapi.Client
  58  
  59  	recordIDs   map[string]int
  60  	recordIDsMu sync.Mutex
  61  }
  62  
  63  // NewDNSProvider returns a DNSProvider instance configured for Regfish.
  64  func NewDNSProvider() (*DNSProvider, error) {
  65  	values, err := env.Get(EnvAPIKey)
  66  	if err != nil {
  67  		return nil, fmt.Errorf("regfish: %w", err)
  68  	}
  69  
  70  	config := NewDefaultConfig()
  71  	config.APIKey = values[EnvAPIKey]
  72  
  73  	return NewDNSProviderConfig(config)
  74  }
  75  
  76  // NewDNSProviderConfig return a DNSProvider instance configured for Regfish.
  77  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  78  	if config == nil {
  79  		return nil, errors.New("regfish: the configuration of the DNS provider is nil")
  80  	}
  81  
  82  	if config.APIKey == "" {
  83  		return nil, errors.New("regfish: credentials missing")
  84  	}
  85  
  86  	client := regfishapi.NewClient(config.APIKey)
  87  
  88  	if config.HTTPClient != nil {
  89  		client.Client = config.HTTPClient
  90  	} else {
  91  		// Because the regfishapi.NewClient uses an empty http.Client.
  92  		client.Client = &http.Client{Timeout: 30 * time.Second}
  93  	}
  94  
  95  	client.Client = clientdebug.Wrap(client.Client)
  96  
  97  	return &DNSProvider{
  98  		config:    config,
  99  		client:    client,
 100  		recordIDs: make(map[string]int),
 101  	}, nil
 102  }
 103  
 104  // Present creates a TXT record using the specified parameters.
 105  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
 106  	info := dns01.GetChallengeInfo(domain, keyAuth)
 107  
 108  	record := regfishapi.Record{
 109  		Name: info.EffectiveFQDN,
 110  		Type: "TXT",
 111  		Data: info.Value,
 112  		TTL:  d.config.TTL,
 113  	}
 114  
 115  	newRecord, err := d.client.CreateRecord(record)
 116  	if err != nil {
 117  		return fmt.Errorf("regfish: create record: %w", err)
 118  	}
 119  
 120  	d.recordIDsMu.Lock()
 121  	d.recordIDs[token] = newRecord.ID
 122  	d.recordIDsMu.Unlock()
 123  
 124  	return nil
 125  }
 126  
 127  // CleanUp removes the TXT record matching the specified parameters.
 128  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 129  	info := dns01.GetChallengeInfo(domain, keyAuth)
 130  
 131  	// get the record's unique ID from when we created it
 132  	d.recordIDsMu.Lock()
 133  	recordID, ok := d.recordIDs[token]
 134  	d.recordIDsMu.Unlock()
 135  
 136  	if !ok {
 137  		return fmt.Errorf("regfish: unknown record ID for '%s'", info.EffectiveFQDN)
 138  	}
 139  
 140  	err := d.client.DeleteRecord(recordID)
 141  	if err != nil {
 142  		return fmt.Errorf("regfish: delete record: %w", err)
 143  	}
 144  
 145  	// Delete record ID from map
 146  	d.recordIDsMu.Lock()
 147  	delete(d.recordIDs, token)
 148  	d.recordIDsMu.Unlock()
 149  
 150  	return nil
 151  }
 152  
 153  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 154  // Adjusting here to cope with spikes in propagation times.
 155  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 156  	return d.config.PropagationTimeout, d.config.PollingInterval
 157  }
 158