-
Notifications
You must be signed in to change notification settings - Fork 329
/
rbac_rule_marshal.go
397 lines (340 loc) · 11.6 KB
/
rbac_rule_marshal.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package store
import (
"context"
"fmt"
"strings"
automationTypes "github.com/cortezaproject/corteza/server/automation/types"
composeTypes "github.com/cortezaproject/corteza/server/compose/types"
federationTypes "github.com/cortezaproject/corteza/server/federation/types"
"github.com/cortezaproject/corteza/server/pkg/envoy/resource"
"github.com/cortezaproject/corteza/server/pkg/rbac"
"github.com/cortezaproject/corteza/server/store"
systemTypes "github.com/cortezaproject/corteza/server/system/types"
)
var (
gRbacRules map[string]bool
)
func newRbacRuleFromResource(res *resource.RbacRule, cfg *EncoderConfig) resourceState {
return &rbacRule{
cfg: mergeConfig(cfg, res.Config()),
rule: res.Res,
refRbacResource: res.RefResource,
refRbacRes: res.RefRes,
refPathRes: res.RefPath,
refRole: res.RefRole,
}
}
func (n *rbacRule) Prepare(ctx context.Context, pl *payload) (err error) {
// Init global state
if gRbacRules == nil {
err = n.initGlobalIndex(ctx, pl.s)
if err != nil {
return
}
}
// Related role
n.role, err = findRole(ctx, pl.s, pl.state.ParentResources, n.refRole.Identifiers)
if err != nil {
return err
}
if n.role == nil {
return resource.RoleErrUnresolved(n.refRole.Identifiers)
}
refRes := n.refRbacRes
if refRes == nil {
return
}
// For now we will only allow resource specific RBAC rules if that resource is
// also present.
// Here we check if we can find it in case we're handling a resource specific rule.
found := false
switch refRes.ResourceType {
case composeTypes.RecordResourceType:
if found = n.handleComposeRecord(pl.state.ParentResources); !found {
return resource.RbacResourceErrNotFound(refRes.Identifiers)
}
case composeTypes.ModuleFieldResourceType:
if found = n.handleComposeModuleField(pl.state.ParentResources); !found {
return resource.RbacResourceErrNotFound(refRes.Identifiers)
}
default:
for _, r := range pl.state.ParentResources {
if refRes.ResourceType == r.ResourceType() && r.Identifiers().HasAny(refRes.Identifiers) {
return
}
}
// We couldn't find it...
return resource.RbacResourceErrNotFound(refRes.Identifiers)
}
return
}
func (n *rbacRule) Encode(ctx context.Context, pl *payload) (err error) {
// Assure correct resource
n.rule.Resource, err = n.makeRBACResource(pl)
if err != nil {
return err
}
// Assure correct role
n.rule.RoleID = n.role.ID
if n.rule.RoleID == 0 {
rl := resource.FindRole(pl.state.ParentResources, n.refRole.Identifiers)
n.rule.RoleID = rl.ID
}
if n.rule.RoleID == 0 {
return resource.RoleErrUnresolved(n.refRole.Identifiers)
}
// Upsert
if _, exists := gRbacRules[n.rbacRuleIndex((n.rule))]; !exists {
return store.CreateRbacRule(ctx, pl.s, n.rule)
}
// On existing rbac rule, replace/merge right basically overwrites the existing rule;
// otherwise, the new rule is ignored.
switch n.cfg.OnExisting {
case resource.Replace,
resource.MergeRight:
return store.UpdateRbacRule(ctx, pl.s, n.rule)
}
return nil
}
func (n *rbacRule) rbacRuleIndex(r *rbac.Rule) string {
return fmt.Sprintf("%d_%s_%s", r.RoleID, r.Resource, r.Operation)
}
func (n *rbacRule) initGlobalIndex(ctx context.Context, s store.Storer) error {
gRbacRules = make(map[string]bool)
rr, _, err := store.SearchRbacRules(ctx, s, rbac.RuleFilter{})
if err != store.ErrNotFound && err != nil {
return err
}
for _, r := range rr {
gRbacRules[n.rbacRuleIndex(r)] = true
}
return nil
}
func (n *rbacRule) makeRBACResource(pl *payload) (string, error) {
p0ID := uint64(0)
p1ID := uint64(0)
p2ID := uint64(0)
rt := strings.Split(n.refRbacResource, "/")[0]
// Component level stuff
switch rt {
case composeTypes.ComponentResourceType:
return composeTypes.ComponentRbacResource(), nil
case systemTypes.ComponentResourceType:
return systemTypes.ComponentRbacResource(), nil
case automationTypes.ComponentResourceType:
return automationTypes.ComponentRbacResource(), nil
case federationTypes.ComponentResourceType:
return federationTypes.ComponentRbacResource(), nil
}
// Component resource stuff
switch rt {
case automationTypes.WorkflowResourceType:
if n.refRbacRes != nil {
p1 := resource.FindAutomationWorkflow(pl.state.ParentResources, n.refRbacRes.Identifiers)
if p1 == nil {
return "", resource.AutomationWorkflowErrUnresolved(n.refRbacRes.Identifiers)
}
p1ID = p1.ID
}
return automationTypes.WorkflowRbacResource(p1ID), nil
case composeTypes.NamespaceResourceType:
if n.refRbacRes != nil {
p1 := resource.FindComposeNamespace(pl.state.ParentResources, n.refRbacRes.Identifiers)
if p1 == nil {
return "", resource.ComposeNamespaceErrUnresolved(n.refRbacRes.Identifiers)
}
p1ID = p1.ID
}
return composeTypes.NamespaceRbacResource(p1ID), nil
case composeTypes.ModuleResourceType:
if len(n.refPathRes) > 0 {
p0 := resource.FindComposeNamespace(pl.state.ParentResources, n.refPathRes[0].Identifiers)
if p0 == nil {
return "", resource.ComposeNamespaceErrUnresolved(n.refPathRes[0].Identifiers)
}
p0ID = p0.ID
}
if n.refRbacRes != nil {
p1 := resource.FindComposeModule(pl.state.ParentResources, n.refRbacRes.Identifiers)
if p1 == nil {
return "", resource.ComposeModuleErrUnresolved(n.refRbacRes.Identifiers)
}
p1ID = p1.ID
}
return composeTypes.ModuleRbacResource(p0ID, p1ID), nil
case composeTypes.ChartResourceType:
if len(n.refPathRes) > 0 {
p0 := resource.FindComposeNamespace(pl.state.ParentResources, n.refPathRes[0].Identifiers)
if p0 == nil {
return "", resource.ComposeNamespaceErrUnresolved(n.refPathRes[0].Identifiers)
}
p0ID = p0.ID
}
if n.refRbacRes != nil {
p1 := resource.FindComposeChart(pl.state.ParentResources, n.refRbacRes.Identifiers)
if p1 == nil {
return "", resource.ComposeChartErrUnresolved(n.refRbacRes.Identifiers)
}
p1ID = p1.ID
}
return composeTypes.ChartRbacResource(p0ID, p1ID), nil
case composeTypes.PageResourceType:
if len(n.refPathRes) > 0 {
p0 := resource.FindComposeNamespace(pl.state.ParentResources, n.refPathRes[0].Identifiers)
if p0 == nil {
return "", resource.ComposeNamespaceErrUnresolved(n.refPathRes[0].Identifiers)
}
p0ID = p0.ID
}
if n.refRbacRes != nil {
p1 := resource.FindComposePage(pl.state.ParentResources, n.refRbacRes.Identifiers)
if p1 == nil {
return "", resource.ComposePageErrUnresolved(n.refRbacRes.Identifiers)
}
p1ID = p1.ID
}
return composeTypes.PageRbacResource(p0ID, p1ID), nil
case composeTypes.RecordResourceType:
if len(n.refPathRes) > 0 {
p0 := resource.FindComposeNamespace(pl.state.ParentResources, n.refPathRes[0].Identifiers)
if p0 == nil {
return "", resource.ComposeNamespaceErrUnresolved(n.refPathRes[0].Identifiers)
}
p0ID = p0.ID
}
if len(n.refPathRes) > 1 {
p1 := resource.FindComposeModule(pl.state.ParentResources, n.refPathRes[1].Identifiers)
if p1 == nil {
return "", resource.ComposeModuleErrUnresolved(n.refPathRes[1].Identifiers)
}
p1ID = p1.ID
}
if n.refRbacRes != nil {
ref := n.refPathRes[1]
p2 := resource.FindComposeRecordResource(pl.state.ParentResources, ref.Identifiers)
if p2 == nil {
return "", resource.ComposeRecordErrUnresolved(n.refRbacRes.Identifiers)
}
for _, i := range n.refRbacRes.Identifiers {
if p2ID = p2.IDMap[i]; p2ID > 0 {
break
}
}
if p2ID == 0 {
return "", resource.ComposeRecordErrUnresolved(n.refRbacRes.Identifiers)
}
}
return composeTypes.RecordRbacResource(p0ID, p1ID, p2ID), nil
case composeTypes.ModuleFieldResourceType:
if len(n.refPathRes) > 0 {
p0 := resource.FindComposeNamespace(pl.state.ParentResources, n.refPathRes[0].Identifiers)
if p0 == nil {
return "", resource.ComposeNamespaceErrUnresolved(n.refPathRes[0].Identifiers)
}
p0ID = p0.ID
}
if len(n.refPathRes) > 1 {
p1 := resource.FindComposeModule(pl.state.ParentResources, n.refPathRes[1].Identifiers)
if p1 == nil {
return "", resource.ComposeModuleErrUnresolved(n.refPathRes[1].Identifiers)
}
p1ID = p1.ID
}
if n.refRbacRes != nil {
modRef := n.refPathRes[1]
p2 := resource.FindComposeModuleField(pl.state.ParentResources, modRef.Identifiers, n.refRbacRes.Identifiers)
if p2 == nil {
return "", resource.ComposeModuleFieldErrUnresolved(n.refRbacRes.Identifiers)
}
p2ID = p2.ID
}
return composeTypes.ModuleFieldRbacResource(p0ID, p1ID, p2ID), nil
case systemTypes.UserResourceType:
if n.refRbacRes != nil {
p1 := resource.FindUser(pl.state.ParentResources, n.refRbacRes.Identifiers)
if p1 == nil {
return "", resource.UserErrUnresolved(n.refRbacRes.Identifiers)
}
p1ID = p1.ID
}
return systemTypes.UserRbacResource(p1ID), nil
case systemTypes.RoleResourceType:
if n.refRbacRes != nil {
p1 := resource.FindRole(pl.state.ParentResources, n.refRbacRes.Identifiers)
if p1 == nil {
return "", resource.RoleErrUnresolved(n.refRbacRes.Identifiers)
}
p1ID = p1.ID
}
return systemTypes.RoleRbacResource(p1ID), nil
case systemTypes.ApplicationResourceType:
if n.refRbacRes != nil {
p1 := resource.FindApplication(pl.state.ParentResources, n.refRbacRes.Identifiers)
if p1 == nil {
return "", resource.ApplicationErrUnresolved(n.refRbacRes.Identifiers)
}
p1ID = p1.ID
}
return systemTypes.ApplicationRbacResource(p1ID), nil
case systemTypes.ApigwRouteResourceType:
if n.refRbacRes != nil {
p1 := resource.FindAPIGateway(pl.state.ParentResources, n.refRbacRes.Identifiers)
if p1 == nil {
return "", resource.APIGatewayErrUnresolved(n.refRbacRes.Identifiers)
}
p1ID = p1.ID
}
return systemTypes.ApigwRouteRbacResource(p1ID), nil
case systemTypes.AuthClientResourceType:
// @todo add support for importing rbac rules for specific client
return systemTypes.AuthClientRbacResource(p1ID), nil
case systemTypes.TemplateResourceType:
if n.refRbacRes != nil {
p1 := resource.FindTemplate(pl.state.ParentResources, n.refRbacRes.Identifiers)
if p1 == nil {
return "", resource.TemplateErrUnresolved(n.refRbacRes.Identifiers)
}
p1ID = p1.ID
}
return systemTypes.TemplateRbacResource(p1ID), nil
case systemTypes.ReportResourceType:
if n.refRbacRes != nil {
p1 := resource.FindReport(pl.state.ParentResources, n.refRbacRes.Identifiers)
if p1 == nil {
return "", resource.ReportErrUnresolved(n.refRbacRes.Identifiers)
}
p1ID = p1.ID
}
return systemTypes.ReportRbacResource(p1ID), nil
case systemTypes.QueueResourceType:
// @todo add support for importing rbac rules for specific queue
return systemTypes.QueueRbacResource(p1ID), nil
case systemTypes.DataPrivacyRequestResourceType:
return systemTypes.DataPrivacyRequestRbacResource(p1ID), nil
case federationTypes.NodeResourceType:
return federationTypes.NodeRbacResource(p1ID), nil
case federationTypes.SharedModuleResourceType:
return federationTypes.SharedModuleRbacResource(p0ID, p1ID), nil
case federationTypes.ExposedModuleResourceType:
return federationTypes.ExposedModuleRbacResource(p0ID, p1ID), nil
}
// @todo if we wish to support rbac for external stuff, this needs to pass through.
// this also requires some tweaks in the path ID thing.
return "", fmt.Errorf("unsupported resource type '%s' for RBAC store encode", rt)
}
func (n *rbacRule) handleComposeRecord(pp []resource.Interface) bool {
for _, p := range pp {
if p.ResourceType() == composeTypes.RecordResourceType && p.Identifiers().HasAny(n.refPathRes[1].Identifiers) {
return true
}
}
return false
}
func (n *rbacRule) handleComposeModuleField(pp []resource.Interface) bool {
for _, p := range pp {
if p.ResourceType() == composeTypes.ModuleResourceType && p.Identifiers().HasAny(n.refPathRes[1].Identifiers) {
return true
}
}
return false
}