From 55df4f95336da0204aeb68defde6aadacc44501e Mon Sep 17 00:00:00 2001 From: Dainius Serplis Date: Thu, 15 Dec 2022 21:25:15 +0200 Subject: [PATCH 1/5] Add 401 check in auth and tests Signed-off-by: Dainius Serplis --- govcd/api_vcd.go | 8 ++++++++ govcd/api_vcd_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/govcd/api_vcd.go b/govcd/api_vcd.go index e4ffc885b..eeb613d62 100644 --- a/govcd/api_vcd.go +++ b/govcd/api_vcd.go @@ -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 will return + // odd errors while doing lookup of resources and confused 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 diff --git a/govcd/api_vcd_test.go b/govcd/api_vcd_test.go index 4da761617..63afb4bf6 100644 --- a/govcd/api_vcd_test.go +++ b/govcd/api_vcd_test.go @@ -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 From e1727a313c73a3f72c1631ac214befbc0e50101b Mon Sep 17 00:00:00 2001 From: Dainius Serplis Date: Thu, 15 Dec 2022 21:35:02 +0200 Subject: [PATCH 2/5] Add changelog note Signed-off-by: Dainius Serplis --- .changes/v2.19.0/536-bug-fixes.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 .changes/v2.19.0/536-bug-fixes.md diff --git a/.changes/v2.19.0/536-bug-fixes.md b/.changes/v2.19.0/536-bug-fixes.md new file mode 100644 index 000000000..fef2bbdfb --- /dev/null +++ b/.changes/v2.19.0/536-bug-fixes.md @@ -0,0 +1 @@ +* Fix a bug, that would not return an error while authenticating with invalid password [GH-536] From 391fa6275b6b5c1039b1317058dc9190d619d706 Mon Sep 17 00:00:00 2001 From: Dainius Serplis Date: Mon, 16 Jan 2023 12:41:26 +0200 Subject: [PATCH 3/5] Fix changelog Signed-off-by: Dainius Serplis --- .changes/{v2.19.0 => v2.20.0}/536-bug-fixes.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .changes/{v2.19.0 => v2.20.0}/536-bug-fixes.md (100%) diff --git a/.changes/v2.19.0/536-bug-fixes.md b/.changes/v2.20.0/536-bug-fixes.md similarity index 100% rename from .changes/v2.19.0/536-bug-fixes.md rename to .changes/v2.20.0/536-bug-fixes.md From 0ada5c702d8073a65109885295cb5cd20809c554 Mon Sep 17 00:00:00 2001 From: Dainius Serplis Date: Mon, 16 Jan 2023 14:16:29 +0200 Subject: [PATCH 4/5] Address coment Signed-off-by: Dainius Serplis --- .changes/v2.20.0/536-bug-fixes.md | 3 ++- govcd/api_vcd.go | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.changes/v2.20.0/536-bug-fixes.md b/.changes/v2.20.0/536-bug-fixes.md index fef2bbdfb..dd511e9c0 100644 --- a/.changes/v2.20.0/536-bug-fixes.md +++ b/.changes/v2.20.0/536-bug-fixes.md @@ -1 +1,2 @@ -* Fix a bug, that would not return an error while authenticating with invalid password [GH-536] +* Fix a bug that prevented returning a specific error while authenticating with invalid password + [GH-536] diff --git a/govcd/api_vcd.go b/govcd/api_vcd.go index eeb613d62..fc3619ed9 100644 --- a/govcd/api_vcd.go +++ b/govcd/api_vcd.go @@ -93,8 +93,8 @@ 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 will return - // odd errors while doing lookup of resources and confused user. + // 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) From b568d7d0ae6094ae219611639338290152fde19f Mon Sep 17 00:00:00 2001 From: Dainius Serplis Date: Wed, 18 Jan 2023 14:13:30 +0200 Subject: [PATCH 5/5] Address comment Signed-off-by: Dainius Serplis --- .changes/v2.20.0/536-bug-fixes.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changes/v2.20.0/536-bug-fixes.md b/.changes/v2.20.0/536-bug-fixes.md index dd511e9c0..e8558e523 100644 --- a/.changes/v2.20.0/536-bug-fixes.md +++ b/.changes/v2.20.0/536-bug-fixes.md @@ -1,2 +1,2 @@ -* Fix a bug that prevented returning a specific error while authenticating with invalid password - [GH-536] +* Fix a bug that prevented returning a specific error while authenticating client with invalid + password [GH-536]