Skip to content

Commit 73c2b36

Browse files
committed
Disallow AttributeRestrictions in PolicyRules
Signed-off-by: Monis Khan <[email protected]>
1 parent 61e203c commit 73c2b36

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

pkg/authorization/api/helpers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ func (r *PolicyRuleBuilder) RuleOrDie() PolicyRule {
366366
}
367367

368368
func (r *PolicyRuleBuilder) Rule() (PolicyRule, error) {
369+
if r.PolicyRule.AttributeRestrictions != nil {
370+
return PolicyRule{}, fmt.Errorf("rule may not have attributeRestrictions as they are deprecated and ignored: %#v", r.PolicyRule)
371+
}
372+
369373
if len(r.PolicyRule.Verbs) == 0 {
370374
return PolicyRule{}, fmt.Errorf("verbs are required: %#v", r.PolicyRule)
371375
}

pkg/authorization/api/validation/validation.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,13 @@ func ValidateRole(role *authorizationapi.Role, isNamespaced bool) field.ErrorLis
252252
}
253253

254254
func validateRole(role *authorizationapi.Role, isNamespaced bool, fldPath *field.Path) field.ErrorList {
255-
return validation.ValidateObjectMeta(&role.ObjectMeta, isNamespaced, path.ValidatePathSegmentName, fldPath.Child("metadata"))
255+
allErrs := validation.ValidateObjectMeta(&role.ObjectMeta, isNamespaced, path.ValidatePathSegmentName, fldPath.Child("metadata"))
256+
for i, rule := range role.Rules {
257+
if rule.AttributeRestrictions != nil {
258+
allErrs = append(allErrs, field.Invalid(fldPath.Child("rules").Index(i).Child("attributeRestrictions"), rule.AttributeRestrictions, "must be null as they are deprecated and ignored"))
259+
}
260+
}
261+
return allErrs
256262
}
257263

258264
func ValidateRoleUpdate(role *authorizationapi.Role, oldRole *authorizationapi.Role, isNamespaced bool) field.ErrorList {

pkg/authorization/api/validation/validation_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ func TestValidatePolicy(t *testing.T) {
5858
T: field.ErrorTypeInvalid,
5959
F: "roles[any1].metadata.name",
6060
},
61+
"invalid role": {
62+
A: authorizationapi.Policy{
63+
ObjectMeta: kapi.ObjectMeta{Namespace: kapi.NamespaceDefault, Name: authorizationapi.PolicyName},
64+
Roles: map[string]*authorizationapi.Role{
65+
"any1": {
66+
ObjectMeta: kapi.ObjectMeta{Namespace: kapi.NamespaceDefault, Name: "any1"},
67+
Rules: []authorizationapi.PolicyRule{
68+
{AttributeRestrictions: &authorizationapi.RoleBinding{}},
69+
},
70+
},
71+
},
72+
},
73+
T: field.ErrorTypeInvalid,
74+
F: "roles[any1].rules[0].attributeRestrictions",
75+
},
6176
}
6277
for k, v := range errorCases {
6378
errs := ValidatePolicy(&v.A, true)
@@ -370,6 +385,16 @@ func TestValidateRole(t *testing.T) {
370385
T: field.ErrorTypeRequired,
371386
F: "metadata.name",
372387
},
388+
"invalid rule": {
389+
A: authorizationapi.Role{
390+
ObjectMeta: kapi.ObjectMeta{Name: authorizationapi.PolicyName, Namespace: kapi.NamespaceDefault},
391+
Rules: []authorizationapi.PolicyRule{
392+
{AttributeRestrictions: &authorizationapi.IsPersonalSubjectAccessReview{}},
393+
},
394+
},
395+
T: field.ErrorTypeInvalid,
396+
F: "rules[0].attributeRestrictions",
397+
},
373398
}
374399
for k, v := range errorCases {
375400
errs := ValidateRole(&v.A, true)

0 commit comments

Comments
 (0)