hostingnl.go raw

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