Skip to content

Commit

Permalink
DXE-4204 fix drift detection for deleted access key
Browse files Browse the repository at this point in the history
  • Loading branch information
mimazaka committed Oct 29, 2024
1 parent 906fbb6 commit c3731e7
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@

#### BUG FIXES:


* Cloud Access
* Added custom error `ErrAccessKeyNotFound` to easier verify if provided access key does not exist.



Expand Down
18 changes: 18 additions & 0 deletions pkg/cloudaccess/access_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,24 @@ func TestGetAccessKey(t *testing.T) {
assert.Equal(t, "get an access key: struct validation: AccessKeyUID: cannot be blank", err.Error())
},
},
"404 access key not found - custom error check": {
params: AccessKeyRequest{
AccessKeyUID: 2,
},
expectedPath: "/cam/v1/access-keys/2",
responseStatus: http.StatusNotFound,
responseBody: `{
"type": "/cam/error-types/access-key-does-not-exist",
"title": "Domain Error",
"detail": "Access key with accessKeyUID '2' does not exist.",
"instance": "test-instance-123",
"status": 404,
"accessKeyUid": 2
}`,
withError: func(t *testing.T, err error) {
assert.True(t, errors.Is(err, ErrAccessKeyNotFound))
},
},
"500 internal server error": {
params: AccessKeyRequest{
AccessKeyUID: 1,
Expand Down
86 changes: 85 additions & 1 deletion pkg/cloudaccess/access_key_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,35 @@ func TestGetAccessKeyVersion(t *testing.T) {
assert.Equal(t, "get access key version: struct validation: AccessKeyUID: cannot be blank\nVersion: cannot be blank", err.Error())
},
},
"404 error": {
"404 error - access key with specific uid not exists": {
params: GetAccessKeyVersionRequest{
AccessKeyUID: 1,
Version: 1,
},
responseStatus: http.StatusNotFound,
responseBody: `
{
"accessKeyUid": 1,
"detail": "Access key with accessKeyUid '1' does not exist.",
"instance": "c111eff1-22ec-4d4e-99c9-55efb5d55f55",
"status": 404,
"title": "Domain Error",
"type": "/cam/error-types/access-key-does-not-exist"
}`,
expectedPath: "/cam/v1/access-keys/1/versions/1",
withError: func(t *testing.T, err error) {
want := &Error{
AccessKeyUID: 1,
Type: "/cam/error-types/access-key-does-not-exist",
Title: "Domain Error",
Detail: "Access key with accessKeyUid '1' does not exist.",
Instance: "c111eff1-22ec-4d4e-99c9-55efb5d55f55",
Status: http.StatusNotFound,
}
assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
},
},
"404 error - wrap into custom error": {
params: GetAccessKeyVersionRequest{
AccessKeyUID: 1,
Version: 1,
Expand All @@ -333,9 +361,65 @@ func TestGetAccessKeyVersion(t *testing.T) {
Instance: "c111eff1-22ec-4d4e-99c9-55efb5d55f55",
Status: http.StatusNotFound,
}
assert.True(t, errors.Is(want, ErrAccessKeyNotFound))
},
},
"404 error - access key version for specific access key not exists": {
params: GetAccessKeyVersionRequest{
AccessKeyUID: 1,
Version: 2,
},
responseStatus: http.StatusNotFound,
responseBody: `
{
"accessKeyUid": 1,
"detail": "Version '2' for access key '1' does not exist.",
"instance": "12345-12345-12345-1234-12345678",
"status": 404,
"title": "Domain Error",
"type": "/cam/error-types/access-key-version-does-not-exist"
}`,
expectedPath: "/cam/v1/access-keys/1/versions/2",
withError: func(t *testing.T, err error) {
want := &Error{
AccessKeyUID: 1,
Type: "/cam/error-types/access-key-version-does-not-exist",
Title: "Domain Error",
Detail: "Version '2' for access key '1' does not exist.",
Instance: "12345-12345-12345-1234-12345678",
Status: http.StatusNotFound,
}
assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
},
},
"404 error - does not wrap into custom error": {
params: GetAccessKeyVersionRequest{
AccessKeyUID: 1,
Version: 2,
},
responseStatus: http.StatusNotFound,
responseBody: `
{
"accessKeyUid": 1,
"detail": "Version '2' for access key '1' does not exist.",
"instance": "12345-12345-12345-1234-12345678",
"status": 404,
"title": "Domain Error",
"type": "/cam/error-types/access-key-version-does-not-exist"
}`,
expectedPath: "/cam/v1/access-keys/1/versions/2",
withError: func(t *testing.T, err error) {
want := &Error{
AccessKeyUID: 1,
Type: "/cam/error-types/access-key-version-does-not-exist",
Title: "Domain Error",
Detail: "Version '2' for access key '1' does not exist.",
Instance: "12345-12345-12345-1234-12345678",
Status: http.StatusNotFound,
}
assert.False(t, errors.Is(want, ErrAccessKeyNotFound))
},
},
}

for name, test := range tests {
Expand Down
9 changes: 9 additions & 0 deletions pkg/cloudaccess/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ type (
}
)

const accessKeyNotFoundType = "/cam/error-types/access-key-does-not-exist"

// ErrAccessKeyNotFound is returned when access key was not found
var ErrAccessKeyNotFound = errors.New("access key not found")

// Error parses an error from the response
func (c *cloudaccess) Error(r *http.Response) error {
var e Error
Expand Down Expand Up @@ -68,6 +73,10 @@ func (e *Error) Error() string {

// Is handles error comparisons
func (e *Error) Is(target error) bool {
if errors.Is(target, ErrAccessKeyNotFound) {
return e.Status == http.StatusNotFound && e.Type == accessKeyNotFoundType
}

var t *Error
if !errors.As(target, &t) {
return false
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudaccess/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestNewError(t *testing.T) {
StatusCode: http.StatusNotFound,
Body: ioutil.NopCloser(strings.NewReader(
`{
"type": "access-key-does-not-exists",
"type": "/cam/error-types/access-key-does-not-exist",
"title": "Domain Error",
"detail": "Access key with accessKeyUID '1' does not exist.",
"instance": "test-instance-123",
Expand All @@ -108,7 +108,7 @@ func TestNewError(t *testing.T) {
Request: req,
},
expected: &Error{
Type: "access-key-does-not-exists",
Type: accessKeyNotFoundType,
Title: "Domain Error",
Detail: "Access key with accessKeyUID '1' does not exist.",
Instance: "test-instance-123",
Expand Down

0 comments on commit c3731e7

Please sign in to comment.