@@ -12,7 +12,9 @@ import (
12
12
13
13
apierrs "k8s.io/apimachinery/pkg/api/errors"
14
14
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15
+ "k8s.io/apimachinery/pkg/labels"
15
16
"k8s.io/apimachinery/pkg/runtime"
17
+ "k8s.io/apiserver/pkg/authentication/authenticator"
16
18
"k8s.io/apiserver/pkg/authentication/user"
17
19
clienttesting "k8s.io/client-go/testing"
18
20
@@ -21,6 +23,7 @@ import (
21
23
"github.com/openshift/origin/pkg/auth/userregistry/identitymapper"
22
24
oapi "github.com/openshift/origin/pkg/oauth/apis/oauth"
23
25
oauthfake "github.com/openshift/origin/pkg/oauth/generated/internalclientset/fake"
26
+ oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset/typed/oauth/internalversion"
24
27
"github.com/openshift/origin/pkg/oauth/server/osinserver"
25
28
"github.com/openshift/origin/pkg/oauth/server/osinserver/registrystorage"
26
29
userapi "github.com/openshift/origin/pkg/user/apis/user"
@@ -287,7 +290,7 @@ func TestRegistryAndServer(t *testing.T) {
287
290
objs = append (objs , testCase .ClientAuth )
288
291
}
289
292
fakeOAuthClient := oauthfake .NewSimpleClientset (objs ... )
290
- storage := registrystorage .New (fakeOAuthClient .Oauth ().OAuthAccessTokens (), fakeOAuthClient .Oauth ().OAuthAuthorizeTokens (), fakeOAuthClient .Oauth ().OAuthClients (), NewUserConversion ())
293
+ storage := registrystorage .New (fakeOAuthClient .Oauth ().OAuthAccessTokens (), fakeOAuthClient .Oauth ().OAuthAuthorizeTokens (), fakeOAuthClient .Oauth ().OAuthClients (), NewUserConversion (), 0 )
291
294
config := osinserver .NewDefaultServerConfig ()
292
295
293
296
h .AuthorizeHandler = osinserver.AuthorizeHandlers {
@@ -473,3 +476,204 @@ func TestAuthenticateTokenValidated(t *testing.T) {
473
476
t .Error ("Did not get a user!" )
474
477
}
475
478
}
479
+
480
+ type fakeOAuthClientLister struct {
481
+ clients oauthclient.OAuthClientInterface
482
+ }
483
+
484
+ func (f fakeOAuthClientLister ) Get (name string ) (* oapi.OAuthClient , error ) {
485
+ return f .clients .Get (name , metav1.GetOptions {})
486
+ }
487
+
488
+ func (f fakeOAuthClientLister ) List (selector labels.Selector ) ([]* oapi.OAuthClient , error ) {
489
+ var list []* oapi.OAuthClient
490
+ ret , _ := f .clients .List (metav1.ListOptions {})
491
+ for i := range ret .Items {
492
+ list = append (list , & ret .Items [i ])
493
+ }
494
+ return list , nil
495
+ }
496
+
497
+ func checkToken (t * testing.T , name string , authf authenticator.Token , tokens oauthclient.OAuthAccessTokenInterface , present bool ) {
498
+ userInfo , found , err := authf .AuthenticateToken (name )
499
+ if present {
500
+ if ! found {
501
+ t .Errorf ("Did not find token %s!" , name )
502
+ }
503
+ if err != nil {
504
+ t .Errorf ("Unexpected error checking for token %s: %v" , name , err )
505
+ }
506
+ if userInfo == nil {
507
+ t .Errorf ("Did not get a user for token %s!" , name )
508
+ }
509
+ } else {
510
+ if found {
511
+ token , _ := tokens .Get (name , metav1.GetOptions {})
512
+ t .Errorf ("Found token (created=%s, timeout=%di, now=%s), but it should be gone!" ,
513
+ token .CreationTimestamp , token .InactivityTimeoutSeconds , time .Now ())
514
+ }
515
+ if err != errTimedout {
516
+ t .Errorf ("Unexpected error checking absence of token %s: %v" , name , err )
517
+ }
518
+ if userInfo != nil {
519
+ t .Errorf ("Unexpected user checking absence of token %s: %v" , name , userInfo )
520
+ }
521
+ }
522
+ }
523
+
524
+ func TestAuthenticateTokenTimeout (t * testing.T ) {
525
+ stopCh := make (chan struct {})
526
+ defer close (stopCh )
527
+
528
+ defaultTimeout := int32 (30 ) // 30 seconds
529
+ clientTimeout := int32 (15 ) // 15 seconds so flush -> 5 seconds
530
+ minTimeout := int32 (10 ) // 10 seconds
531
+
532
+ testClient := oapi.OAuthClient {
533
+ ObjectMeta : metav1.ObjectMeta {Name : "testClient" },
534
+ AccessTokenInactivityTimeoutSeconds : & clientTimeout ,
535
+ }
536
+ quickClient := oapi.OAuthClient {
537
+ ObjectMeta : metav1.ObjectMeta {Name : "quickClient" },
538
+ AccessTokenInactivityTimeoutSeconds : & minTimeout ,
539
+ }
540
+ slowClient := oapi.OAuthClient {
541
+ ObjectMeta : metav1.ObjectMeta {Name : "slowClient" },
542
+ }
543
+ testToken := oapi.OAuthAccessToken {
544
+ ObjectMeta : metav1.ObjectMeta {Name : "testToken" , CreationTimestamp : metav1.Time {Time : time .Now ()}},
545
+ ClientName : "testClient" ,
546
+ ExpiresIn : 600 , // 10 minutes
547
+ UserName : "foo" ,
548
+ UserUID : string ("bar" ),
549
+ InactivityTimeoutSeconds : clientTimeout ,
550
+ }
551
+ quickToken := oapi.OAuthAccessToken {
552
+ ObjectMeta : metav1.ObjectMeta {Name : "quickToken" , CreationTimestamp : metav1.Time {Time : time .Now ()}},
553
+ ClientName : "quickClient" ,
554
+ ExpiresIn : 600 , // 10 minutes
555
+ UserName : "foo" ,
556
+ UserUID : string ("bar" ),
557
+ InactivityTimeoutSeconds : minTimeout ,
558
+ }
559
+ slowToken := oapi.OAuthAccessToken {
560
+ ObjectMeta : metav1.ObjectMeta {Name : "slowToken" , CreationTimestamp : metav1.Time {Time : time .Now ()}},
561
+ ClientName : "slowClient" ,
562
+ ExpiresIn : 600 , // 10 minutes
563
+ UserName : "foo" ,
564
+ UserUID : string ("bar" ),
565
+ InactivityTimeoutSeconds : defaultTimeout ,
566
+ }
567
+ emergToken := oapi.OAuthAccessToken {
568
+ ObjectMeta : metav1.ObjectMeta {Name : "emergToken" , CreationTimestamp : metav1.Time {Time : time .Now ()}},
569
+ ClientName : "quickClient" ,
570
+ ExpiresIn : 600 , // 10 minutes
571
+ UserName : "foo" ,
572
+ UserUID : string ("bar" ),
573
+ InactivityTimeoutSeconds : 5 , // super short timeout
574
+ }
575
+ fakeOAuthClient := oauthfake .NewSimpleClientset (& testToken , & quickToken , & slowToken , & emergToken , & testClient , & quickClient , & slowClient )
576
+ userRegistry := usertest .NewUserRegistry ()
577
+ userRegistry .GetUsers ["foo" ] = & userapi.User {ObjectMeta : metav1.ObjectMeta {UID : "bar" }}
578
+ accessTokenGetter := fakeOAuthClient .Oauth ().OAuthAccessTokens ()
579
+ oauthClients := fakeOAuthClient .Oauth ().OAuthClients ()
580
+ lister := & fakeOAuthClientLister {
581
+ clients : oauthClients ,
582
+ }
583
+ timeouts := NewTimeoutValidator (accessTokenGetter , lister , defaultTimeout , minTimeout )
584
+ tokenAuthenticator := NewTokenAuthenticator (accessTokenGetter , userRegistry , identitymapper.NoopGroupMapper {}, timeouts )
585
+
586
+ go timeouts .Run (stopCh )
587
+
588
+ // wait to see that the other thread has updated the timeouts
589
+ time .Sleep (1 * time .Second )
590
+
591
+ // TIME: 1 seconds have passed here
592
+
593
+ // first time should succeed for all
594
+ checkToken (t , "testToken" , tokenAuthenticator , accessTokenGetter , true )
595
+ checkToken (t , "quickToken" , tokenAuthenticator , accessTokenGetter , true )
596
+ checkToken (t , "slowToken" , tokenAuthenticator , accessTokenGetter , true )
597
+ // this should cause an emergency flush, if not the next auth will fail,
598
+ // as the token will be timed out
599
+ checkToken (t , "emergToken" , tokenAuthenticator , accessTokenGetter , true )
600
+
601
+ // wait 5 seconds
602
+ time .Sleep (5 * time .Second )
603
+
604
+ // TIME: 6th second
605
+
606
+ // See if emergency flush happened
607
+ checkToken (t , "emergToken" , tokenAuthenticator , accessTokenGetter , true )
608
+
609
+ // wait for timeout (minTimeout + 1 - the previously waited 6 seconds)
610
+ time .Sleep (time .Duration (minTimeout - 5 ) * time .Second )
611
+
612
+ // TIME: 11th second
613
+
614
+ // now we change the testClient and see if the testToken will still be
615
+ // valid instead of timing out
616
+ changeClient , ret := oauthClients .Get ("testClient" , metav1.GetOptions {})
617
+ if ret != nil {
618
+ t .Error ("Failed to get testClient" )
619
+ } else {
620
+ longTimeout := int32 (20 )
621
+ changeClient .AccessTokenInactivityTimeoutSeconds = & longTimeout
622
+ _ , ret = oauthClients .Update (changeClient )
623
+ if ret != nil {
624
+ t .Error ("Failed to update testClient" )
625
+ }
626
+ }
627
+
628
+ // this should fail
629
+ checkToken (t , "quickToken" , tokenAuthenticator , accessTokenGetter , false )
630
+ // while this should get updated
631
+ checkToken (t , "testToken" , tokenAuthenticator , accessTokenGetter , true )
632
+
633
+ // wait for timeout
634
+ time .Sleep (time .Duration (clientTimeout + 1 ) * time .Second )
635
+
636
+ // TIME: 27th second
637
+
638
+ // this should get updated
639
+ checkToken (t , "slowToken" , tokenAuthenticator , accessTokenGetter , true )
640
+
641
+ // while this should not fail
642
+ checkToken (t , "testToken" , tokenAuthenticator , accessTokenGetter , true )
643
+ // and should be updated to last at least till the 31st second
644
+ token , err := accessTokenGetter .Get ("testToken" , metav1.GetOptions {})
645
+ if err != nil {
646
+ t .Error ("Failed to get testToken" )
647
+ } else {
648
+ if token .InactivityTimeoutSeconds < 31 {
649
+ t .Errorf ("Expected timeout in more than 31 seconds, found: %d" , token .InactivityTimeoutSeconds )
650
+ }
651
+ }
652
+
653
+ //now change testClient again, so that tokens do not expire anymore
654
+ changeclient , ret := oauthClients .Get ("testClient" , metav1.GetOptions {})
655
+ if ret != nil {
656
+ t .Error ("Failed to get testClient" )
657
+ } else {
658
+ changeclient .AccessTokenInactivityTimeoutSeconds = new (int32 )
659
+ _ , ret = oauthClients .Update (changeclient )
660
+ if ret != nil {
661
+ t .Error ("Failed to update testClient" )
662
+ }
663
+ }
664
+
665
+ // and wait until test token should time out, and has been flushed for sure
666
+ time .Sleep (time .Duration (minTimeout ) * time .Second )
667
+
668
+ // while this should not fail
669
+ checkToken (t , "testToken" , tokenAuthenticator , accessTokenGetter , true )
670
+ // and should be updated to have a ZERO timeout
671
+ token , err = accessTokenGetter .Get ("testToken" , metav1.GetOptions {})
672
+ if err != nil {
673
+ t .Error ("Failed to get testToken" )
674
+ } else {
675
+ if token .InactivityTimeoutSeconds != 0 {
676
+ t .Errorf ("Expected timeout of 0 seconds, found: %d" , token .InactivityTimeoutSeconds )
677
+ }
678
+ }
679
+ }
0 commit comments