1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/engine/variables/operator/in.go

124 lines
3 KiB
Go
Raw Normal View History

2020-06-12 15:48:19 +05:30
package operator
import (
"encoding/json"
2020-06-12 15:48:19 +05:30
"fmt"
2020-11-29 00:35:33 -08:00
2020-11-28 23:29:15 -08:00
"github.com/minio/minio/pkg/wildcard"
2020-06-12 15:48:19 +05:30
"github.com/go-logr/logr"
"github.com/kyverno/kyverno/pkg/engine/context"
2020-06-12 15:48:19 +05:30
)
//NewInHandler returns handler to manage In operations
func NewInHandler(log logr.Logger, ctx context.EvalInterface, subHandler VariableSubstitutionHandler) OperatorHandler {
return InHandler{
ctx: ctx,
subHandler: subHandler,
log: log,
}
}
2020-06-26 12:33:49 +05:30
//InHandler provides implementation to handle In Operator
2020-06-12 15:48:19 +05:30
type InHandler struct {
ctx context.EvalInterface
subHandler VariableSubstitutionHandler
log logr.Logger
}
//Evaluate evaluates expression with In Operator
2020-06-24 12:27:08 +05:30
func (in InHandler) Evaluate(key, value interface{}) bool {
2020-06-12 15:48:19 +05:30
var err error
// substitute the variables
2020-06-24 12:27:08 +05:30
if key, err = in.subHandler(in.log, in.ctx, key); err != nil {
in.log.Error(err, "Failed to resolve variable", "variable", key)
2020-06-12 15:48:19 +05:30
return false
}
2020-06-24 12:27:08 +05:30
if value, err = in.subHandler(in.log, in.ctx, value); err != nil {
in.log.Error(err, "Failed to resolve variable", "variable", value)
2020-06-12 15:48:19 +05:30
return false
}
switch typedKey := key.(type) {
case string:
2020-11-28 23:29:15 -08:00
return in.validateValueWithStringPattern(typedKey, value)
2020-06-12 15:48:19 +05:30
default:
2020-06-24 12:27:08 +05:30
in.log.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 {
in.log.Info("expected type []string", "value", value, "type", fmt.Sprintf("%T", value))
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 {
v, ok := val.(string)
if !ok {
return true, false
}
if ok && wildcard.Match(key, v) {
return false, true
2020-06-24 12:27:08 +05:30
}
}
2020-11-17 13:07:30 -08:00
case string:
2020-11-28 23:29:15 -08:00
2020-11-29 00:35:33 -08:00
if wildcard.Match(valuesAvailable, key) {
return false, true
2020-11-28 23:29:15 -08: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)
return true, false
}
for _, val := range arr {
if key == val {
return false, true
}
}
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
return false, false
2020-06-12 15:48:19 +05:30
}
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
}