Skip to content

Commit

Permalink
review: 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 12, 2023
1 parent 439df49 commit ff0bb62
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
7 changes: 3 additions & 4 deletions providers/dns/nicru/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/xml"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
Expand All @@ -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,
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion providers/dns/nicru/nicru.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package nicru

import (
"context"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 7 additions & 5 deletions providers/dns/nicru/nicru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
},
}

Expand Down

0 comments on commit ff0bb62

Please sign in to comment.