binarylane.go raw

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