simply.go raw
1 // Package simply implements a DNS provider for solving the DNS-01 challenge using Simply.com.
2 package simply
3
4 import (
5 "context"
6 "errors"
7 "fmt"
8 "net/http"
9 "sync"
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/internal/clientdebug"
16 "github.com/go-acme/lego/v4/providers/dns/simply/internal"
17 )
18
19 // Environment variables names.
20 const (
21 envNamespace = "SIMPLY_"
22
23 EnvAccountName = envNamespace + "ACCOUNT_NAME"
24 EnvAPIKey = envNamespace + "API_KEY"
25
26 EnvTTL = envNamespace + "TTL"
27 EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
28 EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
29 EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
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 AccountName string
37 APIKey string
38 PropagationTimeout time.Duration
39 PollingInterval time.Duration
40 TTL int
41 HTTPClient *http.Client
42 }
43
44 // NewDefaultConfig returns a default configuration for the DNSProvider.
45 func NewDefaultConfig() *Config {
46 return &Config{
47 TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
48 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 5*time.Minute),
49 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 10*time.Second),
50 HTTPClient: &http.Client{
51 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
52 },
53 }
54 }
55
56 // DNSProvider implements the challenge.Provider interface.
57 type DNSProvider struct {
58 config *Config
59 client *internal.Client
60
61 recordIDs map[string]int64
62 recordIDsMu sync.Mutex
63 }
64
65 // NewDNSProvider returns a DNSProvider instance configured for Simply.com.
66 // Credentials must be passed in the environment variable: SIMPLY_ACCOUNT_NAME, SIMPLY_API_KEY.
67 func NewDNSProvider() (*DNSProvider, error) {
68 values, err := env.Get(EnvAccountName, EnvAPIKey)
69 if err != nil {
70 return nil, fmt.Errorf("simply: %w", err)
71 }
72
73 config := NewDefaultConfig()
74 config.AccountName = values[EnvAccountName]
75 config.APIKey = values[EnvAPIKey]
76
77 return NewDNSProviderConfig(config)
78 }
79
80 // NewDNSProviderConfig return a DNSProvider instance configured for Simply.com.
81 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
82 if config == nil {
83 return nil, errors.New("simply: the configuration of the DNS provider is nil")
84 }
85
86 if config.AccountName == "" {
87 return nil, errors.New("simply: missing credentials: account name")
88 }
89
90 if config.APIKey == "" {
91 return nil, errors.New("simply: missing credentials: api key")
92 }
93
94 client, err := internal.NewClient(config.AccountName, config.APIKey)
95 if err != nil {
96 return nil, fmt.Errorf("simply: failed to create client: %w", err)
97 }
98
99 if config.HTTPClient != nil {
100 client.HTTPClient = config.HTTPClient
101 }
102
103 client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
104
105 return &DNSProvider{
106 config: config,
107 client: client,
108 recordIDs: make(map[string]int64),
109 }, nil
110 }
111
112 // Timeout returns the timeout and interval to use when checking for DNS propagation.
113 // Adjusting here to cope with spikes in propagation times.
114 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
115 return d.config.PropagationTimeout, d.config.PollingInterval
116 }
117
118 // Present creates a TXT record using the specified parameters.
119 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
120 info := dns01.GetChallengeInfo(domain, keyAuth)
121
122 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
123 if err != nil {
124 return fmt.Errorf("simply: could not find zone for domain %q: %w", domain, err)
125 }
126
127 authZone = dns01.UnFqdn(authZone)
128
129 subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
130 if err != nil {
131 return fmt.Errorf("regru: %w", err)
132 }
133
134 recordBody := internal.Record{
135 Name: subDomain,
136 Data: info.Value,
137 Type: "TXT",
138 TTL: d.config.TTL,
139 }
140
141 recordID, err := d.client.AddRecord(context.Background(), authZone, recordBody)
142 if err != nil {
143 return fmt.Errorf("simply: failed to add record: %w", err)
144 }
145
146 d.recordIDsMu.Lock()
147 d.recordIDs[token] = recordID
148 d.recordIDsMu.Unlock()
149
150 return nil
151 }
152
153 // CleanUp removes the TXT record matching the specified parameters.
154 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
155 info := dns01.GetChallengeInfo(domain, keyAuth)
156
157 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
158 if err != nil {
159 return fmt.Errorf("simply: could not find zone for domain %q: %w", domain, err)
160 }
161
162 authZone = dns01.UnFqdn(authZone)
163
164 // gets the record's unique ID from when we created it
165 d.recordIDsMu.Lock()
166 recordID, ok := d.recordIDs[token]
167 d.recordIDsMu.Unlock()
168
169 if !ok {
170 return fmt.Errorf("simply: unknown record ID for '%s' '%s'", info.EffectiveFQDN, token)
171 }
172
173 err = d.client.DeleteRecord(context.Background(), authZone, recordID)
174 if err != nil {
175 return fmt.Errorf("simply: failed to delete TXT records: fqdn=%s, recordID=%d: %w", info.EffectiveFQDN, recordID, err)
176 }
177
178 // deletes record ID from map
179 d.recordIDsMu.Lock()
180 delete(d.recordIDs, token)
181 d.recordIDsMu.Unlock()
182
183 return nil
184 }
185