-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
834 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// Package combell implements a DNS provider for solving the DNS-01 challenge using Combell DNS. | ||
package combell | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-acme/lego/v4/challenge/dns01" | ||
"github.com/go-acme/lego/v4/platform/config/env" | ||
"github.com/go-acme/lego/v4/providers/dns/combell/internal" | ||
) | ||
|
||
const ( | ||
minTTL = 60 | ||
maxTTL = 8640 | ||
) | ||
|
||
// Environment variables names. | ||
const ( | ||
envNamespace = "COMBELL_" | ||
|
||
EnvAPIKey = envNamespace + "API_KEY" | ||
EnvAPISecret = envNamespace + "API_SECRET" | ||
|
||
EnvTTL = envNamespace + "TTL" | ||
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" | ||
EnvPollingInterval = envNamespace + "POLLING_INTERVAL" | ||
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" | ||
) | ||
|
||
// Config is used to configure the creation of the DNSProvider. | ||
type Config struct { | ||
APIKey string | ||
APISecret string | ||
TTL int | ||
PropagationTimeout time.Duration | ||
PollingInterval time.Duration | ||
HTTPClient *http.Client | ||
} | ||
|
||
// NewDefaultConfig returns a default configuration for the DNSProvider. | ||
func NewDefaultConfig() *Config { | ||
return &Config{ | ||
TTL: env.GetOrDefaultInt(EnvTTL, 3600), | ||
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), | ||
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), | ||
HTTPClient: &http.Client{ | ||
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), | ||
}, | ||
} | ||
} | ||
|
||
// DNSProvider implements the challenge.Provider interface. | ||
type DNSProvider struct { | ||
config *Config | ||
client *internal.Client | ||
} | ||
|
||
// NewDNSProvider returns a DNSProvider instance configured for Combell DNS. | ||
// Credentials must be passed in the environment variables: | ||
// COMBELL_API_KEY, COMBELL_API_SECRET. | ||
func NewDNSProvider() (*DNSProvider, error) { | ||
values, err := env.Get(EnvAPIKey, EnvAPISecret) | ||
if err != nil { | ||
return nil, fmt.Errorf("combell: %w", err) | ||
} | ||
|
||
config := NewDefaultConfig() | ||
config.APIKey = values[EnvAPIKey] | ||
config.APISecret = values[EnvAPISecret] | ||
|
||
return NewDNSProviderConfig(config) | ||
} | ||
|
||
// NewDNSProviderConfig return a DNSProvider instance configured for Combell DNS. | ||
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { | ||
if config == nil { | ||
return nil, errors.New("combell: the configuration of the DNS provider is nil") | ||
} | ||
|
||
if config.APIKey == "" || config.APISecret == "" { | ||
return nil, errors.New("combell: some credentials information are missing") | ||
} | ||
|
||
if config.TTL < minTTL { | ||
return nil, fmt.Errorf("combell: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL) | ||
} | ||
|
||
if config.TTL > maxTTL { | ||
return nil, fmt.Errorf("combell: invalid TTL, TTL (%d) must be lower than %d", config.TTL, maxTTL) | ||
} | ||
|
||
client := internal.NewClient(config.APIKey, config.APISecret, config.HTTPClient) | ||
|
||
return &DNSProvider{config: config, client: client}, nil | ||
} | ||
|
||
// Timeout returns the timeout and interval to use when checking for DNS propagation. | ||
// Adjusting here to cope with spikes in propagation times. | ||
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { | ||
return d.config.PropagationTimeout, d.config.PollingInterval | ||
} | ||
|
||
// Present creates a TXT record to fulfill the dns-01 challenge. | ||
func (d *DNSProvider) Present(domain, token, keyAuth string) error { | ||
info := dns01.GetChallengeInfo(domain, keyAuth) | ||
|
||
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) | ||
if err != nil { | ||
return fmt.Errorf("combell: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err) | ||
} | ||
|
||
record := internal.Record{ | ||
Type: "TXT", | ||
RecordName: dns01.UnFqdn(strings.TrimSuffix(info.EffectiveFQDN, authZone)), | ||
Content: info.Value, | ||
TTL: d.config.TTL, | ||
} | ||
|
||
err = d.client.CreateRecord(context.Background(), authZone, record) | ||
if err != nil { | ||
return fmt.Errorf("combell: create record: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// CleanUp removes the TXT record matching the specified parameters. | ||
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { | ||
ctx := context.Background() | ||
info := dns01.GetChallengeInfo(domain, keyAuth) | ||
|
||
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) | ||
if err != nil { | ||
return fmt.Errorf("combell: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err) | ||
} | ||
|
||
request := &internal.GetRecordsRequest{ | ||
Type: "TXT", | ||
RecordName: dns01.UnFqdn(strings.TrimSuffix(info.EffectiveFQDN, authZone)), | ||
} | ||
records, err := d.client.GetRecords(ctx, authZone, request) | ||
if err != nil { | ||
return fmt.Errorf("combell: get records: %w", err) | ||
} | ||
|
||
for _, record := range records { | ||
if record.Content == info.Value { | ||
err = d.client.DeleteRecord(ctx, authZone, record.ID) | ||
if err != nil { | ||
return fmt.Errorf("combell: delete record: %w", err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Name = "Combell" | ||
Description = '''''' | ||
URL = "https://www.combell.com/" | ||
Code = "combell" | ||
Since = "v4.20.0" | ||
|
||
Example = ''' | ||
COMBELL_API_KEY=xxxxxxxxxxxxxxxxxxxxx \ | ||
COMBELL_API_SECRET=yyyyyyyyyyyyyyyyyyyy \ | ||
lego --email [email protected] --dns combell -d '*.example.com' -d example.com run | ||
''' | ||
|
||
[Configuration] | ||
[Configuration.Credentials] | ||
COMBELL_API_KEY = "The API key" | ||
COMBELL_API_SECRET = "The API secret" | ||
[Configuration.Additional] | ||
COMBELL_POLLING_INTERVAL = "Time between DNS propagation check" | ||
COMBELL_PROPAGATION_TIMEOUT = "Maximum waiting time for DNS propagation" | ||
COMBELL_TTL = "The TTL of the TXT record used for the DNS challenge" | ||
COMBELL_HTTP_TIMEOUT = "API request timeout" | ||
|
||
[Links] | ||
API = "https://api.combell.com/v2/documentation" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package combell | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-acme/lego/v4/platform/tester" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const envDomain = envNamespace + "DOMAIN" | ||
|
||
var envTest = tester.NewEnvTest(EnvAPIKey, EnvAPISecret).WithDomain(envDomain) | ||
|
||
func TestNewDNSProvider(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
envVars map[string]string | ||
expected string | ||
}{ | ||
{ | ||
desc: "success", | ||
envVars: map[string]string{ | ||
EnvAPIKey: "key", | ||
EnvAPISecret: "secret", | ||
}, | ||
}, | ||
{ | ||
desc: "missing API key", | ||
envVars: map[string]string{ | ||
EnvAPISecret: "secret", | ||
}, | ||
expected: "combell: some credentials information are missing: COMBELL_API_KEY", | ||
}, | ||
{ | ||
desc: "missing API secret", | ||
envVars: map[string]string{ | ||
EnvAPIKey: "key", | ||
}, | ||
expected: "combell: some credentials information are missing: COMBELL_API_SECRET", | ||
}, | ||
{ | ||
desc: "missing credentials", | ||
envVars: map[string]string{}, | ||
expected: "combell: some credentials information are missing: COMBELL_API_KEY,COMBELL_API_SECRET", | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
t.Run(test.desc, func(t *testing.T) { | ||
defer envTest.RestoreEnv() | ||
envTest.ClearEnv() | ||
|
||
envTest.Apply(test.envVars) | ||
|
||
p, err := NewDNSProvider() | ||
|
||
if test.expected == "" { | ||
require.NoError(t, err) | ||
require.NotNil(t, p) | ||
require.NotNil(t, p.config) | ||
require.NotNil(t, p.client) | ||
} else { | ||
require.EqualError(t, err, test.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNewDNSProviderConfig(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
apiKey string | ||
apiSecret string | ||
expected string | ||
}{ | ||
{ | ||
desc: "success", | ||
apiKey: "key", | ||
apiSecret: "secret", | ||
}, | ||
{ | ||
desc: "missing API key", | ||
apiSecret: "secret", | ||
expected: "combell: some credentials information are missing", | ||
}, | ||
{ | ||
desc: "missing API secret", | ||
apiKey: "key", | ||
expected: "combell: some credentials information are missing", | ||
}, | ||
{ | ||
desc: "missing credentials", | ||
expected: "combell: some credentials information are missing", | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
t.Run(test.desc, func(t *testing.T) { | ||
config := NewDefaultConfig() | ||
config.APIKey = test.apiKey | ||
config.APISecret = test.apiSecret | ||
|
||
p, err := NewDNSProviderConfig(config) | ||
|
||
if test.expected == "" { | ||
require.NoError(t, err) | ||
require.NotNil(t, p) | ||
require.NotNil(t, p.config) | ||
require.NotNil(t, p.client) | ||
} else { | ||
require.EqualError(t, err, test.expected) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.