2020-06-12 15:48:19 +05:30
|
|
|
package operator
|
|
|
|
|
|
|
|
import (
|
2020-09-22 18:26:52 -07:00
|
|
|
"encoding/json"
|
2020-06-12 15:48:19 +05:30
|
|
|
"fmt"
|
2020-11-29 00:35:33 -08:00
|
|
|
|
2020-06-12 15:48:19 +05:30
|
|
|
"github.com/go-logr/logr"
|
2023-10-30 00:59:53 +01:00
|
|
|
"github.com/kyverno/kyverno/ext/wildcard"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
2020-06-12 15:48:19 +05:30
|
|
|
)
|
|
|
|
|
2021-10-29 16:06:03 +01:00
|
|
|
// NewInHandler returns handler to manage In operations
|
|
|
|
//
|
|
|
|
// Deprecated: Use `NewAllInHandler` or `NewAnyInHandler` instead
|
2021-07-28 19:54:50 +03:00
|
|
|
func NewInHandler(log logr.Logger, ctx context.EvalInterface) OperatorHandler {
|
2020-06-12 15:48:19 +05:30
|
|
|
return InHandler{
|
2021-07-28 19:54:50 +03:00
|
|
|
ctx: ctx,
|
|
|
|
log: log,
|
2020-06-12 15:48:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 16:06:03 +01:00
|
|
|
// InHandler provides implementation to handle In Operator
|
2020-06-12 15:48:19 +05:30
|
|
|
type InHandler struct {
|
2021-07-28 19:54:50 +03:00
|
|
|
ctx context.EvalInterface
|
|
|
|
log logr.Logger
|
2020-06-12 15:48:19 +05:30
|
|
|
}
|
|
|
|
|
2021-10-29 16:06:03 +01:00
|
|
|
// Evaluate evaluates expression with In Operator
|
2021-07-28 19:54:50 +03:00
|
|
|
func (in InHandler) Evaluate(key, value interface{}) bool {
|
2020-06-12 15:48:19 +05:30
|
|
|
switch typedKey := key.(type) {
|
|
|
|
case string:
|
2020-11-28 23:29:15 -08:00
|
|
|
return in.validateValueWithStringPattern(typedKey, value)
|
2023-07-20 00:54:33 +08:00
|
|
|
case int, int32, int64, float32, float64, bool:
|
2021-10-29 09:54:51 +01:00
|
|
|
return in.validateValueWithStringPattern(fmt.Sprint(typedKey), value)
|
2021-02-26 18:53:54 +00:00
|
|
|
case []interface{}:
|
|
|
|
var stringSlice []string
|
|
|
|
for _, v := range typedKey {
|
|
|
|
stringSlice = append(stringSlice, v.(string))
|
|
|
|
}
|
|
|
|
return in.validateValueWithStringSetPattern(stringSlice, value)
|
2020-06-12 15:48:19 +05:30
|
|
|
default:
|
2022-08-18 18:54:59 +05:30
|
|
|
in.log.V(2).Info("Unsupported type", "value", typedKey, "type", fmt.Sprintf("%T", typedKey))
|
2020-06-12 15:48:19 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 23:29:15 -08:00
|
|
|
func (in InHandler) validateValueWithStringPattern(key string, value interface{}) (keyExists bool) {
|
|
|
|
invalidType, keyExists := keyExistsInArray(key, value, in.log)
|
2020-06-24 12:27:08 +05:30
|
|
|
if invalidType {
|
2022-08-18 18:54:59 +05:30
|
|
|
in.log.V(2).Info("expected type []string", "value", value, "type", fmt.Sprintf("%T", value))
|
2020-06-24 12:27:08 +05:30
|
|
|
return false
|
2020-06-12 15:48:19 +05:30
|
|
|
}
|
2020-06-24 12:27:08 +05:30
|
|
|
|
|
|
|
return keyExists
|
2020-06-12 15:48:19 +05:30
|
|
|
}
|
|
|
|
|
2020-11-28 23:29:15 -08:00
|
|
|
// keyExistsInArray checks if the key exists in the array value
|
|
|
|
// The value can be a string, an array of strings, or a JSON format
|
|
|
|
// array of strings (e.g. ["val1", "val2", "val3"].
|
|
|
|
func keyExistsInArray(key string, value interface{}, log logr.Logger) (invalidType bool, keyExists bool) {
|
|
|
|
switch valuesAvailable := value.(type) {
|
2020-06-24 12:27:08 +05:30
|
|
|
case []interface{}:
|
2020-11-28 23:29:15 -08:00
|
|
|
for _, val := range valuesAvailable {
|
2022-04-26 19:03:05 +01:00
|
|
|
if wildcard.Match(fmt.Sprint(val), key) || wildcard.Match(key, fmt.Sprint(val)) {
|
2020-11-30 12:57:26 -08:00
|
|
|
return false, true
|
2020-06-24 12:27:08 +05:30
|
|
|
}
|
|
|
|
}
|
2020-11-17 13:07:30 -08:00
|
|
|
|
2020-09-23 02:41:49 +05:30
|
|
|
case string:
|
2020-11-29 00:35:33 -08:00
|
|
|
if wildcard.Match(valuesAvailable, key) {
|
2020-11-30 12:57:26 -08:00
|
|
|
return false, true
|
2020-11-28 23:29:15 -08:00
|
|
|
}
|
|
|
|
|
2020-09-22 18:26:52 -07:00
|
|
|
var arr []string
|
2020-11-28 23:29:15 -08:00
|
|
|
if err := json.Unmarshal([]byte(valuesAvailable), &arr); err != nil {
|
2020-11-29 00:35:33 -08:00
|
|
|
log.Error(err, "failed to unmarshal value to JSON string array", "key", key, "value", value)
|
2020-11-30 12:57:26 -08:00
|
|
|
return true, false
|
2020-09-22 18:26:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range arr {
|
2020-09-23 02:41:49 +05:30
|
|
|
if key == val {
|
2020-11-30 12:57:26 -08:00
|
|
|
return false, true
|
2020-09-23 02:41:49 +05:30
|
|
|
}
|
|
|
|
}
|
2020-11-28 23:29:15 -08:00
|
|
|
|
2020-06-24 12:27:08 +05:30
|
|
|
default:
|
2020-11-28 23:29:15 -08:00
|
|
|
invalidType = true
|
|
|
|
return
|
2020-06-12 15:48:19 +05:30
|
|
|
}
|
2020-06-24 12:27:08 +05:30
|
|
|
|
2020-11-30 12:57:26 -08:00
|
|
|
return false, false
|
2020-06-12 15:48:19 +05:30
|
|
|
}
|
|
|
|
|
2021-02-10 21:05:36 +00:00
|
|
|
func (in InHandler) validateValueWithStringSetPattern(key []string, value interface{}) (keyExists bool) {
|
2021-03-16 01:29:24 +05:30
|
|
|
invalidType, isIn := setExistsInArray(key, value, in.log, false)
|
2021-02-10 21:05:36 +00:00
|
|
|
if invalidType {
|
2022-08-18 18:54:59 +05:30
|
|
|
in.log.V(2).Info("expected type []string", "value", value, "type", fmt.Sprintf("%T", value))
|
2021-02-10 21:05:36 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-16 01:29:24 +05:30
|
|
|
return isIn
|
2021-02-10 21:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setExistsInArray checks if the key is a subset of value
|
|
|
|
// The value can be a string, an array of strings, or a JSON format
|
|
|
|
// array of strings (e.g. ["val1", "val2", "val3"].
|
2021-03-16 01:29:24 +05:30
|
|
|
// notIn argument if set to true will check for NotIn
|
|
|
|
func setExistsInArray(key []string, value interface{}, log logr.Logger, notIn bool) (invalidType bool, keyExists bool) {
|
2021-02-10 21:05:36 +00:00
|
|
|
switch valuesAvailable := value.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
var valueSlice []string
|
|
|
|
for _, val := range valuesAvailable {
|
|
|
|
v, ok := val.(string)
|
|
|
|
if !ok {
|
|
|
|
return true, false
|
|
|
|
}
|
|
|
|
valueSlice = append(valueSlice, v)
|
|
|
|
}
|
2021-03-16 01:29:24 +05:30
|
|
|
if notIn {
|
|
|
|
return false, isNotIn(key, valueSlice)
|
|
|
|
}
|
|
|
|
return false, isIn(key, valueSlice)
|
2021-02-10 21:05:36 +00:00
|
|
|
|
|
|
|
case string:
|
|
|
|
|
|
|
|
if len(key) == 1 && key[0] == valuesAvailable {
|
|
|
|
return false, true
|
|
|
|
}
|
|
|
|
|
|
|
|
var arr []string
|
|
|
|
if err := json.Unmarshal([]byte(valuesAvailable), &arr); err != nil {
|
|
|
|
log.Error(err, "failed to unmarshal value to JSON string array", "key", key, "value", value)
|
|
|
|
return true, false
|
|
|
|
}
|
2021-03-16 01:29:24 +05:30
|
|
|
if notIn {
|
|
|
|
return false, isNotIn(key, arr)
|
|
|
|
}
|
2021-02-26 18:53:54 +00:00
|
|
|
|
2021-03-16 01:29:24 +05:30
|
|
|
return false, isIn(key, arr)
|
2021-02-10 21:05:36 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return true, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 01:29:24 +05:30
|
|
|
// isIn checks if all values in S1 are in S2
|
|
|
|
func isIn(key []string, value []string) bool {
|
|
|
|
set := make(map[string]bool)
|
2021-02-10 21:05:36 +00:00
|
|
|
|
|
|
|
for _, val := range value {
|
2021-03-16 01:29:24 +05:30
|
|
|
set[val] = true
|
2021-02-10 21:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range key {
|
2021-03-16 01:29:24 +05:30
|
|
|
_, found := set[val]
|
2021-02-10 21:05:36 +00:00
|
|
|
if !found {
|
|
|
|
return false
|
2021-03-16 01:29:24 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-16 14:26:41 -04:00
|
|
|
// isNotIn checks if any of the values in S1 is not in S2
|
2021-03-16 01:29:24 +05:30
|
|
|
func isNotIn(key []string, value []string) bool {
|
|
|
|
set := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, val := range value {
|
|
|
|
set[val] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range key {
|
|
|
|
_, found := set[val]
|
2021-03-16 14:26:41 -04:00
|
|
|
if !found {
|
|
|
|
return true
|
2021-02-10 21:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 14:26:41 -04:00
|
|
|
return false
|
2021-02-10 21:05:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 23:29:15 -08:00
|
|
|
func (in InHandler) validateValueWithBoolPattern(_ bool, _ interface{}) bool {
|
2020-06-24 12:27:08 +05:30
|
|
|
return false
|
|
|
|
}
|
2020-06-12 15:48:19 +05:30
|
|
|
|
2020-11-28 23:29:15 -08:00
|
|
|
func (in InHandler) validateValueWithIntPattern(_ int64, _ interface{}) bool {
|
2020-06-12 15:48:19 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-28 23:29:15 -08:00
|
|
|
func (in InHandler) validateValueWithFloatPattern(_ float64, _ interface{}) bool {
|
2020-06-12 15:48:19 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-28 23:29:15 -08:00
|
|
|
func (in InHandler) validateValueWithMapPattern(_ map[string]interface{}, _ interface{}) bool {
|
2020-06-24 12:27:08 +05:30
|
|
|
return false
|
2020-06-12 15:48:19 +05:30
|
|
|
}
|
|
|
|
|
2020-11-28 23:29:15 -08:00
|
|
|
func (in InHandler) validateValueWithSlicePattern(_ []interface{}, _ interface{}) bool {
|
2020-06-24 12:27:08 +05:30
|
|
|
return false
|
2020-06-12 15:48:19 +05:30
|
|
|
}
|