bookmyname.go raw

   1  // Package bookmyname implements a DNS provider for solving the DNS-01 challenge using BookMyName.
   2  package bookmyname
   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/bookmyname/internal"
  15  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  16  )
  17  
  18  // Environment variables names.
  19  const (
  20  	envNamespace = "BOOKMYNAME_"
  21  
  22  	EnvUsername = envNamespace + "USERNAME"
  23  	EnvPassword = envNamespace + "PASSWORD"
  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  	Username string
  36  	Password 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 BookMyName.
  63  func NewDNSProvider() (*DNSProvider, error) {
  64  	values, err := env.Get(EnvUsername, EnvPassword)
  65  	if err != nil {
  66  		return nil, fmt.Errorf("bookmyname: %w", err)
  67  	}
  68  
  69  	config := NewDefaultConfig()
  70  	config.Username = values[EnvUsername]
  71  	config.Password = values[EnvPassword]
  72  
  73  	return NewDNSProviderConfig(config)
  74  }
  75  
  76  // NewDNSProviderConfig return a DNSProvider instance configured for BookMyName.
  77  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  78  	if config == nil {
  79  		return nil, errors.New("bookmyname: the configuration of the DNS provider is nil")
  80  	}
  81  
  82  	client, err := internal.NewClient(config.Username, config.Password)
  83  	if err != nil {
  84  		return nil, fmt.Errorf("bookmyname: %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  		Hostname: dns01.UnFqdn(info.EffectiveFQDN),
 105  		Type:     "txt",
 106  		TTL:      d.config.TTL,
 107  		Value:    info.Value,
 108  	}
 109  
 110  	err := d.client.AddRecord(context.Background(), record)
 111  	if err != nil {
 112  		return fmt.Errorf("bookmyname: add record: %w", err)
 113  	}
 114  
 115  	return nil
 116  }
 117  
 118  // CleanUp removes the TXT record matching the specified parameters.
 119  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 120  	info := dns01.GetChallengeInfo(domain, keyAuth)
 121  
 122  	record := internal.Record{
 123  		Hostname: dns01.UnFqdn(info.EffectiveFQDN),
 124  		Type:     "txt",
 125  		TTL:      d.config.TTL,
 126  		Value:    info.Value,
 127  	}
 128  
 129  	err := d.client.RemoveRecord(context.Background(), record)
 130  	if err != nil {
 131  		return fmt.Errorf("bookmyname: add record: %w", err)
 132  	}
 133  
 134  	return nil
 135  }
 136  
 137  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 138  // Adjusting here to cope with spikes in propagation times.
 139  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 140  	return d.config.PropagationTimeout, d.config.PollingInterval
 141  }
 142