From f6f7f41d4358a7c966669a54d428ff399083372d Mon Sep 17 00:00:00 2001 From: Kyle Morton Date: Wed, 24 Aug 2022 12:33:09 -0500 Subject: [PATCH 1/2] feat: added error handling for status 404 Signed-off-by: Kyle Morton --- internal/pkg/vault/secrets.go | 4 ++++ internal/pkg/vault/secrets_test.go | 21 +++++++++++++-------- pkg/errors.go | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/internal/pkg/vault/secrets.go b/internal/pkg/vault/secrets.go index f2a84a62..a8ea55c5 100644 --- a/internal/pkg/vault/secrets.go +++ b/internal/pkg/vault/secrets.go @@ -374,6 +374,10 @@ func (c *Client) getAllKeys(subPath string) (map[string]string, error) { _ = resp.Body.Close() }() + if resp.StatusCode == 404 { + return nil, pkg.NewErrPathNotFound(fmt.Sprintf("Received a '%d' response from the secret store", resp.StatusCode)) + } + if resp.StatusCode < 200 || resp.StatusCode > 299 { return nil, pkg.NewErrSecretStore(fmt.Sprintf("Received a '%d' response from the secret store", resp.StatusCode)) } diff --git a/internal/pkg/vault/secrets_test.go b/internal/pkg/vault/secrets_test.go index 9d1e9ee1..88d5aab2 100644 --- a/internal/pkg/vault/secrets_test.go +++ b/internal/pkg/vault/secrets_test.go @@ -45,8 +45,9 @@ const ( // define as constants to avoid using global variables as global variables are evil to the whole package level scope: // Global variables can cause side effects which are difficult to keep track of. A code in one function may // change the variables state while another unrelated chunk of code may be affected by it. - testPath = "/data" - testNamespace = "database" + testPath = "/data" + testPathNotFound = "/path" + testNamespace = "database" ) func TestNewSecretsClient(t *testing.T) { @@ -381,6 +382,7 @@ func TestConcurrentSecretClientTokenRenewals(t *testing.T) { func TestHttpSecretStoreManager_GetValue(t *testing.T) { TestConnError := pkg.NewErrSecretStore("testing conn error") + TestConnErrorPathNotFound := pkg.NewErrPathNotFound("testing path error") testData := getTestSecretsData() tests := []struct { name string @@ -465,16 +467,17 @@ func TestHttpSecretStoreManager_GetValue(t *testing.T) { }, }, { - name: "Handle HTTP error", + name: "Handle HTTP no path error", path: testPath, keys: []string{"Does not exist"}, expectedValues: nil, expectError: true, - expectedErrorType: TestConnError, + expectedErrorType: TestConnErrorPathNotFound, expectedDoCallNum: 1, caller: &ErrorMockCaller{ - ReturnError: true, + ReturnError: false, StatusCode: 404, + ErrorType: pkg.NewErrPathNotFound("Not found"), }, }, { @@ -487,7 +490,8 @@ func TestHttpSecretStoreManager_GetValue(t *testing.T) { expectedDoCallNum: 1, caller: &ErrorMockCaller{ ReturnError: false, - StatusCode: 404, + StatusCode: 400, + ErrorType: pkg.NewErrSecretStore("Error"), }, }, { @@ -496,7 +500,7 @@ func TestHttpSecretStoreManager_GetValue(t *testing.T) { keys: []string{"one"}, expectedValues: nil, expectError: true, - expectedErrorType: TestConnError, + expectedErrorType: TestConnErrorPathNotFound, expectedDoCallNum: 1, caller: &InMemoryMockCaller{ Data: testData, @@ -790,6 +794,7 @@ type ErrorMockCaller struct { StatusCode int ReturnError bool DoCallCount int + ErrorType error } func (emc *ErrorMockCaller) Do(_ *http.Request) (*http.Response, error) { @@ -797,7 +802,7 @@ func (emc *ErrorMockCaller) Do(_ *http.Request) (*http.Response, error) { if emc.ReturnError { return &http.Response{ StatusCode: emc.StatusCode, - }, pkg.NewErrSecretStore("testing conn error") + }, emc.ErrorType } return &http.Response{ diff --git a/pkg/errors.go b/pkg/errors.go index e3c923a7..ea19e098 100644 --- a/pkg/errors.go +++ b/pkg/errors.go @@ -46,3 +46,17 @@ func (scnf ErrSecretsNotFound) Error() string { func NewErrSecretsNotFound(keys []string) ErrSecretsNotFound { return ErrSecretsNotFound{keys: keys} } + +// ErrPathNotFound error when a secret path cannot be found. +type ErrPathNotFound struct { + description string +} + +func (e ErrPathNotFound) Error() string { + return fmt.Sprintf("Error retreiving secret path: '%s'", e.description) +} + +// NewErrPathNotFound creates a new ErrSecretsNotFound error. +func NewErrPathNotFound(description string) ErrPathNotFound { + return ErrPathNotFound{description: description} +} From 77d5229c3031b05b6f2170c0fdfc384446df1281 Mon Sep 17 00:00:00 2001 From: Kyle Morton Date: Wed, 24 Aug 2022 17:20:23 -0500 Subject: [PATCH 2/2] feat: PR update lint issue Signed-off-by: Kyle Morton --- internal/pkg/vault/secrets_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/pkg/vault/secrets_test.go b/internal/pkg/vault/secrets_test.go index 88d5aab2..b55a8a0d 100644 --- a/internal/pkg/vault/secrets_test.go +++ b/internal/pkg/vault/secrets_test.go @@ -45,9 +45,8 @@ const ( // define as constants to avoid using global variables as global variables are evil to the whole package level scope: // Global variables can cause side effects which are difficult to keep track of. A code in one function may // change the variables state while another unrelated chunk of code may be affected by it. - testPath = "/data" - testPathNotFound = "/path" - testNamespace = "database" + testPath = "/data" + testNamespace = "database" ) func TestNewSecretsClient(t *testing.T) {