spaceship.go raw

   1  // Package spaceship implements a DNS provider for solving the DNS-01 challenge using Spaceship.
   2  package spaceship
   3  
   4  import (
   5  	"context"
   6  	"errors"
   7  	"fmt"
   8  	"net/http"
   9  	"time"
  10  
  11  	"github.com/go-acme/lego/v4/challenge/dns01"
  12  	"github.com/go-acme/lego/v4/platform/config/env"
  13  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  14  	"github.com/go-acme/lego/v4/providers/dns/spaceship/internal"
  15  )
  16  
  17  // Environment variables names.
  18  const (
  19  	envNamespace = "SPACESHIP_"
  20  
  21  	EnvAPIKey    = envNamespace + "API_KEY"
  22  	EnvAPISecret = envNamespace + "API_SECRET"
  23  
  24  	EnvTTL                = envNamespace + "TTL"
  25  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  26  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  27  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  28  )
  29  
  30  // Config is used to configure the creation of the DNSProvider.
  31  type Config struct {
  32  	APIKey    string
  33  	APISecret string
  34  
  35  	PropagationTimeout time.Duration
  36  	PollingInterval    time.Duration
  37  	TTL                int
  38  	HTTPClient         *http.Client
  39  }
  40  
  41  // NewDefaultConfig returns a default configuration for the DNSProvider.
  42  func NewDefaultConfig() *Config {
  43  	return &Config{
  44  		TTL:                env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
  45  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
  46  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  47  		HTTPClient: &http.Client{
  48  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  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 DNSProvider instance configured for Spaceship.
  60  func NewDNSProvider() (*DNSProvider, error) {
  61  	values, err := env.Get(EnvAPIKey, EnvAPISecret)
  62  	if err != nil {
  63  		return nil, fmt.Errorf("spaceship: %w", err)
  64  	}
  65  
  66  	config := NewDefaultConfig()
  67  	config.APIKey = values[EnvAPIKey]
  68  	config.APISecret = values[EnvAPISecret]
  69  
  70  	return NewDNSProviderConfig(config)
  71  }
  72  
  73  // NewDNSProviderConfig return a DNSProvider instance configured for Spaceship.
  74  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  75  	if config == nil {
  76  		return nil, errors.New("spaceship: the configuration of the DNS provider is nil")
  77  	}
  78  
  79  	client, err := internal.NewClient(config.APIKey, config.APISecret)
  80  	if err != nil {
  81  		return nil, fmt.Errorf("spaceship: %w", err)
  82  	}
  83  
  84  	if config.HTTPClient != nil {
  85  		client.HTTPClient = config.HTTPClient
  86  	}
  87  
  88  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
  89  
  90  	return &DNSProvider{
  91  		config: config,
  92  		client: client,
  93  	}, nil
  94  }
  95  
  96  // Present creates a TXT record using the specified parameters.
  97  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
  98  	info := dns01.GetChallengeInfo(domain, keyAuth)
  99  
 100  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
 101  	if err != nil {
 102  		return fmt.Errorf("spaceship: could not find zone for domain %q: %w", domain, err)
 103  	}
 104  
 105  	subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
 106  	if err != nil {
 107  		return fmt.Errorf("spaceship: %w", err)
 108  	}
 109  
 110  	record := internal.Record{
 111  		Type:  "TXT",
 112  		Name:  subDomain,
 113  		Value: info.Value,
 114  		TTL:   d.config.TTL,
 115  	}
 116  
 117  	err = d.client.AddRecord(context.Background(), dns01.UnFqdn(authZone), record)
 118  	if err != nil {
 119  		return fmt.Errorf("spaceship: %w", err)
 120  	}
 121  
 122  	return nil
 123  }
 124  
 125  // CleanUp removes the TXT record matching the specified parameters.
 126  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 127  	info := dns01.GetChallengeInfo(domain, keyAuth)
 128  
 129  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
 130  	if err != nil {
 131  		return fmt.Errorf("spaceship: could not find zone for domain %q: %w", domain, err)
 132  	}
 133  
 134  	subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
 135  	if err != nil {
 136  		return fmt.Errorf("spaceship: %w", err)
 137  	}
 138  
 139  	record := internal.Record{
 140  		Type:  "TXT",
 141  		Name:  subDomain,
 142  		Value: info.Value,
 143  	}
 144  
 145  	err = d.client.DeleteRecord(context.Background(), dns01.UnFqdn(authZone), record)
 146  	if err != nil {
 147  		return fmt.Errorf("spaceship: %w", err)
 148  	}
 149  
 150  	return nil
 151  }
 152  
 153  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 154  // Adjusting here to cope with spikes in propagation times.
 155  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 156  	return d.config.PropagationTimeout, d.config.PollingInterval
 157  }
 158