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

[BUGFIX] Fix HTTP error returned when access permission is missing #1913

Merged
merged 1 commit into from
Apr 16, 2024
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
2 changes: 1 addition & 1 deletion internal/api/e2e/api/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestUnauthorizedEndpoints(t *testing.T) {
token := authResponse.JSON().Object().Value("accessToken").String().Raw()

glRole := e2eframework.NewGlobalRole("test")
expect.POST(fmt.Sprintf("%s/%s", utils.APIV1Prefix, utils.PathGlobalRole)).WithJSON(glRole).WithHeader("Authorization", fmt.Sprintf("Bearer %s", token)).Expect().Status(http.StatusUnauthorized)
expect.POST(fmt.Sprintf("%s/%s", utils.APIV1Prefix, utils.PathGlobalRole)).WithJSON(glRole).WithHeader("Authorization", fmt.Sprintf("Bearer %s", token)).Expect().Status(http.StatusForbidden)

project2Entity := e2eframework.NewProject("mysuperproject2")
expect.POST(fmt.Sprintf("%s/%s", utils.APIV1Prefix, utils.PathProject)).WithJSON(project2Entity).WithHeader("Authorization", "Bearer <bad token>").Expect().Status(http.StatusUnauthorized)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/impl/v1/user/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (e *endpoint) GetPermissions(ctx echo.Context) error {
return apiinterface.HandleUnauthorizedError("you need to be connected to retrieve your permissions")
}
if claims.Subject != parameters.Name {
return apiinterface.HandleUnauthorizedError("you can only retrieve your permissions")
return apiinterface.HandleForbiddenError("you can only retrieve your permissions")
}
permissions := e.rbac.GetPermissions(claims.Subject)
return ctx.JSON(http.StatusOK, permissions)
Expand Down
6 changes: 3 additions & 3 deletions internal/api/toolbox/toolbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (t *toolbox[T, K, V]) checkPermission(ctx echo.Context, entity api.Entity,
}
if role.IsGlobalScope(*scope) {
if ok := t.rbac.HasPermission(claims.Subject, action, rbac.GlobalProject, *scope); !ok {
return apiInterface.HandleUnauthorizedError(fmt.Sprintf("missing '%s' global permission for '%s' kind", action, *scope))
return apiInterface.HandleForbiddenError(fmt.Sprintf("missing '%s' global permission for '%s' kind", action, *scope))
}
return nil
}
Expand All @@ -93,7 +93,7 @@ func (t *toolbox[T, K, V]) checkPermission(ctx echo.Context, entity api.Entity,
// Create is still a "Global" only permission
if action == role.CreateAction {
if ok := t.rbac.HasPermission(claims.Subject, action, rbac.GlobalProject, *scope); !ok {
return apiInterface.HandleUnauthorizedError(fmt.Sprintf("missing '%s' global permission for '%s' kind", action, *scope))
return apiInterface.HandleForbiddenError(fmt.Sprintf("missing '%s' global permission for '%s' kind", action, *scope))
}
return nil
}
Expand All @@ -105,7 +105,7 @@ func (t *toolbox[T, K, V]) checkPermission(ctx echo.Context, entity api.Entity,
projectName = utils.GetMetadataProject(entity.GetMetadata())
}
if ok := t.rbac.HasPermission(claims.Subject, action, projectName, *scope); !ok {
return apiInterface.HandleUnauthorizedError(fmt.Sprintf("missing '%s' permission in '%s' project for '%s' kind", action, projectName, *scope))
return apiInterface.HandleForbiddenError(fmt.Sprintf("missing '%s' permission in '%s' project for '%s' kind", action, projectName, *scope))

}
return nil
Expand Down
Loading