epik.go raw

   1  // Package epik implements a DNS provider for solving the DNS-01 challenge using Epik.
   2  package epik
   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/epik/internal"
  16  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  17  )
  18  
  19  // Environment variables names.
  20  const (
  21  	envNamespace = "EPIK_"
  22  
  23  	EnvSignature = envNamespace + "SIGNATURE"
  24  
  25  	EnvTTL                = envNamespace + "TTL"
  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  	Signature          string
  36  	PropagationTimeout time.Duration
  37  	PollingInterval    time.Duration
  38  	TTL                int
  39  	HTTPClient         *http.Client
  40  }
  41  
  42  // NewDefaultConfig returns a default configuration for the DNSProvider.
  43  func NewDefaultConfig() *Config {
  44  	return &Config{
  45  		TTL:                env.GetOrDefaultInt(EnvTTL, 3600),
  46  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
  47  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  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 DNSProvider instance configured for Epik.
  61  // Credentials must be passed in the environment variable: EPIK_SIGNATURE.
  62  func NewDNSProvider() (*DNSProvider, error) {
  63  	values, err := env.Get(EnvSignature)
  64  	if err != nil {
  65  		return nil, fmt.Errorf("epik: %w", err)
  66  	}
  67  
  68  	config := NewDefaultConfig()
  69  	config.Signature = values[EnvSignature]
  70  
  71  	return NewDNSProviderConfig(config)
  72  }
  73  
  74  // NewDNSProviderConfig return a DNSProvider instance configured for Epik.
  75  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  76  	if config == nil {
  77  		return nil, errors.New("epik: the configuration of the DNS provider is nil")
  78  	}
  79  
  80  	if config.Signature == "" {
  81  		return nil, errors.New("epik: missing credentials")
  82  	}
  83  
  84  	client := internal.NewClient(config.Signature)
  85  
  86  	if config.HTTPClient != nil {
  87  		client.HTTPClient = config.HTTPClient
  88  	}
  89  
  90  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
  91  
  92  	return &DNSProvider{config: config, client: client}, nil
  93  }
  94  
  95  // Timeout returns the timeout and interval to use when checking for DNS propagation.
  96  // Adjusting here to cope with spikes in propagation times.
  97  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
  98  	return d.config.PropagationTimeout, d.config.PollingInterval
  99  }
 100  
 101  // Present creates a TXT record using the specified parameters.
 102  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
 103  	info := dns01.GetChallengeInfo(domain, keyAuth)
 104  
 105  	// find authZone
 106  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
 107  	if err != nil {
 108  		return fmt.Errorf("epik: could not find zone for domain %q: %w", domain, err)
 109  	}
 110  
 111  	subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
 112  	if err != nil {
 113  		return fmt.Errorf("epik: %w", err)
 114  	}
 115  
 116  	record := internal.RecordRequest{
 117  		Host: subDomain,
 118  		Type: "TXT",
 119  		Data: info.Value,
 120  		TTL:  d.config.TTL,
 121  	}
 122  
 123  	_, err = d.client.CreateHostRecord(context.Background(), dns01.UnFqdn(authZone), record)
 124  	if err != nil {
 125  		return fmt.Errorf("epik: %w", err)
 126  	}
 127  
 128  	return nil
 129  }
 130  
 131  // CleanUp removes the TXT record matching the specified parameters.
 132  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 133  	info := dns01.GetChallengeInfo(domain, keyAuth)
 134  
 135  	// find authZone
 136  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
 137  	if err != nil {
 138  		return fmt.Errorf("epik: could not find zone for domain %q: %w", domain, err)
 139  	}
 140  
 141  	dom := dns01.UnFqdn(authZone)
 142  
 143  	ctx := context.Background()
 144  
 145  	records, err := d.client.GetDNSRecords(ctx, dom)
 146  	if err != nil {
 147  		return fmt.Errorf("epik: %w", err)
 148  	}
 149  
 150  	subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
 151  	if err != nil {
 152  		return fmt.Errorf("epik: %w", err)
 153  	}
 154  
 155  	for _, record := range records {
 156  		if strings.EqualFold(record.Type, "TXT") && record.Data == info.Value && record.Name == subDomain {
 157  			_, err = d.client.RemoveHostRecord(ctx, dom, record.ID)
 158  			if err != nil {
 159  				return fmt.Errorf("epik: %w", err)
 160  			}
 161  		}
 162  	}
 163  
 164  	return nil
 165  }
 166