Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid credential error handling in authentication process #536

Merged
merged 7 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changes/v2.20.0/536-bug-fixes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Fix a bug that prevented returning a specific error while authenticating client with invalid
password [GH-536]
8 changes: 8 additions & 0 deletions govcd/api_vcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ func (vcdClient *VCDClient) vcdCloudApiAuthorize(user, pass, org string) (*http.
}

defer resp.Body.Close()

// Catch HTTP 401 (Status Unauthorized) to return an error as otherwise this library would return
// odd errors while doing lookup of resources and confuse user.
if resp.StatusCode == http.StatusUnauthorized {
return nil, fmt.Errorf("received response HTTP %d (Unauthorized). Please check if your credentials are valid",
resp.StatusCode)
}

// Store the authorization header
vcdClient.Client.VCDToken = resp.Header.Get(BearerTokenHeader)
vcdClient.Client.VCDAuthHeader = BearerTokenHeader
Expand Down
32 changes: 32 additions & 0 deletions govcd/api_vcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,38 @@ func TestVCDClient_Authenticate(t *testing.T) {
}
}

func TestVCDClient_AuthenticateInvalidPassword(t *testing.T) {
config, err := GetConfigStruct()
if err != nil {
t.Fatalf("err: %s", err)
}
client, err := GetTestVCDFromYaml(config)
if err != nil {
t.Fatalf("error getting client structure: %s", err)
}

err = client.Authenticate(config.Provider.User, "INVALID-PASSWORD", config.Provider.SysOrg)
if err == nil || !strings.Contains(err.Error(), "401") {
t.Fatalf("expected error for invalid credentials")
}
}

func TestVCDClient_AuthenticateInvalidToken(t *testing.T) {
config, err := GetConfigStruct()
if err != nil {
t.Fatalf("err: %s", err)
}
client, err := GetTestVCDFromYaml(config)
if err != nil {
t.Fatalf("error getting client structure: %s", err)
}

err = client.SetToken(config.Provider.SysOrg, AuthorizationHeader, "invalid-token")
if err == nil || !strings.Contains(err.Error(), "401") {
t.Fatalf("expected error for invalid credentials")
}
}

func Test_splitParent(t *testing.T) {
type args struct {
parent string
Expand Down