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
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
Prev Previous commit
Had to remove the unit test for cancel
Rego uses a go routine to track the ctx cancelled, so if the rego
query completes before that go routine even runs, or checks, then
there is no error. This is a race condition in how rego handles
context cancels.
  • Loading branch information
Emyrk committed Nov 17, 2023
commit 038f7973b94d50860f4b6b17817a3fc7b7abd09a
18 changes: 0 additions & 18 deletions coderd/rbac/authz_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,24 +1023,6 @@ 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