glesys.go raw
1 // Package glesys implements a DNS provider for solving the DNS-01 challenge using GleSYS api.
2 package glesys
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/glesys/internal"
16 "github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
17 )
18
19 // Environment variables names.
20 const (
21 envNamespace = "GLESYS_"
22
23 EnvAPIUser = envNamespace + "API_USER"
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 const minTTL = 60
33
34 var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
35
36 // Config is used to configure the creation of the DNSProvider.
37 type Config struct {
38 APIUser string
39 APIKey string
40 PropagationTimeout time.Duration
41 PollingInterval time.Duration
42 TTL int
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, minTTL),
50 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 20*time.Minute),
51 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 20*time.Second),
52 HTTPClient: &http.Client{
53 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second),
54 },
55 }
56 }
57
58 // DNSProvider implements the challenge.Provider interface.
59 type DNSProvider struct {
60 config *Config
61 client *internal.Client
62
63 activeRecords map[string]int
64 inProgressMu sync.Mutex
65 }
66
67 // NewDNSProvider returns a DNSProvider instance configured for GleSYS.
68 // Credentials must be passed in the environment variables:
69 // GLESYS_API_USER and GLESYS_API_KEY.
70 func NewDNSProvider() (*DNSProvider, error) {
71 values, err := env.Get(EnvAPIUser, EnvAPIKey)
72 if err != nil {
73 return nil, fmt.Errorf("glesys: %w", err)
74 }
75
76 config := NewDefaultConfig()
77 config.APIUser = values[EnvAPIUser]
78 config.APIKey = values[EnvAPIKey]
79
80 return NewDNSProviderConfig(config)
81 }
82
83 // NewDNSProviderConfig return a DNSProvider instance configured for GleSYS.
84 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
85 if config == nil {
86 return nil, errors.New("glesys: the configuration of the DNS provider is nil")
87 }
88
89 if config.APIUser == "" || config.APIKey == "" {
90 return nil, errors.New("glesys: incomplete credentials provided")
91 }
92
93 if config.TTL < minTTL {
94 return nil, fmt.Errorf("glesys: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
95 }
96
97 client := internal.NewClient(config.APIUser, config.APIKey)
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 activeRecords: make(map[string]int),
109 }, nil
110 }
111
112 // Present creates a TXT record using the specified parameters.
113 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
114 info := dns01.GetChallengeInfo(domain, keyAuth)
115
116 // find authZone
117 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
118 if err != nil {
119 return fmt.Errorf("glesys: could not find zone for domain %q: %w", domain, err)
120 }
121
122 subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
123 if err != nil {
124 return fmt.Errorf("glesys: %w", err)
125 }
126
127 // acquire lock and check there is not a challenge already in progress for this value of authZone
128 d.inProgressMu.Lock()
129 defer d.inProgressMu.Unlock()
130
131 // add TXT record into authZone
132 recordID, err := d.client.AddTXTRecord(context.Background(), dns01.UnFqdn(authZone), subDomain, info.Value, d.config.TTL)
133 if err != nil {
134 return err
135 }
136
137 // save data necessary for CleanUp
138 d.activeRecords[info.EffectiveFQDN] = recordID
139
140 return nil
141 }
142
143 // CleanUp removes the TXT record matching the specified parameters.
144 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
145 info := dns01.GetChallengeInfo(domain, keyAuth)
146
147 // acquire lock and retrieve authZone
148 d.inProgressMu.Lock()
149 defer d.inProgressMu.Unlock()
150
151 if _, ok := d.activeRecords[info.EffectiveFQDN]; !ok {
152 // if there is no cleanup information then just return
153 return nil
154 }
155
156 recordID := d.activeRecords[info.EffectiveFQDN]
157 delete(d.activeRecords, info.EffectiveFQDN)
158
159 // delete TXT record from authZone
160 return d.client.DeleteTXTRecord(context.Background(), recordID)
161 }
162
163 // Timeout returns the values (20*time.Minute, 20*time.Second) which
164 // are used by the acme package as timeout and check interval values
165 // when checking for DNS record propagation with GleSYS.
166 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
167 return d.config.PropagationTimeout, d.config.PollingInterval
168 }
169