-
-
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
10 changed files
with
1,630 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,175 @@ | ||
// Package f5xc implements a DNS provider for solving the DNS-01 challenge using F5 XC. | ||
package f5xc | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"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/f5xc/internal" | ||
) | ||
|
||
// Environment variables names. | ||
const ( | ||
envNamespace = "F5XC_" | ||
|
||
EnvToken = envNamespace + "API_TOKEN" | ||
EnvTenantName = envNamespace + "TENANT_NAME" | ||
EnvGroupName = envNamespace + "GROUP_NAME" | ||
|
||
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 { | ||
APIToken string | ||
TenantName string | ||
GroupName string | ||
|
||
PropagationTimeout time.Duration | ||
PollingInterval time.Duration | ||
TTL int | ||
HTTPClient *http.Client | ||
} | ||
|
||
// NewDefaultConfig returns a default configuration for the DNSProvider. | ||
func NewDefaultConfig() *Config { | ||
return &Config{ | ||
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), | ||
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 F5 XC. | ||
func NewDNSProvider() (*DNSProvider, error) { | ||
values, err := env.Get(EnvToken, EnvTenantName, EnvGroupName) | ||
if err != nil { | ||
return nil, fmt.Errorf("f5xc: %w", err) | ||
} | ||
|
||
config := NewDefaultConfig() | ||
config.APIToken = values[EnvToken] | ||
config.TenantName = values[EnvTenantName] | ||
config.GroupName = values[EnvGroupName] | ||
|
||
return NewDNSProviderConfig(config) | ||
} | ||
|
||
// NewDNSProviderConfig return a DNSProvider instance configured for F5 XC. | ||
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { | ||
if config == nil { | ||
return nil, errors.New("f5xc: the configuration of the DNS provider is nil") | ||
} | ||
|
||
if config.GroupName == "" { | ||
return nil, errors.New("f5xc: missing group name") | ||
} | ||
|
||
client, err := internal.NewClient(config.APIToken, config.TenantName) | ||
if err != nil { | ||
return nil, fmt.Errorf("f5xc: %w", err) | ||
} | ||
|
||
if config.HTTPClient != nil { | ||
client.HTTPClient = config.HTTPClient | ||
} | ||
|
||
return &DNSProvider{ | ||
config: config, | ||
client: client, | ||
}, nil | ||
} | ||
|
||
// Present creates a TXT record using the specified parameters. | ||
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("f5xc: could not find zone for domain %q: %w", domain, err) | ||
} | ||
|
||
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone) | ||
if err != nil { | ||
return fmt.Errorf("f5xc: %w", err) | ||
} | ||
|
||
existingRRSet, err := d.client.GetRRSet(context.Background(), dns01.UnFqdn(authZone), d.config.GroupName, subDomain, "TXT") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// New RRSet. | ||
if existingRRSet.RRSet.TXTRecord == nil { | ||
rrSet := internal.RRSet{ | ||
Description: "lego", | ||
TTL: d.config.TTL, | ||
TXTRecord: &internal.TXTRecord{ | ||
Name: subDomain, | ||
Values: []string{info.Value}, | ||
}, | ||
} | ||
|
||
_, err = d.client.CreateRRSet(context.Background(), dns01.UnFqdn(authZone), d.config.GroupName, rrSet) | ||
if err != nil { | ||
return fmt.Errorf("f5xc: create RR set: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Update RRSet. | ||
existingRRSet.RRSet.TXTRecord.Values = append(existingRRSet.RRSet.TXTRecord.Values, info.Value) | ||
|
||
_, err = d.client.ReplaceRRSet(context.Background(), dns01.UnFqdn(authZone), d.config.GroupName, subDomain, "TXT", existingRRSet.RRSet) | ||
if err != nil { | ||
return fmt.Errorf("f5xc: replace RR set: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// CleanUp removes the TXT record matching the specified parameters. | ||
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { | ||
info := dns01.GetChallengeInfo(domain, keyAuth) | ||
|
||
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) | ||
if err != nil { | ||
return fmt.Errorf("f5xc: could not find zone for domain %q: %w", domain, err) | ||
} | ||
|
||
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone) | ||
if err != nil { | ||
return fmt.Errorf("f5xc: %w", err) | ||
} | ||
|
||
_, err = d.client.DeleteRRSet(context.Background(), dns01.UnFqdn(authZone), d.config.GroupName, subDomain, "TXT") | ||
if err != nil { | ||
return fmt.Errorf("f5xc: delete RR set: %w", err) | ||
} | ||
|
||
return 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 | ||
} |
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,27 @@ | ||
Name = "F5 XC" | ||
Description = '''''' | ||
URL = "https://www.f5.com/products/distributed-cloud-services" | ||
Code = "f5xc" | ||
Since = "v4.22.0" | ||
|
||
Example = ''' | ||
F5XC_API_TOKEN="xxx" \ | ||
F5XC_TENANT_NAME="yyy" \ | ||
F5XC_GROUP_NAME="zzz" \ | ||
lego --email you@example.com --dns f5xc -d '*.example.com' -d example.com run | ||
''' | ||
|
||
[Configuration] | ||
[Configuration.Credentials] | ||
F5XC_API_TOKEN = "API token" | ||
F5XC_TENANT_NAME = "XC Tenant shortname" | ||
F5XC_GROUP_NAME = "Group name" | ||
[Configuration.Additional] | ||
F5XC_POLLING_INTERVAL = "Time between DNS propagation check in seconds (Default: 2)" | ||
F5XC_PROPAGATION_TIMEOUT = "Maximum waiting time for DNS propagation in seconds (Default: 60)" | ||
F5XC_TTL = "The TTL of the TXT record used for the DNS challenge in seconds (Default: 120)" | ||
F5XC_HTTP_TIMEOUT = "API request timeout in seconds (Default: 30)" | ||
|
||
[Links] | ||
API = "https://docs.cloud.f5.com/docs-v2/api/dns-zone-rrset" | ||
Documentation = "https://my.f5.com/manage/s/article/K000147937" |
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,166 @@ | ||
package f5xc | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-acme/lego/v4/platform/tester" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const envDomain = envNamespace + "DOMAIN" | ||
|
||
var envTest = tester.NewEnvTest(EnvToken, EnvTenantName, EnvGroupName).WithDomain(envDomain) | ||
|
||
func TestNewDNSProvider(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
envVars map[string]string | ||
expected string | ||
}{ | ||
{ | ||
desc: "success", | ||
envVars: map[string]string{ | ||
EnvToken: "secret", | ||
EnvTenantName: "shortname", | ||
EnvGroupName: "group", | ||
}, | ||
}, | ||
{ | ||
desc: "missing API token", | ||
envVars: map[string]string{ | ||
EnvToken: "", | ||
EnvTenantName: "shortname", | ||
EnvGroupName: "group", | ||
}, | ||
expected: "f5xc: some credentials information are missing: F5XC_API_TOKEN", | ||
}, | ||
{ | ||
desc: "missing tenant name", | ||
envVars: map[string]string{ | ||
EnvToken: "secret", | ||
EnvTenantName: "", | ||
EnvGroupName: "group", | ||
}, | ||
expected: "f5xc: some credentials information are missing: F5XC_TENANT_NAME", | ||
}, | ||
{ | ||
desc: "missing group name", | ||
envVars: map[string]string{ | ||
EnvToken: "secret", | ||
EnvTenantName: "shortname", | ||
EnvGroupName: "", | ||
}, | ||
expected: "f5xc: some credentials information are missing: F5XC_GROUP_NAME", | ||
}, | ||
{ | ||
desc: "missing credentials", | ||
envVars: map[string]string{}, | ||
expected: "f5xc: some credentials information are missing: F5XC_API_TOKEN,F5XC_TENANT_NAME,F5XC_GROUP_NAME", | ||
}, | ||
} | ||
|
||
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 | ||
apiToken string | ||
tenantName string | ||
groupName string | ||
expected string | ||
}{ | ||
{ | ||
desc: "success", | ||
apiToken: "secret", | ||
tenantName: "shortname", | ||
groupName: "group", | ||
}, | ||
{ | ||
desc: "missing API token", | ||
tenantName: "shortname", | ||
groupName: "group", | ||
expected: "f5xc: credentials missing", | ||
}, | ||
{ | ||
desc: "missing tenant name", | ||
apiToken: "secret", | ||
groupName: "group", | ||
expected: "f5xc: missing tenant name", | ||
}, | ||
{ | ||
desc: "missing group name", | ||
apiToken: "secret", | ||
tenantName: "shortname", | ||
expected: "f5xc: missing group name", | ||
}, | ||
{ | ||
desc: "missing credentials", | ||
expected: "f5xc: missing group name", | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
t.Run(test.desc, func(t *testing.T) { | ||
config := NewDefaultConfig() | ||
config.APIToken = test.apiToken | ||
config.TenantName = test.tenantName | ||
config.GroupName = test.groupName | ||
|
||
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) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestLivePresent(t *testing.T) { | ||
if !envTest.IsLiveTest() { | ||
t.Skip("skipping live test") | ||
} | ||
|
||
envTest.RestoreEnv() | ||
provider, err := NewDNSProvider() | ||
require.NoError(t, err) | ||
|
||
err = provider.Present(envTest.GetDomain(), "", "123d==") | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestLiveCleanUp(t *testing.T) { | ||
if !envTest.IsLiveTest() { | ||
t.Skip("skipping live test") | ||
} | ||
|
||
envTest.RestoreEnv() | ||
provider, err := NewDNSProvider() | ||
require.NoError(t, err) | ||
|
||
err = provider.CleanUp(envTest.GetDomain(), "", "123d==") | ||
require.NoError(t, err) | ||
} |
Oops, something went wrong.