Skip to content

chore: Update rego to be partial execution friendly #3449

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 30 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
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
Next Next commit
Cleanup
  • Loading branch information
Emyrk committed Aug 10, 2022
commit c44d4d112a042120fa0d7df467f77c97ab580949
8 changes: 3 additions & 5 deletions coderd/rbac/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"encoding/json"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"

"github.com/coder/coder/coderd/rbac"
"github.com/coder/coder/cryptorand"

"github.com/coder/coder/testutil"

"github.com/coder/coder/coderd/rbac"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)

// subject is required because rego needs
Expand Down
25 changes: 16 additions & 9 deletions coderd/rbac/partial.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ func newPartialAuthorizer(ctx context.Context, subjectID string, roles []Role, a
return nil, xerrors.Errorf("prepare: %w", err)
}

alwaysTrue := false
pAuth := &PartialAuthorizer{
PartialQueries: partialQueries,
PreparedQueries: []rego.PreparedEvalQuery{},
Input: input,
}

preparedQueries := make([]rego.PreparedEvalQuery, 0, len(partialQueries.Queries))
for _, q := range partialQueries.Queries {
if q.String() == "" {
alwaysTrue = true
continue
// No more work needed, this will always be true,
pAuth.AlwaysTrue = true
preparedQueries = []rego.PreparedEvalQuery{}
break
}
results, err := rego.New(
rego.ParsedQuery(q),
Expand All @@ -56,13 +63,9 @@ func newPartialAuthorizer(ctx context.Context, subjectID string, roles []Role, a
}
preparedQueries = append(preparedQueries, results)
}
pAuth.PreparedQueries = preparedQueries

return &PartialAuthorizer{
PartialQueries: partialQueries,
PreparedQueries: preparedQueries,
Input: input,
AlwaysTrue: alwaysTrue,
}, nil
return pAuth, nil
}

// Authorize authorizes a single object using teh partially prepared queries.
Expand All @@ -73,13 +76,17 @@ func (a PartialAuthorizer) Authorize(ctx context.Context, object Object) error {

EachQueryLoop:
for _, q := range a.PreparedQueries {
// We need to eval each query with the newly known fields.
results, err := q.Eval(ctx, rego.EvalInput(map[string]interface{}{
"object": object,
}))
if err != nil {
continue EachQueryLoop
}

// The below code is intended to fail safe. We only support queries that
// return simple results.

// 0 results means the query is false.
if len(results) == 0 {
continue EachQueryLoop
Expand Down