From ff0bb6231b9276631efc8c289060858042e57742 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 12 Apr 2023 19:18:01 +0200 Subject: [PATCH] review: 3 --- providers/dns/nicru/internal/client.go | 7 +++---- providers/dns/nicru/nicru.go | 3 ++- providers/dns/nicru/nicru_test.go | 12 +++++++----- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/providers/dns/nicru/internal/client.go b/providers/dns/nicru/internal/client.go index 61d4b71cec..091619d114 100644 --- a/providers/dns/nicru/internal/client.go +++ b/providers/dns/nicru/internal/client.go @@ -5,6 +5,7 @@ import ( "context" "encoding/xml" "errors" + "fmt" "net/http" "net/url" "strconv" @@ -27,14 +28,12 @@ type OauthConfiguration struct { Password string } -func NewOauthClient(config *OauthConfiguration) (*http.Client, error) { +func NewOauthClient(ctx context.Context, config *OauthConfiguration) (*http.Client, error) { err := validateAuthOptions(config) if err != nil { return nil, err } - ctx := context.TODO() - oauth2Config := oauth2.Config{ ClientID: config.OAuth2ClientID, ClientSecret: config.OAuth2SecretID, @@ -47,7 +46,7 @@ func NewOauthClient(config *OauthConfiguration) (*http.Client, error) { oauth2Token, err := oauth2Config.PasswordCredentialsToken(ctx, config.Username, config.Password) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to create oauth2 token: %w", err) } return oauth2Config.Client(ctx, oauth2Token), nil diff --git a/providers/dns/nicru/nicru.go b/providers/dns/nicru/nicru.go index 232a0446cd..5f755612fa 100644 --- a/providers/dns/nicru/nicru.go +++ b/providers/dns/nicru/nicru.go @@ -2,6 +2,7 @@ package nicru import ( + "context" "errors" "fmt" "time" @@ -87,7 +88,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { Password: config.Password, } - oauthClient, err := internal.NewOauthClient(clientCfg) + oauthClient, err := internal.NewOauthClient(context.Background(), clientCfg) if err != nil { return nil, fmt.Errorf("nicru: %w", err) } diff --git a/providers/dns/nicru/nicru_test.go b/providers/dns/nicru/nicru_test.go index 031bed0002..10c7dc36a4 100644 --- a/providers/dns/nicru/nicru_test.go +++ b/providers/dns/nicru/nicru_test.go @@ -36,6 +36,7 @@ func TestNewDNSProvider(t *testing.T) { EnvUsername: fakeUsername, EnvPassword: fakePassword, }, + expected: "nicru: failed to create oauth2 token: oauth2: cannot fetch token: 401 Unauthorized\nResponse: {\"error\":\"invalid_client\"}", }, { desc: "missing serviceId", @@ -127,6 +128,7 @@ func TestNewDNSProviderConfig(t *testing.T) { PropagationTimeout: defaultPropagationTimeout, PollingInterval: defaultPollingInterval, }, + expected: "nicru: failed to create oauth2 token: oauth2: cannot fetch token: 401 Unauthorized\nResponse: {\"error\":\"invalid_client\"}", }, { desc: "nil config", @@ -142,7 +144,7 @@ func TestNewDNSProviderConfig(t *testing.T) { PropagationTimeout: defaultPropagationTimeout, PollingInterval: defaultPollingInterval, }, - expected: "nicru: unable to build RU CENTER client: service name is missing in credentials information", + expected: "nicru: serviceId is missing in credentials information", }, { desc: "missing username", @@ -154,7 +156,7 @@ func TestNewDNSProviderConfig(t *testing.T) { PropagationTimeout: defaultPropagationTimeout, PollingInterval: defaultPollingInterval, }, - expected: "nicru: unable to build RU CENTER client: username is missing in credentials information", + expected: "nicru: username is missing in credentials information", }, { desc: "missing password", @@ -167,7 +169,7 @@ func TestNewDNSProviderConfig(t *testing.T) { PropagationTimeout: defaultPropagationTimeout, PollingInterval: defaultPollingInterval, }, - expected: "nicru: unable to build RU CENTER client: password is missing in credentials information", + expected: "nicru: password is missing in credentials information", }, { desc: "missing secret", @@ -179,7 +181,7 @@ func TestNewDNSProviderConfig(t *testing.T) { PropagationTimeout: defaultPropagationTimeout, PollingInterval: defaultPollingInterval, }, - expected: "nicru: unable to build RU CENTER client: secret is missing in credentials information", + expected: "nicru: secret is missing in credentials information", }, { desc: "missing serviceId", @@ -190,7 +192,7 @@ func TestNewDNSProviderConfig(t *testing.T) { Password: fakePassword, Domain: defaultDomainName, }, - expected: "nicru: unable to build RU CENTER client: serviceId is missing in credentials information", + expected: "nicru: serviceId is missing in credentials information", }, }