Skip to content

chore: Optimize parial rego execution byte allocations #6144

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 9 commits into from
Feb 10, 2023
Merged
Prev Previous commit
Next Next commit
Pre-parse all rego inputs to partial execution
  • Loading branch information
Emyrk committed Feb 9, 2023
commit 891ecc50b84d3b146e117dffef309a305ab97ed0
29 changes: 20 additions & 9 deletions coderd/rbac/partial.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ EachQueryLoop:
pa.subjectInput, pa.subjectAction, pa.subjectResourceType, nil)
}

// Precompiled values to be reused for each Prepare call.
// These values are static and do not change.
var (
// unknownTerms are the unknown values in the rego input.
// These values are pre-parsed to prevent reparsing on every Prepare call.
unknownTerms = []*ast.Term{
ast.MustParseTerm("input.object.id"),
ast.MustParseTerm("input.object.owner"),
ast.MustParseTerm("input.object.org_owner"),
ast.MustParseTerm("input.object.acl_user_list"),
ast.MustParseTerm("input.object.acl_group_list"),
}

partialQuery = ast.MustParseBody("data.authz.allow = true")
policyModule = ast.MustParseModule(policy)
)

func newPartialAuthorizer(ctx context.Context, subject Subject, action Action, objectType string) (*PartialAuthorizer, error) {
if subject.Roles == nil {
return nil, xerrors.Errorf("subject must have roles")
Expand All @@ -143,15 +160,9 @@ func newPartialAuthorizer(ctx context.Context, subject Subject, action Action, o
// Run the rego policy with a few unknown fields. This should simplify our
// policy to a set of queries.
partialQueries, err := rego.New(
rego.Query("data.authz.allow = true"),
rego.Module("policy.rego", policy),
rego.Unknowns([]string{
"input.object.id",
"input.object.owner",
"input.object.org_owner",
"input.object.acl_user_list",
"input.object.acl_group_list",
}),
rego.ParsedQuery(partialQuery),
rego.ParsedModule(policyModule),
rego.ParsedUnknowns(unknownTerms),
rego.ParsedInput(input),
).Partial(ctx)
if err != nil {
Expand Down