| 
 | 1 | +// Package combell implements a DNS provider for solving the DNS-01 challenge using Combell DNS.  | 
 | 2 | +package combell  | 
 | 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/dns01"  | 
 | 13 | +	"github.com/go-acme/lego/v4/platform/config/env"  | 
 | 14 | +	"github.com/go-acme/lego/v4/providers/dns/combell/internal"  | 
 | 15 | +)  | 
 | 16 | + | 
 | 17 | +const (  | 
 | 18 | +	minTTL = 60  | 
 | 19 | +	maxTTL = 8640  | 
 | 20 | +)  | 
 | 21 | + | 
 | 22 | +// Environment variables names.  | 
 | 23 | +const (  | 
 | 24 | +	envNamespace = "COMBELL_"  | 
 | 25 | + | 
 | 26 | +	EnvAPIKey    = envNamespace + "API_KEY"  | 
 | 27 | +	EnvAPISecret = envNamespace + "API_SECRET"  | 
 | 28 | + | 
 | 29 | +	EnvTTL                = envNamespace + "TTL"  | 
 | 30 | +	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"  | 
 | 31 | +	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"  | 
 | 32 | +	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"  | 
 | 33 | +)  | 
 | 34 | + | 
 | 35 | +// Config is used to configure the creation of the DNSProvider.  | 
 | 36 | +type Config struct {  | 
 | 37 | +	APIKey             string  | 
 | 38 | +	APISecret          string  | 
 | 39 | +	TTL                int  | 
 | 40 | +	PropagationTimeout time.Duration  | 
 | 41 | +	PollingInterval    time.Duration  | 
 | 42 | +	HTTPClient         *http.Client  | 
 | 43 | +}  | 
 | 44 | + | 
 | 45 | +// NewDefaultConfig returns a default configuration for the DNSProvider.  | 
 | 46 | +func NewDefaultConfig() *Config {  | 
 | 47 | +	return &Config{  | 
 | 48 | +		TTL:                env.GetOrDefaultInt(EnvTTL, 3600),  | 
 | 49 | +		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),  | 
 | 50 | +		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),  | 
 | 51 | +		HTTPClient: &http.Client{  | 
 | 52 | +			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),  | 
 | 53 | +		},  | 
 | 54 | +	}  | 
 | 55 | +}  | 
 | 56 | + | 
 | 57 | +// DNSProvider implements the challenge.Provider interface.  | 
 | 58 | +type DNSProvider struct {  | 
 | 59 | +	config *Config  | 
 | 60 | +	client *internal.Client  | 
 | 61 | +}  | 
 | 62 | + | 
 | 63 | +// NewDNSProvider returns a DNSProvider instance configured for Combell DNS.  | 
 | 64 | +// Credentials must be passed in the environment variables:  | 
 | 65 | +// COMBELL_API_KEY, COMBELL_API_SECRET.  | 
 | 66 | +func NewDNSProvider() (*DNSProvider, error) {  | 
 | 67 | +	values, err := env.Get(EnvAPIKey, EnvAPISecret)  | 
 | 68 | +	if err != nil {  | 
 | 69 | +		return nil, fmt.Errorf("combell: %w", err)  | 
 | 70 | +	}  | 
 | 71 | + | 
 | 72 | +	config := NewDefaultConfig()  | 
 | 73 | +	config.APIKey = values[EnvAPIKey]  | 
 | 74 | +	config.APISecret = values[EnvAPISecret]  | 
 | 75 | + | 
 | 76 | +	return NewDNSProviderConfig(config)  | 
 | 77 | +}  | 
 | 78 | + | 
 | 79 | +// NewDNSProviderConfig return a DNSProvider instance configured for Combell DNS.  | 
 | 80 | +func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {  | 
 | 81 | +	if config == nil {  | 
 | 82 | +		return nil, errors.New("combell: the configuration of the DNS provider is nil")  | 
 | 83 | +	}  | 
 | 84 | + | 
 | 85 | +	if config.APIKey == "" || config.APISecret == "" {  | 
 | 86 | +		return nil, errors.New("combell: some credentials information are missing")  | 
 | 87 | +	}  | 
 | 88 | + | 
 | 89 | +	if config.TTL < minTTL {  | 
 | 90 | +		return nil, fmt.Errorf("combell: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)  | 
 | 91 | +	}  | 
 | 92 | + | 
 | 93 | +	if config.TTL > maxTTL {  | 
 | 94 | +		return nil, fmt.Errorf("combell: invalid TTL, TTL (%d) must be lower than %d", config.TTL, maxTTL)  | 
 | 95 | +	}  | 
 | 96 | + | 
 | 97 | +	client := internal.NewClient(config.APIKey, config.APISecret, config.HTTPClient)  | 
 | 98 | + | 
 | 99 | +	return &DNSProvider{config: config, client: client}, 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 to fulfill the dns-01 challenge.  | 
 | 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("combell: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)  | 
 | 115 | +	}  | 
 | 116 | + | 
 | 117 | +	record := internal.Record{  | 
 | 118 | +		Type:       "TXT",  | 
 | 119 | +		RecordName: dns01.UnFqdn(strings.TrimSuffix(info.EffectiveFQDN, authZone)),  | 
 | 120 | +		Content:    info.Value,  | 
 | 121 | +		TTL:        d.config.TTL,  | 
 | 122 | +	}  | 
 | 123 | + | 
 | 124 | +	err = d.client.CreateRecord(context.Background(), authZone, record)  | 
 | 125 | +	if err != nil {  | 
 | 126 | +		return fmt.Errorf("combell: create record: %w", err)  | 
 | 127 | +	}  | 
 | 128 | + | 
 | 129 | +	return nil  | 
 | 130 | +}  | 
 | 131 | + | 
 | 132 | +// CleanUp removes the TXT record matching the specified parameters.  | 
 | 133 | +func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {  | 
 | 134 | +	ctx := context.Background()  | 
 | 135 | +	info := dns01.GetChallengeInfo(domain, keyAuth)  | 
 | 136 | + | 
 | 137 | +	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)  | 
 | 138 | +	if err != nil {  | 
 | 139 | +		return fmt.Errorf("combell: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)  | 
 | 140 | +	}  | 
 | 141 | + | 
 | 142 | +	request := &internal.GetRecordsRequest{  | 
 | 143 | +		Type:       "TXT",  | 
 | 144 | +		RecordName: dns01.UnFqdn(strings.TrimSuffix(info.EffectiveFQDN, authZone)),  | 
 | 145 | +	}  | 
 | 146 | +	records, err := d.client.GetRecords(ctx, authZone, request)  | 
 | 147 | +	if err != nil {  | 
 | 148 | +		return fmt.Errorf("combell: get records: %w", err)  | 
 | 149 | +	}  | 
 | 150 | + | 
 | 151 | +	for _, record := range records {  | 
 | 152 | +		if record.Content == info.Value {  | 
 | 153 | +			err = d.client.DeleteRecord(ctx, authZone, record.ID)  | 
 | 154 | +			if err != nil {  | 
 | 155 | +				return fmt.Errorf("combell: delete record: %w", err)  | 
 | 156 | +			}  | 
 | 157 | +		}  | 
 | 158 | +	}  | 
 | 159 | + | 
 | 160 | +	return nil  | 
 | 161 | +}  | 
0 commit comments