/
trackingsubjectset.go
185 lines (160 loc) · 6.02 KB
/
trackingsubjectset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package membership
import (
"fmt"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/tuple"
)
func isWildcard(subject *core.ObjectAndRelation) bool {
return subject.ObjectId == tuple.PublicWildcard
}
// TrackingSubjectSet defines a set that tracks accessible subjects and their associated
// relationships.
//
// NOTE: This is designed solely for the developer API and testing and should *not* be used in any
// performance sensitive code.
//
// NOTE: Unlike a traditional set, unions between wildcards and a concrete subject will result
// in *both* being present in the set, to maintain the proper relationship tracking and reporting
// of concrete subjects.
//
// TODO(jschorr): Once we have stable generics support, break into a standard SubjectSet and
// a tracking variant built on top of it.
type TrackingSubjectSet map[string]FoundSubject
// NewTrackingSubjectSet creates a new TrackingSubjectSet, with optional initial subjects.
func NewTrackingSubjectSet(subjects ...FoundSubject) TrackingSubjectSet {
var toReturn TrackingSubjectSet = make(map[string]FoundSubject)
toReturn.Add(subjects...)
return toReturn
}
// AddFrom adds the subjects found in the other set to this set.
func (tss TrackingSubjectSet) AddFrom(otherSet TrackingSubjectSet) {
for _, value := range otherSet {
tss.Add(value)
}
}
// RemoveFrom removes any subjects found in the other set from this set.
func (tss TrackingSubjectSet) RemoveFrom(otherSet TrackingSubjectSet) {
for _, otherSAR := range otherSet {
tss.Remove(otherSAR.subject)
}
}
// Add adds the given subjects to this set.
func (tss TrackingSubjectSet) Add(subjectsAndResources ...FoundSubject) {
tss.AddWithResources(subjectsAndResources, nil)
}
// AddWithResources adds the given subjects to this set, with the additional resources appended
// for each subject to be included in their relationships.
func (tss TrackingSubjectSet) AddWithResources(subjectsAndResources []FoundSubject, additionalResources *tuple.ONRSet) {
for _, sar := range subjectsAndResources {
found, ok := tss[toKey(sar.subject)]
if ok {
tss[toKey(sar.subject)] = found.union(sar)
} else {
tss[toKey(sar.subject)] = sar
}
}
}
// Get returns the found subject in the set, if any.
func (tss TrackingSubjectSet) Get(subject *core.ObjectAndRelation) (FoundSubject, bool) {
found, ok := tss[toKey(subject)]
return found, ok
}
// Contains returns true if the set contains the given subject.
func (tss TrackingSubjectSet) Contains(subject *core.ObjectAndRelation) bool {
_, ok := tss[toKey(subject)]
return ok
}
// removeExact removes the given subject(s) from the set. If the subject is a wildcard, only
// the exact matching wildcard will be removed.
func (tss TrackingSubjectSet) removeExact(subjects ...*core.ObjectAndRelation) {
for _, subject := range subjects {
delete(tss, toKey(subject))
}
}
// Remove removes the given subject(s) from the set. If the subject is a wildcard, all matching
// subjects are removed. If the subject matches a wildcard in the existing set, then it is added
// to that wildcard as an exclusion.
func (tss TrackingSubjectSet) Remove(subjects ...*core.ObjectAndRelation) {
for _, subject := range subjects {
delete(tss, toKey(subject))
// Delete any entries matching the wildcard, if applicable.
if isWildcard(subject) {
// Remove any subjects matching the type.
for key := range tss {
current := fromKey(key)
if current.Namespace == subject.Namespace {
delete(tss, key)
}
}
} else {
// Check for any wildcards matching and, if found, add to the exclusion.
for _, existing := range tss {
wildcardType, ok := existing.WildcardType()
if ok && wildcardType == subject.Namespace {
existing.excludedSubjects.Add(subject)
}
}
}
}
}
// WithType returns any subjects in the set with the given object type.
func (tss TrackingSubjectSet) WithType(objectType string) []FoundSubject {
toReturn := make([]FoundSubject, 0, len(tss))
for _, current := range tss {
if current.subject.Namespace == objectType {
toReturn = append(toReturn, current)
}
}
return toReturn
}
// Exclude returns a new set that contains the items in this set minus those in the other set.
func (tss TrackingSubjectSet) Exclude(otherSet TrackingSubjectSet) TrackingSubjectSet {
newSet := NewTrackingSubjectSet()
newSet.AddFrom(tss)
newSet.RemoveFrom(otherSet)
return newSet
}
// Intersect returns a new set that contains the items in this set *and* the other set. Note that
// if wildcard is found in *both* sets, it will be returned *along* with any concrete subjects found
// on the other side of the intersection.
func (tss TrackingSubjectSet) Intersect(otherSet TrackingSubjectSet) TrackingSubjectSet {
newSet := NewTrackingSubjectSet()
for _, current := range tss {
// Add directly if shared by both.
other, ok := otherSet.Get(current.subject)
if ok {
newSet.Add(current.intersect(other))
}
// If the current is a wildcard, and add any matching.
if isWildcard(current.subject) {
newSet.AddWithResources(otherSet.WithType(current.subject.Namespace), current.relationships)
}
}
for _, current := range otherSet {
// If the current is a wildcard, add any matching.
if isWildcard(current.subject) {
newSet.AddWithResources(tss.WithType(current.subject.Namespace), current.relationships)
}
}
return newSet
}
// ToSlice returns a slice of all subjects found in the set.
func (tss TrackingSubjectSet) ToSlice() []FoundSubject {
toReturn := make([]FoundSubject, 0, len(tss))
for _, current := range tss {
toReturn = append(toReturn, current)
}
return toReturn
}
// ToFoundSubjects returns the set as a FoundSubjects struct.
func (tss TrackingSubjectSet) ToFoundSubjects() FoundSubjects {
return FoundSubjects{tss}
}
func toKey(subject *core.ObjectAndRelation) string {
return fmt.Sprintf("%s %s %s", subject.Namespace, subject.ObjectId, subject.Relation)
}
func fromKey(key string) *core.ObjectAndRelation {
subject := &core.ObjectAndRelation{}
fmt.Sscanf(key, "%s %s %s", &subject.Namespace, &subject.ObjectId, &subject.Relation)
return subject
}