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 0781662 commit a29c8a7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 38 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
9 changes: 0 additions & 9 deletions test/extended/testdata/roles/policy-clusterroles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@ items:
- projects
verbs:
- list
- apiGroups: null
attributeRestrictions:
apiVersion: v1
kind: IsPersonalSubjectAccessReview
resources:
- localsubjectaccessreviews
- subjectaccessreviews
verbs:
- create
- apiVersion: v1
groupNames:
- system:authenticated
Expand Down
11 changes: 1 addition & 10 deletions test/extended/testdata/roles/policy-roles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ objects:
- projects
verbs:
- list
- apiGroups: null
attributeRestrictions:
apiVersion: v1
kind: IsPersonalSubjectAccessReview
resources:
- localsubjectaccessreviews
- subjectaccessreviews
verbs:
- create
- apiVersion: v1
groupNames:
- system:authenticated
Expand All @@ -72,4 +63,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,15 +40,6 @@ rules:
verbs:
- list
- watch
- apiGroups: null
attributeRestrictions:
apiVersion: v1
kind: IsPersonalSubjectAccessReview
resources:
- localsubjectaccessreviews
- subjectaccessreviews
verbs:
- create
- apiGroups: null
attributeRestrictions: null
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ rules:
verbs:
- list
- watch
- apiGroups: null
attributeRestrictions:
apiVersion: v1
kind: IsPersonalSubjectAccessReview
resources:
- localsubjectaccessreviews
- subjectaccessreviews
verbs:
- create
- apiGroups: null
attributeRestrictions: null
resources:
Expand Down

0 comments on commit a29c8a7

Please sign in to comment.