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 ,
2024-09-14 12:26:25 +00:00
EnvClientSecret ,
EnvAccessToken ) .
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
} ,
} ,
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
} ,
2024-06-26 12:44:05 +00:00
expected : "ovh: new client: invalid authentication config, both application_key and application_secret must be given" ,
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
} ,
2024-06-26 12:44:05 +00:00
expected : "ovh: new client: invalid authentication config, both application_key and application_secret must be given" ,
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" ,
} ,
} ,
2024-09-14 12:26:25 +00:00
{
desc : "access token: success" ,
envVars : map [ string ] string {
EnvEndpoint : "ovh-eu" ,
EnvAccessToken : "G" ,
} ,
} ,
2024-05-06 13:43:25 +00:00
{
desc : "oauth2: missing client secret" ,
envVars : map [ string ] string {
EnvEndpoint : "ovh-eu" ,
EnvClientID : "E" ,
EnvClientSecret : "" ,
} ,
2024-06-26 12:44:05 +00:00
expected : "ovh: new client: invalid oauth2 config, both client_id and client_secret must be given" ,
2024-05-06 13:43:25 +00:00
} ,
{
desc : "oauth2: missing client ID" ,
envVars : map [ string ] string {
EnvEndpoint : "ovh-eu" ,
EnvClientID : "" ,
EnvClientSecret : "F" ,
} ,
2024-06-26 12:44:05 +00:00
expected : "ovh: new client: invalid oauth2 config, both client_id and client_secret must be given" ,
2024-05-06 13:43:25 +00:00
} ,
{
desc : "missing credentials" ,
envVars : map [ string ] string {
EnvEndpoint : "" ,
EnvApplicationKey : "" ,
EnvApplicationSecret : "" ,
EnvConsumerKey : "" ,
EnvClientID : "" ,
EnvClientSecret : "" ,
2024-09-14 12:26:25 +00:00
EnvAccessToken : "" ,
2024-05-06 13:43:25 +00:00
} ,
2024-09-14 12:26:25 +00:00
expected : "ovh: new client: missing authentication information, you need to provide one of the following: application_key/application_secret, client_id/client_secret, or access_token" ,
2024-05-06 13:43:25 +00:00
} ,
{
2024-09-14 12:26:25 +00:00
desc : "mixed auth (all)" ,
2024-05-06 13:43:25 +00:00
envVars : map [ string ] string {
EnvEndpoint : "ovh-eu" ,
EnvApplicationKey : "B" ,
EnvApplicationSecret : "C" ,
EnvConsumerKey : "D" ,
EnvClientID : "E" ,
EnvClientSecret : "F" ,
2024-09-14 12:26:25 +00:00
EnvAccessToken : "G" ,
} ,
expected : "ovh: can't use multiple authentication systems (ApplicationKey, OAuth2, Access Token)" ,
} ,
{
desc : "mixed auth (ApplicationKey, OAuth2)" ,
envVars : map [ string ] string {
EnvEndpoint : "ovh-eu" ,
EnvApplicationKey : "B" ,
EnvApplicationSecret : "C" ,
EnvConsumerKey : "D" ,
EnvClientID : "E" ,
EnvClientSecret : "F" ,
} ,
expected : "ovh: can't use multiple authentication systems (ApplicationKey, OAuth2)" ,
} ,
{
desc : "mixed auth (ApplicationKey, Access Token)" ,
envVars : map [ string ] string {
EnvEndpoint : "ovh-eu" ,
EnvApplicationKey : "B" ,
EnvApplicationSecret : "C" ,
EnvConsumerKey : "D" ,
EnvAccessToken : "G" ,
} ,
expected : "ovh: can't use multiple authentication systems (ApplicationKey, Access Token)" ,
} ,
{
desc : "mixed auth (OAuth2, Access Token)" ,
envVars : map [ string ] string {
EnvEndpoint : "ovh-eu" ,
EnvClientID : "E" ,
EnvClientSecret : "F" ,
EnvAccessToken : "G" ,
2024-05-06 13:43:25 +00:00
} ,
2024-09-14 12:26:25 +00:00
expected : "ovh: can't use multiple authentication systems (OAuth2, Access Token)" ,
2024-05-06 13:43:25 +00:00
} ,
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
2024-09-14 12:26:25 +00:00
accessToken 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-06-26 12:44:05 +00:00
expected : "ovh: new client: unknown endpoint '', 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: 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-06-26 12:44:05 +00:00
expected : "ovh: new client: invalid authentication config, both application_key and application_secret must be given" ,
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-06-26 12:44:05 +00:00
expected : "ovh: new client: invalid authentication config, both application_key and application_secret must be given" ,
2024-05-06 13:43:25 +00:00
} ,
{
desc : "oauth2: success" ,
apiEndpoint : "ovh-eu" ,
clientID : "B" ,
clientSecret : "C" ,
} ,
{
desc : "oauth2: missing api endpoint" ,
apiEndpoint : "" ,
clientID : "B" ,
clientSecret : "C" ,
2024-06-26 12:44:05 +00:00
expected : "ovh: new client: unknown endpoint '', consider checking 'Endpoints' list or using an URL" ,
2024-05-06 13:43:25 +00:00
} ,
{
desc : "oauth2: invalid api endpoint" ,
apiEndpoint : "foobar" ,
clientID : "B" ,
clientSecret : "C" ,
2024-06-26 12:44:05 +00:00
expected : "ovh: new client: unknown endpoint 'foobar', consider checking 'Endpoints' list or using an URL" ,
2024-05-06 13:43:25 +00:00
} ,
{
desc : "oauth2: missing client id" ,
apiEndpoint : "ovh-eu" ,
clientID : "" ,
clientSecret : "C" ,
2024-06-26 12:44:05 +00:00
expected : "ovh: new client: invalid oauth2 config, both client_id and client_secret must be given" ,
2024-05-06 13:43:25 +00:00
} ,
{
desc : "oauth2: missing client secret" ,
apiEndpoint : "ovh-eu" ,
clientID : "B" ,
clientSecret : "" ,
2024-06-26 12:44:05 +00:00
expected : "ovh: new client: invalid oauth2 config, both client_id and client_secret must be given" ,
2024-05-06 13:43:25 +00:00
} ,
2024-09-14 12:26:25 +00:00
{
desc : "access token: success" ,
apiEndpoint : "ovh-eu" ,
accessToken : "G" ,
} ,
2024-05-06 13:43:25 +00:00
{
desc : "missing credentials" ,
2024-09-14 12:26:25 +00:00
expected : "ovh: new client: missing authentication information, you need to provide one of the following: application_key/application_secret, client_id/client_secret, or access_token" ,
2024-05-06 13:43:25 +00:00
} ,
{
2024-09-14 12:26:25 +00:00
desc : "mixed auth (all)" ,
2024-05-06 13:43:25 +00:00
apiEndpoint : "ovh-eu" ,
applicationKey : "B" ,
applicationSecret : "C" ,
consumerKey : "D" ,
clientID : "B" ,
clientSecret : "C" ,
2024-09-14 12:26:25 +00:00
accessToken : "G" ,
expected : "ovh: can't use multiple authentication systems (ApplicationKey, OAuth2, Access Token)" ,
} ,
{
desc : "mixed auth (ApplicationKey, OAuth2)" ,
apiEndpoint : "ovh-eu" ,
applicationKey : "B" ,
applicationSecret : "C" ,
consumerKey : "D" ,
clientID : "B" ,
clientSecret : "C" ,
expected : "ovh: can't use multiple authentication systems (ApplicationKey, OAuth2)" ,
} ,
{
desc : "mixed auth (ApplicationKey, Access Token)" ,
apiEndpoint : "ovh-eu" ,
applicationKey : "B" ,
applicationSecret : "C" ,
consumerKey : "D" ,
accessToken : "G" ,
expected : "ovh: can't use multiple authentication systems (ApplicationKey, Access Token)" ,
} ,
{
desc : "mixed auth (OAuth2, Access Token)" ,
apiEndpoint : "ovh-eu" ,
clientID : "B" ,
clientSecret : "C" ,
accessToken : "G" ,
expected : "ovh: can't use multiple authentication systems (OAuth2, Access Token)" ,
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
2024-09-14 12:26:25 +00:00
config . AccessToken = test . accessToken
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
}