Skip to content

chore: return context.Canceled when in Prepare for rbac #10763

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

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
chore: return context.Canceled when in Prepare for rbac
Was returning a custom rego canceled error. This conforms with
how Authorize handles this error.
  • Loading branch information
Emyrk committed Nov 17, 2023
commit df52ecb6640495da76f10b85fd9f93af0b945b91
1 change: 1 addition & 0 deletions coderd/rbac/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ func (a RegoAuthorizer) Prepare(ctx context.Context, subject Subject, action Act

prepared, err := a.newPartialAuthorizer(ctx, subject, action, objectType)
if err != nil {
err = correctCancelError(err)
return nil, xerrors.Errorf("new partial authorizer: %w", err)
}

Expand Down
18 changes: 18 additions & 0 deletions coderd/rbac/authz_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,24 @@ func TestAuthorizeScope(t *testing.T) {
)
}

func TestCanceledPrepare(t *testing.T) {
t.Parallel()

authorizer := NewAuthorizer(prometheus.NewRegistry())
// This context is canceled intentionally to test the prepare error.
ctx, cancel := context.WithCancel(context.Background())
cancel()

// The error should be a `context.Canceled` error.
// By default Rego throws a custom cancelled error.
_, err := authorizer.Prepare(ctx, Subject{
ID: "foo",
Roles: RoleNames{RoleOwner()},
Scope: ScopeAll,
}, ActionRead, ResourceWorkspace.Type)
require.ErrorIs(t, err, context.Canceled, "expected canceled context")
}

// cases applies a given function to all test cases. This makes generalities easier to create.
func cases(opt func(c authTestCase) authTestCase, cases []authTestCase) []authTestCase {
if opt == nil {
Expand Down