autodns.go raw
1 // Package autodns implements a DNS provider for solving the DNS-01 challenge using auto DNS.
2 package autodns
3
4 import (
5 "context"
6 "errors"
7 "fmt"
8 "net/http"
9 "net/url"
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/autodns/internal"
16 "github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
17 )
18
19 // Environment variables names.
20 const (
21 envNamespace = "AUTODNS_"
22
23 EnvAPIUser = envNamespace + "API_USER"
24 EnvAPIPassword = envNamespace + "API_PASSWORD"
25 EnvAPIEndpoint = envNamespace + "ENDPOINT"
26 EnvAPIEndpointContext = envNamespace + "CONTEXT"
27
28 EnvTTL = envNamespace + "TTL"
29 EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
30 EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
31 EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
32 )
33
34 var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
35
36 // Config is used to configure the creation of the DNSProvider.
37 type Config struct {
38 Endpoint *url.URL
39 Username string
40 Password string
41 Context int
42 TTL int
43 PropagationTimeout time.Duration
44 PollingInterval time.Duration
45 HTTPClient *http.Client
46 }
47
48 // NewDefaultConfig returns a default configuration for the DNSProvider.
49 func NewDefaultConfig() *Config {
50 endpoint, _ := url.Parse(env.GetOrDefaultString(EnvAPIEndpoint, internal.DefaultEndpoint))
51
52 return &Config{
53 Endpoint: endpoint,
54 Context: env.GetOrDefaultInt(EnvAPIEndpointContext, internal.DefaultEndpointContext),
55 TTL: env.GetOrDefaultInt(EnvTTL, 600),
56 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
57 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
58 HTTPClient: &http.Client{
59 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
60 },
61 }
62 }
63
64 // DNSProvider implements the challenge.Provider interface.
65 type DNSProvider struct {
66 config *Config
67 client *internal.Client
68 }
69
70 // NewDNSProvider returns a DNSProvider instance configured for autoDNS.
71 // Credentials must be passed in the environment variables.
72 func NewDNSProvider() (*DNSProvider, error) {
73 values, err := env.Get(EnvAPIUser, EnvAPIPassword)
74 if err != nil {
75 return nil, fmt.Errorf("autodns: %w", err)
76 }
77
78 config := NewDefaultConfig()
79 config.Username = values[EnvAPIUser]
80 config.Password = values[EnvAPIPassword]
81
82 return NewDNSProviderConfig(config)
83 }
84
85 // NewDNSProviderConfig return a DNSProvider instance configured for autoDNS.
86 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
87 if config == nil {
88 return nil, errors.New("autodns: config is nil")
89 }
90
91 if config.Username == "" {
92 return nil, errors.New("autodns: missing user")
93 }
94
95 if config.Password == "" {
96 return nil, errors.New("autodns: missing password")
97 }
98
99 client := internal.NewClient(config.Username, config.Password, config.Context)
100
101 if config.Endpoint != nil {
102 client.BaseURL = config.Endpoint
103 }
104
105 if config.HTTPClient != nil {
106 client.HTTPClient = config.HTTPClient
107 }
108
109 client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
110
111 return &DNSProvider{config: config, client: client}, nil
112 }
113
114 // Timeout returns the timeout and interval to use when checking for DNS propagation.
115 // Adjusting here to cope with spikes in propagation times.
116 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
117 return d.config.PropagationTimeout, d.config.PollingInterval
118 }
119
120 // Present creates a TXT record to fulfill the dns-01 challenge.
121 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
122 info := dns01.GetChallengeInfo(domain, keyAuth)
123
124 records := []*internal.ResourceRecord{{
125 Name: info.EffectiveFQDN,
126 TTL: int64(d.config.TTL),
127 Type: "TXT",
128 Value: info.Value,
129 }}
130
131 _, err := d.client.AddRecords(context.Background(), info.EffectiveFQDN, records)
132 if err != nil {
133 return fmt.Errorf("autodns: add record: %w", err)
134 }
135
136 return nil
137 }
138
139 // CleanUp removes the TXT record previously created.
140 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
141 info := dns01.GetChallengeInfo(domain, keyAuth)
142
143 records := []*internal.ResourceRecord{{
144 Name: info.EffectiveFQDN,
145 TTL: int64(d.config.TTL),
146 Type: "TXT",
147 Value: info.Value,
148 }}
149
150 _, err := d.client.RemoveRecords(context.Background(), info.EffectiveFQDN, records)
151 if err != nil {
152 return fmt.Errorf("autodns: remove record: %w", err)
153 }
154
155 return nil
156 }
157