8000 chore: Add benchmark test for rbac.Filter by Emyrk · Pull Request #3121 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

chore: Add benchmark test for rbac.Filter #3121

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

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 3 additions & 13 deletions coderd/rbac/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ var policy string
func NewAuthorizer() (*RegoAuthorizer, error) {
ctx := context.Background()
query, err := rego.New(
// allowed is the `allow` field from the prepared query. This is the field to check if authorization is
// granted.
rego.Query("allowed = data.authz.allow"),
// Query returns true/false for authorization access
rego.Query("data.authz.allow"),
rego.Module("policy.rego", policy),
).PrepareForEval(ctx)

Expand Down Expand Up @@ -92,16 +91,7 @@ func (a RegoAuthorizer) Authorize(ctx context.Context, subjectID string, roles [
return ForbiddenWithInternal(xerrors.Errorf("eval rego: %w", err), input, results)
}

if len(results) != 1 {
return ForbiddenWithInternal(xerrors.Errorf("expect only 1 result, got %d", len(results)), input, results)
}

allowedResult, ok := (results[0].Bindings["allowed"]).(bool)
if !ok {
return ForbiddenWithInternal(xerrors.Errorf("expected allowed to be a bool but got %T", allowedResult), input, results)
}

if !allowedResult {
if !results.Allowed() {
return ForbiddenWithInternal(xerrors.Errorf("policy disallows request"), input, results)
}

Expand Down
23 changes: 23 additions & 0 deletions coderd/rbac/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ type subject struct {
Roles []rbac.Role `json:"roles"`
}

// BenchmarkRBACFilter benchmarks the rbac.Filter method. Authorizing batch
// objects has a noticeable cost on performance.
// go test -bench BenchmarkRBACFilter -benchmem -memprofile memprofile.out -cpuprofile profile.out
func BenchmarkRBACFilter(b *testing.B) {
ctx := context.Background()
objectList := make([]rbac.Object, b.N)
orgID := uuid.New()
for i := range objectList {
objectList[i] = rbac.ResourceWorkspace.
InOrg(orgID).
WithID(uuid.NewString()).
WithOwner("other")
}

authorizer, err := rbac.NewAuthorizer()
require.NoError(b, err)

roles := []string{rbac.RoleOrgAdmin(orgID)}

b.ResetTimer()
rbac.Filter(ctx, authorizer, "me", roles, rbac.ActionRead, objectList)
}

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

Expand Down
0