mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 02:43:36 +00:00
vendor files
This commit is contained in:
46
vendor/k8s.io/kubernetes/pkg/apis/rbac/validation/BUILD
generated
vendored
Normal file
46
vendor/k8s.io/kubernetes/pkg/apis/rbac/validation/BUILD
generated
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["validation.go"],
|
||||
importpath = "k8s.io/kubernetes/pkg/apis/rbac/validation",
|
||||
deps = [
|
||||
"//pkg/apis/core/validation:go_default_library",
|
||||
"//pkg/apis/rbac:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/validation/path:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/validation:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["validation_test.go"],
|
||||
importpath = "k8s.io/kubernetes/pkg/apis/rbac/validation",
|
||||
library = ":go_default_library",
|
||||
deps = [
|
||||
"//pkg/apis/rbac:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
252
vendor/k8s.io/kubernetes/pkg/apis/rbac/validation/validation.go
generated
vendored
Normal file
252
vendor/k8s.io/kubernetes/pkg/apis/rbac/validation/validation.go
generated
vendored
Normal file
@ -0,0 +1,252 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package validation
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/api/validation/path"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
unversionedvalidation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
"k8s.io/kubernetes/pkg/apis/core/validation"
|
||||
"k8s.io/kubernetes/pkg/apis/rbac"
|
||||
)
|
||||
|
||||
// Minimal validation of names for roles and bindings. Identical to the validation for Openshift. See:
|
||||
// * https://github.com/kubernetes/kubernetes/blob/60db50/pkg/api/validation/name.go
|
||||
// * https://github.com/openshift/origin/blob/388478/pkg/api/helpers.go
|
||||
func minimalNameRequirements(name string, prefix bool) []string {
|
||||
return path.IsValidPathSegmentName(name)
|
||||
}
|
||||
|
||||
func ValidateRole(role *rbac.Role) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
allErrs = append(allErrs, validation.ValidateObjectMeta(&role.ObjectMeta, true, minimalNameRequirements, field.NewPath("metadata"))...)
|
||||
|
||||
for i, rule := range role.Rules {
|
||||
if err := validatePolicyRule(rule, true, field.NewPath("rules").Index(i)); err != nil {
|
||||
allErrs = append(allErrs, err...)
|
||||
}
|
||||
}
|
||||
if len(allErrs) != 0 {
|
||||
return allErrs
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateRoleUpdate(role *rbac.Role, oldRole *rbac.Role) field.ErrorList {
|
||||
allErrs := ValidateRole(role)
|
||||
allErrs = append(allErrs, validation.ValidateObjectMetaUpdate(&role.ObjectMeta, &oldRole.ObjectMeta, field.NewPath("metadata"))...)
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func ValidateClusterRole(role *rbac.ClusterRole) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
allErrs = append(allErrs, validation.ValidateObjectMeta(&role.ObjectMeta, false, minimalNameRequirements, field.NewPath("metadata"))...)
|
||||
|
||||
for i, rule := range role.Rules {
|
||||
if err := validatePolicyRule(rule, false, field.NewPath("rules").Index(i)); err != nil {
|
||||
allErrs = append(allErrs, err...)
|
||||
}
|
||||
}
|
||||
|
||||
if role.AggregationRule != nil {
|
||||
if len(role.AggregationRule.ClusterRoleSelectors) == 0 {
|
||||
allErrs = append(allErrs, field.Required(field.NewPath("aggregationRule", "clusterRoleSelectors"), "at least one clusterRoleSelector required if aggregationRule is non-nil"))
|
||||
}
|
||||
for i, selector := range role.AggregationRule.ClusterRoleSelectors {
|
||||
fieldPath := field.NewPath("aggregationRule", "clusterRoleSelectors").Index(i)
|
||||
allErrs = append(allErrs, unversionedvalidation.ValidateLabelSelector(&selector, fieldPath)...)
|
||||
|
||||
selector, err := metav1.LabelSelectorAsSelector(&selector)
|
||||
if err != nil {
|
||||
allErrs = append(allErrs, field.Invalid(fieldPath, selector, "invalid label selector."))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(allErrs) != 0 {
|
||||
return allErrs
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateClusterRoleUpdate(role *rbac.ClusterRole, oldRole *rbac.ClusterRole) field.ErrorList {
|
||||
allErrs := ValidateClusterRole(role)
|
||||
allErrs = append(allErrs, validation.ValidateObjectMetaUpdate(&role.ObjectMeta, &oldRole.ObjectMeta, field.NewPath("metadata"))...)
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func validatePolicyRule(rule rbac.PolicyRule, isNamespaced bool, fldPath *field.Path) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
if len(rule.Verbs) == 0 {
|
||||
allErrs = append(allErrs, field.Required(fldPath.Child("verbs"), "verbs must contain at least one value"))
|
||||
}
|
||||
|
||||
if len(rule.NonResourceURLs) > 0 {
|
||||
if isNamespaced {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceURLs"), rule.NonResourceURLs, "namespaced rules cannot apply to non-resource URLs"))
|
||||
}
|
||||
if len(rule.APIGroups) > 0 || len(rule.Resources) > 0 || len(rule.ResourceNames) > 0 {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceURLs"), rule.NonResourceURLs, "rules cannot apply to both regular resources and non-resource URLs"))
|
||||
}
|
||||
return allErrs
|
||||
}
|
||||
|
||||
if len(rule.APIGroups) == 0 {
|
||||
allErrs = append(allErrs, field.Required(fldPath.Child("apiGroups"), "resource rules must supply at least one api group"))
|
||||
}
|
||||
if len(rule.Resources) == 0 {
|
||||
allErrs = append(allErrs, field.Required(fldPath.Child("resources"), "resource rules must supply at least one resource"))
|
||||
}
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func ValidateRoleBinding(roleBinding *rbac.RoleBinding) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
allErrs = append(allErrs, validation.ValidateObjectMeta(&roleBinding.ObjectMeta, true, minimalNameRequirements, field.NewPath("metadata"))...)
|
||||
|
||||
// TODO allow multiple API groups. For now, restrict to one, but I can envision other experimental roles in other groups taking
|
||||
// advantage of the binding infrastructure
|
||||
if roleBinding.RoleRef.APIGroup != rbac.GroupName {
|
||||
allErrs = append(allErrs, field.NotSupported(field.NewPath("roleRef", "apiGroup"), roleBinding.RoleRef.APIGroup, []string{rbac.GroupName}))
|
||||
}
|
||||
|
||||
switch roleBinding.RoleRef.Kind {
|
||||
case "Role", "ClusterRole":
|
||||
default:
|
||||
allErrs = append(allErrs, field.NotSupported(field.NewPath("roleRef", "kind"), roleBinding.RoleRef.Kind, []string{"Role", "ClusterRole"}))
|
||||
|
||||
}
|
||||
|
||||
if len(roleBinding.RoleRef.Name) == 0 {
|
||||
allErrs = append(allErrs, field.Required(field.NewPath("roleRef", "name"), ""))
|
||||
} else {
|
||||
for _, msg := range minimalNameRequirements(roleBinding.RoleRef.Name, false) {
|
||||
allErrs = append(allErrs, field.Invalid(field.NewPath("roleRef", "name"), roleBinding.RoleRef.Name, msg))
|
||||
}
|
||||
}
|
||||
|
||||
subjectsPath := field.NewPath("subjects")
|
||||
for i, subject := range roleBinding.Subjects {
|
||||
allErrs = append(allErrs, validateRoleBindingSubject(subject, true, subjectsPath.Index(i))...)
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func ValidateRoleBindingUpdate(roleBinding *rbac.RoleBinding, oldRoleBinding *rbac.RoleBinding) field.ErrorList {
|
||||
allErrs := ValidateRoleBinding(roleBinding)
|
||||
allErrs = append(allErrs, validation.ValidateObjectMetaUpdate(&roleBinding.ObjectMeta, &oldRoleBinding.ObjectMeta, field.NewPath("metadata"))...)
|
||||
|
||||
if oldRoleBinding.RoleRef != roleBinding.RoleRef {
|
||||
allErrs = append(allErrs, field.Invalid(field.NewPath("roleRef"), roleBinding.RoleRef, "cannot change roleRef"))
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func ValidateClusterRoleBinding(roleBinding *rbac.ClusterRoleBinding) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
allErrs = append(allErrs, validation.ValidateObjectMeta(&roleBinding.ObjectMeta, false, minimalNameRequirements, field.NewPath("metadata"))...)
|
||||
|
||||
// TODO allow multiple API groups. For now, restrict to one, but I can envision other experimental roles in other groups taking
|
||||
// advantage of the binding infrastructure
|
||||
if roleBinding.RoleRef.APIGroup != rbac.GroupName {
|
||||
allErrs = append(allErrs, field.NotSupported(field.NewPath("roleRef", "apiGroup"), roleBinding.RoleRef.APIGroup, []string{rbac.GroupName}))
|
||||
}
|
||||
|
||||
switch roleBinding.RoleRef.Kind {
|
||||
case "ClusterRole":
|
||||
default:
|
||||
allErrs = append(allErrs, field.NotSupported(field.NewPath("roleRef", "kind"), roleBinding.RoleRef.Kind, []string{"ClusterRole"}))
|
||||
|
||||
}
|
||||
|
||||
if len(roleBinding.RoleRef.Name) == 0 {
|
||||
allErrs = append(allErrs, field.Required(field.NewPath("roleRef", "name"), ""))
|
||||
} else {
|
||||
for _, msg := range minimalNameRequirements(roleBinding.RoleRef.Name, false) {
|
||||
allErrs = append(allErrs, field.Invalid(field.NewPath("roleRef", "name"), roleBinding.RoleRef.Name, msg))
|
||||
}
|
||||
}
|
||||
|
||||
subjectsPath := field.NewPath("subjects")
|
||||
for i, subject := range roleBinding.Subjects {
|
||||
allErrs = append(allErrs, validateRoleBindingSubject(subject, false, subjectsPath.Index(i))...)
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func ValidateClusterRoleBindingUpdate(roleBinding *rbac.ClusterRoleBinding, oldRoleBinding *rbac.ClusterRoleBinding) field.ErrorList {
|
||||
allErrs := ValidateClusterRoleBinding(roleBinding)
|
||||
allErrs = append(allErrs, validation.ValidateObjectMetaUpdate(&roleBinding.ObjectMeta, &oldRoleBinding.ObjectMeta, field.NewPath("metadata"))...)
|
||||
|
||||
if oldRoleBinding.RoleRef != roleBinding.RoleRef {
|
||||
allErrs = append(allErrs, field.Invalid(field.NewPath("roleRef"), roleBinding.RoleRef, "cannot change roleRef"))
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func validateRoleBindingSubject(subject rbac.Subject, isNamespaced bool, fldPath *field.Path) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
|
||||
if len(subject.Name) == 0 {
|
||||
allErrs = append(allErrs, field.Required(fldPath.Child("name"), ""))
|
||||
}
|
||||
|
||||
switch subject.Kind {
|
||||
case rbac.ServiceAccountKind:
|
||||
if len(subject.Name) > 0 {
|
||||
for _, msg := range validation.ValidateServiceAccountName(subject.Name, false) {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), subject.Name, msg))
|
||||
}
|
||||
}
|
||||
if len(subject.APIGroup) > 0 {
|
||||
allErrs = append(allErrs, field.NotSupported(fldPath.Child("apiGroup"), subject.APIGroup, []string{""}))
|
||||
}
|
||||
if !isNamespaced && len(subject.Namespace) == 0 {
|
||||
allErrs = append(allErrs, field.Required(fldPath.Child("namespace"), ""))
|
||||
}
|
||||
|
||||
case rbac.UserKind:
|
||||
// TODO(ericchiang): What other restrictions on user name are there?
|
||||
if len(subject.Name) == 0 {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), subject.Name, "user name cannot be empty"))
|
||||
}
|
||||
if subject.APIGroup != rbac.GroupName {
|
||||
allErrs = append(allErrs, field.NotSupported(fldPath.Child("apiGroup"), subject.APIGroup, []string{rbac.GroupName}))
|
||||
}
|
||||
|
||||
case rbac.GroupKind:
|
||||
// TODO(ericchiang): What other restrictions on group name are there?
|
||||
if len(subject.Name) == 0 {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), subject.Name, "group name cannot be empty"))
|
||||
}
|
||||
if subject.APIGroup != rbac.GroupName {
|
||||
allErrs = append(allErrs, field.NotSupported(fldPath.Child("apiGroup"), subject.APIGroup, []string{rbac.GroupName}))
|
||||
}
|
||||
|
||||
default:
|
||||
allErrs = append(allErrs, field.NotSupported(fldPath.Child("kind"), subject.Kind, []string{rbac.ServiceAccountKind, rbac.UserKind, rbac.GroupKind}))
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
535
vendor/k8s.io/kubernetes/pkg/apis/rbac/validation/validation_test.go
generated
vendored
Normal file
535
vendor/k8s.io/kubernetes/pkg/apis/rbac/validation/validation_test.go
generated
vendored
Normal file
@ -0,0 +1,535 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package validation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
"k8s.io/kubernetes/pkg/apis/rbac"
|
||||
)
|
||||
|
||||
func TestValidateClusterRoleBinding(t *testing.T) {
|
||||
errs := ValidateClusterRoleBinding(
|
||||
&rbac.ClusterRoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "master"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "ClusterRole", Name: "valid"},
|
||||
Subjects: []rbac.Subject{
|
||||
{Name: "validsaname", APIGroup: "", Namespace: "foo", Kind: rbac.ServiceAccountKind},
|
||||
{Name: "valid@username", APIGroup: rbac.GroupName, Kind: rbac.UserKind},
|
||||
{Name: "valid@groupname", APIGroup: rbac.GroupName, Kind: rbac.GroupKind},
|
||||
},
|
||||
},
|
||||
)
|
||||
if len(errs) != 0 {
|
||||
t.Errorf("expected success: %v", errs)
|
||||
}
|
||||
|
||||
errorCases := map[string]struct {
|
||||
A rbac.ClusterRoleBinding
|
||||
T field.ErrorType
|
||||
F string
|
||||
}{
|
||||
"bad group": {
|
||||
A: rbac.ClusterRoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "default"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: "rbac.GroupName", Kind: "ClusterRole", Name: "valid"},
|
||||
},
|
||||
T: field.ErrorTypeNotSupported,
|
||||
F: "roleRef.apiGroup",
|
||||
},
|
||||
"bad kind": {
|
||||
A: rbac.ClusterRoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "default"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Type", Name: "valid"},
|
||||
},
|
||||
T: field.ErrorTypeNotSupported,
|
||||
F: "roleRef.kind",
|
||||
},
|
||||
"reference role": {
|
||||
A: rbac.ClusterRoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "default"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Role", Name: "valid"},
|
||||
},
|
||||
T: field.ErrorTypeNotSupported,
|
||||
F: "roleRef.kind",
|
||||
},
|
||||
"zero-length name": {
|
||||
A: rbac.ClusterRoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "ClusterRole", Name: "valid"},
|
||||
},
|
||||
T: field.ErrorTypeRequired,
|
||||
F: "metadata.name",
|
||||
},
|
||||
"bad role": {
|
||||
A: rbac.ClusterRoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "default"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "ClusterRole"},
|
||||
},
|
||||
T: field.ErrorTypeRequired,
|
||||
F: "roleRef.name",
|
||||
},
|
||||
"bad subject kind": {
|
||||
A: rbac.ClusterRoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "master"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "ClusterRole", Name: "valid"},
|
||||
Subjects: []rbac.Subject{{Name: "subject"}},
|
||||
},
|
||||
T: field.ErrorTypeNotSupported,
|
||||
F: "subjects[0].kind",
|
||||
},
|
||||
"bad subject name": {
|
||||
A: rbac.ClusterRoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "master"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "ClusterRole", Name: "valid"},
|
||||
Subjects: []rbac.Subject{{Namespace: "foo", Name: "subject:bad", Kind: rbac.ServiceAccountKind}},
|
||||
},
|
||||
T: field.ErrorTypeInvalid,
|
||||
F: "subjects[0].name",
|
||||
},
|
||||
"missing SA namespace": {
|
||||
A: rbac.ClusterRoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "master"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "ClusterRole", Name: "valid"},
|
||||
Subjects: []rbac.Subject{{Name: "good", Kind: rbac.ServiceAccountKind}},
|
||||
},
|
||||
T: field.ErrorTypeRequired,
|
||||
F: "subjects[0].namespace",
|
||||
},
|
||||
"missing subject name": {
|
||||
A: rbac.ClusterRoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "master"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "ClusterRole", Name: "valid"},
|
||||
Subjects: []rbac.Subject{{Namespace: "foo", Kind: rbac.ServiceAccountKind}},
|
||||
},
|
||||
T: field.ErrorTypeRequired,
|
||||
F: "subjects[0].name",
|
||||
},
|
||||
}
|
||||
for k, v := range errorCases {
|
||||
errs := ValidateClusterRoleBinding(&v.A)
|
||||
if len(errs) == 0 {
|
||||
t.Errorf("expected failure %s for %v", k, v.A)
|
||||
continue
|
||||
}
|
||||
for i := range errs {
|
||||
if errs[i].Type != v.T {
|
||||
t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
|
||||
}
|
||||
if errs[i].Field != v.F {
|
||||
t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRoleBinding(t *testing.T) {
|
||||
errs := ValidateRoleBinding(
|
||||
&rbac.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "master"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Role", Name: "valid"},
|
||||
Subjects: []rbac.Subject{
|
||||
{Name: "validsaname", APIGroup: "", Kind: rbac.ServiceAccountKind},
|
||||
{Name: "valid@username", APIGroup: rbac.GroupName, Kind: rbac.UserKind},
|
||||
{Name: "valid@groupname", APIGroup: rbac.GroupName, Kind: rbac.GroupKind},
|
||||
},
|
||||
},
|
||||
)
|
||||
if len(errs) != 0 {
|
||||
t.Errorf("expected success: %v", errs)
|
||||
}
|
||||
|
||||
errorCases := map[string]struct {
|
||||
A rbac.RoleBinding
|
||||
T field.ErrorType
|
||||
F string
|
||||
}{
|
||||
"bad group": {
|
||||
A: rbac.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "default"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: "rbac.GroupName", Kind: "ClusterRole", Name: "valid"},
|
||||
},
|
||||
T: field.ErrorTypeNotSupported,
|
||||
F: "roleRef.apiGroup",
|
||||
},
|
||||
"bad kind": {
|
||||
A: rbac.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "default"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Type", Name: "valid"},
|
||||
},
|
||||
T: field.ErrorTypeNotSupported,
|
||||
F: "roleRef.kind",
|
||||
},
|
||||
"zero-length namespace": {
|
||||
A: rbac.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "default"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Role", Name: "valid"},
|
||||
},
|
||||
T: field.ErrorTypeRequired,
|
||||
F: "metadata.namespace",
|
||||
},
|
||||
"zero-length name": {
|
||||
A: rbac.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Role", Name: "valid"},
|
||||
},
|
||||
T: field.ErrorTypeRequired,
|
||||
F: "metadata.name",
|
||||
},
|
||||
"bad role": {
|
||||
A: rbac.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "default"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Role"},
|
||||
},
|
||||
T: field.ErrorTypeRequired,
|
||||
F: "roleRef.name",
|
||||
},
|
||||
"bad subject kind": {
|
||||
A: rbac.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "master"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Role", Name: "valid"},
|
||||
Subjects: []rbac.Subject{{Name: "subject"}},
|
||||
},
|
||||
T: field.ErrorTypeNotSupported,
|
||||
F: "subjects[0].kind",
|
||||
},
|
||||
"bad subject name": {
|
||||
A: rbac.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "master"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Role", Name: "valid"},
|
||||
Subjects: []rbac.Subject{{Name: "subject:bad", Kind: rbac.ServiceAccountKind}},
|
||||
},
|
||||
T: field.ErrorTypeInvalid,
|
||||
F: "subjects[0].name",
|
||||
},
|
||||
"missing subject name": {
|
||||
A: rbac.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "master"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Role", Name: "valid"},
|
||||
Subjects: []rbac.Subject{{Kind: rbac.ServiceAccountKind}},
|
||||
},
|
||||
T: field.ErrorTypeRequired,
|
||||
F: "subjects[0].name",
|
||||
},
|
||||
}
|
||||
for k, v := range errorCases {
|
||||
errs := ValidateRoleBinding(&v.A)
|
||||
if len(errs) == 0 {
|
||||
t.Errorf("expected failure %s for %v", k, v.A)
|
||||
continue
|
||||
}
|
||||
for i := range errs {
|
||||
if errs[i].Type != v.T {
|
||||
t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
|
||||
}
|
||||
if errs[i].Field != v.F {
|
||||
t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRoleBindingUpdate(t *testing.T) {
|
||||
old := &rbac.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "master", ResourceVersion: "1"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Role", Name: "valid"},
|
||||
}
|
||||
|
||||
errs := ValidateRoleBindingUpdate(
|
||||
&rbac.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "master", ResourceVersion: "1"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Role", Name: "valid"},
|
||||
},
|
||||
old,
|
||||
)
|
||||
if len(errs) != 0 {
|
||||
t.Errorf("expected success: %v", errs)
|
||||
}
|
||||
|
||||
errorCases := map[string]struct {
|
||||
A rbac.RoleBinding
|
||||
T field.ErrorType
|
||||
F string
|
||||
}{
|
||||
"changedRef": {
|
||||
A: rbac.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "master", ResourceVersion: "1"},
|
||||
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Role", Name: "changed"},
|
||||
},
|
||||
T: field.ErrorTypeInvalid,
|
||||
F: "roleRef",
|
||||
},
|
||||
}
|
||||
for k, v := range errorCases {
|
||||
errs := ValidateRoleBindingUpdate(&v.A, old)
|
||||
if len(errs) == 0 {
|
||||
t.Errorf("expected failure %s for %v", k, v.A)
|
||||
continue
|
||||
}
|
||||
for i := range errs {
|
||||
if errs[i].Type != v.T {
|
||||
t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
|
||||
}
|
||||
if errs[i].Field != v.F {
|
||||
t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ValidateRoleTest struct {
|
||||
role rbac.Role
|
||||
wantErr bool
|
||||
errType field.ErrorType
|
||||
field string
|
||||
}
|
||||
|
||||
func (v ValidateRoleTest) test(t *testing.T) {
|
||||
errs := ValidateRole(&v.role)
|
||||
if len(errs) == 0 {
|
||||
if v.wantErr {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
return
|
||||
}
|
||||
if !v.wantErr {
|
||||
t.Errorf("didn't expect error, got %v", errs)
|
||||
return
|
||||
}
|
||||
for i := range errs {
|
||||
if errs[i].Type != v.errType {
|
||||
t.Errorf("expected errors to have type %s: %v", v.errType, errs[i])
|
||||
}
|
||||
if errs[i].Field != v.field {
|
||||
t.Errorf("expected errors to have field %s: %v", v.field, errs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ValidateClusterRoleTest struct {
|
||||
role rbac.ClusterRole
|
||||
wantErr bool
|
||||
errType field.ErrorType
|
||||
field string
|
||||
}
|
||||
|
||||
func (v ValidateClusterRoleTest) test(t *testing.T) {
|
||||
errs := ValidateClusterRole(&v.role)
|
||||
if len(errs) == 0 {
|
||||
if v.wantErr {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
return
|
||||
}
|
||||
if !v.wantErr {
|
||||
t.Errorf("didn't expect error, got %v", errs)
|
||||
return
|
||||
}
|
||||
for i := range errs {
|
||||
if errs[i].Type != v.errType {
|
||||
t.Errorf("expected errors to have type %s: %v", v.errType, errs[i])
|
||||
}
|
||||
if errs[i].Field != v.field {
|
||||
t.Errorf("expected errors to have field %s: %v", v.field, errs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRoleZeroLengthNamespace(t *testing.T) {
|
||||
ValidateRoleTest{
|
||||
role: rbac.Role{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "default"},
|
||||
},
|
||||
wantErr: true,
|
||||
errType: field.ErrorTypeRequired,
|
||||
field: "metadata.namespace",
|
||||
}.test(t)
|
||||
}
|
||||
|
||||
func TestValidateRoleZeroLengthName(t *testing.T) {
|
||||
ValidateRoleTest{
|
||||
role: rbac.Role{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "default"},
|
||||
},
|
||||
wantErr: true,
|
||||
errType: field.ErrorTypeRequired,
|
||||
field: "metadata.name",
|
||||
}.test(t)
|
||||
}
|
||||
|
||||
func TestValidateRoleValidRole(t *testing.T) {
|
||||
ValidateRoleTest{
|
||||
role: rbac.Role{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "default",
|
||||
Name: "default",
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
}.test(t)
|
||||
}
|
||||
|
||||
func TestValidateRoleValidRoleNoNamespace(t *testing.T) {
|
||||
ValidateClusterRoleTest{
|
||||
role: rbac.ClusterRole{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "default",
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
}.test(t)
|
||||
}
|
||||
|
||||
func TestValidateRoleNonResourceURL(t *testing.T) {
|
||||
ValidateClusterRoleTest{
|
||||
role: rbac.ClusterRole{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "default",
|
||||
},
|
||||
Rules: []rbac.PolicyRule{
|
||||
{
|
||||
Verbs: []string{"get"},
|
||||
NonResourceURLs: []string{"/*"},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
}.test(t)
|
||||
}
|
||||
|
||||
func TestValidateRoleNamespacedNonResourceURL(t *testing.T) {
|
||||
ValidateRoleTest{
|
||||
role: rbac.Role{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "default",
|
||||
Name: "default",
|
||||
},
|
||||
Rules: []rbac.PolicyRule{
|
||||
{
|
||||
// non-resource URLs are invalid for namespaced rules
|
||||
Verbs: []string{"get"},
|
||||
NonResourceURLs: []string{"/*"},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
errType: field.ErrorTypeInvalid,
|
||||
field: "rules[0].nonResourceURLs",
|
||||
}.test(t)
|
||||
}
|
||||
|
||||
func TestValidateRoleNonResourceURLNoVerbs(t *testing.T) {
|
||||
ValidateClusterRoleTest{
|
||||
role: rbac.ClusterRole{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "default",
|
||||
},
|
||||
Rules: []rbac.PolicyRule{
|
||||
{
|
||||
Verbs: []string{},
|
||||
NonResourceURLs: []string{"/*"},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
errType: field.ErrorTypeRequired,
|
||||
field: "rules[0].verbs",
|
||||
}.test(t)
|
||||
}
|
||||
|
||||
func TestValidateRoleMixedNonResourceAndResource(t *testing.T) {
|
||||
ValidateRoleTest{
|
||||
role: rbac.Role{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "default",
|
||||
Namespace: "default",
|
||||
},
|
||||
Rules: []rbac.PolicyRule{
|
||||
{
|
||||
Verbs: []string{"get"},
|
||||
NonResourceURLs: []string{"/*"},
|
||||
APIGroups: []string{"v1"},
|
||||
Resources: []string{"pods"},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
errType: field.ErrorTypeInvalid,
|
||||
field: "rules[0].nonResourceURLs",
|
||||
}.test(t)
|
||||
}
|
||||
|
||||
func TestValidateRoleValidResource(t *testing.T) {
|
||||
ValidateRoleTest{
|
||||
role: rbac.Role{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "default",
|
||||
Namespace: "default",
|
||||
},
|
||||
Rules: []rbac.PolicyRule{
|
||||
{
|
||||
Verbs: []string{"get"},
|
||||
APIGroups: []string{"v1"},
|
||||
Resources: []string{"pods"},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
}.test(t)
|
||||
}
|
||||
|
||||
func TestValidateRoleNoAPIGroup(t *testing.T) {
|
||||
ValidateRoleTest{
|
||||
role: rbac.Role{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "default",
|
||||
Namespace: "default",
|
||||
},
|
||||
Rules: []rbac.PolicyRule{
|
||||
{
|
||||
Verbs: []string{"get"},
|
||||
Resources: []string{"pods"},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
errType: field.ErrorTypeRequired,
|
||||
field: "rules[0].apiGroups",
|
||||
}.test(t)
|
||||
}
|
||||
|
||||
func TestValidateRoleNoResources(t *testing.T) {
|
||||
ValidateRoleTest{
|
||||
role: rbac.Role{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "default",
|
||||
Namespace: "default",
|
||||
},
|
||||
Rules: []rbac.PolicyRule{
|
||||
{
|
||||
Verbs: []string{"get"},
|
||||
APIGroups: []string{"v1"},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
errType: field.ErrorTypeRequired,
|
||||
field: "rules[0].resources",
|
||||
}.test(t)
|
||||
}
|
Reference in New Issue
Block a user