2020-03-11 18:14:23 -07:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2023-01-30 17:47:19 +05:30
|
|
|
"github.com/kyverno/kyverno/pkg/engine/anchor"
|
2020-03-11 18:14:23 -07:00
|
|
|
)
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// ValidatePattern validates the pattern
|
2023-01-30 17:47:19 +05:30
|
|
|
func ValidatePattern(patternElement interface{}, path string, isSupported func(anchor.Anchor) bool) (string, error) {
|
2020-03-11 18:14:23 -07:00
|
|
|
switch typedPatternElement := patternElement.(type) {
|
|
|
|
case map[string]interface{}:
|
2023-01-30 17:47:19 +05:30
|
|
|
return validateMap(typedPatternElement, path, isSupported)
|
2020-03-11 18:14:23 -07:00
|
|
|
case []interface{}:
|
2023-01-30 17:47:19 +05:30
|
|
|
return validateArray(typedPatternElement, path, isSupported)
|
2020-03-11 18:14:23 -07:00
|
|
|
case string, float64, int, int64, bool, nil:
|
2022-05-17 08:19:03 +02:00
|
|
|
// TODO: check operator
|
2020-03-11 18:14:23 -07:00
|
|
|
return "", nil
|
|
|
|
default:
|
2021-10-03 03:15:22 -07:00
|
|
|
return path, fmt.Errorf("error at '%s', pattern contains unknown type", path)
|
2020-03-11 18:14:23 -07:00
|
|
|
}
|
|
|
|
}
|
2022-05-17 08:19:03 +02:00
|
|
|
|
2023-01-30 17:47:19 +05:30
|
|
|
func validateMap(patternMap map[string]interface{}, path string, isSupported func(anchor.Anchor) bool) (string, error) {
|
2020-03-11 18:14:23 -07:00
|
|
|
// check if anchors are defined
|
|
|
|
for key, value := range patternMap {
|
|
|
|
// if key is anchor
|
2023-01-30 17:47:19 +05:30
|
|
|
a := anchor.Parse(key)
|
2020-03-11 18:14:23 -07:00
|
|
|
// check the type of anchor
|
2023-01-30 17:47:19 +05:30
|
|
|
if a != nil {
|
2020-03-11 18:14:23 -07:00
|
|
|
// some type of anchor
|
|
|
|
// check if valid anchor
|
2023-01-30 17:47:19 +05:30
|
|
|
if !checkAnchors(a, isSupported) {
|
2021-10-12 23:29:20 +02:00
|
|
|
return path + "/" + key, fmt.Errorf("unsupported anchor %s", key)
|
2020-03-11 18:14:23 -07:00
|
|
|
}
|
|
|
|
// addition check for existence anchor
|
|
|
|
// value must be of type list
|
2023-01-30 17:47:19 +05:30
|
|
|
if anchor.IsExistence(a) {
|
2020-03-11 18:14:23 -07:00
|
|
|
typedValue, ok := value.([]interface{})
|
|
|
|
if !ok {
|
2021-10-12 23:29:20 +02:00
|
|
|
return path + "/" + key, fmt.Errorf("existence anchor should have value of type list")
|
2020-03-11 18:14:23 -07:00
|
|
|
}
|
2021-03-20 03:48:26 +05:30
|
|
|
// validate that there is atleast one entry in the list
|
|
|
|
if len(typedValue) == 0 {
|
2021-10-12 23:29:20 +02:00
|
|
|
return path + "/" + key, fmt.Errorf("existence anchor: should have atleast one value")
|
2020-03-11 18:14:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// lets validate the values now :)
|
2023-01-30 17:47:19 +05:30
|
|
|
if errPath, err := ValidatePattern(value, path+"/"+key, isSupported); err != nil {
|
2020-03-11 18:14:23 -07:00
|
|
|
return errPath, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:47:19 +05:30
|
|
|
func validateArray(patternArray []interface{}, path string, isSupported func(anchor.Anchor) bool) (string, error) {
|
2020-03-11 18:14:23 -07:00
|
|
|
for i, patternElement := range patternArray {
|
|
|
|
currentPath := path + strconv.Itoa(i) + "/"
|
|
|
|
// lets validate the values now :)
|
2023-01-30 17:47:19 +05:30
|
|
|
if errPath, err := ValidatePattern(patternElement, currentPath, isSupported); err != nil {
|
2020-03-11 18:14:23 -07:00
|
|
|
return errPath, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:47:19 +05:30
|
|
|
func checkAnchors(a anchor.Anchor, isSupported func(anchor.Anchor) bool) bool {
|
|
|
|
if isSupported == nil {
|
|
|
|
return false
|
2020-03-11 18:14:23 -07:00
|
|
|
}
|
2023-01-30 17:47:19 +05:30
|
|
|
return isSupported(a)
|
2020-03-11 18:14:23 -07:00
|
|
|
}
|