From 99f2a00d3b145b250028595916bf7ec363d088d5 Mon Sep 17 00:00:00 2001 From: Gert Drapers Date: Fri, 3 Nov 2023 17:12:23 -0700 Subject: [PATCH] PathMap prep (#15) --- go.mod | 4 ++-- go.sum | 8 ++++---- model/model.go | 26 ++++++++++++++++++++++++++ v3/manifest.go | 38 +++++++++++++++++++------------------- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/go.mod b/go.mod index 0152531..d888b44 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.20 require ( github.com/alecthomas/kong v0.8.1 github.com/aserto-dev/errors v0.0.6 - github.com/aserto-dev/go-aserto v0.20.4 - github.com/aserto-dev/go-directory v0.30.0 + github.com/aserto-dev/go-aserto v0.30.0 + github.com/aserto-dev/go-directory v0.30.1 github.com/golang/mock v1.1.1 github.com/hashicorp/go-multierror v1.1.1 github.com/magefile/mage v1.15.0 diff --git a/go.sum b/go.sum index 3af91f9..9100a63 100644 --- a/go.sum +++ b/go.sum @@ -8,10 +8,10 @@ github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/aserto-dev/errors v0.0.6 h1:iH5fkJwBGFPbcdS4B8mwvNdwODlhDEXXPduZtjLh6vo= github.com/aserto-dev/errors v0.0.6/go.mod h1:kenI1gamsemaR2wS+M2un0kXIJ9exTrmeRT/fCFwlWc= -github.com/aserto-dev/go-aserto v0.20.4 h1:8vpAr5ojLd0mzL8SXdzXMHRk/8G8kOAho5vrpQ2pWr4= -github.com/aserto-dev/go-aserto v0.20.4/go.mod h1:hZAYXwqs+w4JdrggWzV7SdDK47MqHYi5LtjoWcB78vg= -github.com/aserto-dev/go-directory v0.30.0 h1:mKQhoP7rl9XjxP8X0H6RvjvgjqeRh7WqbWugg/nsCCM= -github.com/aserto-dev/go-directory v0.30.0/go.mod h1:xkZFWZ0u0RcYRBkJLTqantIG/jnVxYhzd2+kIQf8PnU= +github.com/aserto-dev/go-aserto v0.30.0 h1:RZMM8ojXp4O15+qPKsFjQ+6/+KV3pXAwqCJrQjJRD5I= +github.com/aserto-dev/go-aserto v0.30.0/go.mod h1:y+Mrqm9yl3XgBA0lkZ4Pj2cZN1ytL1pjAwpG9rKQCJA= +github.com/aserto-dev/go-directory v0.30.1 h1:2sPtNBntul3tpWxVy5N8jOEqpyf1NGi1Oo9lgrlSees= +github.com/aserto-dev/go-directory v0.30.1/go.mod h1:Gt0RiHgws/m9KJMiCaDXXDYZYoAHFQsd6Vf3KOPWO34= github.com/aserto-dev/header v0.0.4 h1:Bb58n1m2X/qRV3N2PS0avuFOyRcG8Iri7/qq0Wsunq8= github.com/aserto-dev/header v0.0.4/go.mod h1:LNgRo3gStS71lO1d9Oue9e9kfVvirvUHvdZClzDmNwo= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= diff --git a/model/model.go b/model/model.go index c1d612d..2504285 100644 --- a/model/model.go +++ b/model/model.go @@ -3,6 +3,7 @@ package model import ( "bytes" "encoding/json" + "fmt" "io" "time" @@ -22,6 +23,22 @@ type ObjectName Identifier type RelationName Identifier type PermissionName Identifier +func (on ObjectName) String() string { + return string(on) +} + +func (rn RelationName) String() string { + return string(rn) +} + +func (pn PermissionName) String() string { + return string(pn) +} + +func (pn PermissionName) RN() RelationName { + return RelationName(pn) +} + type Object struct { Relations map[RelationName][]*Relation `json:"relations,omitempty"` Permissions map[PermissionName]*Permission `json:"permissions,omitempty"` @@ -33,6 +50,15 @@ type Relation struct { Wildcard ObjectName `json:"wildcard,omitempty"` } +type ObjectRelation struct { + Object ObjectName `json:"object"` + Relation RelationName `json:"relation,omitempty"` +} + +func (or ObjectRelation) String() string { + return fmt.Sprintf("%s:%s", or.Object, or.Relation) +} + type SubjectRelation struct { Object ObjectName `json:"object,omitempty"` Relation RelationName `json:"relation,omitempty"` diff --git a/v3/manifest.go b/v3/manifest.go index 6bc9bca..c50996c 100644 --- a/v3/manifest.go +++ b/v3/manifest.go @@ -11,12 +11,12 @@ import ( const SupportedSchemaVersion int = 3 const ( - unionIdentifier string = "|" - intersectionIdentifier string = "&" - exclusionIdentifier string = " - " - relationIdentifier string = "#" - wildcardIdentifier string = ":*" - arrowIdentifier string = "->" + UnionIdentifier string = "|" + IntersectionIdentifier string = "&" + ExclusionIdentifier string = " - " + RelationIdentifier string = "#" + WildcardIdentifier string = ":*" + ArrowIdentifier string = "->" ) type Manifest struct { @@ -114,19 +114,19 @@ func (v *SchemaVersion) UnmarshalYAML(value *yaml.Node) error { } func (r *RelationDefinition) UnmarshalYAML(value *yaml.Node) error { - s := strings.Split(value.Value, unionIdentifier) + s := strings.Split(value.Value, UnionIdentifier) for _, v := range s { switch { // subject relation - case strings.Contains(v, relationIdentifier): - sr := strings.Split(v, relationIdentifier) + case strings.Contains(v, RelationIdentifier): + sr := strings.Split(v, RelationIdentifier) r.Definition = append(r.Definition, &SubjectRelation{ ObjectType: strings.TrimSpace(sr[0]), Relation: strings.TrimSpace(sr[1]), }) // wildcard relation - case strings.Contains(v, wildcardIdentifier): - wc := strings.Split(v, wildcardIdentifier) + case strings.Contains(v, WildcardIdentifier): + wc := strings.Split(v, WildcardIdentifier) r.Definition = append(r.Definition, &WildcardRelation{ ObjectType: strings.TrimSpace(wc[0]), }) @@ -144,8 +144,8 @@ func (r *RelationDefinition) UnmarshalYAML(value *yaml.Node) error { func (p *PermissionOperator) UnmarshalYAML(value *yaml.Node) error { switch { // union (OR) - case strings.Contains(value.Value, unionIdentifier): - s := strings.Split(value.Value, unionIdentifier) + case strings.Contains(value.Value, UnionIdentifier): + s := strings.Split(value.Value, UnionIdentifier) union := []string{} for _, v := range s { union = append(union, strings.TrimSpace(v)) @@ -156,8 +156,8 @@ func (p *PermissionOperator) UnmarshalYAML(value *yaml.Node) error { }, } // intersection (AND) - case strings.Contains(value.Value, intersectionIdentifier): - s := strings.Split(value.Value, intersectionIdentifier) + case strings.Contains(value.Value, IntersectionIdentifier): + s := strings.Split(value.Value, IntersectionIdentifier) intersect := []string{} for _, v := range s { intersect = append(intersect, strings.TrimSpace(v)) @@ -168,8 +168,8 @@ func (p *PermissionOperator) UnmarshalYAML(value *yaml.Node) error { }, } // arrow - case strings.Contains(value.Value, arrowIdentifier): - s := strings.Split(value.Value, arrowIdentifier) + case strings.Contains(value.Value, ArrowIdentifier): + s := strings.Split(value.Value, ArrowIdentifier) *p = PermissionOperator{ Operator: &ArrowOperator{ Relation: strings.TrimSpace(s[0]), @@ -177,8 +177,8 @@ func (p *PermissionOperator) UnmarshalYAML(value *yaml.Node) error { }, } // exclusion (NOT) - case strings.Contains(value.Value, exclusionIdentifier): - s := strings.Split(value.Value, exclusionIdentifier) + case strings.Contains(value.Value, ExclusionIdentifier): + s := strings.Split(value.Value, ExclusionIdentifier) *p = PermissionOperator{ Operator: &ExclusionOperator{ Base: strings.TrimSpace(s[0]),