webnames.go raw

   1  // Package webnames implements a DNS provider for solving the DNS-01 challenge using webnames.ru DNS.
   2  package webnames
   3  
   4  import (
   5  	"context"
   6  	"errors"
   7  	"fmt"
   8  	"net/http"
   9  	"strings"
  10  	"time"
  11  
  12  	"github.com/go-acme/lego/v4/challenge"
  13  	"github.com/go-acme/lego/v4/challenge/dns01"
  14  	"github.com/go-acme/lego/v4/platform/config/env"
  15  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  16  	"github.com/go-acme/lego/v4/providers/dns/webnames/internal"
  17  )
  18  
  19  // Environment variables names.
  20  const (
  21  	envNamespace    = "WEBNAMESRU_"
  22  	altEnvNamespace = "WEBNAMES_"
  23  
  24  	EnvAPIKey = envNamespace + "API_KEY"
  25  
  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  	APIKey string
  36  
  37  	PropagationTimeout time.Duration
  38  	PollingInterval    time.Duration
  39  	HTTPClient         *http.Client
  40  }
  41  
  42  // NewDefaultConfig returns a default configuration for the DNSProvider.
  43  func NewDefaultConfig() *Config {
  44  	return &Config{
  45  		PropagationTimeout: env.GetOneWithFallback(EnvPropagationTimeout, dns01.DefaultPropagationTimeout, env.ParseSecond, altEnvName(EnvPropagationTimeout)),
  46  		PollingInterval:    env.GetOneWithFallback(EnvPollingInterval, dns01.DefaultPollingInterval, env.ParseSecond, altEnvName(EnvPollingInterval)),
  47  		HTTPClient: &http.Client{
  48  			Timeout: env.GetOneWithFallback(EnvHTTPTimeout, 20*time.Second, env.ParseSecond, altEnvName(EnvHTTPTimeout)),
  49  		},
  50  	}
  51  }
  52  
  53  // DNSProvider implements the challenge.Provider interface.
  54  type DNSProvider struct {
  55  	config *Config
  56  	client *internal.Client
  57  }
  58  
  59  // NewDNSProvider returns a new DNS provider using
  60  // environment variable WEBNAMESRU_API_KEY for adding and removing the DNS record.
  61  func NewDNSProvider() (*DNSProvider, error) {
  62  	values, err := env.GetWithFallback([]string{EnvAPIKey, altEnvName(EnvAPIKey)})
  63  	if err != nil {
  64  		return nil, fmt.Errorf("webnamesru: %w", err)
  65  	}
  66  
  67  	config := NewDefaultConfig()
  68  	config.APIKey = values[EnvAPIKey]
  69  
  70  	return NewDNSProviderConfig(config)
  71  }
  72  
  73  // NewDNSProviderConfig return a DNSProvider instance configured for Webnames.
  74  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  75  	if config == nil {
  76  		return nil, errors.New("webnamesru: the configuration of the DNS provider is nil")
  77  	}
  78  
  79  	if config.APIKey == "" {
  80  		return nil, errors.New("webnamesru: credentials missing")
  81  	}
  82  
  83  	client := internal.NewClient(config.APIKey)
  84  
  85  	if config.HTTPClient != nil {
  86  		client.HTTPClient = config.HTTPClient
  87  	}
  88  
  89  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
  90  
  91  	return &DNSProvider{config: config, client: client}, nil
  92  }
  93  
  94  // Present creates a TXT record to fulfill the dns-01 challenge.
  95  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
  96  	info := dns01.GetChallengeInfo(domain, keyAuth)
  97  
  98  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
  99  	if err != nil {
 100  		return fmt.Errorf("webnamesru: could not find zone for domain %q: %w", domain, err)
 101  	}
 102  
 103  	subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
 104  	if err != nil {
 105  		return fmt.Errorf("webnamesru: %w", err)
 106  	}
 107  
 108  	err = d.client.AddTXTRecord(context.Background(), dns01.UnFqdn(authZone), subDomain, info.Value)
 109  	if err != nil {
 110  		return fmt.Errorf("webnamesru: failed to create TXT records [domain: %s, sub domain: %s]: %w",
 111  			dns01.UnFqdn(authZone), subDomain, err)
 112  	}
 113  
 114  	return nil
 115  }
 116  
 117  // CleanUp clears Webnames TXT record.
 118  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 119  	info := dns01.GetChallengeInfo(domain, keyAuth)
 120  
 121  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
 122  	if err != nil {
 123  		return fmt.Errorf("webnamesru: could not find zone for domain %q: %w", domain, err)
 124  	}
 125  
 126  	subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
 127  	if err != nil {
 128  		return fmt.Errorf("webnamesru: %w", err)
 129  	}
 130  
 131  	err = d.client.RemoveTXTRecord(context.Background(), dns01.UnFqdn(authZone), subDomain, info.Value)
 132  	if err != nil {
 133  		return fmt.Errorf("webnamesru: failed to remove TXT records [domain: %s, sub domain: %s]: %w",
 134  			dns01.UnFqdn(authZone), subDomain, err)
 135  	}
 136  
 137  	return nil
 138  }
 139  
 140  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 141  // Adjusting here to cope with spikes in propagation times.
 142  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 143  	return d.config.PropagationTimeout, d.config.PollingInterval
 144  }
 145  
 146  func altEnvName(v string) string {
 147  	return strings.ReplaceAll(v, envNamespace, altEnvNamespace)
 148  }
 149