1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-29 10:55:05 +00:00

Relax JMESPath variable validation (#3826)

This commit is contained in:
Sambhav Kothari 2022-05-07 12:10:53 +01:00 committed by GitHub
parent b4f2b63f53
commit e55bf0bf6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 12 deletions

View file

@ -868,18 +868,6 @@ func validateRuleContext(rule kyverno.Rule) error {
return err
}
}
ruleBytes, _ := json.Marshal(rule)
for _, contextName := range contextNames {
contextRegex, err := regexp.Compile(fmt.Sprintf(`{{.*\b%s\b.*}}`, contextName))
if err != nil {
return fmt.Errorf("unable to validate context variable `%s`, %w", contextName, err)
}
if !contextRegex.Match(ruleBytes) {
return fmt.Errorf("context variable `%s` is not used in the policy", contextName)
}
}
return nil
}

View file

@ -54,3 +54,8 @@ results:
resource: example
kind: Pod
result: pass
- policy: example
rule: unused-var
resource: example
kind: Pod
result: pass

View file

@ -188,3 +188,30 @@ spec:
- key: "{{ obj }}"
operator: NotEqual
value: "{{ expected }}"
- name: unused-var
context:
- name: obj
variable:
value:
a: 1
b: 2
- name: modifiedObj
variable:
jmesPath: items(obj, 'key', 'value')
- name: expected
variable:
value:
- key: a
value: 1
- key: b
value: 2
match:
resources:
kinds:
- Pod
validate:
deny:
conditions:
- key: "{{ modifiedObj }}"
operator: NotEqual
value: "{{ expected }}"