Skip to content

Commit a8a63d4

Browse files
committed
Update generator
1 parent cdd7b9f commit a8a63d4

File tree

7 files changed

+1686
-202
lines changed

7 files changed

+1686
-202
lines changed

coderd/database/spice/policy/playground/export.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,28 @@ import (
77
"github.com/coder/coder/v2/coderd/database/spice/policy"
88
)
99

10+
type AssertStruct struct {
11+
True []string `yaml:"assertTrue"`
12+
False []string `yaml:"assertFalse"`
13+
}
14+
1015
type PlaygroundYAML struct {
11-
Schema string `yaml:"schema"`
12-
Relationships string `yaml:"relationships"`
13-
Assertions struct {
14-
True []string `yaml:"assertTrue"`
15-
False []string `yaml:"assertFalse"`
16-
} `yaml:"assertions"`
17-
Validation map[string][]string `yaml:"validation"`
16+
Schema string `yaml:"schema"`
17+
Relationships string `yaml:"relationships"`
18+
Assertions AssertStruct `yaml:"assertions"`
19+
Validation map[string][]string `yaml:"validation"`
1820
}
1921

2022
func PlaygroundExport() string {
2123
relationships.GenerateRelationships()
2224
y := PlaygroundYAML{
2325
Schema: policy.Schema,
2426
Relationships: relationships.AllRelationsToStrings(),
27+
Assertions: AssertStruct{
28+
True: relationships.AllAssertTrue(),
29+
False: relationships.AllAssertFalse(),
30+
},
31+
Validation: relationships.AllValidations(),
2532
}
2633
out, err := yaml.Marshal(y)
2734
if err != nil {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
go run ./generate/main.go > objects_tmp.go && mv objects_tmp.go objects.go

coderd/database/spice/policy/playground/relationships/generate/main.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func Generate() string {
3737

3838
tpl := template.New("zanzobjects").Funcs(template.FuncMap{
3939
"capitalize": capitalize,
40+
"unique": uniquePermissions,
4041
})
4142

4243
tpl, err = tpl.Parse(templateText)
@@ -45,6 +46,8 @@ func Generate() string {
4546
}
4647

4748
var output strings.Builder
49+
output.WriteString(`// Code generated. DO NOT EDIT.`)
50+
output.WriteString("\n")
4851
output.WriteString(`package relationships`)
4952
output.WriteString("\n")
5053
output.WriteString(`import v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"`)
@@ -62,6 +65,7 @@ func Generate() string {
6265

6366
formatted, err := format.Source([]byte(output.String()))
6467
if err != nil {
68+
fmt.Println(output.String())
6569
panic(err)
6670
}
6771
return string(formatted)
@@ -71,9 +75,15 @@ type objectDefinition struct {
7175
// The core type
7276
*core.NamespaceDefinition
7377

78+
Permissions []objectPermission
7479
DirectRelations []objectDirectRelation
7580
}
7681

82+
type objectPermission struct {
83+
Permission string
84+
FunctionName string
85+
}
86+
7787
type objectDirectRelation struct {
7888
RelationName string
7989
FunctionName string
@@ -85,14 +95,15 @@ func newDef(obj *core.NamespaceDefinition) objectDefinition {
8595
NamespaceDefinition: obj,
8696
}
8797
rels := make([]objectDirectRelation, 0)
88-
89-
//if obj.Name == "group" {
90-
// fmt.Println("")
91-
//}
98+
perms := make([]objectPermission, 0)
9299

93100
for _, r := range obj.Relation {
94101
if r.UsersetRewrite != nil {
95102
// This is a permission.
103+
perms = append(perms, objectPermission{
104+
Permission: r.Name,
105+
FunctionName: capitalize(r.Name),
106+
})
96107
continue
97108
}
98109

@@ -144,5 +155,20 @@ func newDef(obj *core.NamespaceDefinition) objectDefinition {
144155
rels = append(rels, multipleSubjects...)
145156
}
146157
d.DirectRelations = rels
158+
d.Permissions = perms
147159
return d
148160
}
161+
162+
func uniquePermissions(perms []objectPermission) []objectPermission {
163+
seen := make(map[string]struct{})
164+
out := make([]objectPermission, 0)
165+
for _, perm := range perms {
166+
perm := perm
167+
if _, ok := seen[perm.Permission]; ok {
168+
continue
169+
}
170+
seen[perm.Permission] = struct{}{}
171+
out = append(out, perm)
172+
}
173+
return out
174+
}

coderd/database/spice/policy/playground/relationships/generate/relationships.tmpl

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ func (obj *Obj{{ capitalize $outerName }}) Type() string {
2020
return "{{ .Name }}"
2121
}
2222

23+
func (obj *Obj{{ capitalize $outerName }}) Object() *v1.ObjectReference {
24+
return obj.Obj
25+
}
26+
2327

2428
{{ range $index, $element := .DirectRelations }}
2529
{{ if eq $element.Subject.Object.ObjectId "*" }}
2630
func (obj *Obj{{ capitalize $outerName }}) {{ capitalize $element.RelationName }}Wildcard() *Obj{{ capitalize $outerName }}{
27-
obj.Add(v1.Relationship{
31+
obj.AddRelation(v1.Relationship{
2832
Resource: obj.Obj,
2933
Relation: "{{ $element.RelationName }}",
3034
Subject: &v1.SubjectReference{
@@ -44,7 +48,7 @@ func (obj *Obj{{ capitalize $outerName }}) {{ capitalize $element.RelationName }
4448
func (obj *Obj{{ capitalize $outerName }}) {{ capitalize $element.FunctionName }}(subs ...*Obj{{ capitalize $element.Subject.Object.ObjectType }}) *Obj{{ capitalize $outerName }}{
4549
for i := range subs {
4650
sub := subs[i]
47-
obj.Add(v1.Relationship{
51+
obj.AddRelation(v1.Relationship{
4852
Resource: obj.Obj,
4953
Relation: "{{ $element.RelationName }}",
5054
Subject: &v1.SubjectReference{
@@ -57,5 +61,52 @@ func (obj *Obj{{ capitalize $outerName }}) {{ capitalize $element.FunctionName }
5761
return obj
5862
}
5963
{{ end }}
64+
{{ end }}
65+
66+
67+
{{ range $index, $element := unique .Permissions }}
68+
func (obj *Obj{{ capitalize $outerName }}) Validate{{ $element.FunctionName }}() *Obj{{ capitalize $outerName }} {
69+
obj.AddValidation(v1.Relationship{
70+
Resource: obj.Obj,
71+
Relation: "{{ $element.Permission }}",
72+
OptionalCaveat: nil,
73+
})
74+
return obj
75+
}
76+
{{ end }}
77+
78+
79+
{{ range $index, $element := .Permissions }}
80+
func (obj *Obj{{ capitalize $outerName }}) Can{{ capitalize $element.FunctionName }}By(subs ...ObjectWithRelationships) *Obj{{ capitalize $outerName }}{
81+
for i := range subs {
82+
sub := subs[i]
83+
obj.AssertTrue(v1.Relationship{
84+
Resource: obj.Obj,
85+
Relation: "{{ $element.Permission }}",
86+
Subject: &v1.SubjectReference{
87+
Object: sub.Object(),
88+
OptionalRelation: "",
89+
},
90+
OptionalCaveat: nil,
91+
})
92+
}
93+
return obj
94+
}
95+
96+
func (obj *Obj{{ capitalize $outerName }}) Cannot{{ capitalize $element.FunctionName }}By(subs ...ObjectWithRelationships) *Obj{{ capitalize $outerName }}{
97+
for i := range subs {
98+
sub := subs[i]
99+
obj.AssertFalse(v1.Relationship{
100+
Resource: obj.Obj,
101+
Relation: "{{ $element.Permission }}",
102+
Subject: &v1.SubjectReference{
103+
Object: sub.Object(),
104+
OptionalRelation: "",
105+
},
106+
OptionalCaveat: nil,
107+
})
108+
}
109+
return obj
110+
}
60111

61112
{{ end }}

coderd/database/spice/policy/playground/relationships/manualobjects.go

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,116 @@ import (
55
"sort"
66
"strings"
77

8+
core "github.com/authzed/spicedb/pkg/proto/core/v1"
9+
810
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
911
"github.com/authzed/spicedb/pkg/tuple"
1012
)
1113

1214
func NewRelationships() *Relationships {
1315
return &Relationships{
14-
Relations: []v1.Relationship{},
16+
Relations: []v1.Relationship{},
17+
True: []v1.Relationship{},
18+
False: []v1.Relationship{},
19+
Validations: []v1.Relationship{},
1520
}
1621
}
1722

1823
type Relationships struct {
19-
Relations []v1.Relationship
24+
Relations []v1.Relationship
25+
True []v1.Relationship
26+
False []v1.Relationship
27+
Validations []v1.Relationship
28+
}
29+
30+
func (r *Relationships) AddValidation(relationship v1.Relationship) {
31+
r.Validations = append(r.Validations, relationship)
2032
}
2133

34+
// AddRelation adds the graph relation for the playground.
2235
func (r *Relationships) AddRelation(relationship v1.Relationship) {
2336
r.Relations = append(r.Relations, relationship)
2437
}
2538

26-
//func (r *Relationships)
39+
func (r *Relationships) AssertTrue(relationship v1.Relationship) {
40+
r.True = append(r.True, relationship)
41+
}
42+
43+
func (r *Relationships) AssertFalse(relationship v1.Relationship) {
44+
r.False = append(r.False, relationship)
45+
}
2746

2847
func (r Relationships) AllRelations() []v1.Relationship {
2948
return r.Relations
3049
}
3150

51+
func (r Relationships) AllFalse() []v1.Relationship {
52+
return r.False
53+
}
54+
55+
func (r Relationships) AllTrue() []v1.Relationship {
56+
return r.True
57+
}
58+
59+
func (r Relationships) AllValidations() []v1.Relationship {
60+
return r.Validations
61+
}
62+
3263
type ObjectWithRelationships interface {
3364
AllRelations() []v1.Relationship
65+
AllTrue() []v1.Relationship
66+
AllFalse() []v1.Relationship
67+
AllValidations() []v1.Relationship
3468
Type() string
69+
Object() *v1.ObjectReference
3570
}
3671

3772
var allObjects []ObjectWithRelationships
3873

74+
func AllAssertTrue() []string {
75+
all := make([]string, 0)
76+
for _, o := range allObjects {
77+
for _, t := range o.AllTrue() {
78+
rStr, err := tuple.StringRelationship(&t)
79+
if err != nil {
80+
panic(err)
81+
}
82+
all = append(all, rStr)
83+
}
84+
}
85+
return all
86+
}
87+
88+
func AllValidations() map[string][]string {
89+
all := make(map[string][]string, 0)
90+
for _, o := range allObjects {
91+
for _, t := range o.AllValidations() {
92+
rStr := tuple.StringONR(&core.ObjectAndRelation{
93+
Namespace: t.Resource.ObjectType,
94+
ObjectId: t.Resource.ObjectId,
95+
Relation: t.Relation,
96+
})
97+
98+
all[rStr] = []string{}
99+
}
100+
}
101+
return all
102+
}
103+
104+
func AllAssertFalse() []string {
105+
all := make([]string, 0)
106+
for _, o := range allObjects {
107+
for _, t := range o.AllFalse() {
108+
rStr, err := tuple.StringRelationship(&t)
109+
if err != nil {
110+
panic(err)
111+
}
112+
all = append(all, rStr)
113+
}
114+
}
115+
return all
116+
}
117+
39118
func AllRelationsToStrings() string {
40119
// group all the objects
41120
buckets := make(map[string][]ObjectWithRelationships)

0 commit comments

Comments
 (0)