chore: Optimize parial rego execution byte allocations (#6144)

* chore: Implement benchmark for authorizer.Prepare

Identify time + alloc cost before optimizing
This commit is contained in:
Steven Masley
2023-02-10 08:39:45 -06:00
committed by GitHub
parent ab9cba9396
commit 32fbd10a1f
5 changed files with 51 additions and 25 deletions
+3 -15
View File
@@ -127,7 +127,7 @@ EachQueryLoop:
pa.subjectInput, pa.subjectAction, pa.subjectResourceType, nil)
}
func newPartialAuthorizer(ctx context.Context, subject Subject, action Action, objectType string) (*PartialAuthorizer, error) {
func (a RegoAuthorizer) newPartialAuthorizer(ctx context.Context, subject Subject, action Action, objectType string) (*PartialAuthorizer, error) {
if subject.Roles == nil {
return nil, xerrors.Errorf("subject must have roles")
}
@@ -140,20 +140,8 @@ func newPartialAuthorizer(ctx context.Context, subject Subject, action Action, o
return nil, xerrors.Errorf("prepare input: %w", err)
}
// 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.ParsedInput(input),
).Partial(ctx)
partialQueries, err := a.partialQuery.Partial(ctx, rego.EvalParsedInput(input))
if err != nil {
return nil, xerrors.Errorf("prepare: %w", err)
}