1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00

Merge pull request #500 from nirmata/local_test

correct role/clusterrole kind
This commit is contained in:
shuting 2019-11-14 16:00:01 -08:00 committed by GitHub
commit 17ec220711
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 9 deletions

View file

@ -13,6 +13,11 @@ import (
rbaclister "k8s.io/client-go/listers/rbac/v1"
)
const (
clusterrolekind = "ClusterRole"
rolekind = "Role"
)
func GetRoleRef(rbLister rbaclister.RoleBindingLister, crbLister rbaclister.ClusterRoleBindingLister, request *v1beta1.AdmissionRequest) (roles []string, clusterRoles []string, err error) {
// rolebindings
roleBindings, err := rbLister.List(labels.NewSelector())
@ -49,11 +54,10 @@ func getRoleRefByRoleBindings(roleBindings []*rbacv1.RoleBinding, userInfo authe
continue
}
// roleRefMap := roleRef.(map[string]interface{})
switch rolebinding.RoleRef.Kind {
case "role":
case rolekind:
roles = append(roles, rolebinding.Namespace+":"+rolebinding.RoleRef.Name)
case "clusterRole":
case clusterrolekind:
clusterRoles = append(clusterRoles, rolebinding.RoleRef.Name)
}
}
@ -70,7 +74,7 @@ func getRoleRefByClusterRoleBindings(clusterroleBindings []*rbacv1.ClusterRoleBi
continue
}
if clusterRoleBinding.RoleRef.Kind == "clusterRole" {
if clusterRoleBinding.RoleRef.Kind == clusterrolekind {
clusterRoles = append(clusterRoles, clusterRoleBinding.RoleRef.Name)
}
}
@ -80,7 +84,7 @@ func getRoleRefByClusterRoleBindings(clusterroleBindings []*rbacv1.ClusterRoleBi
// matchSubjectsMap checks if userInfo found in subject
// return true directly if found a match
// subject["kind"] can only be ServiceAccount, User and Group
// subject.kind can only be ServiceAccount, User and Group
func matchSubjectsMap(subject rbacv1.Subject, userInfo authenticationv1.UserInfo) bool {
// ServiceAccount
if isServiceaccountUserInfo(userInfo.Username) {

View file

@ -181,7 +181,7 @@ func Test_getRoleRefByRoleBindings(t *testing.T) {
},
},
rbacv1.RoleRef{
Kind: "role",
Kind: rolekind,
Name: "myrole",
},
},
@ -199,7 +199,7 @@ func Test_getRoleRefByRoleBindings(t *testing.T) {
},
},
rbacv1.RoleRef{
Kind: "clusterRole",
Kind: clusterrolekind,
Name: "myclusterrole",
},
},
@ -232,7 +232,7 @@ func Test_getRoleRefByClusterRoleBindings(t *testing.T) {
},
},
rbacv1.RoleRef{
Kind: "clusterRole",
Kind: clusterrolekind,
Name: "fakeclusterrole",
},
},
@ -249,7 +249,7 @@ func Test_getRoleRefByClusterRoleBindings(t *testing.T) {
},
},
rbacv1.RoleRef{
Kind: "clusterRole",
Kind: clusterrolekind,
Name: "myclusterrole",
},
},