2020-07-08 22:16:54 +02:00
|
|
|
package edgedns
|
|
|
|
|
|
|
|
import (
|
2021-02-21 12:56:56 +02:00
|
|
|
"os"
|
2020-07-08 22:16:54 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-21 12:56:56 +02:00
|
|
|
configdns "github.com/akamai/AkamaiOPEN-edgegrid-golang/configdns-v2"
|
|
|
|
"github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
|
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2020-07-08 22:16:54 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-02-21 12:56:56 +02:00
|
|
|
const (
|
|
|
|
envDomain = envNamespace + "TEST_DOMAIN"
|
|
|
|
envTestHost = envNamespace + "TEST_HOST"
|
|
|
|
envTestClientToken = envNamespace + "TEST_CLIENT_TOKEN"
|
|
|
|
envTestClientSecret = envNamespace + "TEST_CLIENT_SECRET"
|
|
|
|
envTestAccessToken = envNamespace + "TEST_ACCESS_TOKEN"
|
|
|
|
)
|
2020-07-08 22:16:54 +02:00
|
|
|
|
|
|
|
var envTest = tester.NewEnvTest(
|
|
|
|
EnvHost,
|
|
|
|
EnvClientToken,
|
|
|
|
EnvClientSecret,
|
2021-02-21 12:56:56 +02:00
|
|
|
EnvAccessToken,
|
|
|
|
EnvEdgeRc,
|
|
|
|
EnvEdgeRcSection,
|
|
|
|
envTestHost,
|
|
|
|
envTestClientToken,
|
|
|
|
envTestClientSecret,
|
|
|
|
envTestAccessToken).
|
|
|
|
WithDomain(envDomain).
|
|
|
|
WithLiveTestRequirements(EnvHost, EnvClientToken, EnvClientSecret, EnvAccessToken, envDomain)
|
|
|
|
|
|
|
|
func TestNewDNSProvider_FromEnv(t *testing.T) {
|
2020-07-08 22:16:54 +02:00
|
|
|
testCases := []struct {
|
2021-02-21 12:56:56 +02:00
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expectedConfig *edgegrid.Config
|
|
|
|
expectedErr string
|
2020-07-08 22:16:54 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2021-02-21 12:56:56 +02:00
|
|
|
EnvHost: "akaa-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net",
|
|
|
|
EnvClientToken: "akab-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx",
|
|
|
|
EnvClientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
|
|
|
EnvAccessToken: "akac-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx",
|
|
|
|
},
|
|
|
|
expectedConfig: &edgegrid.Config{
|
|
|
|
Host: "akaa-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net",
|
|
|
|
ClientToken: "akab-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx",
|
|
|
|
ClientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
|
|
|
AccessToken: "akac-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx",
|
|
|
|
MaxBody: maxBody,
|
2020-07-08 22:16:54 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-02-21 12:56:56 +02:00
|
|
|
desc: "with section",
|
2020-07-08 22:16:54 +02:00
|
|
|
envVars: map[string]string{
|
2021-02-21 12:56:56 +02:00
|
|
|
EnvEdgeRcSection: "test",
|
|
|
|
envTestHost: "akaa-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net",
|
|
|
|
envTestClientToken: "akab-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx",
|
|
|
|
envTestClientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
|
|
|
envTestAccessToken: "akac-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx",
|
|
|
|
},
|
|
|
|
expectedConfig: &edgegrid.Config{
|
|
|
|
Host: "akaa-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net",
|
|
|
|
ClientToken: "akab-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx",
|
|
|
|
ClientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
|
|
|
AccessToken: "akac-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx",
|
|
|
|
MaxBody: maxBody,
|
2020-07-08 22:16:54 +02:00
|
|
|
},
|
2021-02-21 12:56:56 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expectedErr: "edgedns: Unable to create instance using environment or .edgerc file",
|
2020-07-08 22:16:54 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing host",
|
|
|
|
envVars: map[string]string{
|
|
|
|
EnvHost: "",
|
|
|
|
EnvClientToken: "B",
|
|
|
|
EnvClientSecret: "C",
|
|
|
|
EnvAccessToken: "D",
|
|
|
|
},
|
2021-02-21 12:56:56 +02:00
|
|
|
expectedErr: "edgedns: Unable to create instance using environment or .edgerc file",
|
2020-07-08 22:16:54 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing client token",
|
|
|
|
envVars: map[string]string{
|
|
|
|
EnvHost: "A",
|
|
|
|
EnvClientToken: "",
|
|
|
|
EnvClientSecret: "C",
|
|
|
|
EnvAccessToken: "D",
|
|
|
|
},
|
2021-02-21 12:56:56 +02:00
|
|
|
expectedErr: "edgedns: Fatal missing required environment variables: [AKAMAI_CLIENT_TOKEN]",
|
2020-07-08 22:16:54 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing client secret",
|
|
|
|
envVars: map[string]string{
|
|
|
|
EnvHost: "A",
|
|
|
|
EnvClientToken: "B",
|
|
|
|
EnvClientSecret: "",
|
|
|
|
EnvAccessToken: "D",
|
|
|
|
},
|
2021-02-21 12:56:56 +02:00
|
|
|
expectedErr: "edgedns: Fatal missing required environment variables: [AKAMAI_CLIENT_SECRET]",
|
2020-07-08 22:16:54 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing access token",
|
|
|
|
envVars: map[string]string{
|
|
|
|
EnvHost: "A",
|
|
|
|
EnvClientToken: "B",
|
|
|
|
EnvClientSecret: "C",
|
|
|
|
EnvAccessToken: "",
|
|
|
|
},
|
2021-02-21 12:56:56 +02:00
|
|
|
expectedErr: "edgedns: Fatal missing required environment variables: [AKAMAI_ACCESS_TOKEN]",
|
2020-07-08 22:16:54 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
2021-02-21 12:56:56 +02:00
|
|
|
if test.envVars == nil {
|
|
|
|
test.envVars = map[string]string{}
|
|
|
|
}
|
|
|
|
test.envVars[EnvEdgeRc] = "/dev/null"
|
|
|
|
|
2020-07-08 22:16:54 +02:00
|
|
|
envTest.Apply(test.envVars)
|
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
2021-02-21 12:56:56 +02:00
|
|
|
if test.expectedErr != "" {
|
|
|
|
require.EqualError(t, err, test.expectedErr)
|
|
|
|
return
|
2020-07-08 22:16:54 +02:00
|
|
|
}
|
|
|
|
|
2021-02-21 12:56:56 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
2020-07-08 22:16:54 +02:00
|
|
|
|
2021-02-21 12:56:56 +02:00
|
|
|
if test.expectedConfig != nil {
|
|
|
|
require.Equal(t, *test.expectedConfig, configdns.Config)
|
2020-07-08 22:16:54 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_findZone(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
domain string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Extract root record name",
|
2024-02-24 21:49:40 +02:00
|
|
|
domain: "example.com.",
|
|
|
|
expected: "example.com",
|
2020-07-08 22:16:54 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Extract sub record name",
|
2024-02-24 21:49:40 +02:00
|
|
|
domain: "foo.example.com.",
|
|
|
|
expected: "example.com",
|
2020-07-08 22:16:54 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
zone, err := getZone(test.domain)
|
2020-07-08 22:16:54 +02:00
|
|
|
require.NoError(t, err)
|
2021-02-21 12:56:56 +02:00
|
|
|
require.Equal(t, test.expected, zone)
|
2020-07-08 22:16:54 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 12:56:56 +02:00
|
|
|
func TestNewDefaultConfig(t *testing.T) {
|
|
|
|
defer envTest.RestoreEnv()
|
2020-07-08 22:16:54 +02:00
|
|
|
|
2021-02-21 12:56:56 +02:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected *Config
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "default configuration",
|
|
|
|
expected: &Config{
|
|
|
|
TTL: dns01.DefaultTTL,
|
|
|
|
PropagationTimeout: 3 * time.Minute,
|
|
|
|
PollingInterval: 15 * time.Second,
|
|
|
|
Config: edgegrid.Config{
|
|
|
|
MaxBody: maxBody,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "custom values",
|
|
|
|
envVars: map[string]string{
|
|
|
|
EnvTTL: "99",
|
|
|
|
EnvPropagationTimeout: "60",
|
|
|
|
EnvPollingInterval: "60",
|
|
|
|
},
|
|
|
|
expected: &Config{
|
|
|
|
TTL: 99,
|
|
|
|
PropagationTimeout: 60 * time.Second,
|
|
|
|
PollingInterval: 60 * time.Second,
|
|
|
|
Config: edgegrid.Config{
|
|
|
|
MaxBody: maxBody,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-07-08 22:16:54 +02:00
|
|
|
}
|
|
|
|
|
2021-02-21 12:56:56 +02:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
envTest.ClearEnv()
|
|
|
|
for key, value := range test.envVars {
|
|
|
|
os.Setenv(key, value)
|
|
|
|
}
|
2020-07-08 22:16:54 +02:00
|
|
|
|
2021-02-21 12:56:56 +02:00
|
|
|
config := NewDefaultConfig()
|
2020-07-08 22:16:54 +02:00
|
|
|
|
2021-02-21 12:56:56 +02:00
|
|
|
require.Equal(t, test.expected, config)
|
|
|
|
})
|
|
|
|
}
|
2020-07-08 22:16:54 +02:00
|
|
|
}
|