-
Notifications
You must be signed in to change notification settings - Fork 18
/
evaluator.go
1209 lines (1092 loc) · 37.8 KB
/
evaluator.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
//Licensed under the Universal Permissive License (UPL) Version 1.0 as shown at http://oss.oracle.com/licenses/upl.
package eval
import (
"fmt"
"sort"
"strconv"
"strings"
"time"
"github.com/teramoby/speedle-plus/3rdparty/github.com/Knetic/govaluate"
adsapi "github.com/teramoby/speedle-plus/api/ads"
"github.com/teramoby/speedle-plus/pkg/errors"
"github.com/teramoby/speedle-plus/pkg/eval/function"
"github.com/teramoby/speedle-plus/pkg/subjectutils"
"github.com/teramoby/speedle-plus/api/pms"
log "github.com/sirupsen/logrus"
)
var builtinFunctions = map[string]govaluate.ExpressionFunction{
"Sqrt": function.Sqrt,
"Max": function.Max,
"Min": function.Min,
"Sum": function.Sum,
"Avg": function.Avg,
"IsSubSet": function.IsSubSet,
}
type TokenAsserter interface {
// set asserter func for policy evaluator
SetAsserterFunc(f func(ctx *adsapi.RequestContext) error)
// AssertToken assert token and generate subject to represent the identity
AssertToken(ctx *adsapi.RequestContext) error
}
type InternalEvaluator interface {
adsapi.PolicyEvaluator
TokenAsserter
}
type internalRequestContext struct {
Subject *subject
Service *RuntimeService
GlobalService *RuntimeService
Resource string
Action string
Attributes map[string]interface{}
}
type subject struct {
Users []string
Groups []string
Entities []string
Principals []string
TokenType string
Token string
}
type PolicyEvalImpl struct {
RuntimePolicyStore *RuntimePolicyStore //This is runtime policy store
Store pms.PolicyStoreManagerADS
AsserterFunc func(ctx *adsapi.RequestContext) error
}
func (p *PolicyEvalImpl) deleteService(serviceName string) {
// Delete application
p.RuntimePolicyStore.deleteService(serviceName)
}
func (p *PolicyEvalImpl) fullReloadRuntimeCache() {
ps, err := p.Store.ReadPolicyStore()
if err != nil {
log.Errorf("Fail to full reload runtime cache, err:%v", err)
return
}
p.RuntimePolicyStore.reloadPolicyStore(ps)
}
func (p *PolicyEvalImpl) Refresh() error {
p.fullReloadRuntimeCache()
return nil
}
func (p *PolicyEvalImpl) AddServiceInRuntimeCache(service *pms.Service) {
p.RuntimePolicyStore.addService(service)
}
func (p *PolicyEvalImpl) AddPolicyInRuntimeCache(serviceName string, policy *pms.Policy) {
p.RuntimePolicyStore.addPolicy(serviceName, policy)
}
func (p *PolicyEvalImpl) AddRolePolicyInRuntimeCache(serviceName string, rolepolicy *pms.RolePolicy) {
p.RuntimePolicyStore.addRolePolicy(serviceName, rolepolicy)
}
func (p *PolicyEvalImpl) DeletePolicyInRuntimeCache(serviceName string, policyID string) {
p.RuntimePolicyStore.deletePolicy(serviceName, policyID)
}
func (p *PolicyEvalImpl) DeleteRolePolicyInRuntimeCache(serviceName string, rolePolicyID string) {
p.RuntimePolicyStore.deleteRolePolicy(serviceName, rolePolicyID)
}
func (p *PolicyEvalImpl) DeleteFunctionInRuntimeCache(funcName string) {
p.RuntimePolicyStore.deleteFunction(funcName)
}
func (p *PolicyEvalImpl) AddFunctionInRuntimeCache(cf *pms.Function) {
p.RuntimePolicyStore.addFunction(cf)
}
func (p *PolicyEvalImpl) CleanExpiredFunctionResult() {
p.RuntimePolicyStore.expireFunctionResultCache()
}
func (p *PolicyEvalImpl) SetAsserterFunc(f func(ctx *adsapi.RequestContext) error) {
p.AsserterFunc = f
}
func (p *PolicyEvalImpl) AssertToken(ctx *adsapi.RequestContext) error {
// Assert identity token
if ctx.Subject != nil &&
p.AsserterFunc != nil &&
len(ctx.Subject.TokenType) != 0 &&
len(ctx.Subject.Token) != 0 && !ctx.Subject.Asserted {
err := p.AsserterFunc(ctx)
if err == nil {
ctx.Subject.Asserted = true
}
return err
}
return nil
}
func (p *PolicyEvalImpl) populateContext(ctx *adsapi.RequestContext) (*internalRequestContext, error) {
service, err := p.getService(ctx.ServiceName)
if err != nil {
return nil, err
}
// Assert identity token
err = p.AssertToken(ctx)
if err != nil {
return nil, err
}
var globalService *RuntimeService
if ctx.ServiceName != pms.GlobalService {
globalService, _ = p.getService(pms.GlobalService)
}
newCtx := internalRequestContext{
Resource: ctx.Resource,
Action: ctx.Action,
Service: service,
GlobalService: globalService,
Attributes: make(map[string]interface{}),
}
now := time.Now()
newCtx.Attributes[adsapi.BuiltIn_Attr_RequestTime] = now.Unix()
year, month, day := now.Date()
newCtx.Attributes[adsapi.BuiltIn_Attr_RequestYear] = year
newCtx.Attributes[adsapi.BuiltIn_Attr_RequestMonth] = int(month)
newCtx.Attributes[adsapi.BuiltIn_Attr_RequestDay] = day
newCtx.Attributes[adsapi.BuiltIn_Attr_RequestWeekday] = now.Weekday().String()
newCtx.Attributes[adsapi.BuiltIn_Attr_RequestHour] = now.Hour()
newCtx.Subject = &subject{
Users: []string{},
Groups: []string{},
Entities: []string{},
}
if ctx.Subject != nil {
groups := []interface{}{}
var user, entity interface{}
for _, principal := range ctx.Subject.Principals {
encodedPrincipal := subjectutils.EncodePrincipal(principal)
principalWithoutIDD := ""
if len(principal.IDD) != 0 {
principalWithoutIDD = subjectutils.EncodePrincipal(&adsapi.Principal{
Type: principal.Type,
Name: principal.Name,
})
}
switch principal.Type {
case adsapi.PRINCIPAL_TYPE_USER:
newCtx.Subject.Users = append(newCtx.Subject.Users, encodedPrincipal)
if len(principalWithoutIDD) != 0 {
newCtx.Subject.Users = append(newCtx.Subject.Users, principalWithoutIDD)
}
if user == nil {
user = principal.Name
}
break
case adsapi.PRINCIPAL_TYPE_GROUP:
newCtx.Subject.Groups = append(newCtx.Subject.Groups, encodedPrincipal)
groups = append(groups, principal.Name)
if len(principalWithoutIDD) != 0 {
newCtx.Subject.Groups = append(newCtx.Subject.Groups, principalWithoutIDD)
}
break
case adsapi.PRINCIPAL_TYPE_ENTITY:
newCtx.Subject.Entities = append(newCtx.Subject.Entities, encodedPrincipal)
if len(principalWithoutIDD) != 0 {
newCtx.Subject.Entities = append(newCtx.Subject.Entities, principalWithoutIDD)
}
if entity == nil {
entity = principal.Name
}
break
}
}
if user != nil {
newCtx.Attributes[adsapi.BuiltIn_Attr_RequestUser] = user
}
newCtx.Attributes[adsapi.BuiltIn_Attr_RequestGroups] = groups
if entity != nil {
newCtx.Attributes[adsapi.BuiltIn_Attr_RequestEntity] = entity
}
}
newCtx.Attributes[adsapi.BuiltIn_Attr_RequestResource] = ctx.Resource
newCtx.Attributes[adsapi.BuiltIn_Attr_RequestAction] = ctx.Action
for key, value := range ctx.Attributes {
newCtx.Attributes[key] = value
}
updateSubjectWithBuiltInRoles(newCtx.Subject)
return &newCtx, nil
}
func (p *PolicyEvalImpl) IsAllowed(ctx adsapi.RequestContext) (bool, adsapi.Reason, error) {
//IsAllowed don't need return EvaluationResult, so pass nil
return p.InternalIsAllowed(&ctx, nil)
}
func (p *PolicyEvalImpl) InternalIsAllowed(ctx *adsapi.RequestContext, evaluationResult *adsapi.EvaluationResult) (bool, adsapi.Reason, error) {
p.RuntimePolicyStore.RLock()
defer p.RuntimePolicyStore.RUnlock()
newCtx, err := p.populateContext(ctx)
if err != nil {
return false, adsapi.SERVICE_NOT_FOUND, err
}
newCtx.Service.RLock()
defer newCtx.Service.RUnlock()
if newCtx.Service.PoliciesCache.isEmpty() {
return false, adsapi.NO_APPLICABLE_POLICIES, nil
}
if evaluationResult != nil {
evaluationResult.Attributes = newCtx.Attributes
}
if err := p.resolveSubject(newCtx, evaluationResult); err != nil {
return false, adsapi.ERROR_IN_EVALUATION, err
}
grantedPolicies, deniedPolicies, err := p.getPolicyList(newCtx, true, true, evaluationResult)
if err != nil {
return false, adsapi.ERROR_IN_EVALUATION, err
}
allowed, reason := denyOverwriteCombiner(grantedPolicies, deniedPolicies, newCtx, evaluationResult)
return allowed, reason, nil
}
// Return all the policies related to a subject
func (p *PolicyEvalImpl) Diagnose(ctx adsapi.RequestContext) (*adsapi.EvaluationResult, error) {
// Construct the evaluation result
retCtx := ctx
evaResult := adsapi.EvaluationResult{
Allowed: false,
RequestCtx: &retCtx,
Attributes: nil,
GrantedRoles: make([]string, 0),
RolePolicies: make([]*adsapi.EvaluatedRolePolicy, 0),
Policies: make([]*adsapi.EvaluatedPolicy, 0),
}
allowed, reason, err := p.InternalIsAllowed(&ctx, &evaResult)
evaResult.Allowed = allowed
evaResult.Reason = reason
return &evaResult, err
}
func (p *PolicyEvalImpl) GetAllGrantedRoles(ctx adsapi.RequestContext) ([]string, error) {
p.RuntimePolicyStore.RLock()
defer p.RuntimePolicyStore.RUnlock()
newCtx, err := p.populateContext(&ctx)
if err != nil {
return nil, err
}
newCtx.Service.RLock()
defer newCtx.Service.RUnlock()
ret, err := p.getGrantedRolesFromService(newCtx, nil)
return ret, err
}
//Limitations: This function only calculate granted permissions with resource, will not calculate granted permissions with resource expression.
func (p *PolicyEvalImpl) GetAllGrantedPermissions(ctx adsapi.RequestContext) ([]pms.Permission, error) {
p.RuntimePolicyStore.RLock()
defer p.RuntimePolicyStore.RUnlock()
newCtx, err := p.populateContext(&ctx)
if err != nil {
return nil, err
}
newCtx.Service.RLock()
defer newCtx.Service.RUnlock()
if newCtx.Service.PoliciesCache.isEmpty() {
return []pms.Permission{}, nil
}
if err := p.resolveSubject(newCtx, nil); err != nil {
return nil, err
}
grantedPolicies, deniedPolicies, err := p.getPolicyList(newCtx, false, true, nil)
if err != nil {
return nil, err
}
var grantedPermissionList, deniedPermissionList []pms.Permission
for _, policy := range grantedPolicies {
permissions := policy.Permissions
if permissions == nil { //means grant any permissions, ignore here
continue
}
for _, permission := range permissions {
if len(permission.Resource) != 0 {
grantedPermissionList = append(grantedPermissionList, pms.Permission{
Resource: permission.Resource,
Actions: permission.Actions,
})
}
}
}
for _, policy := range deniedPolicies {
permissions := policy.Permissions
if permissions == nil { //means deny any permission
return []pms.Permission{}, nil
}
for _, permission := range permissions {
deniedPermissionList = append(deniedPermissionList, pms.Permission{
Resource: permission.Resource,
Actions: permission.Actions,
ResourceExpression: permission.ResourceExpression,
})
}
}
ret := calculatePermissions(grantedPermissionList, deniedPermissionList)
return ret, nil
}
func (p *PolicyEvalImpl) getService(serviceName string) (*RuntimeService, error) {
if runtimeService, exist := p.RuntimePolicyStore.RuntimeServices[serviceName]; exist {
return runtimeService, nil
}
return nil, errors.Errorf(errors.EvalEngineError, "Application %s is not found ", serviceName)
}
func (p *PolicyEvalImpl) resolveSubject(ctx *internalRequestContext, evaluationResult *adsapi.EvaluationResult) error {
roles, err := p.getGrantedRolesFromService(ctx, evaluationResult)
if err != nil {
return err
}
for _, role := range roles {
ctx.Subject.Principals = append(ctx.Subject.Principals, convertRoleToPrincipal(role))
}
//Set EvalutionResult
if evaluationResult != nil {
evaluationResult.GrantedRoles = roles
}
return nil
}
// The firstly returned is granted rolePolicies.
// The second returned value is denied rolePolicies.
func (p *PolicyEvalImpl) getDirectRolePolices(principals []string,
ctx *internalRequestContext, policyIDMap map[string]bool, evaluationResult *adsapi.EvaluationResult) ([]*pms.RolePolicy, []*pms.RolePolicy, error) {
grantedRolePolicies := make([]*pms.RolePolicy, 0)
deniedRolePolicies := make([]*pms.RolePolicy, 0)
grantedRolePolicies, deniedRolePolicies, err := p.getDirectRolePolicesInService(principals, ctx.Service, ctx.Resource, ctx.Attributes, policyIDMap, evaluationResult, grantedRolePolicies, deniedRolePolicies)
if err != nil {
return nil, nil, err
}
if ctx.GlobalService != nil {
grantedRolePolicies, deniedRolePolicies, err = p.getDirectRolePolicesInService(principals, ctx.GlobalService, ctx.Resource, ctx.Attributes, policyIDMap, evaluationResult, grantedRolePolicies, deniedRolePolicies)
if err != nil {
return nil, nil, err
}
}
return grantedRolePolicies, deniedRolePolicies, nil
}
func (p *PolicyEvalImpl) getDirectRolePolicesInService(principals []string,
service *RuntimeService, resource string, attributes map[string]interface{}, policyIDMap map[string]bool, evaluationResult *adsapi.EvaluationResult, grantedRolePolicies []*pms.RolePolicy, deniedRolePolicies []*pms.RolePolicy) ([]*pms.RolePolicy, []*pms.RolePolicy, error) {
for _, policy := range service.GetRelatedRolePolicyMap(principals, resource) {
if policyIDMap[policy.ID] {
continue
}
// No principal defined. that means the roles are granted to any user
if (policy.Principals == nil || len(policy.Principals) == 0 || matchRolePolicyPrincipals(principals, policy.Principals)) && matchResource(resource, policy.Resources, policy.ResourceExpressions) {
// Evaluate conditions
condition, ok := service.RolePoliciesCache.Conditions[policy.ID]
// If no conditions defined, the condition evaluation result is true
result := true
if !ok && len(policy.Condition) != 0 {
if cond, err := p.RuntimePolicyStore.recompileRolePolicyConditionAtRuntime(service.Name, policy); err != nil {
return nil, nil, err
} else {
condition = cond
}
}
if condition != nil {
result, _ = evaluateCondition(condition, attributes)
}
if evaluationResult != nil {
evaluationResult.AddRolePolicy(policy, result)
}
if result {
switch policy.Effect {
case pms.Grant:
grantedRolePolicies = append(grantedRolePolicies, policy)
break
case pms.Deny:
deniedRolePolicies = append(deniedRolePolicies, policy)
break
default:
// TODO: Log a warning, currently do nothing
}
}
}
}
return grantedRolePolicies, deniedRolePolicies, nil
}
type Role struct {
Name string
ParentPrincipals map[string]bool
ParentRoles map[string]bool
ChildRoles map[string]bool
DeniedRoles map[string]bool
DeniedByRoles map[string]bool
DeniedByPrincipals map[string]bool //TODO this could be removed?
}
//assume role policy does not support AND Principal
//assume ctx.Subject.Principals does not contain user defined roles,
//assume built-in role like anonymous role and authenticated role can't be used in role policy
func (p *PolicyEvalImpl) getGrantedRolesFromService(ctx *internalRequestContext, evaluationResult *adsapi.EvaluationResult) ([]string, error) {
if ctx.GlobalService != nil {
ctx.GlobalService.RLock()
defer ctx.GlobalService.RUnlock()
}
relatedRolesMap := make(map[string]*Role) //contain all role info related to the role calculation
policyIDMap := make(map[string]bool) // this is to avoid repeat processing of same policy.
grantedRoleMap := make(map[string]bool) //this is to keep all possiblely granted roles
subjectPrincipalMap := make(map[string]bool) //this contains non-role principals of ctx.Subject
for _, principal := range ctx.Subject.Principals {
if !strings.HasPrefix(principal, "role:") {
subjectPrincipalMap[principal] = true
}
}
directGrantedRolePolicies, directDeniedRolePolicies, err := p.getDirectRolePolices(ctx.Subject.Principals, ctx, policyIDMap, evaluationResult)
if err != nil {
return nil, err
}
directDeniedRoleMap := make(map[string]bool) //roles directly denied by user/group in ctx.subject , which always could be denied safely
deniedRoleMap := make(map[string]bool) //this contains roles possiblely denied by another role.
var newlyGrantedRoles []string
for _, rolePolicy := range directDeniedRolePolicies {
if _, ok := policyIDMap[rolePolicy.ID]; !ok {
policyIDMap[rolePolicy.ID] = true
updateRelatedRoleMapWithDenyRolePolicy(rolePolicy, relatedRolesMap, subjectPrincipalMap, directDeniedRoleMap, grantedRoleMap, deniedRoleMap)
}
}
for _, rolePolicy := range directGrantedRolePolicies {
if _, ok := policyIDMap[rolePolicy.ID]; !ok {
policyIDMap[rolePolicy.ID] = true
newlyGrantedRoles = append(newlyGrantedRoles, updateRelatedRoleMapWithGrantRolePolicy(rolePolicy, relatedRolesMap, subjectPrincipalMap, directDeniedRoleMap, grantedRoleMap)...)
}
}
for len(newlyGrantedRoles) != 0 {
newSubjectPrincipals := []string{}
for _, role := range newlyGrantedRoles {
newSubjectPrincipals = append(newSubjectPrincipals, convertRoleToPrincipal(role))
}
indirectGrantedRolePolicies, _, err := p.getDirectRolePolices(newSubjectPrincipals, ctx, policyIDMap, evaluationResult)
if err != nil {
return nil, err
}
newlyGrantedRoles = []string{}
for _, rolePolicy := range indirectGrantedRolePolicies {
if _, ok := policyIDMap[rolePolicy.ID]; !ok {
policyIDMap[rolePolicy.ID] = true
newlyGrantedRoles = append(newlyGrantedRoles, updateRelatedRoleMapWithGrantRolePolicy(rolePolicy, relatedRolesMap, subjectPrincipalMap, directDeniedRoleMap, grantedRoleMap)...)
}
}
}
newSubjectPrincipals := []string{}
for role := range grantedRoleMap {
newSubjectPrincipals = append(newSubjectPrincipals, convertRoleToPrincipal(role))
}
_, DeniedRolePolicies, err := p.getDirectRolePolices(newSubjectPrincipals, ctx, policyIDMap, evaluationResult)
if err != nil {
return nil, err
}
for _, rolePolicy := range DeniedRolePolicies {
if _, ok := policyIDMap[rolePolicy.ID]; !ok {
policyIDMap[rolePolicy.ID] = true
updateRelatedRoleMapWithDenyRolePolicy(rolePolicy, relatedRolesMap, subjectPrincipalMap, directDeniedRoleMap, grantedRoleMap, deniedRoleMap)
}
}
//only keep role node that is in possible granted roles
cleanRelatedRoleMap(relatedRolesMap, grantedRoleMap)
for {
//find safely denied role
safelyDeniedRoles := []string{}
for deniedRole := range deniedRoleMap {
if couldRoleSafelyBeDenied(deniedRole, relatedRolesMap, deniedRoleMap) {
safelyDeniedRoles = append(safelyDeniedRoles, deniedRole)
}
}
if len(safelyDeniedRoles) > 0 {
for _, deniedRole := range safelyDeniedRoles {
denyRoleAndDescendants(deniedRole, relatedRolesMap, grantedRoleMap, deniedRoleMap)
//printRelatedRoleMap(relatedRolesMap)
}
//get deniedRoles based on the left role nodes.
deniedRoleMap = getDeniedRoles(relatedRolesMap, grantedRoleMap)
continue
} else {
//when no safely denied roles found, just delete all denied roles one by one
for deniedRole := range deniedRoleMap {
denyRoleAndDescendants(deniedRole, relatedRolesMap, grantedRoleMap, deniedRoleMap)
}
break
}
}
finalGrantedRoles := []string{}
for role := range grantedRoleMap {
finalGrantedRoles = append(finalGrantedRoles, role)
}
return finalGrantedRoles, nil
}
func printRelatedRoleMap(relatedRoleMap map[string]*Role) {
fmt.Println("----related role map start----")
for roleName, roleNode := range relatedRoleMap {
fmt.Printf("%s :\n parentPrincipals=%v\n parentRoles=%v\n deniedByRoles=%v\n deniedByPrinncipals=%v\n childRoles=%v\n denedRoles=%v\n",
roleName, roleNode.ParentPrincipals, roleNode.ParentRoles, roleNode.DeniedByRoles, roleNode.DeniedByPrincipals, roleNode.ChildRoles, roleNode.DeniedRoles)
}
fmt.Println("----related role map end----")
}
func updateRelatedRoleMapWithGrantRolePolicy(rolePolicy *pms.RolePolicy, relatedRolesMap map[string]*Role,
subjectPrincipalMap map[string]bool, directDeniedRoleMap map[string]bool, grantedRoleMap map[string]bool) []string {
newlyGrantedRoles := []string{}
parentRoles := []string{}
parentPrincipals := []string{}
for _, principal := range rolePolicy.Principals {
if strings.HasPrefix(principal, "role:") {
parentRoles = append(parentRoles, strings.TrimPrefix(principal, "role:"))
} else {
if _, ok := subjectPrincipalMap[principal]; ok {
parentPrincipals = append(parentPrincipals, principal)
}
}
}
for _, role := range rolePolicy.Roles {
if _, ok := directDeniedRoleMap[role]; !ok {
roleNode, ok := relatedRolesMap[role]
if !ok {
roleNode = &Role{
Name: role,
ParentPrincipals: make(map[string]bool),
ParentRoles: make(map[string]bool),
ChildRoles: make(map[string]bool),
DeniedRoles: make(map[string]bool),
DeniedByRoles: make(map[string]bool),
DeniedByPrincipals: make(map[string]bool),
}
relatedRolesMap[role] = roleNode
}
for _, proleName := range parentRoles {
roleNode.ParentRoles[proleName] = true
}
for _, pprincipalName := range parentPrincipals {
roleNode.ParentPrincipals[pprincipalName] = true
}
if _, ok := grantedRoleMap[role]; !ok {
grantedRoleMap[role] = true
newlyGrantedRoles = append(newlyGrantedRoles, role)
}
}
}
for _, proleName := range parentRoles {
parentRoleNode, ok := relatedRolesMap[proleName]
if !ok {
parentRoleNode = &Role{
Name: proleName,
ParentPrincipals: make(map[string]bool),
ParentRoles: make(map[string]bool),
ChildRoles: make(map[string]bool),
DeniedRoles: make(map[string]bool),
DeniedByRoles: make(map[string]bool),
DeniedByPrincipals: make(map[string]bool),
}
relatedRolesMap[proleName] = parentRoleNode
}
for _, childRole := range rolePolicy.Roles {
if _, ok := directDeniedRoleMap[childRole]; !ok {
parentRoleNode.ChildRoles[childRole] = true
}
}
}
return newlyGrantedRoles
}
func updateRelatedRoleMapWithDenyRolePolicy(rolePolicy *pms.RolePolicy, relatedRolesMap map[string]*Role,
subjectPrincipalMap map[string]bool, directDeniedRoleMap map[string]bool, grantedRoleMap map[string]bool,
deniedRoleMap map[string]bool) {
deniedByRoles := []string{}
deniedByPrincipals := []string{}
for _, principal := range rolePolicy.Principals {
if strings.HasPrefix(principal, "role:") {
deniedByRoles = append(deniedByRoles, strings.TrimPrefix(principal, "role:"))
} else {
if _, ok := subjectPrincipalMap[principal]; ok {
deniedByPrincipals = append(deniedByPrincipals, principal)
}
}
}
for _, role := range deniedByRoles {
roleNode, ok := relatedRolesMap[role]
if !ok {
roleNode = &Role{
Name: role,
ParentPrincipals: make(map[string]bool),
ParentRoles: make(map[string]bool),
ChildRoles: make(map[string]bool),
DeniedRoles: make(map[string]bool),
DeniedByRoles: make(map[string]bool),
DeniedByPrincipals: make(map[string]bool),
}
relatedRolesMap[role] = roleNode
}
for _, deniedRole := range rolePolicy.Roles {
roleNode.DeniedRoles[deniedRole] = true
}
}
for _, role := range rolePolicy.Roles {
roleNode, ok := relatedRolesMap[role]
if !ok {
roleNode = &Role{
Name: role,
ParentPrincipals: make(map[string]bool),
ParentRoles: make(map[string]bool),
ChildRoles: make(map[string]bool),
DeniedRoles: make(map[string]bool),
DeniedByRoles: make(map[string]bool),
DeniedByPrincipals: make(map[string]bool),
}
relatedRolesMap[role] = roleNode
}
for _, deniedByRole := range deniedByRoles {
roleNode.DeniedByRoles[deniedByRole] = true
}
for _, deniedByPrincipal := range deniedByPrincipals {
roleNode.DeniedByPrincipals[deniedByPrincipal] = true
}
if len(deniedByPrincipals) > 0 {
directDeniedRoleMap[role] = true
} else {
if _, ok := grantedRoleMap[role]; ok {
if _, ok := deniedRoleMap[role]; !ok {
deniedRoleMap[role] = true
}
}
}
}
}
func cleanRelatedRoleMap(relatedRoleMap map[string]*Role, grantedRoleMap map[string]bool) {
for role, roleNode := range relatedRoleMap {
if _, ok := grantedRoleMap[role]; !ok {
delete(relatedRoleMap, role)
} else {
for pRole := range roleNode.ParentRoles {
if _, ok := grantedRoleMap[pRole]; !ok {
delete(roleNode.ParentRoles, pRole)
}
}
for pRole := range roleNode.DeniedByRoles {
if _, ok := grantedRoleMap[pRole]; !ok {
delete(roleNode.DeniedByRoles, pRole)
}
}
for cRole := range roleNode.ChildRoles {
if _, ok := grantedRoleMap[cRole]; !ok {
delete(roleNode.ChildRoles, cRole)
}
}
for cRole := range roleNode.DeniedRoles {
if _, ok := grantedRoleMap[cRole]; !ok {
delete(roleNode.DeniedRoles, cRole)
}
}
}
}
}
func getDeniedRoles(relatedRoleMap map[string]*Role, grantedRoleMap map[string]bool) map[string]bool {
deniedRoleMap := make(map[string]bool)
for role, roleNode := range relatedRoleMap {
if len(roleNode.DeniedByRoles) > 0 {
if _, ok := grantedRoleMap[role]; ok {
deniedRoleMap[role] = true
}
}
}
return deniedRoleMap
}
func denyRoleAndDescendants(role string, relatedRoleMap map[string]*Role, grantedRoleMap map[string]bool, deniedRoleMap map[string]bool) {
deletedRoles := make(map[string]bool)
deletedRoles[role] = true
descendants := getDeniableDescendantRoles(role, relatedRoleMap)
//fmt.Printf("deniable descendants for %s are %v \n", role, descendants)
for _, d := range descendants {
deletedRoles[d] = true
}
for role, roleNode := range relatedRoleMap {
if _, ok := deletedRoles[role]; !ok {
for deletedRole := range deletedRoles {
delete(roleNode.ChildRoles, deletedRole)
delete(roleNode.ParentRoles, deletedRole)
delete(roleNode.DeniedRoles, deletedRole)
delete(roleNode.DeniedByRoles, deletedRole)
if len(roleNode.ParentRoles) == 0 && len(roleNode.ParentPrincipals) == 0 {
delete(relatedRoleMap, role)
delete(grantedRoleMap, role)
}
}
}
}
for deletedRole := range deletedRoles {
delete(relatedRoleMap, deletedRole)
delete(grantedRoleMap, deletedRole)
}
delete(deniedRoleMap, role)
}
func getDeniableDescendantRoles(role string, relatedRoleMap map[string]*Role) []string {
descendants := []string{}
if roleNode, ok := relatedRoleMap[role]; ok {
//get descendant nodes
for childrole := range roleNode.ChildRoles {
if childRoleNode, ok := relatedRoleMap[childrole]; ok {
allParentRolesDenied := true
for parentRole := range childRoleNode.ParentRoles {
if parentRole != role && !contains(descendants, parentRole) {
allParentRolesDenied = false
break
}
}
if allParentRolesDenied && len(childRoleNode.ParentPrincipals) == 0 {
descendants = append(descendants, childrole)
descendants = append(descendants, getDeniableDescendantRoles(childrole, relatedRoleMap)...)
}
}
}
}
return descendants
}
func contains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}
//If any of the deniedByRole and all its ancestors are not denied, we take it as could be safely denied.
func couldRoleSafelyBeDenied(role string, relatedRoleMap map[string]*Role, deniedRoleMap map[string]bool) bool {
allDeniedByRoleBeDenied := true
if roleNode, ok := relatedRoleMap[role]; ok {
for deniedByRole := range roleNode.DeniedByRoles {
if !selfOrAncestorsBeDenied(deniedByRole, relatedRoleMap, deniedRoleMap) {
allDeniedByRoleBeDenied = false
break
}
}
} else {
fmt.Println("error in couldRoleSafelyBeDenied")
}
return !allDeniedByRoleBeDenied
}
func selfOrAncestorsBeDenied(deniedByRole string, relatedRoleMap map[string]*Role, deniedRoleMap map[string]bool) bool {
if _, ok := deniedRoleMap[deniedByRole]; ok { //self be denied
return true
}
if deniedByRoleNode, ok := relatedRoleMap[deniedByRole]; ok {
if len(deniedByRoleNode.ParentPrincipals) > 0 { //first level role, we could end up here
return false
} else {
for parentRole := range deniedByRoleNode.ParentRoles {
if !selfOrAncestorsBeDenied(parentRole, relatedRoleMap, deniedRoleMap) {
return false
}
}
}
} else {
fmt.Println("error in selfOrAncestorsBeDenied")
}
return true
}
// Returns granted and denied policies
// The first returned value is granted policies
// The second returned value is denied policies
func (p *PolicyEvalImpl) getPolicyList(ctx *internalRequestContext, matchResource bool, matchCondition bool, evaluationResult *adsapi.EvaluationResult) ([]*pms.Policy, []*pms.Policy, error) {
var grantedPolicyList []*pms.Policy
var deniedPolicyList []*pms.Policy
principals := ctx.Subject.Principals
for _, policy := range ctx.Service.GetRelatedPolicyMap(principals, ctx.Resource, matchResource) {
// No principal defined. that means the resource actions are granted to any user
if policy.Principals == nil || len(policy.Principals) == 0 || matchPrincipals(principals, policy.Principals) {
// Check the resource and action
if !matchResource || (matchResource && matchResourceAction(policy, ctx)) {
// Evaluate conditions
condition, ok := ctx.Service.PoliciesCache.Conditions[policy.ID]
// If no conditions defined, the condition evaluation result is true
result := true
if !ok && len(policy.Condition) != 0 {
if cond, err := p.RuntimePolicyStore.recompilePolicyConditionAtRuntime(ctx.Service.Name, policy); err != nil {
return nil, nil, err
} else {
condition = cond
}
}
if condition != nil {
result, _ = evaluateCondition(condition, ctx.Attributes)
}
if result {
switch policy.Effect {
case pms.Grant:
grantedPolicyList = append(grantedPolicyList, policy)
break
case pms.Deny:
deniedPolicyList = append(deniedPolicyList, policy)
break
default:
// TODO: Log a warning, currently do nothing.
}
} else if evaluationResult != nil {
//addConditionFailedPolicyToEvaluationResult(policy, result, evaluationResult)
evaluationResult.AddPolicy(policy, adsapi.Evaluation_ConditionFailed, result)
}
}
}
}
return grantedPolicyList, deniedPolicyList, nil
}
//dataSet should be this:
// {
// [] string, //service name slice
// map[int]string, //map of policy ID to it's serviceName
// map[int]string, //map of rolePolicy ID to it's serviceName
// [] string, //custom function slice
// }
func (p *PolicyEvalImpl) syncRuntimeCache(dataSet []interface{}) error {
log.Info("start to sync runtime cache data.")
if len(dataSet) != 4 {
return errors.New(errors.EvalCacheError, "invalid data set for cache reloading")
}
policyStore := p.Store
log.Info("start to sync service data.")
if serviceArr, ok := dataSet[0].([]string); ok {
for _, serviceInDB := range serviceArr { //add new services
if p.RuntimePolicyStore.RuntimeServices[serviceInDB] == nil {
service, err := policyStore.GetService(serviceInDB)
if err != nil {
return err
}
p.AddServiceInRuntimeCache(service)
}
}
for serviceInCache := range p.RuntimePolicyStore.RuntimeServices {
isInDB := false
for _, serviceInDB := range serviceArr {
if serviceInDB == serviceInCache {
isInDB = true
break
}
}
if !isInDB {
p.deleteService(serviceInCache)
}
}
} else {
return errors.New(errors.EvalCacheError, "invalid service data set for cache reloading")
}
log.Info("start to sync policy data.")
if policySetInDB, ok := dataSet[1].(map[int]string); ok {
idInDB := getSortedIndexOfMap(policySetInDB)
policySetInCache, idInCache, err := p.getPolicySetInCache()
if err != nil {
return err
}
missed, removed := difPolicySets(idInCache, idInDB)
for _, id := range missed {
serviceName := policySetInDB[id]
policy, err := policyStore.GetPolicy(serviceName, strconv.Itoa(id))
if err != nil {
return err
} else {
p.AddPolicyInRuntimeCache(serviceName, policy)
}
}
for _, id := range removed {
serviceName := policySetInCache[id]
p.DeletePolicyInRuntimeCache(serviceName, strconv.Itoa(id))
}
} else {
return errors.New(errors.EvalCacheError, "invalid policy data set for cache reloading")
}
log.Info("start to sync rolePolicy data.")
if rolePolicySetInDB, ok := dataSet[2].(map[int]string); ok {
idInDB := getSortedIndexOfMap(rolePolicySetInDB)
rolePolicySetInCache, idInCache, err := p.getRolePolicySetInCache()
if err != nil {
return err
}
missed, removed := difPolicySets(idInCache, idInDB)
for _, id := range missed {
serviceName := rolePolicySetInDB[id]
policy, err := policyStore.GetRolePolicy(serviceName, strconv.Itoa(id))
if err != nil {
return err
} else {
p.AddRolePolicyInRuntimeCache(serviceName, policy)
}
}
for _, id := range removed {
serviceName := rolePolicySetInCache[id]
p.DeleteRolePolicyInRuntimeCache(serviceName, strconv.Itoa(id))
}
} else {
return errors.New(errors.EvalCacheError, "invalid rolePolicy data set for cache reloading")
}