neodigit.go raw

   1  // Package neodigit implements a DNS provider for solving the DNS-01 challenge using Neodigit DNS.
   2  package neodigit
   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/tecnocratica"
  14  )
  15  
  16  // Environment variables names.
  17  const (
  18  	envNamespace = "NEODIGIT_"
  19  
  20  	EnvToken = envNamespace + "TOKEN"
  21  
  22  	EnvTTL                = envNamespace + "TTL"
  23  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  24  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  25  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  26  )
  27  
  28  const defaultBaseURL = "https://api.neodigit.net/v1"
  29  
  30  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  31  
  32  // Config is used to configure the creation of the DNSProvider.
  33  type Config = tecnocratica.Config
  34  
  35  // NewDefaultConfig returns a default configuration for the DNSProvider.
  36  func NewDefaultConfig() *Config {
  37  	return &Config{
  38  		TTL:                env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
  39  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 5*time.Minute),
  40  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, 10*time.Second),
  41  		HTTPClient: &http.Client{
  42  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  43  		},
  44  	}
  45  }
  46  
  47  // DNSProvider implements the challenge.Provider interface.
  48  type DNSProvider struct {
  49  	prv challenge.ProviderTimeout
  50  }
  51  
  52  // NewDNSProvider returns a DNSProvider instance configured for Neodigit.
  53  func NewDNSProvider() (*DNSProvider, error) {
  54  	values, err := env.Get(EnvToken)
  55  	if err != nil {
  56  		return nil, fmt.Errorf("neodigit: %w", err)
  57  	}
  58  
  59  	config := NewDefaultConfig()
  60  	config.Token = values[EnvToken]
  61  
  62  	return NewDNSProviderConfig(config)
  63  }
  64  
  65  // NewDNSProviderConfig return a DNSProvider instance configured for Neodigit.
  66  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  67  	if config == nil {
  68  		return nil, errors.New("neodigit: the configuration of the DNS provider is nil")
  69  	}
  70  
  71  	provider, err := tecnocratica.NewDNSProviderConfig(config, defaultBaseURL)
  72  	if err != nil {
  73  		return nil, fmt.Errorf("neodigit: %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("neodigit: %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("neodigit: %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