cloudns.go raw

   1  // Package cloudns implements a DNS provider for solving the DNS-01 challenge using ClouDNS DNS.
   2  package cloudns
   3  
   4  import (
   5  	"context"
   6  	"errors"
   7  	"fmt"
   8  	"net/http"
   9  	"time"
  10  
  11  	"github.com/cenkalti/backoff/v5"
  12  	"github.com/go-acme/lego/v4/challenge"
  13  	"github.com/go-acme/lego/v4/challenge/dns01"
  14  	"github.com/go-acme/lego/v4/log"
  15  	"github.com/go-acme/lego/v4/platform/config/env"
  16  	"github.com/go-acme/lego/v4/platform/wait"
  17  	"github.com/go-acme/lego/v4/providers/dns/cloudns/internal"
  18  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  19  )
  20  
  21  // Environment variables names.
  22  const (
  23  	envNamespace = "CLOUDNS_"
  24  
  25  	EnvAuthID       = envNamespace + "AUTH_ID"
  26  	EnvSubAuthID    = envNamespace + "SUB_AUTH_ID"
  27  	EnvAuthPassword = envNamespace + "AUTH_PASSWORD"
  28  
  29  	EnvTTL                = envNamespace + "TTL"
  30  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  31  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  32  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  33  )
  34  
  35  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  36  
  37  // Config is used to configure the creation of the DNSProvider.
  38  type Config struct {
  39  	AuthID             string
  40  	SubAuthID          string
  41  	AuthPassword       string
  42  	PropagationTimeout time.Duration
  43  	PollingInterval    time.Duration
  44  	TTL                int
  45  	HTTPClient         *http.Client
  46  }
  47  
  48  // NewDefaultConfig returns a default configuration for the DNSProvider.
  49  func NewDefaultConfig() *Config {
  50  	return &Config{
  51  		TTL:                env.GetOrDefaultInt(EnvTTL, 60),
  52  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 180*time.Second),
  53  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, 10*time.Second),
  54  		HTTPClient: &http.Client{
  55  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  56  		},
  57  	}
  58  }
  59  
  60  // DNSProvider implements the challenge.Provider interface.
  61  type DNSProvider struct {
  62  	config *Config
  63  	client *internal.Client
  64  }
  65  
  66  // NewDNSProvider returns a DNSProvider instance configured for ClouDNS.
  67  // Credentials must be passed in the environment variables:
  68  // CLOUDNS_AUTH_ID and CLOUDNS_AUTH_PASSWORD.
  69  func NewDNSProvider() (*DNSProvider, error) {
  70  	var subAuthID string
  71  
  72  	authID := env.GetOrFile(EnvAuthID)
  73  	if authID == "" {
  74  		subAuthID = env.GetOrFile(EnvSubAuthID)
  75  	}
  76  
  77  	if authID == "" && subAuthID == "" {
  78  		return nil, fmt.Errorf("ClouDNS: some credentials information are missing: %s or %s", EnvAuthID, EnvSubAuthID)
  79  	}
  80  
  81  	values, err := env.Get(EnvAuthPassword)
  82  	if err != nil {
  83  		return nil, fmt.Errorf("ClouDNS: %w", err)
  84  	}
  85  
  86  	config := NewDefaultConfig()
  87  	config.AuthID = authID
  88  	config.SubAuthID = subAuthID
  89  	config.AuthPassword = values[EnvAuthPassword]
  90  
  91  	return NewDNSProviderConfig(config)
  92  }
  93  
  94  // NewDNSProviderConfig return a DNSProvider instance configured for ClouDNS.
  95  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  96  	if config == nil {
  97  		return nil, errors.New("ClouDNS: the configuration of the DNS provider is nil")
  98  	}
  99  
 100  	client, err := internal.NewClient(config.AuthID, config.SubAuthID, config.AuthPassword)
 101  	if err != nil {
 102  		return nil, fmt.Errorf("ClouDNS: %w", err)
 103  	}
 104  
 105  	if config.HTTPClient != nil {
 106  		client.HTTPClient = config.HTTPClient
 107  	}
 108  
 109  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
 110  
 111  	return &DNSProvider{client: client, config: config}, nil
 112  }
 113  
 114  // Present creates a TXT record to fulfill the dns-01 challenge.
 115  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
 116  	info := dns01.GetChallengeInfo(domain, keyAuth)
 117  
 118  	ctx := context.Background()
 119  
 120  	zone, err := d.client.GetZone(ctx, info.EffectiveFQDN)
 121  	if err != nil {
 122  		return fmt.Errorf("ClouDNS: %w", err)
 123  	}
 124  
 125  	err = d.client.AddTxtRecord(ctx, zone.Name, info.EffectiveFQDN, info.Value, d.config.TTL)
 126  	if err != nil {
 127  		return fmt.Errorf("ClouDNS: %w", err)
 128  	}
 129  
 130  	return d.waitNameservers(ctx, domain, zone)
 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  	ctx := context.Background()
 138  
 139  	zone, err := d.client.GetZone(ctx, info.EffectiveFQDN)
 140  	if err != nil {
 141  		return fmt.Errorf("ClouDNS: %w", err)
 142  	}
 143  
 144  	records, err := d.client.ListTxtRecords(ctx, zone.Name, info.EffectiveFQDN)
 145  	if err != nil {
 146  		return fmt.Errorf("ClouDNS: %w", err)
 147  	}
 148  
 149  	if len(records) == 0 {
 150  		return nil
 151  	}
 152  
 153  	for _, record := range records {
 154  		err = d.client.RemoveTxtRecord(ctx, record.ID, zone.Name)
 155  		if err != nil {
 156  			return fmt.Errorf("ClouDNS: %w", err)
 157  		}
 158  	}
 159  
 160  	return nil
 161  }
 162  
 163  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 164  // Adjusting here to cope with spikes in propagation times.
 165  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 166  	return d.config.PropagationTimeout, d.config.PollingInterval
 167  }
 168  
 169  // waitNameservers At the time of writing 4 servers are found as authoritative, but 8 are reported during the sync.
 170  // If this is not done, the secondary verification done by Let's Encrypt server will fail quire a bit.
 171  func (d *DNSProvider) waitNameservers(ctx context.Context, domain string, zone *internal.Zone) error {
 172  	return wait.Retry(ctx,
 173  		func() error {
 174  			syncProgress, err := d.client.GetUpdateStatus(ctx, zone.Name)
 175  			if err != nil {
 176  				return fmt.Errorf("nameserver sync on %s: %w", domain, err)
 177  			}
 178  
 179  			log.Infof("[%s] Sync %d/%d complete", domain, syncProgress.Updated, syncProgress.Total)
 180  
 181  			if !syncProgress.Complete {
 182  				return fmt.Errorf("nameserver sync on %s not complete", domain)
 183  			}
 184  
 185  			return nil
 186  		},
 187  		backoff.WithBackOff(backoff.NewConstantBackOff(d.config.PollingInterval)),
 188  		backoff.WithMaxElapsedTime(d.config.PropagationTimeout),
 189  	)
 190  }
 191