1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-04-08 10:04:25 +00:00

Merge pull request #2336 from NoSkillGirl/2289/context_policy_validation

Bug fix | CLI panic | Context policy validation
This commit is contained in:
Pooja Singh 2021-08-30 17:28:45 +05:30 committed by GitHub
commit 84f4f802fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -754,10 +754,13 @@ func validateRuleContext(rule kyverno.Rule) error {
return nil
}
contextNames := make([]string, 0)
for _, entry := range rule.Context {
if entry.Name == "" {
return fmt.Errorf("a name is required for context entries")
}
contextNames = append(contextNames, entry.Name)
var err error
if entry.ConfigMap != nil {
@ -773,6 +776,14 @@ func validateRuleContext(rule kyverno.Rule) error {
}
}
ruleBytes, _ := json.Marshal(rule)
ruleString := strings.ReplaceAll(string(ruleBytes), " ", "")
for _, contextName := range contextNames {
if !strings.Contains(ruleString, fmt.Sprintf("{{"+contextName)) {
return fmt.Errorf("context variable `%s` is not used in the policy", contextName)
}
}
return nil
}