-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathzpeclient_test.go
796 lines (746 loc) · 20.8 KB
/
zpeclient_test.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
package webhook
import (
"log"
"os"
"regexp"
"testing"
"time"
"github.com/ardielle/ardielle-go/rdl"
"github.com/stretchr/testify/assert"
"github.com/yahoo/athenz/clients/go/zms"
v1 "github.com/yahoo/k8s-athenz-syncer/pkg/apis/athenz/v1"
"github.com/yahoo/k8s-athenz-syncer/pkg/client/clientset/versioned/fake"
athenzInformer "github.com/yahoo/k8s-athenz-syncer/pkg/client/informers/externalversions/athenz/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/tools/cache"
)
const (
domainName = "home.domain"
username = "user.name"
trustDomainName = "test.trust.domain"
trustusername = "trustuser.name"
domainWithDeny = "home.domain.deny"
)
var (
ad = &v1.AthenzDomain{
ObjectMeta: metav1.ObjectMeta{
Name: domainName,
},
Spec: v1.AthenzDomainSpec{
SignedDomain: getFakeDomain(),
},
}
ad1 = &v1.AthenzDomain{
ObjectMeta: metav1.ObjectMeta{
Name: trustDomainName,
},
Spec: v1.AthenzDomainSpec{
SignedDomain: getFakeTrustDomain(),
},
}
cm0 = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cm",
},
Data: map[string]string{
"latest_contact": "wrong time format",
},
}
cm = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cm",
},
Data: map[string]string{
"latest_contact": time.Now().Format(time.RFC3339Nano),
},
}
cm1 = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cm1",
},
Data: map[string]string{
"latest_contact": "2020-02-11T17:44:38.080Z",
},
}
)
func getFakeAthenzDomain() *v1.AthenzDomain {
spec := v1.AthenzDomainSpec{
SignedDomain: getFakeDomain(),
}
item := &v1.AthenzDomain{
ObjectMeta: metav1.ObjectMeta{
Name: domainName,
},
Spec: spec,
}
return item
}
func getFakeDomain() zms.SignedDomain {
allow := zms.ALLOW
timestamp, err := rdl.TimestampParse("2019-06-21T19:28:09.305Z")
if err != nil {
panic(err)
}
return zms.SignedDomain{
Domain: &zms.DomainData{
Modified: timestamp,
Name: zms.DomainName(domainName),
Policies: &zms.SignedPolicies{
Contents: &zms.DomainPolicies{
Domain: zms.DomainName(domainName),
Policies: []*zms.Policy{
{
Assertions: []*zms.Assertion{
{
Role: domainName + ":role.admin",
Resource: domainName + ":*",
Action: "*",
Effect: &allow,
},
},
Modified: ×tamp,
Name: zms.ResourceName(domainName + ":policy.admin"),
},
{
Assertions: []*zms.Assertion{
{
Role: domainName + ":role.delegated",
Resource: domainName + ":services",
Action: "create",
Effect: &allow,
},
},
Modified: ×tamp,
Name: zms.ResourceName(domainName + ":policy.delegated"),
},
{
Assertions: []*zms.Assertion{
{
Role: domainName + ":role.invaliduser",
Resource: domainName + ":*",
Action: "*",
Effect: &allow,
},
},
Modified: ×tamp,
Name: zms.ResourceName(domainName + ":policy.invaliduser"),
},
},
},
KeyId: "col-env-1.1",
Signature: "signature-policy",
},
Roles: []*zms.Role{
{
Members: []zms.MemberName{zms.MemberName(username)},
Modified: ×tamp,
Name: zms.ResourceName(domainName + ":role.admin"),
RoleMembers: []*zms.RoleMember{
{
MemberName: zms.MemberName(username),
},
},
},
{
Name: zms.ResourceName(domainName + ":role.delegated"),
Trust: trustDomainName,
},
{
Name: zms.ResourceName(domainName + ":role.invaliduser"),
Trust: trustDomainName,
},
},
Services: []*zms.ServiceIdentity{},
Entities: []*zms.Entity{},
},
KeyId: "colo-env-1.1",
Signature: "signature",
}
}
func getFakeTrustAthenzDomain() *v1.AthenzDomain {
spec := v1.AthenzDomainSpec{
SignedDomain: getFakeTrustDomain(),
}
item := &v1.AthenzDomain{
ObjectMeta: metav1.ObjectMeta{
Name: trustDomainName,
},
Spec: spec,
}
return item
}
func getFakeTrustDomain() zms.SignedDomain {
allow := zms.ALLOW
timestamp, err := rdl.TimestampParse("2019-07-22T20:29:10.305Z")
if err != nil {
panic(err)
}
return zms.SignedDomain{
Domain: &zms.DomainData{
Modified: timestamp,
Name: zms.DomainName(trustDomainName),
Policies: &zms.SignedPolicies{
Contents: &zms.DomainPolicies{
Domain: zms.DomainName(trustDomainName),
Policies: []*zms.Policy{
{
Assertions: []*zms.Assertion{
{
Role: trustDomainName + ":role.admin",
Resource: "*:role.delegated",
Action: "assume_role",
Effect: &allow,
},
},
Modified: ×tamp,
Name: zms.ResourceName(trustDomainName + ":policy.admin"),
},
},
},
KeyId: "col-env-1.1",
Signature: "signature-policy",
},
Roles: []*zms.Role{
{
Members: []zms.MemberName{zms.MemberName(trustusername)},
Modified: ×tamp,
Name: zms.ResourceName(trustDomainName + ":role.admin"),
RoleMembers: []*zms.RoleMember{
{
MemberName: zms.MemberName(trustusername),
},
},
},
},
Services: []*zms.ServiceIdentity{},
Entities: []*zms.Entity{},
},
KeyId: "colo-env-1.1",
Signature: "signature",
}
}
func getFakeAthenzDomainWithExplicitDeny() *v1.AthenzDomain {
spec := v1.AthenzDomainSpec{
SignedDomain: getFakeDomainWithExplicitDeny(),
}
item := &v1.AthenzDomain{
ObjectMeta: metav1.ObjectMeta{
Name: domainWithDeny,
},
Spec: spec,
}
return item
}
func getFakeDomainWithExplicitDeny() zms.SignedDomain {
allow := zms.ALLOW
deny := zms.DENY
timestamp, err := rdl.TimestampParse("2020-02-17T20:29:10.305Z")
if err != nil {
panic(err)
}
return zms.SignedDomain{
Domain: &zms.DomainData{
Modified: timestamp,
Name: zms.DomainName(domainWithDeny),
Policies: &zms.SignedPolicies{
Contents: &zms.DomainPolicies{
Domain: zms.DomainName(domainWithDeny),
Policies: []*zms.Policy{
{
Assertions: []*zms.Assertion{
{
Role: domainWithDeny + ":role.admin",
Resource: domainWithDeny + ":*",
Action: "*",
Effect: &allow,
},
},
Modified: ×tamp,
Name: zms.ResourceName(domainWithDeny + ":policy.admin"),
},
{
Assertions: []*zms.Assertion{
{
Role: domainWithDeny + ":role.admin",
Resource: domainWithDeny + ":services",
Action: "delete",
Effect: &deny,
},
},
Modified: ×tamp,
Name: zms.ResourceName(domainWithDeny + ":policy.admin"),
},
},
},
KeyId: "col-env-1.1",
Signature: "signature-policy",
},
Roles: []*zms.Role{
{
Members: []zms.MemberName{zms.MemberName(username)},
Modified: ×tamp,
Name: zms.ResourceName(domainWithDeny + ":role.admin"),
RoleMembers: []*zms.RoleMember{
{
MemberName: zms.MemberName(username),
},
},
},
},
Services: []*zms.ServiceIdentity{},
Entities: []*zms.Entity{},
},
KeyId: "colo-env-1.1",
Signature: "signature",
}
}
func newCache() *Cache {
domainMap := make(map[string]roleMappings)
athenzclientset := fake.NewSimpleClientset()
crIndexInformer := athenzInformer.NewAthenzDomainInformer(athenzclientset, 0, cache.Indexers{})
k8sclientset := fake.NewSimpleClientset()
cmListWatcher := cache.NewListWatchFromClient(k8sclientset.AthenzV1().RESTClient(), "", "", fields.Everything())
cmIndexInformer := cache.NewSharedIndexInformer(cmListWatcher, &corev1.ConfigMap{}, time.Hour, cache.Indexers{})
c := &Cache{
crIndexInformer: crIndexInformer,
cmIndexInformer: cmIndexInformer,
domainMap: domainMap,
log: log.New(os.Stderr, "", log.LstdFlags),
}
roleToPrincipals := make(map[string][]*simplePrincipal)
roleToAllowAssertion := make(map[string][]*simpleAssertion)
roleToDenyAssertion := make(map[string][]*simpleAssertion)
crMap := roleMappings{
roleToPrincipals: roleToPrincipals,
roleToAllowAssertion: roleToAllowAssertion,
roleToDenyAssertion: roleToDenyAssertion,
}
c.crIndexInformer.GetStore().Add(ad.DeepCopy())
c.crIndexInformer.GetStore().Add(ad1.DeepCopy())
c.cmIndexInformer.GetStore().Add(cm.DeepCopy())
c.cmIndexInformer.GetStore().Add(cm1.DeepCopy())
c.domainMap[domainName] = crMap
c.domainMap[trustDomainName] = crMap
return c
}
func TestParseData(t *testing.T) {
c := newCache()
// load fake trust domain object
item := getFakeTrustAthenzDomain()
crMap, err := c.parseData(item)
if err != nil {
t.Error(err)
}
// if the map creation fails, report error
if len(crMap.roleToPrincipals) != 1 || crMap.roleToPrincipals[trustDomainName+":role.admin"] == nil {
t.Error("Failed to create RoleToPrincipals map")
}
item = getFakeAthenzDomain()
crMap, err = c.parseData(item)
if err != nil {
t.Error(err)
}
// if trust domain exist, it will pull the members from the delegated role
if len(crMap.roleToPrincipals) != 2 || crMap.roleToPrincipals["home.domain:role.admin"] == nil || crMap.roleToPrincipals["home.domain:role.delegated"] == nil {
t.Error("Failed to create RoleToPrincipals map")
}
if len(crMap.roleToAllowAssertion) != 3 || crMap.roleToPrincipals["home.domain:role.admin"] == nil || crMap.roleToPrincipals["home.domain:role.delegated"] == nil {
t.Error("Failed to create RoleToAssertion map")
}
}
func TestParseDataNilCase(t *testing.T) {
c := newCache()
item := getFakeAthenzDomain()
item.Spec.SignedDomain.Domain.Policies.Contents = nil
_, err := c.parseData(item)
if err.Error() != "One of AthenzDomain, Domain field in SignedDomain, Domain Policies field or Policies Contents is nil" {
t.Error("did not catch policies content nil")
}
item.Spec.SignedDomain.Domain.Policies = nil
_, err = c.parseData(item)
if err.Error() != "One of AthenzDomain, Domain field in SignedDomain, Domain Policies field or Policies Contents is nil" {
t.Error("did not catch policies nil")
}
item.Spec.SignedDomain.Domain = nil
_, err = c.parseData(item)
if err.Error() != "One of AthenzDomain, Domain field in SignedDomain, Domain Policies field or Policies Contents is nil" {
t.Error("did not catch Domain data nil")
}
_, err = c.parseData(nil)
if err.Error() != "One of AthenzDomain, Domain field in SignedDomain, Domain Policies field or Policies Contents is nil" {
t.Error("did not catch item nil")
}
}
func TestParseDataPrincipal(t *testing.T) {
c := newCache()
item := getFakeAthenzDomain()
// role has nil field or empty object
item.Spec.SignedDomain.Domain.Roles = []*zms.Role{
{},
nil,
}
crmap, err := c.parseData(item)
if err != nil {
t.Error(err)
}
if len(crmap.roleToPrincipals) != 0 {
t.Error("roleToPrincipal map should be empty since roles are empty or nil")
}
// roleMember is nil or roleMember name is nil
item.Spec.SignedDomain.Domain.Roles = []*zms.Role{
{
Members: []zms.MemberName{zms.MemberName(username)},
Name: zms.ResourceName(domainName + ":role.admin"),
RoleMembers: []*zms.RoleMember{
{
MemberName: zms.MemberName(""),
},
},
},
{
Members: []zms.MemberName{zms.MemberName(username)},
Name: zms.ResourceName(domainName + ":role.admin"),
RoleMembers: []*zms.RoleMember{
{},
nil,
},
},
}
crmap, err = c.parseData(item)
if err != nil {
t.Error(err)
}
if len(crmap.roleToPrincipals["home.domain:role.admin"]) != 0 {
t.Error("roleToPrincipal array should be empty since role members are empty")
}
// regex conversion fail
item.Spec.SignedDomain.Domain.Roles = []*zms.Role{
{
Members: []zms.MemberName{zms.MemberName(username)},
Name: zms.ResourceName(domainName + ":role.admin"),
RoleMembers: []*zms.RoleMember{
{
MemberName: zms.MemberName("/?([a-zA-Z0-9_+-\\s+]+)"),
},
},
},
}
crmap, err = c.parseData(item)
if err != nil {
t.Error(err)
}
if len(crmap.roleToPrincipals["home.domain:role.admin"]) != 0 {
t.Error("member shouldn't be added to the map because member name regex is invalid")
}
}
func TestParseDataPolicy(t *testing.T) {
c := newCache()
item := getFakeAthenzDomain()
// policy is nil
item.Spec.SignedDomain.Domain.Policies.Contents.Policies = []*zms.Policy{
{},
nil,
}
crmap, err := c.parseData(item)
if err != nil {
t.Error(err)
}
if len(crmap.roleToAllowAssertion) != 0 {
t.Error("map entries shouldn't be added because policies are nil or empty")
}
// assertion is nil
item.Spec.SignedDomain.Domain.Policies.Contents.Policies = []*zms.Policy{
{
Assertions: []*zms.Assertion{
{},
nil,
},
Name: zms.ResourceName(domainName + ":policy.admin"),
},
}
crmap, err = c.parseData(item)
if err != nil {
t.Error(err)
}
if len(crmap.roleToAllowAssertion) != 0 {
t.Error("map entries shouldn't be added because assertions are nil or empty")
}
}
func TestAddOrUpdateObj(t *testing.T) {
c := newCache()
item := getFakeAthenzDomain()
// add athenz domain
c.addOrUpdateObj(item)
obj, ok := c.domainMap[domainName]
if !ok {
t.Error("Failed to add AthenzDomain to domainMap")
}
if len(obj.roleToPrincipals["home.domain:role.admin"]) != 1 {
t.Error("Failed to add AthenzDomain to domainMap. RoleToPrincipals is empty.")
}
targetRegex, err := regexp.Compile("^user.name$")
if err != nil {
t.Error(err)
}
actualRegex := obj.roleToPrincipals["home.domain:role.admin"][0].memberRegex
if actualRegex.String() != targetRegex.String() {
t.Error("member added to the map does not match target regex")
}
// update athenz domains
item.Spec.Domain.Roles = []*zms.Role{
{
Members: []zms.MemberName{zms.MemberName(username)},
Name: zms.ResourceName(domainName + ":role.admin.test1"),
RoleMembers: []*zms.RoleMember{
{
MemberName: zms.MemberName(username),
Expiration: &rdl.Timestamp{
Time: time.Now().Add(time.Hour),
},
},
},
},
{
Members: []zms.MemberName{zms.MemberName(username + "1")},
Name: zms.ResourceName(domainName + ":role.admin.test2"),
RoleMembers: []*zms.RoleMember{
{
MemberName: zms.MemberName(username),
Expiration: &rdl.Timestamp{
Time: time.Now().Add(2 * time.Hour),
},
},
},
},
}
c.addOrUpdateObj(item)
crMap, ok := c.domainMap[domainName]
if !ok {
t.Error("Failed to keep AthenzDomain to domainMap")
}
if len(crMap.roleToPrincipals) != 2 {
t.Error("Failed to update AthenzDomain roles")
}
if crMap.roleToPrincipals["home.domain:role.admin.test1"][0].memberRegex.String() != targetRegex.String() {
t.Error("Unable to find correct member for role home.domain:role.admin.test1")
}
if crMap.roleToPrincipals["home.domain:role.admin.test2"][0].memberRegex.String() != targetRegex.String() {
t.Error("Unable to find correct member for role home.domain:role.admin.test2")
}
}
func TestDeleteObj(t *testing.T) {
c := newCache()
item := getFakeAthenzDomain()
c.deleteObj(item)
_, ok := c.domainMap[domainName]
if ok {
t.Error("Failed to delete AthenzDomain in domainMap")
}
}
func TestAuthorize(t *testing.T) {
privateCache := newCache()
item := getFakeAthenzDomain()
crMap, err := privateCache.parseData(item)
if err != nil {
t.Error(err)
}
privateCache.domainMap[domainName] = crMap
item = getFakeTrustAthenzDomain()
crMap, err = privateCache.parseData(item)
if err != nil {
t.Error(err)
}
privateCache.domainMap[trustDomainName] = crMap
item = getFakeAthenzDomainWithExplicitDeny()
crMap, err = privateCache.parseData(item)
if err != nil {
t.Error(err)
}
privateCache.domainMap[domainWithDeny] = crMap
// grant access
check := AthenzAccessCheck{
Action: "get",
Resource: "home.domain:pods",
}
res, err := privateCache.authorize(username, check)
if err != nil {
t.Error(err)
}
if !res {
t.Error("Wrong authorization result, authorization should pass.")
}
// deny trust role members access
res, err = privateCache.authorize(trustusername, check)
if err != nil {
t.Error(err)
}
if res {
t.Error("Wrong authorization result, authorization should not pass because trustuser only has access to create services.")
}
// grant trust role members access
check = AthenzAccessCheck{
Action: "create",
Resource: "home.domain:services",
}
res, err = privateCache.authorize(trustusername, check)
if err != nil {
t.Error(err)
}
if !res {
t.Error("Wrong authorization result, authorization should pass.")
}
// deny delegated user access because assume role for this member doesn't exist in trust domain
res, err = privateCache.authorize("invaliduser", check)
if err != nil {
t.Error(err)
}
if res {
t.Error("Wrong authorization result, authorization should not pass.")
}
// deny access
check = AthenzAccessCheck{
Action: "get",
Resource: "home.domain:pods",
}
res, err = privateCache.authorize("fakeclient", check)
if err != nil {
t.Error(err)
}
if res {
t.Error("Wrong authorization result, fakeclient's request should be denied")
}
// test case: one policy has two assertions, first assertion with explicitly Allow,
// second assertion with explicitly Deny.
check = AthenzAccessCheck{
Action: "delete",
Resource: "home.domain.deny:services",
}
res, err = privateCache.authorize(username, check)
if err != nil {
t.Error(err)
}
if res {
t.Error("Wrong authorization result, username's request should be denied since assertion has an explicit DENY")
}
check = AthenzAccessCheck{
Action: "create",
Resource: "home.domain.deny:services",
}
res, err = privateCache.authorize(username, check)
if err != nil {
t.Error(err)
}
if !res {
t.Error("Wrong authorization result, username's request should be allowed")
}
check = AthenzAccessCheck{
Action: "delete",
Resource: "domain.does.not.exist:services",
}
res, err = privateCache.authorize(username, check)
if err == nil {
t.Error("there should be error because such domain doesn't exist in the cache")
} else {
assert.Equal(t, err.Error(), "domain.does.not.exist does not exist in cache map")
}
check = AthenzAccessCheck{
Action: "delete",
Resource: "domain.wrong.format.services",
}
res, err = privateCache.authorize(username, check)
if err == nil {
t.Error("there should be error because resource string is invalid")
} else {
assert.Equal(t, err.Error(), "Error splitting domain name")
}
// check resource does not exist in cache
check = AthenzAccessCheck{
Action: "get",
Resource: "home.domain.test:pods",
}
res, err = privateCache.authorize(username, check)
if err.Error() != "home.domain.test does not exist in cache map" {
t.Error("should throw an error when domain does not exist in map")
}
// expired membership
check = AthenzAccessCheck{
Action: "get",
Resource: "home.domain.deny:pods",
}
item.Spec.Domain.Roles = []*zms.Role{
{
Members: []zms.MemberName{zms.MemberName(username)},
Name: zms.ResourceName(domainWithDeny + ":role.admin"),
RoleMembers: []*zms.RoleMember{
{
MemberName: zms.MemberName(username + "1"),
Expiration: &rdl.Timestamp{
Time: time.Now().Add(time.Duration(-10) * time.Hour),
},
},
},
},
}
privateCache.addOrUpdateObj(item)
res, err = privateCache.authorize(username+"1", check)
if err != nil {
t.Error(err)
}
if res {
t.Error("Wrong authorization result. Membership has expired")
}
// disabled role principal
systemDisabled := int32(1)
check = AthenzAccessCheck{
Action: "get",
Resource: "home.domain.deny:pods",
}
item.Spec.SignedDomain.Domain.Roles = []*zms.Role{
{
Members: []zms.MemberName{zms.MemberName(username + "2")},
Name: zms.ResourceName(domainWithDeny + ":role.admin"),
RoleMembers: []*zms.RoleMember{
{
MemberName: zms.MemberName(username + "2"),
Expiration: &rdl.Timestamp{
Time: time.Now().Add(time.Duration(6) * time.Hour),
},
SystemDisabled: &systemDisabled,
},
},
},
}
privateCache.addOrUpdateObj(item)
res, err = privateCache.authorize(username+"2", check)
if err != nil {
t.Error(err)
}
if res {
t.Error("Wrong authorization result. Member has been disabled by system.")
}
}
func TestCheckUpdateTime(t *testing.T) {
privateCache := newCache()
privateCache.maxContactTime = 2 * time.Hour
// wrong cm input, should give error
err := privateCache.parseUpdateTime(cm0)
if err == nil {
t.Error("parseUpdateTime function should return error")
} else {
assert.Equal(t, err.Error(), "timestamp format in syncer config map is wrong")
}
// check if last update time is less than 2 hrs
err = privateCache.parseUpdateTime(cm)
if err != nil {
t.Error("parseUpdateTime function should not return error")
}
assert.Equal(t, privateCache.cacheStatus, true)
// check if last update is more than 2 hrs
err = privateCache.parseUpdateTime(cm1)
if err != nil {
t.Error("parseUpdateTime function should not return error")
}
assert.Equal(t, privateCache.cacheStatus, false)
}