beget.go raw
1 // Package beget implements a DNS provider for solving the DNS-01 challenge using beget.com DNS.
2 package beget
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/beget/internal"
15 "github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
16 )
17
18 // Environment variables names.
19 const (
20 envNamespace = "BEGET_"
21
22 EnvUsername = envNamespace + "USERNAME"
23 EnvPassword = envNamespace + "PASSWORD"
24
25 EnvTTL = envNamespace + "TTL"
26 EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
27 EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
28 EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
29 )
30
31 var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
32
33 // Config is used to configure the creation of the DNSProvider.
34 type Config struct {
35 Username string
36 Password string
37
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, 300),
48 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 5*time.Minute),
49 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 30*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
62 // NewDNSProvider returns a DNSProvider instance configured for beget.com.
63 // Credentials must be passed in the environment variables:
64 // BEGET_USERNAME and BEGET_PASSWORD.
65 func NewDNSProvider() (*DNSProvider, error) {
66 values, err := env.Get(EnvUsername, EnvPassword)
67 if err != nil {
68 return nil, fmt.Errorf("beget: %w", err)
69 }
70
71 config := NewDefaultConfig()
72 config.Username = values[EnvUsername]
73 config.Password = values[EnvPassword]
74
75 return NewDNSProviderConfig(config)
76 }
77
78 // NewDNSProviderConfig return a DNSProvider instance configured for beget.com.
79 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
80 if config == nil {
81 return nil, errors.New("beget: the configuration of the DNS provider is nil")
82 }
83
84 if config.Username == "" || config.Password == "" {
85 return nil, errors.New("beget: incomplete credentials, missing username and/or password")
86 }
87
88 client := internal.NewClient(config.Username, config.Password)
89
90 if config.HTTPClient != nil {
91 client.HTTPClient = config.HTTPClient
92 }
93
94 client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
95
96 return &DNSProvider{config: config, client: client}, nil
97 }
98
99 // Timeout returns the timeout and interval to use when checking for DNS propagation.
100 // Adjusting here to cope with spikes in propagation times.
101 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
102 return d.config.PropagationTimeout, d.config.PollingInterval
103 }
104
105 // Present creates a TXT record using the specified parameters.
106 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
107 ctx := context.Background()
108
109 info := dns01.GetChallengeInfo(domain, keyAuth)
110
111 records, err := d.client.GetTXTRecords(ctx, dns01.UnFqdn(info.EffectiveFQDN))
112 if err != nil {
113 return fmt.Errorf("beget: get TXT records: %w", err)
114 }
115
116 records = append(records, internal.Record{
117 Value: info.Value,
118 Data: "", // NOTE: there are 2 fields in the API for the same thing.
119 Priority: 10,
120 TTL: d.config.TTL,
121 })
122
123 err = d.client.ChangeTXTRecord(ctx, dns01.UnFqdn(info.EffectiveFQDN), records)
124 if err != nil {
125 return fmt.Errorf("beget: failed to create TXT records [domain: %s]: %w",
126 dns01.UnFqdn(info.EffectiveFQDN), err)
127 }
128
129 return nil
130 }
131
132 // CleanUp removes the TXT record matching the specified parameters.
133 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
134 ctx := context.Background()
135
136 info := dns01.GetChallengeInfo(domain, keyAuth)
137
138 records, err := d.client.GetTXTRecords(ctx, dns01.UnFqdn(info.EffectiveFQDN))
139 if err != nil {
140 return fmt.Errorf("beget: get TXT records: %w", err)
141 }
142
143 if len(records) == 0 {
144 return nil
145 }
146
147 var updatedRecords []internal.Record
148
149 for _, record := range records {
150 if record.Data == info.Value {
151 continue
152 }
153
154 updatedRecords = append(updatedRecords, record)
155 }
156
157 err = d.client.ChangeTXTRecord(ctx, dns01.UnFqdn(info.EffectiveFQDN), updatedRecords)
158 if err != nil {
159 return fmt.Errorf("beget: failed to remove TXT records [domain: %s]: %w",
160 dns01.UnFqdn(info.EffectiveFQDN), err)
161 }
162
163 return nil
164 }
165