@@ -22,6 +22,7 @@ import (
22
22
"net"
23
23
"os"
24
24
"strings"
25
+ "sync/atomic"
25
26
"testing"
26
27
"time"
27
28
@@ -31,6 +32,7 @@ import (
31
32
"google.golang.org/grpc/credentials/insecure"
32
33
"google.golang.org/protobuf/types/known/timestamppb"
33
34
35
+ "k8s.io/apimachinery/pkg/util/wait"
34
36
externaljwtv1alpha1 "k8s.io/externaljwt/apis/v1alpha1"
35
37
"k8s.io/kubernetes/pkg/serviceaccount"
36
38
)
@@ -167,7 +169,7 @@ func TestExternalPublicKeyGetter(t *testing.T) {
167
169
t .Run (tc .desc , func (t * testing.T ) {
168
170
ctx := context .Background ()
169
171
170
- sockname := fmt .Sprintf ("@test-external-public-key-getter-%d.sock" , i )
172
+ sockname := fmt .Sprintf ("@test-external-public-key-getter-%d-%d .sock" , time . Now (). Nanosecond () , i )
171
173
t .Cleanup (func () { _ = os .Remove (sockname ) })
172
174
173
175
addr := & net.UnixAddr {Name : sockname , Net : "unix" }
@@ -238,7 +240,7 @@ func TestExternalPublicKeyGetter(t *testing.T) {
238
240
func TestInitialFill (t * testing.T ) {
239
241
ctx := context .Background ()
240
242
241
- sockname := "@test-initial-fill.sock"
243
+ sockname := fmt . Sprintf ( "@test-initial-fill-%d .sock" , time . Now (). Nanosecond ())
242
244
t .Cleanup (func () { _ = os .Remove (sockname ) })
243
245
244
246
addr := & net.UnixAddr {Name : sockname , Net : "unix" }
@@ -304,7 +306,7 @@ func TestInitialFill(t *testing.T) {
304
306
func TestReflectChanges (t * testing.T ) {
305
307
ctx := context .Background ()
306
308
307
- sockname := "@test-reflect-changes.sock"
309
+ sockname := fmt . Sprintf ( "@test-reflect-changes-%d .sock" , time . Now (). Nanosecond ())
308
310
t .Cleanup (func () { _ = os .Remove (sockname ) })
309
311
310
312
addr := & net.UnixAddr {Name : sockname , Net : "unix" }
@@ -357,18 +359,25 @@ func TestReflectChanges(t *testing.T) {
357
359
358
360
plugin := newPlugin ("iss" , clientConn , true )
359
361
362
+ dummyListener := & dummyListener {}
363
+ plugin .keyCache .AddListener (dummyListener )
364
+
365
+ dummyListener .waitForCount (t , 0 )
360
366
if err := plugin .keyCache .initialFill (ctx ); err != nil {
361
367
t .Fatalf ("Error during InitialFill: %v" , err )
362
368
}
369
+ dummyListener .waitForCount (t , 1 )
363
370
364
371
gotPubKeysT1 := plugin .keyCache .GetPublicKeys (ctx , "" )
365
372
if diff := cmp .Diff (gotPubKeysT1 , wantPubKeysT1 , cmpopts .SortSlices (sortPublicKeySlice )); diff != "" {
366
373
t .Fatalf ("Bad public keys; diff (-got +want)\n %s" , diff )
367
374
}
368
375
369
- if _ , err := plugin .keyCache .syncKeys (ctx ); err != nil {
376
+ dummyListener .waitForCount (t , 1 )
377
+ if err := plugin .keyCache .syncKeys (ctx ); err != nil {
370
378
t .Fatalf ("Error while calling syncKeys: %v" , err )
371
379
}
380
+ dummyListener .waitForCount (t , 1 )
372
381
373
382
supportedKeysT2 := map [string ]supportedKeyT {
374
383
"key-1" : {
@@ -396,12 +405,108 @@ func TestReflectChanges(t *testing.T) {
396
405
backend .supportedKeys = supportedKeysT2
397
406
backend .keyLock .Unlock ()
398
407
399
- if _ , err := plugin .keyCache .syncKeys (ctx ); err != nil {
408
+ dummyListener .waitForCount (t , 1 )
409
+ if err := plugin .keyCache .syncKeys (ctx ); err != nil {
400
410
t .Fatalf ("Error while calling syncKeys: %v" , err )
401
411
}
412
+ dummyListener .waitForCount (t , 2 )
402
413
403
414
gotPubKeysT2 := plugin .keyCache .GetPublicKeys (ctx , "" )
404
415
if diff := cmp .Diff (gotPubKeysT2 , wantPubKeysT2 , cmpopts .SortSlices (sortPublicKeySlice )); diff != "" {
405
416
t .Fatalf ("Bad public keys; diff (-got +want)\n %s" , diff )
406
417
}
418
+ dummyListener .waitForCount (t , 2 )
419
+ }
420
+
421
+ type dummyListener struct {
422
+ count atomic.Int64
423
+ }
424
+
425
+ func (d * dummyListener ) waitForCount (t * testing.T , expect int ) {
426
+ t .Helper ()
427
+ err := wait .PollUntilContextTimeout (context .Background (), time .Millisecond , 10 * time .Second , true , func (_ context.Context ) (bool , error ) {
428
+ actual := int (d .count .Load ())
429
+ switch {
430
+ case actual > expect :
431
+ return false , fmt .Errorf ("expected %d broadcasts, got %d broadcasts" , expect , actual )
432
+ case actual == expect :
433
+ return true , nil
434
+ default :
435
+ t .Logf ("expected %d broadcasts, got %d broadcasts, waiting..." , expect , actual )
436
+ return false , nil
437
+ }
438
+ })
439
+ if err != nil {
440
+ t .Fatal (err )
441
+ }
442
+ }
443
+
444
+ func (d * dummyListener ) Enqueue () {
445
+ d .count .Add (1 )
446
+ }
447
+
448
+ func TestKeysChanged (t * testing.T ) {
449
+ testcases := []struct {
450
+ name string
451
+ oldKeys VerificationKeys
452
+ newKeys VerificationKeys
453
+ expect bool
454
+ }{
455
+ {
456
+ name : "empty" ,
457
+ oldKeys : VerificationKeys {},
458
+ newKeys : VerificationKeys {},
459
+ expect : false ,
460
+ },
461
+ {
462
+ name : "identical" ,
463
+ oldKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "a" }, {KeyID : "b" }}},
464
+ newKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "a" }, {KeyID : "b" }}},
465
+ expect : false ,
466
+ },
467
+ {
468
+ name : "changed datatimestamp" ,
469
+ oldKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "a" }, {KeyID : "b" }}},
470
+ newKeys : VerificationKeys {DataTimestamp : time .Unix (1001 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "a" }, {KeyID : "b" }}},
471
+ expect : true ,
472
+ },
473
+ {
474
+ name : "reordered keyid" ,
475
+ oldKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "a" }, {KeyID : "b" }}},
476
+ newKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "b" }, {KeyID : "a" }}},
477
+ expect : true ,
478
+ },
479
+ {
480
+ name : "changed keyid" ,
481
+ oldKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "a" }}},
482
+ newKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "b" }}},
483
+ expect : true ,
484
+ },
485
+ {
486
+ name : "added key" ,
487
+ oldKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "a" }}},
488
+ newKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "a" }, {KeyID : "b" }}},
489
+ expect : true ,
490
+ },
491
+ {
492
+ name : "removed key" ,
493
+ oldKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "a" }, {KeyID : "b" }}},
494
+ newKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "a" }}},
495
+ expect : true ,
496
+ },
497
+ {
498
+ name : "changed oidc" ,
499
+ oldKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "a" , ExcludeFromOIDCDiscovery : false }}},
500
+ newKeys : VerificationKeys {DataTimestamp : time .Unix (1000 , 0 ), Keys : []serviceaccount.PublicKey {{KeyID : "a" , ExcludeFromOIDCDiscovery : true }}},
501
+ expect : true ,
502
+ },
503
+ }
504
+ for _ , tc := range testcases {
505
+ t .Run (tc .name , func (t * testing.T ) {
506
+ result := keysChanged (& tc .oldKeys , & tc .newKeys )
507
+ if result != tc .expect {
508
+ t .Errorf ("got %v, expected %v" , result , tc .expect )
509
+ }
510
+ })
511
+ }
407
512
}
0 commit comments