clouddns.go raw

   1  // Package clouddns implements a DNS provider for solving the DNS-01 challenge using CloudDNS API.
   2  package clouddns
   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/clouddns/internal"
  15  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  16  )
  17  
  18  // Environment variables names.
  19  const (
  20  	envNamespace = "CLOUDDNS_"
  21  
  22  	EnvClientID = envNamespace + "CLIENT_ID"
  23  	EnvEmail    = envNamespace + "EMAIL"
  24  	EnvPassword = envNamespace + "PASSWORD"
  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 DNSProvider.
  35  type Config struct {
  36  	ClientID string
  37  	Email    string
  38  	Password string
  39  
  40  	TTL                int
  41  	PropagationTimeout time.Duration
  42  	PollingInterval    time.Duration
  43  	HTTPClient         *http.Client
  44  }
  45  
  46  // NewDefaultConfig returns a default configuration for the DNSProvider.
  47  func NewDefaultConfig() *Config {
  48  	return &Config{
  49  		TTL:                env.GetOrDefaultInt(EnvTTL, 300),
  50  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second),
  51  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second),
  52  		HTTPClient: &http.Client{
  53  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  54  		},
  55  	}
  56  }
  57  
  58  // DNSProvider implements the challenge.Provider interface.
  59  type DNSProvider struct {
  60  	client *internal.Client
  61  	config *Config
  62  }
  63  
  64  // NewDNSProvider returns a DNSProvider instance configured for CloudDNS.
  65  // Credentials must be passed in the environment variables:
  66  // CLOUDDNS_CLIENT_ID, CLOUDDNS_EMAIL, CLOUDDNS_PASSWORD.
  67  func NewDNSProvider() (*DNSProvider, error) {
  68  	values, err := env.Get(EnvClientID, EnvEmail, EnvPassword)
  69  	if err != nil {
  70  		return nil, fmt.Errorf("clouddns: %w", err)
  71  	}
  72  
  73  	config := NewDefaultConfig()
  74  	config.ClientID = values[EnvClientID]
  75  	config.Email = values[EnvEmail]
  76  	config.Password = values[EnvPassword]
  77  
  78  	return NewDNSProviderConfig(config)
  79  }
  80  
  81  // NewDNSProviderConfig return a DNSProvider instance configured for CloudDNS.
  82  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  83  	if config == nil {
  84  		return nil, errors.New("clouddns: the configuration of the DNS provider is nil")
  85  	}
  86  
  87  	if config.ClientID == "" || config.Email == "" || config.Password == "" {
  88  		return nil, errors.New("clouddns: credentials missing")
  89  	}
  90  
  91  	client := internal.NewClient(config.ClientID, config.Email, config.Password, config.TTL)
  92  
  93  	if config.HTTPClient != nil {
  94  		client.HTTPClient = config.HTTPClient
  95  	}
  96  
  97  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
  98  
  99  	return &DNSProvider{client: client, config: config}, nil
 100  }
 101  
 102  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 103  // Adjusting here to cope with spikes in propagation times.
 104  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 105  	return d.config.PropagationTimeout, d.config.PollingInterval
 106  }
 107  
 108  // Present creates a TXT record using the specified parameters.
 109  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
 110  	info := dns01.GetChallengeInfo(domain, keyAuth)
 111  
 112  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
 113  	if err != nil {
 114  		return fmt.Errorf("clouddns: could not find zone for domain %q: %w", domain, err)
 115  	}
 116  
 117  	ctx, err := d.client.CreateAuthenticatedContext(context.Background())
 118  	if err != nil {
 119  		return err
 120  	}
 121  
 122  	err = d.client.AddRecord(ctx, authZone, info.EffectiveFQDN, info.Value)
 123  	if err != nil {
 124  		return fmt.Errorf("clouddns: add record: %w", err)
 125  	}
 126  
 127  	return nil
 128  }
 129  
 130  // CleanUp removes the TXT record matching the specified parameters.
 131  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 132  	info := dns01.GetChallengeInfo(domain, keyAuth)
 133  
 134  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
 135  	if err != nil {
 136  		return fmt.Errorf("clouddns: could not find zone for domain %q: %w", domain, err)
 137  	}
 138  
 139  	ctx, err := d.client.CreateAuthenticatedContext(context.Background())
 140  	if err != nil {
 141  		return err
 142  	}
 143  
 144  	err = d.client.DeleteRecord(ctx, authZone, info.EffectiveFQDN)
 145  	if err != nil {
 146  		return fmt.Errorf("clouddns: delete record: %w", err)
 147  	}
 148  
 149  	return nil
 150  }
 151