Skip to content

Commit 450e026

Browse files
committed
review
1 parent 63eccef commit 450e026

File tree

9 files changed

+638
-487
lines changed

9 files changed

+638
-487
lines changed

providers/dns/beget/beget.go

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

providers/dns/beget/beget.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Name = "Beget.com"
22
Description = ''''''
33
URL = "https://beget.com/"
44
Code = "beget"
5-
Since = "v1.0.0"
5+
Since = "v4.21.0"
66

77
Example = '''
88
BEGET_USERNAME=xxxxxx \

0 commit comments

Comments
 (0)