Skip to content

Commit

Permalink
Disallow AttributeRestrictions in PolicyRules
Browse files Browse the repository at this point in the history
Signed-off-by: Monis Khan <[email protected]>
  • Loading branch information
enj committed Mar 20, 2017
1 parent 4a5fb49 commit 847e3fc
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 39 deletions.
4 changes: 4 additions & 0 deletions pkg/authorization/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ func (r *PolicyRuleBuilder) RuleOrDie() PolicyRule {
}

func (r *PolicyRuleBuilder) Rule() (PolicyRule, error) {
if r.PolicyRule.AttributeRestrictions != nil {
return PolicyRule{}, fmt.Errorf("rule may not have attributeRestrictions because they are deprecated and ignored: %#v", r.PolicyRule)
}

if len(r.PolicyRule.Verbs) == 0 {
return PolicyRule{}, fmt.Errorf("verbs are required: %#v", r.PolicyRule)
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/authorization/api/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@ func ValidateRole(role *authorizationapi.Role, isNamespaced bool) field.ErrorLis
}

func validateRole(role *authorizationapi.Role, isNamespaced bool, fldPath *field.Path) field.ErrorList {
return validation.ValidateObjectMeta(&role.ObjectMeta, isNamespaced, path.ValidatePathSegmentName, fldPath.Child("metadata"))
allErrs := validation.ValidateObjectMeta(&role.ObjectMeta, isNamespaced, path.ValidatePathSegmentName, fldPath.Child("metadata"))
for i, rule := range role.Rules {
if rule.AttributeRestrictions != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("rules").Index(i).Child("attributeRestrictions"), rule.AttributeRestrictions, "must be null"))
}
}
return allErrs
}

func ValidateRoleUpdate(role *authorizationapi.Role, oldRole *authorizationapi.Role, isNamespaced bool) field.ErrorList {
Expand Down
25 changes: 25 additions & 0 deletions pkg/authorization/api/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ func TestValidatePolicy(t *testing.T) {
T: field.ErrorTypeInvalid,
F: "roles[any1].metadata.name",
},
"invalid role": {
A: authorizationapi.Policy{
ObjectMeta: kapi.ObjectMeta{Namespace: kapi.NamespaceDefault, Name: authorizationapi.PolicyName},
Roles: map[string]*authorizationapi.Role{
"any1": {
ObjectMeta: kapi.ObjectMeta{Namespace: kapi.NamespaceDefault, Name: "any1"},
Rules: []authorizationapi.PolicyRule{
{AttributeRestrictions: &authorizationapi.RoleBinding{}},
},
},
},
},
T: field.ErrorTypeInvalid,
F: "roles[any1].rules[0].attributeRestrictions",
},
}
for k, v := range errorCases {
errs := ValidatePolicy(&v.A, true)
Expand Down Expand Up @@ -370,6 +385,16 @@ func TestValidateRole(t *testing.T) {
T: field.ErrorTypeRequired,
F: "metadata.name",
},
"invalid rule": {
A: authorizationapi.Role{
ObjectMeta: kapi.ObjectMeta{Name: authorizationapi.PolicyName, Namespace: kapi.NamespaceDefault},
Rules: []authorizationapi.PolicyRule{
{AttributeRestrictions: &authorizationapi.IsPersonalSubjectAccessReview{}},
},
},
T: field.ErrorTypeInvalid,
F: "rules[0].attributeRestrictions",
},
}
for k, v := range errorCases {
errs := ValidateRole(&v.A, true)
Expand Down
23 changes: 10 additions & 13 deletions test/extended/testdata/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions test/extended/testdata/roles/policy-clusterroles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ items:
- projects
verbs:
- list
- apiGroups: null
attributeRestrictions:
apiVersion: v1
kind: IsPersonalSubjectAccessReview
- apiGroups:
- authorization.k8s.io
attributeRestrictions: null
resources:
- localsubjectaccessreviews
- subjectaccessreviews
- selfsubjectaccessreviews
verbs:
- create
- apiVersion: v1
Expand Down
12 changes: 5 additions & 7 deletions test/extended/testdata/roles/policy-roles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ objects:
- projects
verbs:
- list
- apiGroups: null
attributeRestrictions:
apiVersion: v1
kind: IsPersonalSubjectAccessReview
- apiGroups:
- authorization.k8s.io
attributeRestrictions: null
resources:
- localsubjectaccessreviews
- subjectaccessreviews
- selfsubjectaccessreviews
verbs:
- create
- apiVersion: v1
Expand All @@ -72,4 +70,4 @@ objects:
subjects:
- kind: SystemGroup
name: system:authenticated
userNames: null
userNames: null
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ rules:
verbs:
- list
- watch
- apiGroups: null
attributeRestrictions:
apiVersion: v1
kind: IsPersonalSubjectAccessReview
- apiGroups:
- authorization.k8s.io
attributeRestrictions: null
resources:
- localsubjectaccessreviews
- subjectaccessreviews
- selfsubjectaccessreviews
verbs:
- create
- apiGroups: null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ rules:
verbs:
- list
- watch
- apiGroups: null
attributeRestrictions:
apiVersion: v1
kind: IsPersonalSubjectAccessReview
- apiGroups:
- authorization.k8s.io
attributeRestrictions: null
resources:
- localsubjectaccessreviews
- subjectaccessreviews
- selfsubjectaccessreviews
verbs:
- create
- apiGroups: null
Expand Down

0 comments on commit 847e3fc

Please sign in to comment.