dreamhost.go raw

   1  // Package dreamhost implements a DNS provider for solving the DNS-01 challenge using DreamHost.
   2  // See https://help.dreamhost.com/hc/en-us/articles/217560167-API_overview
   3  // and https://help.dreamhost.com/hc/en-us/articles/217555707-DNS-API-commands for the API spec.
   4  package dreamhost
   5  
   6  import (
   7  	"context"
   8  	"errors"
   9  	"fmt"
  10  	"net/http"
  11  	"time"
  12  
  13  	"github.com/go-acme/lego/v4/challenge"
  14  	"github.com/go-acme/lego/v4/challenge/dns01"
  15  	"github.com/go-acme/lego/v4/platform/config/env"
  16  	"github.com/go-acme/lego/v4/providers/dns/dreamhost/internal"
  17  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  18  )
  19  
  20  // Environment variables names.
  21  const (
  22  	envNamespace = "DREAMHOST_"
  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  	BaseURL            string
  36  	APIKey             string
  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  		BaseURL:            internal.DefaultBaseURL,
  46  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 60*time.Minute),
  47  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, 1*time.Minute),
  48  		HTTPClient: &http.Client{
  49  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  50  		},
  51  	}
  52  }
  53  
  54  // DNSProvider implements the challenge.Provider interface.
  55  type DNSProvider struct {
  56  	config *Config
  57  	client *internal.Client
  58  }
  59  
  60  // NewDNSProvider returns a new DNS provider using
  61  // environment variable DREAMHOST_API_KEY for adding and removing the DNS record.
  62  func NewDNSProvider() (*DNSProvider, error) {
  63  	values, err := env.Get(EnvAPIKey)
  64  	if err != nil {
  65  		return nil, fmt.Errorf("dreamhost: %w", err)
  66  	}
  67  
  68  	config := NewDefaultConfig()
  69  	config.APIKey = values[EnvAPIKey]
  70  
  71  	return NewDNSProviderConfig(config)
  72  }
  73  
  74  // NewDNSProviderConfig return a DNSProvider instance configured for DreamHost.
  75  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  76  	if config == nil {
  77  		return nil, errors.New("dreamhost: the configuration of the DNS provider is nil")
  78  	}
  79  
  80  	if config.APIKey == "" {
  81  		return nil, errors.New("dreamhost: credentials missing")
  82  	}
  83  
  84  	client := internal.NewClient(config.APIKey)
  85  
  86  	if config.HTTPClient != nil {
  87  		client.HTTPClient = config.HTTPClient
  88  	}
  89  
  90  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
  91  
  92  	if config.BaseURL != "" {
  93  		client.BaseURL = config.BaseURL
  94  	}
  95  
  96  	return &DNSProvider{config: config, client: client}, 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  	err := d.client.AddRecord(context.Background(), dns01.UnFqdn(info.EffectiveFQDN), info.Value)
 104  	if err != nil {
 105  		return fmt.Errorf("dreamhost: %w", err)
 106  	}
 107  
 108  	return nil
 109  }
 110  
 111  // CleanUp removes the TXT record matching the specified parameters.
 112  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 113  	info := dns01.GetChallengeInfo(domain, keyAuth)
 114  
 115  	err := d.client.RemoveRecord(context.Background(), dns01.UnFqdn(info.EffectiveFQDN), info.Value)
 116  	if err != nil {
 117  		return fmt.Errorf("dreamhost: %w", err)
 118  	}
 119  
 120  	return nil
 121  }
 122  
 123  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 124  // Adjusting here to cope with spikes in propagation times.
 125  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 126  	return d.config.PropagationTimeout, d.config.PollingInterval
 127  }
 128