Skip to content

Commit

Permalink
registry/errors: Parse http forbidden as denied
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Gronowski <[email protected]>
(cherry picked from commit 5f1df02)
Signed-off-by: Paweł Gronowski <[email protected]>
  • Loading branch information
vvoland committed May 11, 2023
1 parent 320d6a1 commit 483ad69
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions registry/client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func parseHTTPErrorResponse(statusCode int, r io.Reader) error {
switch statusCode {
case http.StatusUnauthorized:
return errcode.ErrorCodeUnauthorized.WithMessage(detailsErr.Details)
case http.StatusForbidden:
return errcode.ErrorCodeDenied.WithMessage(detailsErr.Details)
case http.StatusTooManyRequests:
return errcode.ErrorCodeTooManyRequests.WithMessage(detailsErr.Details)
default:
Expand Down
15 changes: 15 additions & 0 deletions registry/client/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,18 @@ func TestHandleErrorResponseUnexpectedStatusCode501(t *testing.T) {
t.Errorf("Expected \"%s\", got: \"%s\"", expectedMsg, err.Error())
}
}

func TestHandleErrorResponseInsufficientPrivileges403(t *testing.T) {
json := `{"details":"requesting higher privileges than access token allows"}`
response := &http.Response{
Status: "403 Forbidden",
StatusCode: 403,
Body: nopCloser{bytes.NewBufferString(json)},
}
err := HandleErrorResponse(response)

expectedMsg := "denied: requesting higher privileges than access token allows"
if !strings.Contains(err.Error(), expectedMsg) {
t.Errorf("Expected \"%s\", got: \"%s\"", expectedMsg, err.Error())
}
}

0 comments on commit 483ad69

Please sign in to comment.