...

Source file src/k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/controller_policy_test.go

Documentation: k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac/bootstrappolicy

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package bootstrappolicy
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"k8s.io/apimachinery/pkg/api/meta"
    24  	"k8s.io/apimachinery/pkg/util/sets"
    25  )
    26  
    27  // rolesWithAllowStar are the controller roles which are allowed to contain a *.  These are
    28  // namespace lifecycle and GC which have to delete anything.  If you're adding to this list
    29  // tag sig-auth
    30  var rolesWithAllowStar = sets.NewString(
    31  	saRolePrefix+"namespace-controller",
    32  	saRolePrefix+"generic-garbage-collector",
    33  	saRolePrefix+"resourcequota-controller",
    34  	saRolePrefix+"horizontal-pod-autoscaler",
    35  	saRolePrefix+"clusterrole-aggregation-controller",
    36  	saRolePrefix+"disruption-controller",
    37  )
    38  
    39  // TestNoStarsForControllers confirms that no controller role has star verbs, groups,
    40  // or resources.  There are three known exceptions: namespace lifecycle and GC which have to
    41  // delete anything, and HPA, which has the power to read metrics associated
    42  // with any object.
    43  func TestNoStarsForControllers(t *testing.T) {
    44  	for _, role := range ControllerRoles() {
    45  		if rolesWithAllowStar.Has(role.Name) {
    46  			continue
    47  		}
    48  
    49  		for i, rule := range role.Rules {
    50  			for j, verb := range rule.Verbs {
    51  				if verb == "*" {
    52  					t.Errorf("%s.Rule[%d].Verbs[%d] is star", role.Name, i, j)
    53  				}
    54  			}
    55  			for j, group := range rule.APIGroups {
    56  				if group == "*" {
    57  					t.Errorf("%s.Rule[%d].APIGroups[%d] is star", role.Name, i, j)
    58  				}
    59  			}
    60  			for j, resource := range rule.Resources {
    61  				if resource == "*" {
    62  					t.Errorf("%s.Rule[%d].Resources[%d] is star", role.Name, i, j)
    63  				}
    64  			}
    65  		}
    66  	}
    67  }
    68  
    69  func TestControllerRoleLabel(t *testing.T) {
    70  	roles := ControllerRoles()
    71  	for i := range roles {
    72  		role := roles[i]
    73  		accessor, err := meta.Accessor(&role)
    74  		if err != nil {
    75  			t.Fatalf("unexpected error: %v", err)
    76  		}
    77  		if got, want := accessor.GetLabels(), map[string]string{"kubernetes.io/bootstrapping": "rbac-defaults"}; !reflect.DeepEqual(got, want) {
    78  			t.Errorf("ClusterRole: %s GetLabels() = %s, want %s", accessor.GetName(), got, want)
    79  		}
    80  	}
    81  
    82  	rolebindings := ControllerRoleBindings()
    83  	for i := range rolebindings {
    84  		rolebinding := rolebindings[i]
    85  		accessor, err := meta.Accessor(&rolebinding)
    86  		if err != nil {
    87  			t.Fatalf("unexpected error: %v", err)
    88  		}
    89  		if got, want := accessor.GetLabels(), map[string]string{"kubernetes.io/bootstrapping": "rbac-defaults"}; !reflect.DeepEqual(got, want) {
    90  			t.Errorf("ClusterRoleBinding: %s GetLabels() = %s, want %s", accessor.GetName(), got, want)
    91  		}
    92  	}
    93  }
    94  

View as plain text