Skip to content

Commit 2cf5542

Browse files
committed
feat: Add DNS provider for Combell
1 parent 35c259e commit 2cf5542

File tree

12 files changed

+837
-0
lines changed

12 files changed

+837
-0
lines changed

providers/dns/combell/combell.go

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Package combell implements a DNS provider for solving the DNS-01 challenge using Combell DNS.
2+
package combell
3+
4+
import (
5+
"context"
6+
"errors"
7+
"fmt"
8+
"net/http"
9+
"strings"
10+
"time"
11+
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/combell/internal"
15+
)
16+
17+
const (
18+
minTTL = 60
19+
maxTTL = 8640
20+
)
21+
22+
// Environment variables names.
23+
const (
24+
envNamespace = "COMBELL_"
25+
26+
EnvAPIKey = envNamespace + "API_KEY"
27+
EnvAPISecret = envNamespace + "API_SECRET"
28+
29+
EnvTTL = envNamespace + "TTL"
30+
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
31+
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
32+
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
33+
)
34+
35+
// Config is used to configure the creation of the DNSProvider.
36+
type Config struct {
37+
APIKey string
38+
APISecret string
39+
TTL int
40+
PropagationTimeout time.Duration
41+
PollingInterval time.Duration
42+
HTTPClient *http.Client
43+
}
44+
45+
// NewDefaultConfig returns a default configuration for the DNSProvider.
46+
func NewDefaultConfig() *Config {
47+
return &Config{
48+
TTL: env.GetOrDefaultInt(EnvTTL, 3600),
49+
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
50+
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
51+
HTTPClient: &http.Client{
52+
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
53+
},
54+
}
55+
}
56+
57+
// DNSProvider implements the challenge.Provider interface.
58+
type DNSProvider struct {
59+
config *Config
60+
client *internal.Client
61+
}
62+
63+
// NewDNSProvider returns a DNSProvider instance configured for Combell DNS.
64+
// Credentials must be passed in the environment variables:
65+
// COMBELL_API_KEY, COMBELL_API_SECRET.
66+
func NewDNSProvider() (*DNSProvider, error) {
67+
values, err := env.Get(EnvAPIKey, EnvAPISecret)
68+
if err != nil {
69+
return nil, fmt.Errorf("combell: %w", err)
70+
}
71+
72+
config := NewDefaultConfig()
73+
config.APIKey = values[EnvAPIKey]
74+
config.APISecret = values[EnvAPISecret]
75+
76+
return NewDNSProviderConfig(config)
77+
}
78+
79+
// NewDNSProviderConfig return a DNSProvider instance configured for Combell DNS.
80+
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
81+
if config == nil {
82+
return nil, errors.New("combell: the configuration of the DNS provider is nil")
83+
}
84+
85+
if config.APIKey == "" || config.APISecret == "" {
86+
return nil, errors.New("combell: some credentials information are missing")
87+
}
88+
89+
if config.TTL < minTTL {
90+
return nil, fmt.Errorf("combell: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
91+
}
92+
93+
if config.TTL > maxTTL {
94+
return nil, fmt.Errorf("combell: invalid TTL, TTL (%d) must be lower than %d", config.TTL, maxTTL)
95+
}
96+
97+
client := internal.NewClient(config.APIKey, config.APISecret, config.HTTPClient)
98+
99+
return &DNSProvider{config: config, client: client}, nil
100+
}
101+
102+
// Timeout returns the timeout and interval to use when checking for DNS propagation.
103+
// Adjusting here to cope with spikes in propagation times.
104+
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
105+
return d.config.PropagationTimeout, d.config.PollingInterval
106+
}
107+
108+
// Present creates a TXT record to fulfill the dns-01 challenge.
109+
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
110+
info := dns01.GetChallengeInfo(domain, keyAuth)
111+
112+
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
113+
if err != nil {
114+
return fmt.Errorf("combell: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
115+
}
116+
117+
record := internal.Record{
118+
Type: "TXT",
119+
RecordName: dns01.UnFqdn(strings.TrimSuffix(info.EffectiveFQDN, authZone)),
120+
Content: info.Value,
121+
TTL: d.config.TTL,
122+
}
123+
124+
err = d.client.CreateRecord(context.Background(), authZone, record)
125+
if err != nil {
126+
return fmt.Errorf("combell: create record: %w", 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+
info := dns01.GetChallengeInfo(domain, keyAuth)
136+
137+
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
138+
if err != nil {
139+
return fmt.Errorf("combell: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
140+
}
141+
142+
request := &internal.GetRecordsRequest{
143+
Type: "TXT",
144+
RecordName: dns01.UnFqdn(strings.TrimSuffix(info.EffectiveFQDN, authZone)),
145+
}
146+
records, err := d.client.GetRecords(ctx, authZone, request)
147+
if err != nil {
148+
return fmt.Errorf("combell: get records: %w", err)
149+
}
150+
151+
for _, record := range records {
152+
if record.Content == info.Value {
153+
err = d.client.DeleteRecord(ctx, authZone, record.ID)
154+
if err != nil {
155+
return fmt.Errorf("combell: delete record: %w", err)
156+
}
157+
}
158+
}
159+
160+
return nil
161+
}

providers/dns/combell/combell.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Name = "Combell"
2+
Description = ''''''
3+
URL = "https://www.combell.com/"
4+
Code = "combell"
5+
Since = "v4.7.0"
6+
7+
Example = '''
8+
COMBELL_API_KEY=xxxxxxxxxxxxxxxxxxxxx \
9+
COMBELL_API_SECRET=yyyyyyyyyyyyyyyyyyyy \
10+
lego --email myemail@example.com --dns combell --domains my.example.org run
11+
'''
12+
13+
[Configuration]
14+
[Configuration.Credentials]
15+
COMBELL_API_KEY = "The API key"
16+
COMBELL_API_SECRET = "The API secret"
17+
[Configuration.Additional]
18+
COMBELL_POLLING_INTERVAL = "Time between DNS propagation check"
19+
COMBELL_PROPAGATION_TIMEOUT = "Maximum waiting time for DNS propagation"
20+
COMBELL_TTL = "The TTL of the TXT record used for the DNS challenge"
21+
COMBELL_HTTP_TIMEOUT = "API request timeout"
22+
23+
[Links]
24+
API = "https://api.combell.com/v2/documentation"
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package combell
2+
3+
import (
4+
"testing"
5+
6+
"github.com/go-acme/lego/v4/platform/tester"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
const envDomain = envNamespace + "DOMAIN"
11+
12+
var envTest = tester.NewEnvTest(EnvAPIKey, EnvAPISecret).WithDomain(envDomain)
13+
14+
func TestNewDNSProvider(t *testing.T) {
15+
testCases := []struct {
16+
desc string
17+
envVars map[string]string
18+
expected string
19+
}{
20+
{
21+
desc: "success",
22+
envVars: map[string]string{
23+
EnvAPIKey: "key",
24+
EnvAPISecret: "secret",
25+
},
26+
},
27+
{
28+
desc: "missing API key",
29+
envVars: map[string]string{
30+
EnvAPISecret: "secret",
31+
},
32+
expected: "combell: some credentials information are missing: COMBELL_API_KEY",
33+
},
34+
{
35+
desc: "missing API secret",
36+
envVars: map[string]string{
37+
EnvAPIKey: "key",
38+
},
39+
expected: "combell: some credentials information are missing: COMBELL_API_SECRET",
40+
},
41+
{
42+
desc: "missing credentials",
43+
envVars: map[string]string{},
44+
expected: "combell: some credentials information are missing: COMBELL_API_KEY,COMBELL_API_SECRET",
45+
},
46+
}
47+
48+
for _, test := range testCases {
49+
t.Run(test.desc, func(t *testing.T) {
50+
defer envTest.RestoreEnv()
51+
envTest.ClearEnv()
52+
53+
envTest.Apply(test.envVars)
54+
55+
p, err := NewDNSProvider()
56+
57+
if test.expected == "" {
58+
require.NoError(t, err)
59+
require.NotNil(t, p)
60+
require.NotNil(t, p.config)
61+
require.NotNil(t, p.client)
62+
} else {
63+
require.EqualError(t, err, test.expected)
64+
}
65+
})
66+
}
67+
}
68+
69+
func TestNewDNSProviderConfig(t *testing.T) {
70+
testCases := []struct {
71+
desc string
72+
apiKey string
73+
apiSecret string
74+
expected string
75+
}{
76+
{
77+
desc: "success",
78+
apiKey: "key",
79+
apiSecret: "secret",
80+
},
81+
{
82+
desc: "missing API key",
83+
apiSecret: "secret",
84+
expected: "combell: some credentials information are missing",
85+
},
86+
{
87+
desc: "missing API secret",
88+
apiKey: "key",
89+
expected: "combell: some credentials information are missing",
90+
},
91+
{
92+
desc: "missing credentials",
93+
expected: "combell: some credentials information are missing",
94+
},
95+
}
96+
97+
for _, test := range testCases {
98+
t.Run(test.desc, func(t *testing.T) {
99+
config := NewDefaultConfig()
100+
config.APIKey = test.apiKey
101+
config.APISecret = test.apiSecret
102+
103+
p, err := NewDNSProviderConfig(config)
104+
105+
if test.expected == "" {
106+
require.NoError(t, err)
107+
require.NotNil(t, p)
108+
require.NotNil(t, p.config)
109+
require.NotNil(t, p.client)
110+
} else {
111+
require.EqualError(t, err, test.expected)
112+
}
113+
})
114+
}
115+
}

0 commit comments

Comments
 (0)