2019-12-30 17:08:50 -08:00
package validate
import (
"fmt"
2022-10-20 13:36:46 +02:00
"sort"
2019-12-30 17:08:50 -08:00
"strconv"
2020-03-17 11:05:20 -07:00
"github.com/go-logr/logr"
2020-10-07 11:12:31 -07:00
"github.com/kyverno/kyverno/pkg/engine/anchor"
2023-01-10 14:07:26 +01:00
"github.com/kyverno/kyverno/pkg/engine/pattern"
2022-05-17 07:56:48 +02:00
"github.com/kyverno/kyverno/pkg/engine/wildcards"
2022-08-02 07:54:02 -07:00
"go.uber.org/multierr"
2019-12-30 17:08:50 -08:00
)
2021-09-30 23:34:04 -07:00
type PatternError struct {
2021-10-02 16:57:40 -07:00
Err error
2021-09-30 23:34:04 -07:00
Path string
Skip bool
}
func ( e * PatternError ) Error ( ) string {
if e . Err == nil {
return ""
}
return e . Err . Error ( )
}
2021-09-26 02:12:31 -07:00
// MatchPattern is a start of element-by-element pattern validation process.
2020-02-13 13:57:48 -08:00
// It assumes that validation is started from root, so "/" is passed
2021-09-30 23:34:04 -07:00
func MatchPattern ( logger logr . Logger , resource , pattern interface { } ) error {
2020-08-29 06:52:22 +05:30
// newAnchorMap - to check anchor key has values
2022-01-04 17:36:33 -08:00
ac := anchor . NewAnchorMap ( )
2021-07-23 20:53:37 +03:00
elemPath , err := validateResourceElement ( logger , resource , pattern , pattern , "/" , ac )
2020-02-13 13:57:48 -08:00
if err != nil {
2022-01-04 17:36:33 -08:00
if skip ( err ) {
logger . V ( 2 ) . Info ( "resource skipped" , "reason" , ac . AnchorError . Error ( ) )
2021-10-02 16:53:02 -07:00
return & PatternError { err , "" , true }
2020-12-08 22:52:37 -08:00
}
2022-01-19 15:39:07 +05:30
if fail ( err ) {
logger . V ( 2 ) . Info ( "failed to apply rule on resource" , "msg" , ac . AnchorError . Error ( ) )
return & PatternError { err , elemPath , false }
}
2021-09-27 14:28:55 -07:00
// check if an anchor defined in the policy rule is missing in the resource
2023-01-30 17:47:19 +05:30
if ac . KeysAreMissing ( ) {
2021-09-26 02:12:31 -07:00
logger . V ( 3 ) . Info ( "missing anchor in resource" )
2021-09-30 23:34:04 -07:00
return & PatternError { err , "" , false }
2020-08-29 06:52:22 +05:30
}
2021-09-26 02:12:31 -07:00
2021-09-30 23:34:04 -07:00
return & PatternError { err , elemPath , false }
2020-02-13 13:57:48 -08:00
}
2021-10-02 16:53:02 -07:00
return nil
2020-02-13 13:57:48 -08:00
}
2022-01-04 17:36:33 -08:00
func skip ( err error ) bool {
// if conditional or global anchors report errors, the rule does not apply to the resource
2023-01-30 17:47:19 +05:30
return anchor . IsConditionalAnchorError ( err ) || anchor . IsGlobalAnchorError ( err )
2022-01-04 17:36:33 -08:00
}
2022-01-19 15:39:07 +05:30
func fail ( err error ) bool {
// if negation anchors report errors, the rule will fail
2023-01-30 17:47:19 +05:30
return anchor . IsNegationAnchorError ( err )
2022-01-19 15:39:07 +05:30
}
2019-12-30 17:08:50 -08:00
// validateResourceElement detects the element type (map, array, nil, string, int, bool, float)
// and calls corresponding handler
// Pattern tree and resource tree can have different structure. In this case validation fails
2023-01-30 17:47:19 +05:30
func validateResourceElement ( log logr . Logger , resourceElement , patternElement , originPattern interface { } , path string , ac * anchor . AnchorMap ) ( string , error ) {
2019-12-30 17:08:50 -08:00
switch typedPatternElement := patternElement . ( type ) {
// map
case map [ string ] interface { } :
typedResourceElement , ok := resourceElement . ( map [ string ] interface { } )
if ! ok {
2020-03-17 11:05:20 -07:00
log . V ( 4 ) . Info ( "Pattern and resource have different structures." , "path" , path , "expected" , fmt . Sprintf ( "%T" , patternElement ) , "current" , fmt . Sprintf ( "%T" , resourceElement ) )
2021-10-12 23:29:20 +02:00
return path , fmt . Errorf ( "pattern and resource have different structures. Path: %s. Expected %T, found %T" , path , patternElement , resourceElement )
2019-12-30 17:08:50 -08:00
}
2021-09-27 23:40:05 -07:00
// CheckAnchorInResource - check anchor key exists in resource and update the AnchorKey fields.
2020-08-29 06:52:22 +05:30
ac . CheckAnchorInResource ( typedPatternElement , typedResourceElement )
return validateMap ( log , typedResourceElement , typedPatternElement , originPattern , path , ac )
2019-12-30 17:08:50 -08:00
// array
case [ ] interface { } :
typedResourceElement , ok := resourceElement . ( [ ] interface { } )
if ! ok {
2020-03-17 11:05:20 -07:00
log . V ( 4 ) . Info ( "Pattern and resource have different structures." , "path" , path , "expected" , fmt . Sprintf ( "%T" , patternElement ) , "current" , fmt . Sprintf ( "%T" , resourceElement ) )
2022-05-15 02:34:35 +05:30
return path , fmt . Errorf ( "validation rule failed at path %s, resource does not satisfy the expected overlay pattern" , path )
2019-12-30 17:08:50 -08:00
}
2020-08-29 06:52:22 +05:30
return validateArray ( log , typedResourceElement , typedPatternElement , originPattern , path , ac )
2019-12-30 17:08:50 -08:00
// elementary values
case string , float64 , int , int64 , bool , nil :
/*Analyze pattern */
2020-12-04 09:28:30 -08:00
2021-04-28 23:31:55 +03:00
switch resource := resourceElement . ( type ) {
case [ ] interface { } :
for _ , res := range resource {
2023-01-10 14:07:26 +01:00
if ! pattern . Validate ( log , res , patternElement ) {
2021-10-03 03:15:22 -07:00
return path , fmt . Errorf ( "resource value '%v' does not match '%v' at path %s" , resourceElement , patternElement , path )
2021-04-28 23:31:55 +03:00
}
}
return "" , nil
default :
2023-01-10 14:07:26 +01:00
if ! pattern . Validate ( log , resourceElement , patternElement ) {
2021-10-03 03:15:22 -07:00
return path , fmt . Errorf ( "resource value '%v' does not match '%v' at path %s" , resourceElement , patternElement , path )
2021-04-28 23:31:55 +03:00
}
2019-12-30 17:08:50 -08:00
}
default :
2020-03-17 11:05:20 -07:00
log . V ( 4 ) . Info ( "Pattern contains unknown type" , "path" , path , "current" , fmt . Sprintf ( "%T" , patternElement ) )
2021-10-03 03:15:22 -07:00
return path , fmt . Errorf ( "failed at '%s', pattern contains unknown type" , path )
2019-12-30 17:08:50 -08:00
}
return "" , nil
}
// If validateResourceElement detects map element inside resource and pattern trees, it goes to validateMap
// For each element of the map we must detect the type again, so we pass these elements to validateResourceElement
2023-01-30 17:47:19 +05:30
func validateMap ( log logr . Logger , resourceMap , patternMap map [ string ] interface { } , origPattern interface { } , path string , ac * anchor . AnchorMap ) ( string , error ) {
2020-12-04 09:28:30 -08:00
patternMap = wildcards . ExpandInMetadata ( patternMap , resourceMap )
2019-12-30 17:08:50 -08:00
// check if there is anchor in pattern
// Phase 1 : Evaluate all the anchors
// Phase 2 : Evaluate non-anchors
anchors , resources := anchor . GetAnchorsResourcesFromMap ( patternMap )
2022-10-20 13:36:46 +02:00
keys := make ( [ ] string , 0 , len ( anchors ) )
for k := range anchors {
keys = append ( keys , k )
}
sort . Strings ( keys )
2019-12-30 17:08:50 -08:00
// Evaluate anchors
2024-01-29 21:06:39 +08:00
var skipErrors [ ] error
var applyCount int
2022-10-20 13:36:46 +02:00
for _ , key := range keys {
patternElement := anchors [ key ]
2019-12-30 17:08:50 -08:00
// get handler for each pattern in the pattern
// - Conditional
2020-01-24 12:05:53 -08:00
// - Existence
2019-12-30 17:08:50 -08:00
// - Equality
handler := anchor . CreateElementHandler ( key , patternElement , path )
2020-08-29 06:52:22 +05:30
handlerPath , err := handler . Handle ( validateResourceElement , resourceMap , origPattern , ac )
2019-12-30 17:08:50 -08:00
if err != nil {
2024-01-29 21:06:39 +08:00
if skip ( err ) {
skipErrors = append ( skipErrors , err )
continue
}
2024-05-22 12:04:14 +03:00
skipSiblingExists := false
for _ , skipError := range skipErrors {
if _ , ok := skipError . ( * PatternError ) ; ! ok {
skipSiblingExists = true
break
}
}
if ! skipSiblingExists {
return handlerPath , err
} else {
continue
}
2019-12-30 17:08:50 -08:00
}
2024-01-29 21:06:39 +08:00
applyCount ++
}
2024-05-22 12:04:14 +03:00
if len ( skipErrors ) > 0 {
if applyCount == 0 {
return path , & PatternError {
Err : multierr . Combine ( skipErrors ... ) ,
Path : path ,
Skip : true ,
}
} else {
skipSiblingExists := false
for _ , skipError := range skipErrors {
if _ , ok := skipError . ( * PatternError ) ; ! ok {
skipSiblingExists = true
break
}
}
if skipSiblingExists {
return path , & PatternError {
Err : multierr . Combine ( skipErrors ... ) ,
Path : path ,
Skip : true ,
}
}
2024-01-29 21:06:39 +08:00
}
2019-12-30 17:08:50 -08:00
}
2020-12-04 09:28:30 -08:00
2019-12-30 17:08:50 -08:00
// Evaluate resources
2020-08-29 06:52:22 +05:30
// getSortedNestedAnchorResource - keeps the anchor key to start of the list
sortedResourceKeys := getSortedNestedAnchorResource ( resources )
for e := sortedResourceKeys . Front ( ) ; e != nil ; e = e . Next ( ) {
key := e . Value . ( string )
handler := anchor . CreateElementHandler ( key , resources [ key ] , path )
handlerPath , err := handler . Handle ( validateResourceElement , resourceMap , origPattern , ac )
2019-12-30 17:08:50 -08:00
if err != nil {
return handlerPath , err
}
}
2022-01-04 17:36:33 -08:00
2019-12-30 17:08:50 -08:00
return "" , nil
}
2023-01-30 17:47:19 +05:30
func validateArray ( log logr . Logger , resourceArray , patternArray [ ] interface { } , originPattern interface { } , path string , ac * anchor . AnchorMap ) ( string , error ) {
2021-10-13 11:29:45 +02:00
if len ( patternArray ) == 0 {
2021-10-12 23:29:20 +02:00
return path , fmt . Errorf ( "pattern Array empty" )
2019-12-30 17:08:50 -08:00
}
switch typedPatternElement := patternArray [ 0 ] . ( type ) {
case map [ string ] interface { } :
// This is special case, because maps in arrays can have anchors that must be
// processed with the special way affecting the entire array
2020-12-04 09:28:30 -08:00
elemPath , err := validateArrayOfMaps ( log , resourceArray , typedPatternElement , originPattern , path , ac )
2019-12-30 17:08:50 -08:00
if err != nil {
2020-12-04 09:28:30 -08:00
return elemPath , err
2019-12-30 17:08:50 -08:00
}
2021-04-28 23:31:55 +03:00
case string , float64 , int , int64 , bool , nil :
elemPath , err := validateResourceElement ( log , resourceArray , typedPatternElement , originPattern , path , ac )
if err != nil {
return elemPath , err
}
2019-12-30 17:08:50 -08:00
default :
// In all other cases - detect type and handle each array element with validateResourceElement
2022-01-04 17:36:33 -08:00
if len ( resourceArray ) < len ( patternArray ) {
return "" , fmt . Errorf ( "validate Array failed, array length mismatch, resource Array len is %d and pattern Array len is %d" , len ( resourceArray ) , len ( patternArray ) )
}
var applyCount int
var skipErrors [ ] error
for i , patternElement := range patternArray {
currentPath := path + strconv . Itoa ( i ) + "/"
elemPath , err := validateResourceElement ( log , resourceArray [ i ] , patternElement , originPattern , currentPath , ac )
if err != nil {
if skip ( err ) {
skipErrors = append ( skipErrors , err )
continue
2020-07-09 11:50:05 -07:00
}
2022-01-04 17:36:33 -08:00
return elemPath , err
}
applyCount ++
}
if applyCount == 0 && len ( skipErrors ) > 0 {
return path , & PatternError {
2022-08-02 07:54:02 -07:00
Err : multierr . Combine ( skipErrors ... ) ,
2022-01-04 17:36:33 -08:00
Path : path ,
Skip : true ,
2019-12-30 17:08:50 -08:00
}
}
}
2022-01-04 17:36:33 -08:00
2019-12-30 17:08:50 -08:00
return "" , nil
}
// validateArrayOfMaps gets anchors from pattern array map element, applies anchors logic
// and then validates each map due to the pattern
2023-01-30 17:47:19 +05:30
func validateArrayOfMaps ( log logr . Logger , resourceMapArray [ ] interface { } , patternMap map [ string ] interface { } , originPattern interface { } , path string , ac * anchor . AnchorMap ) ( string , error ) {
2022-01-04 17:36:33 -08:00
applyCount := 0
skipErrors := make ( [ ] error , 0 )
2019-12-30 17:08:50 -08:00
for i , resourceElement := range resourceMapArray {
// check the types of resource element
2022-01-04 17:36:33 -08:00
// expect it to be a map, but can be anything ?:(
2019-12-30 17:08:50 -08:00
currentPath := path + strconv . Itoa ( i ) + "/"
2022-01-04 17:36:33 -08:00
returnPath , err := validateResourceElement ( log , resourceElement , patternMap , originPattern , currentPath , ac )
2019-12-30 17:08:50 -08:00
if err != nil {
2022-01-04 17:36:33 -08:00
if skip ( err ) {
skipErrors = append ( skipErrors , err )
2020-12-08 22:52:37 -08:00
continue
}
2022-01-04 17:36:33 -08:00
return returnPath , err
}
applyCount ++
}
if applyCount == 0 && len ( skipErrors ) > 0 {
return path , & PatternError {
2022-08-02 07:54:02 -07:00
Err : multierr . Combine ( skipErrors ... ) ,
2022-01-04 17:36:33 -08:00
Path : path ,
Skip : true ,
2019-12-30 17:08:50 -08:00
}
}
2022-01-04 17:36:33 -08:00
2019-12-30 17:08:50 -08:00
return "" , nil
}