@@ -23,6 +23,7 @@ import (
23
23
"io/ioutil"
24
24
"net/http"
25
25
"net/http/httputil"
26
+ "reflect"
26
27
"strings"
27
28
"testing"
28
29
"time"
@@ -680,3 +681,91 @@ func TestBootstrapping(t *testing.T) {
680
681
}
681
682
t .Errorf ("error bootstrapping roles: %s" , string (healthBytes ))
682
683
}
684
+
685
+ // TestDiscoveryUpgradeBootstrapping is primarily meant to test the behavior of
686
+ // primePublicInfoClusterRoleBinding in storage_rbac.go during cluster upgrades.
687
+ func TestDiscoveryUpgradeBootstrapping (t * testing.T ) {
688
+ var tearDownFn func ()
689
+ defer func () {
690
+ if tearDownFn != nil {
691
+ tearDownFn ()
692
+ }
693
+ }()
694
+
695
+ superUser := "admin/system:masters"
696
+
697
+ masterConfig := framework .NewIntegrationTestMasterConfig ()
698
+ masterConfig .GenericConfig .Authorization .Authorizer = newRBACAuthorizer (masterConfig )
699
+ masterConfig .GenericConfig .Authentication .Authenticator = bearertoken .New (tokenfile .New (map [string ]* user.DefaultInfo {
700
+ superUser : {Name : "admin" , Groups : []string {"system:masters" }},
701
+ }))
702
+ _ , s , tearDownFn := framework .RunAMaster (masterConfig )
703
+
704
+ client := clientset .NewForConfigOrDie (& restclient.Config {BearerToken : superUser , Host : s .URL , ContentConfig : restclient.ContentConfig {GroupVersion : testapi .Groups [api .GroupName ].GroupVersion ()}})
705
+
706
+ // Modify the default RBAC discovery ClusterRoleBidnings to look more like the defaults that
707
+ // existed prior to v1.14, but with user modifications.
708
+ t .Logf ("Modifying default `system:discovery` ClusterRoleBinding" )
709
+ discRoleBinding , err := client .Rbac ().ClusterRoleBindings ().Get ("system:discovery" , metav1.GetOptions {})
710
+ discRoleBinding .Annotations ["rbac.authorization.kubernetes.io/autoupdate" ] = "false"
711
+ discRoleBinding .Annotations ["rbac-discovery-upgrade-test" ] = "pass"
712
+ discRoleBinding .Subjects = []rbacapi.Subject {
713
+ {
714
+ Name : "system:authenticated" ,
715
+ Kind : "Group" ,
716
+ APIGroup : "rbac.authorization.k8s.io" ,
717
+ },
718
+ }
719
+ if discRoleBinding , err = client .Rbac ().ClusterRoleBindings ().Update (discRoleBinding ); err != nil {
720
+ t .Fatalf ("Failed to update `system:discovery` ClusterRoleBinding: %v" , err )
721
+ }
722
+ t .Logf ("Modifying default `system:basic-user` ClusterRoleBinding" )
723
+ basicUserRoleBinding , err := client .Rbac ().ClusterRoleBindings ().Get ("system:basic-user" , metav1.GetOptions {})
724
+ basicUserRoleBinding .Annotations ["rbac.authorization.kubernetes.io/autoupdate" ] = "false"
725
+ basicUserRoleBinding .Annotations ["rbac-discovery-upgrade-test" ] = "pass"
726
+ if basicUserRoleBinding , err = client .Rbac ().ClusterRoleBindings ().Update (basicUserRoleBinding ); err != nil {
727
+ t .Fatalf ("Failed to update `system:basic-user` ClusterRoleBinding: %v" , err )
728
+ }
729
+ t .Logf ("Deleting default `system:public-info-viewer` ClusterRoleBinding" )
730
+ if err = client .Rbac ().ClusterRoleBindings ().Delete ("system:public-info-viewer" , & metav1.DeleteOptions {}); err != nil {
731
+ t .Fatalf ("Failed to delete `system:public-info-viewer` ClusterRoleBinding: %v" , err )
732
+ }
733
+
734
+ // Stop the first API server.
735
+ tearDownFn ()
736
+ tearDownFn = nil
737
+
738
+ // Check that upgraded API servers inherit `system:public-info-viewer` settings from
739
+ // `system:discovery`, and respect auto-reconciliation annotations.
740
+ _ , s , tearDownFn = framework .RunAMaster (masterConfig )
741
+
742
+ client = clientset .NewForConfigOrDie (& restclient.Config {BearerToken : superUser , Host : s .URL , ContentConfig : restclient.ContentConfig {GroupVersion : testapi .Groups [api .GroupName ].GroupVersion ()}})
743
+
744
+ newDiscRoleBinding , err := client .Rbac ().ClusterRoleBindings ().Get ("system:discovery" , metav1.GetOptions {})
745
+ if err != nil {
746
+ t .Fatalf ("Failed to get `system:discovery` ClusterRoleBinding: %v" , err )
747
+ }
748
+ if ! reflect .DeepEqual (newDiscRoleBinding , discRoleBinding ) {
749
+ t .Errorf ("`system:discovery` should have been unmodified. Wanted: %v, got %v" , discRoleBinding , newDiscRoleBinding )
750
+ }
751
+ newBasicUserRoleBinding , err := client .Rbac ().ClusterRoleBindings ().Get ("system:basic-user" , metav1.GetOptions {})
752
+ if err != nil {
753
+ t .Fatalf ("Failed to get `system:basic-user` ClusterRoleBinding: %v" , err )
754
+ }
755
+ if ! reflect .DeepEqual (newBasicUserRoleBinding , basicUserRoleBinding ) {
756
+ t .Errorf ("`system:basic-user` should have been unmodified. Wanted: %v, got %v" , basicUserRoleBinding , newBasicUserRoleBinding )
757
+ }
758
+ publicInfoViewerRoleBinding , err := client .Rbac ().ClusterRoleBindings ().Get ("system:public-info-viewer" , metav1.GetOptions {})
759
+ if err != nil {
760
+ t .Fatalf ("Failed to get `system:public-info-viewer` ClusterRoleBinding: %v" , err )
761
+ }
762
+ if publicInfoViewerRoleBinding .Annotations ["rbac.authorization.kubernetes.io/autoupdate" ] != "false" {
763
+ t .Errorf ("publicInfoViewerRoleBinding.Annotations[\" rbac.authorization.kubernetes.io/autoupdate\" ] should be %v, got %v" , publicInfoViewerRoleBinding .Annotations ["rbac.authorization.kubernetes.io/autoupdate" ], "false" )
764
+ }
765
+ if publicInfoViewerRoleBinding .Annotations ["rbac-discovery-upgrade-test" ] != "pass" {
766
+ t .Errorf ("publicInfoViewerRoleBinding.Annotations[\" rbac-discovery-upgrade-test\" ] should be %v, got %v" , publicInfoViewerRoleBinding .Annotations ["rbac-discovery-upgrade-test" ], "pass" )
767
+ }
768
+ if ! reflect .DeepEqual (publicInfoViewerRoleBinding .Subjects , newDiscRoleBinding .Subjects ) {
769
+ t .Errorf ("`system:public-info-viewer` should have inherited Subjects from `system:discovery` Wanted: %v, got %v" , newDiscRoleBinding .Subjects , publicInfoViewerRoleBinding .Subjects )
770
+ }
771
+ }
0 commit comments