Skip to content

chore: Rewrite rbac rego -> SQL clause #5138

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 23 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
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
Next Next commit
Cleanup and comments
  • Loading branch information
Emyrk committed Nov 21, 2022
commit 203bc7cebc3e9c5de27a042d9519a4b89ceddfd3
26 changes: 2 additions & 24 deletions coderd/rbac/regosql/aclGroupVar.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
)

var _ sqltypes.VariableMatcher = ACLGroupVar{}

// ACLGroupVar is also the Node type to reduce the number of types that we need
// to export.
var _ sqltypes.Node = ACLGroupVar{}

// ACLGroupVar is a variable matcher that handles group_acl and user_acl.
Expand All @@ -23,11 +20,9 @@ var _ sqltypes.Node = ACLGroupVar{}
//
// This is a custom variable matcher as json objects have arbitrary complexity.
type ACLGroupVar struct {
StructSQL string
StructSQL string
// input.object.group_acl -> ["input", "object", "group_acl"]
StructPath []string
// DenyAll is helpful for when we don't care about ACL groups.
// We need to default to denying access.
DenyAll bool

// FieldReference handles referencing the subfields, which could be
// more variables. We pass one in as the global one might not be correctly
Expand All @@ -45,15 +40,6 @@ func ACLGroupMatcher(fieldRefernce sqltypes.VariableMatcher, structSQL string, s

func (ACLGroupVar) UseAs() sqltypes.Node { return ACLGroupVar{} }

// Disable is a helper to disable the ACL group matching in the SQL generation.
// This is because some tables do not have ACL columns, and in this case we
// do not want to grant access based on columns that do not exist.
// This replaces any clause with "group_acl" or "user_acl" with "false".
func (g *ACLGroupVar) Disable() *ACLGroupVar {
g.DenyAll = true
return g
}

func (g ACLGroupVar) ConvertVariable(rego ast.Ref) (sqltypes.Node, bool) {
// "left" will be a map of group names to actions in rego.
// {
Expand All @@ -65,7 +51,6 @@ func (g ACLGroupVar) ConvertVariable(rego ast.Ref) (sqltypes.Node, bool) {
}

aclGrp := ACLGroupVar{
DenyAll: g.DenyAll,
StructSQL: g.StructSQL,
StructPath: g.StructPath,
FieldReference: g.FieldReference,
Expand Down Expand Up @@ -101,17 +86,10 @@ func (g ACLGroupVar) ConvertVariable(rego ast.Ref) (sqltypes.Node, bool) {
}

func (g ACLGroupVar) SQLString(cfg *sqltypes.SQLGenerator) string {
if g.DenyAll {
return "false"
}
return fmt.Sprintf("%s->%s", g.StructSQL, g.GroupNode.SQLString(cfg))
}

func (g ACLGroupVar) ContainsSQL(cfg *sqltypes.SQLGenerator, other sqltypes.Node) (string, error) {
if g.DenyAll {
return "false", nil
}

switch other.UseAs().(type) {
// Only supports containing other strings.
case sqltypes.AstString:
Expand Down
44 changes: 28 additions & 16 deletions coderd/rbac/regosql/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@ package regosql

import "github.com/coder/coder/coderd/rbac/regosql/sqltypes"

// For the templates table
func organizationOwnerMatcher() sqltypes.VariableMatcher {
return sqltypes.StringVarMatcher("organization_id :: text", []string{"input", "object", "org_owner"})
}

func userOwnerMatcher() sqltypes.VariableMatcher {
return sqltypes.StringVarMatcher("owner_id :: text", []string{"input", "object", "owner"})
}

func groupACLMatcher(m sqltypes.VariableMatcher) sqltypes.VariableMatcher {
return ACLGroupMatcher(m, "group_acl", []string{"input", "object", "acl_group_list"})
}

func userACLMatcher(m sqltypes.VariableMatcher) sqltypes.VariableMatcher {
return ACLGroupMatcher(m, "user_acl", []string{"input", "object", "acl_user_list"})
}

func TemplateConverter() *sqltypes.VariableConverter {
matcher := sqltypes.NewVariableConverter().RegisterMatcher(
// Basic strings
sqltypes.StringVarMatcher("organization_id :: text", []string{"input", "object", "org_owner"}),
organizationOwnerMatcher(),
// Templates have no user owner, only owner by an organization.
sqltypes.AlwaysFalse(sqltypes.StringVarMatcher("owner_id :: text", []string{"input", "object", "owner"})),
sqltypes.AlwaysFalse(userOwnerMatcher()),
)
matcher.RegisterMatcher(
ACLGroupMatcher(matcher, "group_acl", []string{"input", "object", "acl_group_list"}),
ACLGroupMatcher(matcher, "user_acl", []string{"input", "object", "acl_user_list"}),
groupACLMatcher(matcher),
userACLMatcher(matcher),
)
return matcher
}
Expand All @@ -21,27 +35,25 @@ func TemplateConverter() *sqltypes.VariableConverter {
// group or user ACL columns.
func NoACLConverter() *sqltypes.VariableConverter {
matcher := sqltypes.NewVariableConverter().RegisterMatcher(
// Basic strings
sqltypes.StringVarMatcher("organization_id :: text", []string{"input", "object", "org_owner"}),
sqltypes.StringVarMatcher("owner_id :: text", []string{"input", "object", "owner"}),
organizationOwnerMatcher(),
userOwnerMatcher(),
)
matcher.RegisterMatcher(
sqltypes.AlwaysFalse(ACLGroupMatcher(matcher, "group_acl", []string{"input", "object", "acl_group_list"})),
sqltypes.AlwaysFalse(ACLGroupMatcher(matcher, "user_acl", []string{"input", "object", "acl_user_list"})),
sqltypes.AlwaysFalse(groupACLMatcher(matcher)),
sqltypes.AlwaysFalse(userACLMatcher(matcher)),
)

return matcher
}

func DefaultVariableConverter() *sqltypes.VariableConverter {
matcher := sqltypes.NewVariableConverter().RegisterMatcher(
// Basic strings
sqltypes.StringVarMatcher("organization_id :: text", []string{"input", "object", "org_owner"}),
sqltypes.StringVarMatcher("owner_id :: text", []string{"input", "object", "owner"}),
organizationOwnerMatcher(),
userOwnerMatcher(),
)
matcher.RegisterMatcher(
ACLGroupMatcher(matcher, "group_acl", []string{"input", "object", "acl_group_list"}),
ACLGroupMatcher(matcher, "user_acl", []string{"input", "object", "acl_user_list"}),
groupACLMatcher(matcher),
userACLMatcher(matcher),
)

return matcher
Expand Down
5 changes: 3 additions & 2 deletions coderd/rbac/regosql/sqltypes/alwaysFalse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ type alwaysFalse struct {
InnerNode Node
}

func AlwaysFalse(m VariableMatcher) alwaysFalse {
// AlwaysFalse overrides the inner node with a constant "false".
func AlwaysFalse(m VariableMatcher) VariableMatcher {
return alwaysFalse{
Matcher: m,
}
}

// AlwaysFalseNode is mainly used for unit testing to make a Node immediately.
func AlwaysFalseNode(n Node) alwaysFalse {
func AlwaysFalseNode(n Node) Node {
return alwaysFalse{
InnerNode: n,
Matcher: nil,
Expand Down
3 changes: 0 additions & 3 deletions coderd/rbac/regosql/sqltypes/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ func Array(source RegoSource, nodes ...Node) (Node, error) {
func (ASTArray) UseAs() Node { return ASTArray{} }

func (a ASTArray) ContainsSQL(cfg *SQLGenerator, needle Node) (string, error) {
// TODO: Handle ASTArray.Contains(ASTArray). Must handle types correctly.
// Should implement as strict subset.

// If we have no elements in our set, then our needle is never in the set.
if len(a.Value) == 0 {
return "false", nil
Expand Down
7 changes: 1 addition & 6 deletions coderd/rbac/regosql/sqltypes/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ type SupportsContainedIn interface {

var _ BooleanNode = memberOf{}
var _ Node = memberOf{}

//var _ SupportsMemberOf = memberOf{}
var _ SupportsEquality = memberOf{}

type memberOf struct {
Needle Node
Haystack Node

// Not just inverses the result of the comparison. We could implement this
// as a Not node wrapping the equality, but this is more efficient.
Not bool
}

func MemberOf(needle, haystack Node) BooleanNode {
Expand Down