|
| 1 | +// Package hostingnl implements a DNS provider for solving the DNS-01 challenge using hosting.nl. |
| 2 | +package hostingnl |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "strconv" |
| 10 | + "sync" |
| 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/hostingnl/internal" |
| 17 | +) |
| 18 | + |
| 19 | +// Environment variables names. |
| 20 | +const ( |
| 21 | + envNamespace = "HOSTINGNL_" |
| 22 | + |
| 23 | + EnvAPIKey = envNamespace + "API_KEY" |
| 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 | + APIKey string |
| 36 | + HTTPClient *http.Client |
| 37 | + PropagationTimeout time.Duration |
| 38 | + PollingInterval time.Duration |
| 39 | + TTL int |
| 40 | +} |
| 41 | + |
| 42 | +// NewDefaultConfig returns a default configuration for the DNSProvider. |
| 43 | +func NewDefaultConfig() *Config { |
| 44 | + return &Config{ |
| 45 | + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), |
| 46 | + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), |
| 47 | + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), |
| 48 | + HTTPClient: &http.Client{ |
| 49 | + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*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 | + recordIDs map[string]string |
| 60 | + recordIDsMu sync.Mutex |
| 61 | +} |
| 62 | + |
| 63 | +// NewDNSProvider returns a DNSProvider instance configured for hosting.nl. |
| 64 | +// Credentials must be passed in the environment variables: |
| 65 | +// HOSTINGNL_APIKEY. |
| 66 | +func NewDNSProvider() (*DNSProvider, error) { |
| 67 | + values, err := env.Get(EnvAPIKey) |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("hostingnl: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + config := NewDefaultConfig() |
| 73 | + config.APIKey = values[EnvAPIKey] |
| 74 | + |
| 75 | + return NewDNSProviderConfig(config) |
| 76 | +} |
| 77 | + |
| 78 | +// NewDNSProviderConfig return a DNSProvider instance configured for hosting.nl. |
| 79 | +func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { |
| 80 | + if config == nil { |
| 81 | + return nil, errors.New("hostingnl: the configuration of the DNS provider is nil") |
| 82 | + } |
| 83 | + |
| 84 | + if config.APIKey == "" { |
| 85 | + return nil, errors.New("hostingnl: APIKey is missing") |
| 86 | + } |
| 87 | + |
| 88 | + client := internal.NewClient(config.APIKey) |
| 89 | + |
| 90 | + if config.HTTPClient != nil { |
| 91 | + client.HTTPClient = config.HTTPClient |
| 92 | + } |
| 93 | + |
| 94 | + return &DNSProvider{ |
| 95 | + config: config, |
| 96 | + client: client, |
| 97 | + recordIDs: make(map[string]string), |
| 98 | + }, nil |
| 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 | + authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("hostingnl: could not find zone for domain %q: %w", domain, err) |
| 108 | + } |
| 109 | + |
| 110 | + record := internal.Record{ |
| 111 | + Name: info.EffectiveFQDN, |
| 112 | + Type: "TXT", |
| 113 | + Content: strconv.Quote(info.Value), |
| 114 | + TTL: strconv.Itoa(d.config.TTL), |
| 115 | + Priority: "0", |
| 116 | + } |
| 117 | + |
| 118 | + newRecord, err := d.client.AddRecord(context.Background(), dns01.UnFqdn(authZone), record) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("hostingnl: failed to create TXT record, fqdn=%s: %w", info.EffectiveFQDN, err) |
| 121 | + } |
| 122 | + |
| 123 | + d.recordIDsMu.Lock() |
| 124 | + d.recordIDs[token] = newRecord.ID |
| 125 | + d.recordIDsMu.Unlock() |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +// CleanUp removes the TXT records 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("hostingnl: could not find zone for domain %q: %w", domain, err) |
| 137 | + } |
| 138 | + |
| 139 | + // gets the record's unique ID |
| 140 | + d.recordIDsMu.Lock() |
| 141 | + recordID, ok := d.recordIDs[token] |
| 142 | + d.recordIDsMu.Unlock() |
| 143 | + if !ok { |
| 144 | + return fmt.Errorf("hostingnl: unknown record ID for '%s' '%s'", info.EffectiveFQDN, token) |
| 145 | + } |
| 146 | + |
| 147 | + err = d.client.DeleteRecord(context.Background(), dns01.UnFqdn(authZone), recordID) |
| 148 | + if err != nil { |
| 149 | + return fmt.Errorf("hostingnl: failed to delete TXT record, id=%s: %w", recordID, err) |
| 150 | + } |
| 151 | + |
| 152 | + // deletes record ID from map |
| 153 | + d.recordIDsMu.Lock() |
| 154 | + delete(d.recordIDs, token) |
| 155 | + d.recordIDsMu.Unlock() |
| 156 | + |
| 157 | + return nil |
| 158 | +} |
| 159 | + |
| 160 | +// Timeout returns the timeout and interval to use when checking for DNS propagation. |
| 161 | +// Adjusting here to cope with spikes in propagation times. |
| 162 | +func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { |
| 163 | + return d.config.PropagationTimeout, d.config.PollingInterval |
| 164 | +} |
0 commit comments