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-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"
|
2020-10-07 11:12:31 -07:00
|
|
|
"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 {
|
2020-11-30 12:57:26 -08:00
|
|
|
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
|
|
|
|
2020-09-23 02:41:49 +05:30
|
|
|
case string:
|
2020-11-28 23:29:15 -08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|