|
1 |
| -// Package beget implements a DNS provider for solving the DNS-01 challenge using beget.com DNS. |
2 |
| -package beget |
3 |
| - |
4 |
| -import ( |
5 |
| - "errors" |
6 |
| - "fmt" |
7 |
| - "net/http" |
8 |
| - "time" |
9 |
| - |
10 |
| - "github.com/go-acme/lego/v4/challenge/dns01" |
11 |
| - "github.com/go-acme/lego/v4/platform/config/env" |
12 |
| - "github.com/go-acme/lego/v4/providers/dns/beget/internal" |
13 |
| -) |
14 |
| - |
15 |
| -// Environment variables names. |
16 |
| -const ( |
17 |
| - envNamespace = "BEGET_" |
18 |
| - |
19 |
| - EnvUsername = envNamespace + "USERNAME" |
20 |
| - EnvPassword = envNamespace + "PASSWORD" |
21 |
| - |
22 |
| - EnvTTL = envNamespace + "TTL" |
23 |
| - EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" |
24 |
| - EnvPollingInterval = envNamespace + "POLLING_INTERVAL" |
25 |
| - EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" |
26 |
| -) |
27 |
| - |
28 |
| -// Config is used to configure the creation of the DNSProvider. |
29 |
| -type Config struct { |
30 |
| - Username string |
31 |
| - Password string |
32 |
| - |
33 |
| - PropagationTimeout time.Duration |
34 |
| - PollingInterval time.Duration |
35 |
| - TTL int |
36 |
| - HTTPClient *http.Client |
37 |
| -} |
38 |
| - |
39 |
| -// NewDefaultConfig returns a default configuration for the DNSProvider. |
40 |
| -func NewDefaultConfig() *Config { |
41 |
| - return &Config{ |
42 |
| - TTL: env.GetOrDefaultInt(EnvTTL, 300), |
43 |
| - PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout*2), //2m |
44 |
| - PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), |
45 |
| - HTTPClient: &http.Client{ |
46 |
| - Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), |
47 |
| - }, |
48 |
| - } |
49 |
| -} |
50 |
| - |
51 |
| -// DNSProvider implements the challenge.Provider interface. |
52 |
| -type DNSProvider struct { |
53 |
| - config *Config |
54 |
| - client *internal.Client |
55 |
| -} |
56 |
| - |
57 |
| -// NewDNSProvider returns a DNSProvider instance configured for beget.com. |
58 |
| -// Credentials must be passed in the environment variables: |
59 |
| -// BEGET_USERNAME and BEGET_PASSWORD. |
60 |
| -func NewDNSProvider() (*DNSProvider, error) { |
61 |
| - values, err := env.Get(EnvUsername, EnvPassword) |
62 |
| - if err != nil { |
63 |
| - return nil, fmt.Errorf("beget: %w", err) |
64 |
| - } |
65 |
| - |
66 |
| - config := NewDefaultConfig() |
67 |
| - config.Username = values[EnvUsername] |
68 |
| - config.Password = values[EnvPassword] |
69 |
| - |
70 |
| - return NewDNSProviderConfig(config) |
71 |
| -} |
72 |
| - |
73 |
| -// NewDNSProviderConfig return a DNSProvider instance configured for beget.com. |
74 |
| -func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { |
75 |
| - if config == nil { |
76 |
| - return nil, errors.New("beget: the configuration of the DNS provider is nil") |
77 |
| - } |
78 |
| - |
79 |
| - if config.Username == "" || config.Password == "" { |
80 |
| - return nil, errors.New("beget: incomplete credentials, missing username and/or password") |
81 |
| - } |
82 |
| - |
83 |
| - client := internal.NewClient(config.Username, config.Password) |
84 |
| - |
85 |
| - if config.HTTPClient != nil { |
86 |
| - client.HTTPClient = config.HTTPClient |
87 |
| - } |
88 |
| - |
89 |
| - return &DNSProvider{config: config, client: client}, nil |
90 |
| -} |
91 |
| - |
92 |
| -// Timeout returns the timeout and interval to use when checking for DNS propagation. |
93 |
| -// Adjusting here to cope with spikes in propagation times. |
94 |
| -func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { |
95 |
| - return d.config.PropagationTimeout, d.config.PollingInterval |
96 |
| -} |
97 |
| - |
98 |
| -// Present creates a TXT record using the specified parameters. |
99 |
| -func (d *DNSProvider) Present(domain, token, keyAuth string) error { |
100 |
| - info := dns01.GetChallengeInfo(domain, keyAuth) |
101 |
| - |
102 |
| - authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) |
103 |
| - if err != nil { |
104 |
| - return fmt.Errorf("beget: could not find zone for domain %q and fqdn %q : %w", domain, info.EffectiveFQDN, err) |
105 |
| - } |
106 |
| - |
107 |
| - subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone) |
108 |
| - if err != nil { |
109 |
| - return fmt.Errorf("beget: %w", err) |
110 |
| - } |
111 |
| - |
112 |
| - err = d.client.AddTXTRecord(dns01.UnFqdn(authZone), subDomain, info.Value) |
113 |
| - if err != nil { |
114 |
| - return fmt.Errorf("beget: failed to create TXT records [domain: %s, sub domain: %s]: %w", |
115 |
| - dns01.UnFqdn(authZone), subDomain, err) |
116 |
| - } |
117 |
| - |
118 |
| - return nil |
119 |
| -} |
120 |
| - |
121 |
| -// CleanUp removes the TXT record matching the specified parameters. |
122 |
| -func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { |
123 |
| - info := dns01.GetChallengeInfo(domain, keyAuth) |
124 |
| - |
125 |
| - authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) |
126 |
| - if err != nil { |
127 |
| - return fmt.Errorf("beget: could not find zone for domain %q and fqdn %q : %w", domain, info.EffectiveFQDN, err) |
128 |
| - } |
129 |
| - |
130 |
| - subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone) |
131 |
| - if err != nil { |
132 |
| - return fmt.Errorf("beget: %w", err) |
133 |
| - } |
134 |
| - |
135 |
| - err = d.client.RemoveTxtRecord(dns01.UnFqdn(authZone), subDomain) |
136 |
| - if err != nil { |
137 |
| - return fmt.Errorf("beget: failed to remove TXT records [domain: %s, sub domain: %s]: %w", |
138 |
| - dns01.UnFqdn(authZone), subDomain, err) |
139 |
| - } |
140 |
| - |
141 |
| - return nil |
142 |
| -} |
| 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/dns01" |
| 12 | + "github.com/go-acme/lego/v4/platform/config/env" |
| 13 | + "github.com/go-acme/lego/v4/providers/dns/beget/internal" |
| 14 | +) |
| 15 | + |
| 16 | +// Environment variables names. |
| 17 | +const ( |
| 18 | + envNamespace = "BEGET_" |
| 19 | + |
| 20 | + EnvUsername = envNamespace + "USERNAME" |
| 21 | + EnvPassword = envNamespace + "PASSWORD" |
| 22 | + |
| 23 | + EnvTTL = envNamespace + "TTL" |
| 24 | + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" |
| 25 | + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" |
| 26 | + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" |
| 27 | +) |
| 28 | + |
| 29 | +// Config is used to configure the creation of the DNSProvider. |
| 30 | +type Config struct { |
| 31 | + Username string |
| 32 | + Password string |
| 33 | + |
| 34 | + PropagationTimeout time.Duration |
| 35 | + PollingInterval time.Duration |
| 36 | + TTL int |
| 37 | + HTTPClient *http.Client |
| 38 | +} |
| 39 | + |
| 40 | +// NewDefaultConfig returns a default configuration for the DNSProvider. |
| 41 | +func NewDefaultConfig() *Config { |
| 42 | + return &Config{ |
| 43 | + TTL: env.GetOrDefaultInt(EnvTTL, 300), |
| 44 | + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout*2), // 2m |
| 45 | + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), |
| 46 | + HTTPClient: &http.Client{ |
| 47 | + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), |
| 48 | + }, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// DNSProvider implements the challenge.Provider interface. |
| 53 | +type DNSProvider struct { |
| 54 | + config *Config |
| 55 | + client *internal.Client |
| 56 | +} |
| 57 | + |
| 58 | +// NewDNSProvider returns a DNSProvider instance configured for beget.com. |
| 59 | +// Credentials must be passed in the environment variables: |
| 60 | +// BEGET_USERNAME and BEGET_PASSWORD. |
| 61 | +func NewDNSProvider() (*DNSProvider, error) { |
| 62 | + values, err := env.Get(EnvUsername, EnvPassword) |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("beget: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + config := NewDefaultConfig() |
| 68 | + config.Username = values[EnvUsername] |
| 69 | + config.Password = values[EnvPassword] |
| 70 | + |
| 71 | + return NewDNSProviderConfig(config) |
| 72 | +} |
| 73 | + |
| 74 | +// NewDNSProviderConfig return a DNSProvider instance configured for beget.com. |
| 75 | +func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { |
| 76 | + if config == nil { |
| 77 | + return nil, errors.New("beget: the configuration of the DNS provider is nil") |
| 78 | + } |
| 79 | + |
| 80 | + if config.Username == "" || config.Password == "" { |
| 81 | + return nil, errors.New("beget: incomplete credentials, missing username and/or password") |
| 82 | + } |
| 83 | + |
| 84 | + client := internal.NewClient(config.Username, config.Password) |
| 85 | + |
| 86 | + if config.HTTPClient != nil { |
| 87 | + client.HTTPClient = config.HTTPClient |
| 88 | + } |
| 89 | + |
| 90 | + return &DNSProvider{config: config, client: client}, nil |
| 91 | +} |
| 92 | + |
| 93 | +// Timeout returns the timeout and interval to use when checking for DNS propagation. |
| 94 | +// Adjusting here to cope with spikes in propagation times. |
| 95 | +func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { |
| 96 | + return d.config.PropagationTimeout, d.config.PollingInterval |
| 97 | +} |
| 98 | + |
| 99 | +// Present creates a TXT record using the specified parameters. |
| 100 | +func (d *DNSProvider) Present(domain, token, keyAuth string) error { |
| 101 | + info := dns01.GetChallengeInfo(domain, keyAuth) |
| 102 | + |
| 103 | + authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("beget: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err) |
| 106 | + } |
| 107 | + |
| 108 | + subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone) |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("beget: %w", err) |
| 111 | + } |
| 112 | + |
| 113 | + err = d.client.AddTXTRecord(context.Background(), dns01.UnFqdn(authZone), subDomain, info.Value) |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("beget: failed to create TXT records [domain: %s, sub domain: %s]: %w", |
| 116 | + dns01.UnFqdn(authZone), subDomain, err) |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +// CleanUp removes the TXT record matching the specified parameters. |
| 123 | +func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { |
| 124 | + info := dns01.GetChallengeInfo(domain, keyAuth) |
| 125 | + |
| 126 | + authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("beget: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err) |
| 129 | + } |
| 130 | + |
| 131 | + subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone) |
| 132 | + if err != nil { |
| 133 | + return fmt.Errorf("beget: %w", err) |
| 134 | + } |
| 135 | + |
| 136 | + err = d.client.RemoveTxtRecord(context.Background(), dns01.UnFqdn(authZone), subDomain) |
| 137 | + if err != nil { |
| 138 | + return fmt.Errorf("beget: failed to remove TXT records [domain: %s, sub domain: %s]: %w", |
| 139 | + dns01.UnFqdn(authZone), subDomain, err) |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
0 commit comments