vegadns.go raw
1 // Package vegadns implements a DNS provider for solving the DNS-01 challenge using VegaDNS.
2 package vegadns
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/nrdcg/vegadns"
16 )
17
18 // Environment variables names.
19 const (
20 envNamespace = "VEGADNS_"
21
22 EnvKey = "SECRET_VEGADNS_KEY"
23 EnvSecret = "SECRET_VEGADNS_SECRET"
24 EnvURL = envNamespace + "URL"
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 BaseURL string
37 APIKey string
38 APISecret string
39
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, 10),
50 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 12*time.Minute),
51 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, time.Minute),
52 HTTPClient: &http.Client{
53 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
54 },
55 }
56 }
57
58 // DNSProvider implements the challenge.Provider interface.
59 type DNSProvider struct {
60 config *Config
61 client *vegadns.Client
62 }
63
64 // NewDNSProvider returns a DNSProvider instance configured for VegaDNS.
65 // Credentials must be passed in the environment variables:
66 // VEGADNS_URL, SECRET_VEGADNS_KEY, SECRET_VEGADNS_SECRET.
67 func NewDNSProvider() (*DNSProvider, error) {
68 values, err := env.Get(EnvURL)
69 if err != nil {
70 return nil, fmt.Errorf("vegadns: %w", err)
71 }
72
73 config := NewDefaultConfig()
74 config.BaseURL = values[EnvURL]
75 config.APIKey = env.GetOrFile(EnvKey)
76 config.APISecret = env.GetOrFile(EnvSecret)
77
78 return NewDNSProviderConfig(config)
79 }
80
81 // NewDNSProviderConfig return a DNSProvider instance configured for VegaDNS.
82 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
83 if config == nil {
84 return nil, errors.New("vegadns: the configuration of the DNS provider is nil")
85 }
86
87 if config.HTTPClient == nil {
88 config.HTTPClient = &http.Client{Timeout: 30 * time.Second}
89 }
90
91 config.HTTPClient = clientdebug.Wrap(config.HTTPClient)
92
93 client, err := vegadns.NewClient(config.BaseURL,
94 vegadns.WithOAuth(config.APIKey, config.APISecret),
95 vegadns.WithHTTPClient(config.HTTPClient),
96 )
97 if err != nil {
98 return nil, fmt.Errorf("vegadns: %w", err)
99 }
100
101 return &DNSProvider{client: client, config: config}, 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 // Present creates a TXT record to fulfill the dns-01 challenge.
111 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
112 ctx := context.Background()
113
114 info := dns01.GetChallengeInfo(domain, keyAuth)
115
116 domainID, err := d.findDomainID(ctx, info.EffectiveFQDN)
117 if err != nil {
118 return fmt.Errorf("vegadns: find domain ID for %s: %w", info.EffectiveFQDN, err)
119 }
120
121 err = d.client.CreateTXTRecord(ctx, domainID, dns01.UnFqdn(info.EffectiveFQDN), info.Value, d.config.TTL)
122 if err != nil {
123 return fmt.Errorf("vegadns: create TXT record: %w", err)
124 }
125
126 return nil
127 }
128
129 // CleanUp removes the TXT record matching the specified parameters.
130 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
131 ctx := context.Background()
132
133 info := dns01.GetChallengeInfo(domain, keyAuth)
134
135 domainID, err := d.findDomainID(ctx, info.EffectiveFQDN)
136 if err != nil {
137 return fmt.Errorf("vegadns: find domain ID for %s: %w", info.EffectiveFQDN, err)
138 }
139
140 recordID, err := d.findRecordID(ctx, domainID, dns01.UnFqdn(info.EffectiveFQDN))
141 if err != nil {
142 return fmt.Errorf("vegadns: find record ID for %d: %w", domainID, err)
143 }
144
145 err = d.client.DeleteRecord(ctx, recordID)
146 if err != nil {
147 return fmt.Errorf("vegadns: delete record: %w", err)
148 }
149
150 return nil
151 }
152
153 func (d *DNSProvider) findDomainID(ctx context.Context, fqdn string) (int, error) {
154 for host := range dns01.UnFqdnDomainsSeq(fqdn) {
155 id, err := d.client.GetDomainID(ctx, host)
156 if err != nil {
157 continue
158 }
159
160 return id, nil
161 }
162
163 return 0, errors.New("domain not found")
164 }
165
166 func (d *DNSProvider) findRecordID(ctx context.Context, domainID int, name string) (int, error) {
167 records, err := d.client.GetRecords(ctx, domainID)
168 if err != nil {
169 return 0, fmt.Errorf("get records: %w", err)
170 }
171
172 for _, r := range records {
173 if r.Name == name && r.RecordType == "TXT" {
174 return r.RecordID, nil
175 }
176 }
177
178 return 0, errors.New("record not found")
179 }
180