@@ -23,11 +23,11 @@ import (
23
23
"errors"
24
24
"io/ioutil"
25
25
"net/http"
26
- "reflect"
27
26
"sort"
28
27
"testing"
29
28
"time"
30
29
30
+ "github.com/google/go-cmp/cmp"
31
31
"k8s.io/apimachinery/pkg/util/sets"
32
32
"k8s.io/apiserver/pkg/authentication/authenticator"
33
33
"k8s.io/apiserver/pkg/authentication/user"
@@ -581,9 +581,8 @@ func TestX509(t *testing.T) {
581
581
Opts x509.VerifyOptions
582
582
User UserConversion
583
583
584
- ExpectUserName string
585
- ExpectGroups []string
586
584
ExpectOK bool
585
+ ExpectResponse * authenticator.Response
587
586
ExpectErr bool
588
587
}{
589
588
"non-tls" : {
@@ -618,21 +617,35 @@ func TestX509(t *testing.T) {
618
617
Certs : getCerts (t , serverCert ),
619
618
User : CommonNameUserConversion ,
620
619
621
- ExpectUserName : "127.0.0.1" ,
622
- ExpectGroups : []string {"My Org" },
623
- ExpectOK : true ,
624
- ExpectErr : false ,
620
+ ExpectOK : true ,
621
+ ExpectResponse : & authenticator.Response {
622
+ User : & user.DefaultInfo {
623
+ Name : "127.0.0.1" ,
624
+ Groups : []string {"My Org" },
625
+ Extra : map [string ][]string {
626
+ user .CredentialIDKey : {"X509SHA256=92209d1e0dd36a018f244f5e1b88e2d47b049e9cfcd4b7c87c65875866872230" },
627
+ },
628
+ },
629
+ },
630
+ ExpectErr : false ,
625
631
},
626
632
627
633
"common name" : {
628
634
Opts : getDefaultVerifyOptions (t ),
629
635
Certs : getCerts (t , clientCNCert ),
630
636
User : CommonNameUserConversion ,
631
637
632
- ExpectUserName : "client_cn" ,
633
- ExpectGroups : []string {"My Org" },
634
- ExpectOK : true ,
635
- ExpectErr : false ,
638
+ ExpectOK : true ,
639
+ ExpectResponse : & authenticator.Response {
640
+ User : & user.DefaultInfo {
641
+ Name : "client_cn" ,
642
+ Groups : []string {"My Org" },
643
+ Extra : map [string ][]string {
644
+ user .CredentialIDKey : {"X509SHA256=dd0a6a295055fa94455c522b0d54ef0499186f454a7cf978b8b346dc35b254f7" },
645
+ },
646
+ },
647
+ },
648
+ ExpectErr : false ,
636
649
},
637
650
"ca with multiple organizations" : {
638
651
Opts : x509.VerifyOptions {
@@ -641,10 +654,17 @@ func TestX509(t *testing.T) {
641
654
Certs : getCerts (t , caWithGroups ),
642
655
User : CommonNameUserConversion ,
643
656
644
- ExpectUserName : "ROOT CA WITH GROUPS" ,
645
- ExpectGroups : []string {"My Org" , "My Org 1" , "My Org 2" },
646
- ExpectOK : true ,
647
- ExpectErr : false ,
657
+ ExpectOK : true ,
658
+ ExpectResponse : & authenticator.Response {
659
+ User : & user.DefaultInfo {
660
+ Name : "ROOT CA WITH GROUPS" ,
661
+ Groups : []string {"My Org" , "My Org 1" , "My Org 2" },
662
+ Extra : map [string ][]string {
663
+ user .CredentialIDKey : {"X509SHA256=6f337bb6576b6f942bd5ac5256f621e352aa7b34d971bda9b8f8981f51bba456" },
664
+ },
665
+ },
666
+ },
667
+ ExpectErr : false ,
648
668
},
649
669
650
670
"custom conversion error" : {
@@ -664,9 +684,13 @@ func TestX509(t *testing.T) {
664
684
return & authenticator.Response {User : & user.DefaultInfo {Name : "custom" }}, true , nil
665
685
}),
666
686
667
- ExpectUserName : "custom" ,
668
- ExpectOK : true ,
669
- ExpectErr : false ,
687
+ ExpectOK : true ,
688
+ ExpectResponse : & authenticator.Response {
689
+ User : & user.DefaultInfo {
690
+ Name : "custom" ,
691
+ },
692
+ },
693
+ ExpectErr : false ,
670
694
},
671
695
672
696
"future cert" : {
@@ -697,9 +721,16 @@ func TestX509(t *testing.T) {
697
721
Certs : getCertsFromFile (t , "client-valid" , "intermediate" ),
698
722
User : CommonNameUserConversion ,
699
723
700
- ExpectUserName : "My Client" ,
701
- ExpectOK : true ,
702
- ExpectErr : false ,
724
+ ExpectOK : true ,
725
+ ExpectResponse : & authenticator.Response {
726
+ User : & user.DefaultInfo {
727
+ Name : "My Client" ,
728
+ Extra : map [string ][]string {
729
+ user .CredentialIDKey : {"X509SHA256=794b0529fd1a72d55d52d98be9bab5b822d16f9ae86c4373fa7beee3cafe8582" },
730
+ },
731
+ },
732
+ },
733
+ ExpectErr : false ,
703
734
},
704
735
"multi-level, expired" : {
705
736
Opts : multilevelOpts ,
@@ -712,42 +743,36 @@ func TestX509(t *testing.T) {
712
743
}
713
744
714
745
for k , testCase := range testCases {
715
- req , _ := http .NewRequest ("GET" , "/" , nil )
716
- if ! testCase .Insecure {
717
- req .TLS = & tls.ConnectionState {PeerCertificates : testCase .Certs }
718
- }
719
-
720
- // this effectively tests the simple dynamic verify function.
721
- a := New (testCase .Opts , testCase .User )
746
+ t .Run (k , func (t * testing.T ) {
747
+ req , _ := http .NewRequest ("GET" , "/" , nil )
748
+ if ! testCase .Insecure {
749
+ req .TLS = & tls.ConnectionState {PeerCertificates : testCase .Certs }
750
+ }
722
751
723
- resp , ok , err := a .AuthenticateRequest (req )
752
+ // this effectively tests the simple dynamic verify function.
753
+ a := New (testCase .Opts , testCase .User )
724
754
725
- if testCase .ExpectErr && err == nil {
726
- t .Errorf ("%s: Expected error, got none" , k )
727
- continue
728
- }
729
- if ! testCase .ExpectErr && err != nil {
730
- t .Errorf ("%s: Got unexpected error: %v" , k , err )
731
- continue
732
- }
755
+ resp , ok , err := a .AuthenticateRequest (req )
733
756
734
- if testCase .ExpectOK != ok {
735
- t .Errorf ("%s: Expected ok=%v, got %v" , k , testCase .ExpectOK , ok )
736
- continue
737
- }
757
+ if testCase .ExpectErr && err == nil {
758
+ t .Fatalf ("Expected error, got none" )
759
+ }
760
+ if ! testCase .ExpectErr && err != nil {
761
+ t .Fatalf ("Got unexpected error: %v" , err )
762
+ }
738
763
739
- if testCase .ExpectOK {
740
- if testCase .ExpectUserName != resp .User .GetName () {
741
- t .Errorf ("%s: Expected user.name=%v, got %v" , k , testCase .ExpectUserName , resp .User .GetName ())
764
+ if testCase .ExpectOK != ok {
765
+ t .Fatalf ("Expected ok=%v, got %v" , testCase .ExpectOK , ok )
742
766
}
743
767
744
- groups := resp .User .GetGroups ()
745
- sort .Strings (testCase .ExpectGroups )
746
- sort .Strings (groups )
747
- if ! reflect .DeepEqual (testCase .ExpectGroups , groups ) {
748
- t .Errorf ("%s: Expected user.groups=%v, got %v" , k , testCase .ExpectGroups , groups )
768
+ if testCase .ExpectOK {
769
+ sort .Strings (testCase .ExpectResponse .User .GetGroups ())
770
+ sort .Strings (resp .User .GetGroups ())
771
+ if diff := cmp .Diff (testCase .ExpectResponse , resp ); diff != "" {
772
+ t .Errorf ("Bad response; diff (-want +got)\n %s" , diff )
773
+ }
749
774
}
750
- }
775
+ })
751
776
}
752
777
}
753
778
0 commit comments