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

feat: added error handling for status 404 #160

Merged
merged 2 commits into from
Aug 24, 2022
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
4 changes: 4 additions & 0 deletions internal/pkg/vault/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
16 changes: 10 additions & 6 deletions internal/pkg/vault/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,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
Expand Down Expand Up @@ -465,16 +466,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"),
},
},
{
Expand All @@ -487,7 +489,8 @@ func TestHttpSecretStoreManager_GetValue(t *testing.T) {
expectedDoCallNum: 1,
caller: &ErrorMockCaller{
ReturnError: false,
StatusCode: 404,
StatusCode: 400,
ErrorType: pkg.NewErrSecretStore("Error"),
},
},
{
Expand All @@ -496,7 +499,7 @@ func TestHttpSecretStoreManager_GetValue(t *testing.T) {
keys: []string{"one"},
expectedValues: nil,
expectError: true,
expectedErrorType: TestConnError,
expectedErrorType: TestConnErrorPathNotFound,
expectedDoCallNum: 1,
caller: &InMemoryMockCaller{
Data: testData,
Expand Down Expand Up @@ -790,14 +793,15 @@ type ErrorMockCaller struct {
StatusCode int
ReturnError bool
DoCallCount int
ErrorType error
}

func (emc *ErrorMockCaller) Do(_ *http.Request) (*http.Response, error) {
emc.DoCallCount++
if emc.ReturnError {
return &http.Response{
StatusCode: emc.StatusCode,
}, pkg.NewErrSecretStore("testing conn error")
}, emc.ErrorType
}

return &http.Response{
Expand Down
14 changes: 14 additions & 0 deletions pkg/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}