-
Notifications
You must be signed in to change notification settings - Fork 142
/
service_policy_store.go
365 lines (306 loc) · 12.9 KB
/
service_policy_store.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package db
import (
"fmt"
"github.com/openziti/foundation/v2/errorz"
"github.com/openziti/foundation/v2/stringz"
"github.com/openziti/storage/ast"
"github.com/openziti/storage/boltz"
"github.com/openziti/ziti/common/pb/edge_ctrl_pb"
"sort"
)
type PolicyType string
func (self PolicyType) String() string {
return string(self)
}
func (self PolicyType) Id() int32 {
if self == PolicyTypeDial {
return 1
}
if self == PolicyTypeBind {
return 2
}
return 0
}
func (self PolicyType) IsDial() bool {
return self == PolicyTypeDial
}
func (self PolicyType) IsBind() bool {
return self == PolicyTypeBind
}
func GetPolicyTypeForId(policyTypeId int32) PolicyType {
policyType := PolicyTypeInvalid
if policyTypeId == PolicyTypeDial.Id() {
policyType = PolicyTypeDial
} else if policyTypeId == PolicyTypeBind.Id() {
policyType = PolicyTypeBind
}
return policyType
}
const (
FieldServicePolicyType = "type"
PolicyTypeInvalidName = "Invalid"
PolicyTypeDialName = "Dial"
PolicyTypeBindName = "Bind"
PolicyTypeInvalid PolicyType = PolicyTypeInvalidName
PolicyTypeDial PolicyType = PolicyTypeDialName
PolicyTypeBind PolicyType = PolicyTypeBindName
)
type ServicePolicy struct {
boltz.BaseExtEntity
PolicyType PolicyType `json:"policyType"`
Name string `json:"name"`
Semantic string `json:"semantic"`
IdentityRoles []string `json:"identityRoles"`
ServiceRoles []string `json:"serviceRoles"`
PostureCheckRoles []string `json:"postureCheckRoles"`
}
func (entity *ServicePolicy) GetName() string {
return entity.Name
}
func (entity *ServicePolicy) GetSemantic() string {
return entity.Semantic
}
func (entity *ServicePolicy) GetEntityType() string {
return EntityTypeServicePolicies
}
type ServicePolicyChangeEventListener func(event *edge_ctrl_pb.DataState_ServicePolicyChange)
var _ ServicePolicyStore = (*servicePolicyStoreImpl)(nil)
type ServicePolicyStore interface {
NameIndexed
Store[*ServicePolicy]
}
func newServicePolicyStore(stores *stores) *servicePolicyStoreImpl {
store := &servicePolicyStoreImpl{}
store.baseStore = newBaseStore[*ServicePolicy](stores, store)
store.InitImpl(store)
return store
}
type servicePolicyStoreImpl struct {
*baseStore[*ServicePolicy]
indexName boltz.ReadIndex
symbolPolicyType boltz.EntitySymbol
symbolSemantic boltz.EntitySymbol
symbolIdentityRoles boltz.EntitySetSymbol
symbolServiceRoles boltz.EntitySetSymbol
symbolPostureCheckRoles boltz.EntitySetSymbol
symbolIdentities boltz.EntitySetSymbol
symbolServices boltz.EntitySetSymbol
symbolPostureChecks boltz.EntitySetSymbol
identityCollection boltz.LinkCollection
serviceCollection boltz.LinkCollection
postureCheckCollection boltz.LinkCollection
}
func (store *servicePolicyStoreImpl) GetNameIndex() boltz.ReadIndex {
return store.indexName
}
func (store *servicePolicyStoreImpl) NewEntity() *ServicePolicy {
return &ServicePolicy{}
}
func (store *servicePolicyStoreImpl) initializeLocal() {
store.AddExtEntitySymbols()
store.indexName = store.addUniqueNameField()
store.symbolPolicyType = store.AddSymbol(FieldServicePolicyType, ast.NodeTypeInt64)
store.symbolSemantic = store.AddSymbol(FieldSemantic, ast.NodeTypeString)
store.symbolIdentityRoles = store.AddPublicSetSymbol(FieldIdentityRoles, ast.NodeTypeString)
store.symbolServiceRoles = store.AddPublicSetSymbol(FieldServiceRoles, ast.NodeTypeString)
store.symbolPostureCheckRoles = store.AddPublicSetSymbol(FieldPostureCheckRoles, ast.NodeTypeString)
store.symbolIdentities = store.AddFkSetSymbol(EntityTypeIdentities, store.stores.identity)
store.symbolServices = store.AddFkSetSymbol(EntityTypeServices, store.stores.edgeService)
store.symbolPostureChecks = store.AddFkSetSymbol(EntityTypePostureChecks, store.stores.postureCheck)
}
func (store *servicePolicyStoreImpl) initializeLinked() {
store.serviceCollection = store.AddLinkCollection(store.symbolServices, store.stores.edgeService.symbolServicePolicies)
store.identityCollection = store.AddLinkCollection(store.symbolIdentities, store.stores.identity.symbolServicePolicies)
store.postureCheckCollection = store.AddLinkCollection(store.symbolPostureChecks, store.stores.postureCheck.symbolServicePolicies)
}
func (store *servicePolicyStoreImpl) FillEntity(entity *ServicePolicy, bucket *boltz.TypedBucket) {
entity.LoadBaseValues(bucket)
entity.Name = bucket.GetStringOrError(FieldName)
entity.PolicyType = GetPolicyTypeForId(bucket.GetInt32WithDefault(FieldServicePolicyType, PolicyTypeDial.Id()))
entity.Semantic = bucket.GetStringWithDefault(FieldSemantic, SemanticAllOf)
entity.IdentityRoles = bucket.GetStringList(FieldIdentityRoles)
entity.ServiceRoles = bucket.GetStringList(FieldServiceRoles)
entity.PostureCheckRoles = bucket.GetStringList(FieldPostureCheckRoles)
}
func (store *servicePolicyStoreImpl) PersistEntity(entity *ServicePolicy, ctx *boltz.PersistContext) {
if ctx.ProceedWithSet(FieldServicePolicyType) {
if entity.PolicyType != PolicyTypeBind && entity.PolicyType != PolicyTypeDial {
ctx.Bucket.SetError(errorz.NewFieldError("invalid policy type", FieldServicePolicyType, entity.PolicyType))
return
}
} else {
// PolicyType needs to be correct in the entity as we use it later
// TODO: Add test for this
entity.PolicyType = GetPolicyTypeForId(ctx.Bucket.GetInt32WithDefault(FieldServicePolicyType, PolicyTypeDial.Id()))
}
if err := validateRolesAndIds(FieldIdentityRoles, entity.IdentityRoles); err != nil {
ctx.Bucket.SetError(err)
}
if err := validateRolesAndIds(FieldServiceRoles, entity.ServiceRoles); err != nil {
ctx.Bucket.SetError(err)
}
if err := validateRolesAndIds(FieldPostureCheckRoles, entity.PostureCheckRoles); err != nil {
ctx.Bucket.SetError(err)
}
if ctx.ProceedWithSet(FieldSemantic) && !isSemanticValid(entity.Semantic) {
ctx.Bucket.SetError(errorz.NewFieldError("invalid semantic", FieldSemantic, entity.Semantic))
return
}
entity.SetBaseValues(ctx)
ctx.SetRequiredString(FieldName, entity.Name)
ctx.SetInt32(FieldServicePolicyType, entity.PolicyType.Id())
ctx.SetRequiredString(FieldSemantic, entity.Semantic)
servicePolicyStore := ctx.Store.(*servicePolicyStoreImpl)
sort.Strings(entity.ServiceRoles)
sort.Strings(entity.IdentityRoles)
sort.Strings(entity.PostureCheckRoles)
oldIdentityRoles, valueSet := ctx.GetAndSetStringList(FieldIdentityRoles, entity.IdentityRoles)
if valueSet && !stringz.EqualSlices(oldIdentityRoles, entity.IdentityRoles) {
servicePolicyStore.identityRolesUpdated(ctx, entity)
}
oldServiceRoles, valueSet := ctx.GetAndSetStringList(FieldServiceRoles, entity.ServiceRoles)
if valueSet && !stringz.EqualSlices(oldServiceRoles, entity.ServiceRoles) {
servicePolicyStore.serviceRolesUpdated(ctx, entity)
}
oldPostureCheckRoles, valueSet := ctx.GetAndSetStringList(FieldPostureCheckRoles, entity.PostureCheckRoles)
if valueSet && !stringz.EqualSlices(oldPostureCheckRoles, entity.PostureCheckRoles) {
servicePolicyStore.postureCheckRolesUpdated(ctx, entity)
}
}
/*
Optimizations
1. When changing policies if only ids have changed, only add/remove ids from groups as needed
2. When related entities added/changed, only evaluate policies against that one entity (identity/edge router/service),
and just add/remove/ignore
3. Related entity deletes should be handled automatically by FK Indexes on those entities (need to verify the reverse as well/deleting policy)
*/
func (store *servicePolicyStoreImpl) serviceRolesUpdated(persistCtx *boltz.PersistContext, policy *ServicePolicy) {
ctx := &roleAttributeChangeContext{
mutateCtx: persistCtx.MutateContext,
rolesSymbol: store.symbolServiceRoles,
linkCollection: store.serviceCollection,
relatedLinkCollection: store.identityCollection,
ErrorHolder: persistCtx.Bucket,
}
if policy.PolicyType == PolicyTypeDial {
ctx.denormLinkCollection = store.stores.edgeService.dialIdentitiesCollection
ctx.denormChangeHandler = func(fromId, toId []byte, add bool) {
ctx.addServicePolicyEvent(toId, fromId, PolicyTypeDial, add)
}
} else {
ctx.denormLinkCollection = store.stores.edgeService.bindIdentitiesCollection
ctx.denormChangeHandler = func(fromId, toId []byte, add bool) {
ctx.addServicePolicyEvent(toId, fromId, PolicyTypeBind, add)
}
}
ctx.changeHandler = func(policyId []byte, relatedId []byte, add bool) {
ctx.notifyOfPolicyChangeEvent(policyId, relatedId, edge_ctrl_pb.ServicePolicyRelatedEntityType_RelatedService, add)
}
EvaluatePolicy(ctx, policy, store.stores.edgeService.symbolRoleAttributes)
}
func (store *servicePolicyStoreImpl) identityRolesUpdated(persistCtx *boltz.PersistContext, policy *ServicePolicy) {
ctx := &roleAttributeChangeContext{
mutateCtx: persistCtx.MutateContext,
rolesSymbol: store.symbolIdentityRoles,
linkCollection: store.identityCollection,
relatedLinkCollection: store.serviceCollection,
ErrorHolder: persistCtx.Bucket,
}
if policy.PolicyType == PolicyTypeDial {
ctx.denormLinkCollection = store.stores.identity.dialServicesCollection
ctx.denormChangeHandler = func(fromId, toId []byte, add bool) {
ctx.addServicePolicyEvent(fromId, toId, PolicyTypeDial, add)
}
} else {
ctx.denormLinkCollection = store.stores.identity.bindServicesCollection
ctx.denormChangeHandler = func(fromId, toId []byte, add bool) {
ctx.addServicePolicyEvent(fromId, toId, PolicyTypeBind, add)
}
}
ctx.changeHandler = func(policyId []byte, relatedId []byte, add bool) {
ctx.notifyOfPolicyChangeEvent(policyId, relatedId, edge_ctrl_pb.ServicePolicyRelatedEntityType_RelatedIdentity, add)
}
EvaluatePolicy(ctx, policy, store.stores.identity.symbolRoleAttributes)
}
func (store *servicePolicyStoreImpl) postureCheckRolesUpdated(persistCtx *boltz.PersistContext, policy *ServicePolicy) {
ctx := &roleAttributeChangeContext{
mutateCtx: persistCtx.MutateContext,
rolesSymbol: store.symbolPostureCheckRoles,
linkCollection: store.postureCheckCollection,
relatedLinkCollection: store.serviceCollection,
ErrorHolder: persistCtx.Bucket,
}
ctx.denormChangeHandler = func(fromId, toId []byte, add bool) {
ctx.addServiceUpdatedEvent(store.stores, ctx.tx(), toId)
}
if policy.PolicyType == PolicyTypeDial {
ctx.denormLinkCollection = store.stores.postureCheck.dialServicesCollection
} else {
ctx.denormLinkCollection = store.stores.postureCheck.bindServicesCollection
}
ctx.changeHandler = func(policyId []byte, relatedId []byte, add bool) {
ctx.notifyOfPolicyChangeEvent(policyId, relatedId, edge_ctrl_pb.ServicePolicyRelatedEntityType_RelatedPostureCheck, add)
}
EvaluatePolicy(ctx, policy, store.stores.postureCheck.symbolRoleAttributes)
}
func (store *servicePolicyStoreImpl) DeleteById(ctx boltz.MutateContext, id string) error {
policy, err := store.LoadById(ctx.Tx(), id)
if err != nil {
return err
}
policy.IdentityRoles = nil
policy.ServiceRoles = nil
policy.PostureCheckRoles = nil
err = store.Update(ctx, policy, nil)
if err != nil {
return fmt.Errorf("failure while clearing policy before delete: %w", err)
}
return store.BaseStore.DeleteById(ctx, id)
}
func (store *servicePolicyStoreImpl) CheckIntegrity(mutateCtx boltz.MutateContext, fix bool, errorSink func(err error, fixed bool)) error {
ctx := &denormCheckCtx{
name: "service-policies/bind",
mutateCtx: mutateCtx,
sourceStore: store.stores.identity,
targetStore: store.stores.edgeService,
policyStore: store,
sourceCollection: store.identityCollection,
targetCollection: store.serviceCollection,
targetDenormCollection: store.stores.identity.bindServicesCollection,
errorSink: errorSink,
repair: fix,
policyFilter: func(policyId []byte) bool {
policyType := PolicyTypeInvalid
if result := boltz.FieldToInt32(store.symbolPolicyType.Eval(mutateCtx.Tx(), policyId)); result != nil {
policyType = GetPolicyTypeForId(*result)
}
return policyType == PolicyTypeBind
},
}
if err := validatePolicyDenormalization(ctx); err != nil {
return err
}
ctx = &denormCheckCtx{
name: "service-policies/dial",
mutateCtx: mutateCtx,
sourceStore: store.stores.identity,
targetStore: store.stores.edgeService,
policyStore: store,
sourceCollection: store.identityCollection,
targetCollection: store.serviceCollection,
targetDenormCollection: store.stores.identity.dialServicesCollection,
errorSink: errorSink,
repair: fix,
policyFilter: func(policyId []byte) bool {
policyType := PolicyTypeInvalid
if result := boltz.FieldToInt32(store.symbolPolicyType.Eval(mutateCtx.Tx(), policyId)); result != nil {
policyType = GetPolicyTypeForId(*result)
}
return policyType == PolicyTypeDial
},
}
if err := validatePolicyDenormalization(ctx); err != nil {
return err
}
return store.BaseStore.CheckIntegrity(mutateCtx, fix, errorSink)
}