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:
commit
84f4f802fc
1 changed files with 11 additions and 0 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue