nearlyfreespeech.go raw
1 // Package nearlyfreespeech implements a DNS provider for solving the DNS-01 challenge using NearlyFreeSpeech.NET.
2 package nearlyfreespeech
3
4 import (
5 "context"
6 "errors"
7 "fmt"
8 "net/http"
9 "time"
10
11 "github.com/go-acme/lego/v4/challenge"
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/internal/clientdebug"
15 "github.com/go-acme/lego/v4/providers/dns/nearlyfreespeech/internal"
16 )
17
18 // Environment variables names.
19 const (
20 envNamespace = "NEARLYFREESPEECH_"
21
22 EnvLogin = envNamespace + "LOGIN"
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 EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL"
30 )
31
32 var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
33
34 // Config is used to configure the creation of the DNSProvider.
35 type Config struct {
36 APIKey string
37 Login string
38
39 TTL int
40 PropagationTimeout time.Duration
41 PollingInterval time.Duration
42 SequenceInterval time.Duration
43 HTTPClient *http.Client
44 }
45
46 // NewDefaultConfig returns a default configuration for the DNSProvider.
47 func NewDefaultConfig() *Config {
48 return &Config{
49 TTL: env.GetOrDefaultInt(EnvTTL, 3600),
50 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
51 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
52 SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout),
53 HTTPClient: &http.Client{
54 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
55 },
56 }
57 }
58
59 // DNSProvider implements the challenge.Provider interface.
60 type DNSProvider struct {
61 config *Config
62 client *internal.Client
63 }
64
65 // NewDNSProvider returns a DNSProvider instance configured for NearlyFreeSpeech.NET.
66 // Credentials must be passed in the environment variable: NEARLYFREESPEECH_LOGIN, NEARLYFREESPEECH_API_KEY.
67 func NewDNSProvider() (*DNSProvider, error) {
68 values, err := env.Get(EnvAPIKey, EnvLogin)
69 if err != nil {
70 return nil, fmt.Errorf("nearlyfreespeech: %w", err)
71 }
72
73 config := NewDefaultConfig()
74 config.APIKey = values[EnvAPIKey]
75 config.Login = values[EnvLogin]
76
77 return NewDNSProviderConfig(config)
78 }
79
80 // NewDNSProviderConfig return a DNSProvider instance configured for NearlyFreeSpeech.NET.
81 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
82 if config == nil {
83 return nil, errors.New("nearlyfreespeech: the configuration of the DNS provider is nil")
84 }
85
86 if config.Login == "" || config.APIKey == "" {
87 return nil, errors.New("nearlyfreespeech: API credentials are missing")
88 }
89
90 client := internal.NewClient(config.Login, config.APIKey)
91
92 if config.HTTPClient != nil {
93 client.HTTPClient = config.HTTPClient
94 }
95
96 client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
97
98 return &DNSProvider{
99 config: config,
100 client: client,
101 }, nil
102 }
103
104 // Timeout returns the timeout and interval to use when checking for DNS propagation.
105 // Adjusting here to cope with spikes in propagation times.
106 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
107 return d.config.PropagationTimeout, d.config.PollingInterval
108 }
109
110 // Sequential All DNS challenges for this provider will be resolved sequentially.
111 // Returns the interval between each iteration.
112 func (d *DNSProvider) Sequential() time.Duration {
113 return d.config.SequenceInterval
114 }
115
116 // Present creates a TXT record to fulfill the dns-01 challenge.
117 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
118 info := dns01.GetChallengeInfo(domain, keyAuth)
119
120 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
121 if err != nil {
122 return fmt.Errorf("nearlyfreespeech: could not find zone for domain %q: %w", domain, err)
123 }
124
125 recordName, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
126 if err != nil {
127 return fmt.Errorf("nearlyfreespeech: %w", err)
128 }
129
130 record := internal.Record{
131 Name: recordName,
132 Type: "TXT",
133 Data: info.Value,
134 TTL: d.config.TTL,
135 }
136
137 err = d.client.AddRecord(context.Background(), authZone, record)
138 if err != nil {
139 return fmt.Errorf("nearlyfreespeech: %w", err)
140 }
141
142 return nil
143 }
144
145 // CleanUp removes the TXT record matching the specified parameters.
146 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
147 info := dns01.GetChallengeInfo(domain, keyAuth)
148
149 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
150 if err != nil {
151 return fmt.Errorf("nearlyfreespeech: could not find zone for domain %q: %w", domain, err)
152 }
153
154 recordName, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
155 if err != nil {
156 return fmt.Errorf("nearlyfreespeech: %w", err)
157 }
158
159 record := internal.Record{
160 Name: recordName,
161 Type: "TXT",
162 Data: info.Value,
163 }
164
165 err = d.client.RemoveRecord(context.Background(), domain, record)
166 if err != nil {
167 return fmt.Errorf("nearlyfreespeech: %w", err)
168 }
169
170 return nil
171 }
172