2019-03-11 16:56:48 +00:00
|
|
|
package ovh
|
2016-06-16 19:11:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2018-09-24 19:07:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-06-16 19:11:19 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
const envDomain = envNamespace + "DOMAIN"
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvEndpoint,
|
|
|
|
EnvApplicationKey,
|
|
|
|
EnvApplicationSecret,
|
2024-05-06 13:43:25 +00:00
|
|
|
EnvConsumerKey,
|
|
|
|
EnvClientID,
|
|
|
|
EnvClientSecret).
|
2020-03-11 22:51:10 +00:00
|
|
|
WithDomain(envDomain)
|
2016-06-16 19:11:19 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2024-05-06 13:43:25 +00:00
|
|
|
desc: "application key: success",
|
2018-10-12 17:29:18 +00:00
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvEndpoint: "ovh-eu",
|
|
|
|
EnvApplicationKey: "B",
|
|
|
|
EnvApplicationSecret: "C",
|
|
|
|
EnvConsumerKey: "D",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-05-06 13:43:25 +00:00
|
|
|
desc: "application key: missing endpoint",
|
2018-06-11 15:32:50 +00:00
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvEndpoint: "",
|
|
|
|
EnvApplicationKey: "B",
|
|
|
|
EnvApplicationSecret: "C",
|
|
|
|
EnvConsumerKey: "D",
|
2018-06-11 15:32:50 +00:00
|
|
|
},
|
2018-09-15 17:07:24 +00:00
|
|
|
expected: "ovh: some credentials information are missing: OVH_ENDPOINT",
|
2018-06-11 15:32:50 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-06 13:43:25 +00:00
|
|
|
desc: "application key: missing invalid endpoint",
|
2018-10-12 17:29:18 +00:00
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvEndpoint: "foobar",
|
|
|
|
EnvApplicationKey: "B",
|
|
|
|
EnvApplicationSecret: "C",
|
|
|
|
EnvConsumerKey: "D",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
2024-05-06 13:43:25 +00:00
|
|
|
expected: "ovh: new client: unknown endpoint 'foobar', consider checking 'Endpoints' list or using an URL",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-06 13:43:25 +00:00
|
|
|
desc: "application key: missing application key",
|
2018-06-11 15:32:50 +00:00
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvEndpoint: "ovh-eu",
|
|
|
|
EnvApplicationKey: "",
|
|
|
|
EnvApplicationSecret: "C",
|
|
|
|
EnvConsumerKey: "D",
|
2018-06-11 15:32:50 +00:00
|
|
|
},
|
2018-09-15 17:07:24 +00:00
|
|
|
expected: "ovh: some credentials information are missing: OVH_APPLICATION_KEY",
|
2018-06-11 15:32:50 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-06 13:43:25 +00:00
|
|
|
desc: "application key: missing application secret",
|
2018-06-11 15:32:50 +00:00
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvEndpoint: "ovh-eu",
|
|
|
|
EnvApplicationKey: "B",
|
|
|
|
EnvApplicationSecret: "",
|
|
|
|
EnvConsumerKey: "D",
|
2018-06-11 15:32:50 +00:00
|
|
|
},
|
2018-09-15 17:07:24 +00:00
|
|
|
expected: "ovh: some credentials information are missing: OVH_APPLICATION_SECRET",
|
2018-06-11 15:32:50 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-06 13:43:25 +00:00
|
|
|
desc: "application key: missing consumer key",
|
2018-06-11 15:32:50 +00:00
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvEndpoint: "ovh-eu",
|
|
|
|
EnvApplicationKey: "B",
|
|
|
|
EnvApplicationSecret: "C",
|
|
|
|
EnvConsumerKey: "",
|
2018-06-11 15:32:50 +00:00
|
|
|
},
|
2018-09-15 17:07:24 +00:00
|
|
|
expected: "ovh: some credentials information are missing: OVH_CONSUMER_KEY",
|
2018-06-11 15:32:50 +00:00
|
|
|
},
|
2024-05-06 13:43:25 +00:00
|
|
|
{
|
|
|
|
desc: "oauth2: success",
|
|
|
|
envVars: map[string]string{
|
|
|
|
EnvEndpoint: "ovh-eu",
|
|
|
|
EnvClientID: "E",
|
|
|
|
EnvClientSecret: "F",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "oauth2: missing client secret",
|
|
|
|
envVars: map[string]string{
|
|
|
|
EnvEndpoint: "ovh-eu",
|
|
|
|
EnvClientID: "E",
|
|
|
|
EnvClientSecret: "",
|
|
|
|
},
|
|
|
|
expected: "ovh: some credentials information are missing: OVH_CLIENT_SECRET",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "oauth2: missing client ID",
|
|
|
|
envVars: map[string]string{
|
|
|
|
EnvEndpoint: "ovh-eu",
|
|
|
|
EnvClientID: "",
|
|
|
|
EnvClientSecret: "F",
|
|
|
|
},
|
|
|
|
expected: "ovh: some credentials information are missing: OVH_CLIENT_ID",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
|
|
|
EnvEndpoint: "",
|
|
|
|
EnvApplicationKey: "",
|
|
|
|
EnvApplicationSecret: "",
|
|
|
|
EnvConsumerKey: "",
|
|
|
|
EnvClientID: "",
|
|
|
|
EnvClientSecret: "",
|
|
|
|
},
|
|
|
|
expected: "ovh: some credentials information are missing: OVH_ENDPOINT,OVH_APPLICATION_KEY,OVH_APPLICATION_SECRET,OVH_CONSUMER_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "mixed auth",
|
|
|
|
envVars: map[string]string{
|
|
|
|
EnvEndpoint: "ovh-eu",
|
|
|
|
EnvApplicationKey: "B",
|
|
|
|
EnvApplicationSecret: "C",
|
|
|
|
EnvConsumerKey: "D",
|
|
|
|
EnvClientID: "E",
|
|
|
|
EnvClientSecret: "F",
|
|
|
|
},
|
|
|
|
expected: "ovh: can't use both OVH_APPLICATION_KEY and OVH_CLIENT_ID at the same time",
|
|
|
|
},
|
2018-10-12 17:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.client)
|
|
|
|
require.NotNil(t, p.recordIDs)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
apiEndpoint string
|
|
|
|
applicationKey string
|
|
|
|
applicationSecret string
|
|
|
|
consumerKey string
|
2024-05-06 13:43:25 +00:00
|
|
|
clientID string
|
|
|
|
clientSecret string
|
2018-10-12 17:29:18 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2024-05-06 13:43:25 +00:00
|
|
|
desc: "application key: success",
|
2018-10-12 17:29:18 +00:00
|
|
|
apiEndpoint: "ovh-eu",
|
|
|
|
applicationKey: "B",
|
|
|
|
applicationSecret: "C",
|
|
|
|
consumerKey: "D",
|
|
|
|
},
|
2018-06-11 15:32:50 +00:00
|
|
|
{
|
2024-05-06 13:43:25 +00:00
|
|
|
desc: "application key: missing api endpoint",
|
2018-10-12 17:29:18 +00:00
|
|
|
apiEndpoint: "",
|
|
|
|
applicationKey: "B",
|
|
|
|
applicationSecret: "C",
|
|
|
|
consumerKey: "D",
|
2024-05-06 13:43:25 +00:00
|
|
|
expected: "ovh: credentials are missing",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-06 13:43:25 +00:00
|
|
|
desc: "application key: invalid api endpoint",
|
2018-10-12 17:29:18 +00:00
|
|
|
apiEndpoint: "foobar",
|
|
|
|
applicationKey: "B",
|
|
|
|
applicationSecret: "C",
|
|
|
|
consumerKey: "D",
|
2024-05-06 13:43:25 +00:00
|
|
|
expected: "ovh: new client: unknown endpoint 'foobar', consider checking 'Endpoints' list or using an URL",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-06 13:43:25 +00:00
|
|
|
desc: "application key: missing application key",
|
2018-10-12 17:29:18 +00:00
|
|
|
apiEndpoint: "ovh-eu",
|
|
|
|
applicationKey: "",
|
|
|
|
applicationSecret: "C",
|
|
|
|
consumerKey: "D",
|
2024-05-06 13:43:25 +00:00
|
|
|
expected: "ovh: credentials are missing",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-06 13:43:25 +00:00
|
|
|
desc: "application key: missing application secret",
|
2018-10-12 17:29:18 +00:00
|
|
|
apiEndpoint: "ovh-eu",
|
|
|
|
applicationKey: "B",
|
|
|
|
applicationSecret: "",
|
|
|
|
consumerKey: "D",
|
2024-05-06 13:43:25 +00:00
|
|
|
expected: "ovh: credentials are missing",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-06 13:43:25 +00:00
|
|
|
desc: "application key: missing consumer key",
|
2018-10-12 17:29:18 +00:00
|
|
|
apiEndpoint: "ovh-eu",
|
|
|
|
applicationKey: "B",
|
|
|
|
applicationSecret: "C",
|
|
|
|
consumerKey: "",
|
2024-05-06 13:43:25 +00:00
|
|
|
expected: "ovh: credentials are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "oauth2: success",
|
|
|
|
apiEndpoint: "ovh-eu",
|
|
|
|
clientID: "B",
|
|
|
|
clientSecret: "C",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "oauth2: missing api endpoint",
|
|
|
|
apiEndpoint: "",
|
|
|
|
clientID: "B",
|
|
|
|
clientSecret: "C",
|
|
|
|
expected: "ovh: credentials are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "oauth2: invalid api endpoint",
|
|
|
|
apiEndpoint: "foobar",
|
|
|
|
clientID: "B",
|
|
|
|
clientSecret: "C",
|
|
|
|
expected: "ovh: new OAuth2 client: unknown endpoint 'foobar', consider checking 'Endpoints' list or using an URL",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "oauth2: missing client id",
|
|
|
|
apiEndpoint: "ovh-eu",
|
|
|
|
clientID: "",
|
|
|
|
clientSecret: "C",
|
|
|
|
expected: "ovh: credentials are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "oauth2: missing client secret",
|
|
|
|
apiEndpoint: "ovh-eu",
|
|
|
|
clientID: "B",
|
|
|
|
clientSecret: "",
|
|
|
|
expected: "ovh: credentials are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "ovh: credentials are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "mixed auth",
|
|
|
|
apiEndpoint: "ovh-eu",
|
|
|
|
applicationKey: "B",
|
|
|
|
applicationSecret: "C",
|
|
|
|
consumerKey: "D",
|
|
|
|
clientID: "B",
|
|
|
|
clientSecret: "C",
|
|
|
|
expected: "ovh: can't use both authentication systems (ApplicationKey and OAuth2)",
|
2018-06-11 15:32:50 +00:00
|
|
|
},
|
|
|
|
}
|
2016-06-16 19:11:19 +00:00
|
|
|
|
2024-05-06 13:43:25 +00:00
|
|
|
// The OVH client use the same env vars than lego, so it requires to clean them.
|
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-12 17:29:18 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIEndpoint = test.apiEndpoint
|
|
|
|
config.ApplicationKey = test.applicationKey
|
|
|
|
config.ApplicationSecret = test.applicationSecret
|
|
|
|
config.ConsumerKey = test.consumerKey
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2024-05-06 13:43:25 +00:00
|
|
|
if test.clientID != "" || test.clientSecret != "" {
|
|
|
|
config.OAuth2Config = &OAuth2Config{
|
|
|
|
ClientID: test.clientID,
|
|
|
|
ClientSecret: test.clientSecret,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.client)
|
|
|
|
require.NotNil(t, p.recordIDs)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
})
|
|
|
|
}
|
2016-06-16 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLivePresent(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2016-06-16 19:11:19 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2016-06-16 19:11:19 +00:00
|
|
|
provider, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-06-16 19:11:19 +00:00
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-06-16 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2016-06-16 19:11:19 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2016-06-16 19:11:19 +00:00
|
|
|
provider, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-06-16 19:11:19 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-06-16 19:11:19 +00:00
|
|
|
}
|