2020-01-07 23:13:57 +00:00
|
|
|
package operator
|
|
|
|
|
|
|
|
import (
|
2020-03-17 18:05:20 +00:00
|
|
|
"fmt"
|
2020-01-07 23:13:57 +00:00
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
"github.com/go-logr/logr"
|
2020-10-07 18:12:31 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
2023-03-24 10:01:49 +00:00
|
|
|
datautils "github.com/kyverno/kyverno/pkg/utils/data"
|
2022-08-25 05:23:01 +00:00
|
|
|
wildcard "github.com/kyverno/kyverno/pkg/utils/wildcard"
|
2022-05-17 05:56:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
2020-01-07 23:13:57 +00:00
|
|
|
)
|
|
|
|
|
2022-05-17 06:19:03 +00:00
|
|
|
// NewEqualHandler returns handler to manage Equal operations
|
2021-07-28 16:54:50 +00:00
|
|
|
func NewEqualHandler(log logr.Logger, ctx context.EvalInterface) OperatorHandler {
|
2020-01-07 23:13:57 +00:00
|
|
|
return EqualHandler{
|
2021-07-28 16:54:50 +00:00
|
|
|
ctx: ctx,
|
|
|
|
log: log,
|
2020-01-07 23:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 06:19:03 +00:00
|
|
|
// EqualHandler provides implementation to handle NotEqual Operator
|
2020-01-07 23:13:57 +00:00
|
|
|
type EqualHandler struct {
|
2021-07-28 16:54:50 +00:00
|
|
|
ctx context.EvalInterface
|
|
|
|
log logr.Logger
|
2020-01-07 23:13:57 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 06:19:03 +00:00
|
|
|
// Evaluate evaluates expression with Equal Operator
|
2021-07-28 16:54:50 +00:00
|
|
|
func (eh EqualHandler) Evaluate(key, value interface{}) bool {
|
2020-01-07 23:13:57 +00:00
|
|
|
// key and value need to be of same type
|
2020-02-14 19:59:28 +00:00
|
|
|
switch typedKey := key.(type) {
|
2020-01-07 23:13:57 +00:00
|
|
|
case bool:
|
2020-11-29 07:29:15 +00:00
|
|
|
return eh.validateValueWithBoolPattern(typedKey, value)
|
2020-01-07 23:13:57 +00:00
|
|
|
case int:
|
2020-11-29 07:29:15 +00:00
|
|
|
return eh.validateValueWithIntPattern(int64(typedKey), value)
|
2020-01-07 23:13:57 +00:00
|
|
|
case int64:
|
2020-11-29 07:29:15 +00:00
|
|
|
return eh.validateValueWithIntPattern(typedKey, value)
|
2020-01-07 23:13:57 +00:00
|
|
|
case float64:
|
2020-11-29 07:29:15 +00:00
|
|
|
return eh.validateValueWithFloatPattern(typedKey, value)
|
2020-01-07 23:13:57 +00:00
|
|
|
case string:
|
2020-11-29 07:29:15 +00:00
|
|
|
return eh.validateValueWithStringPattern(typedKey, value)
|
2020-01-07 23:13:57 +00:00
|
|
|
case map[string]interface{}:
|
2020-02-14 19:59:28 +00:00
|
|
|
return eh.validateValueWithMapPattern(typedKey, value)
|
2020-01-07 23:13:57 +00:00
|
|
|
case []interface{}:
|
2020-02-14 19:59:28 +00:00
|
|
|
return eh.validateValueWithSlicePattern(typedKey, value)
|
2020-01-07 23:13:57 +00:00
|
|
|
default:
|
2022-08-18 13:24:59 +00:00
|
|
|
eh.log.V(2).Info("Unsupported type", "value", typedKey, "type", fmt.Sprintf("%T", typedKey))
|
2020-01-07 23:13:57 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eh EqualHandler) validateValueWithSlicePattern(key []interface{}, value interface{}) bool {
|
|
|
|
if val, ok := value.([]interface{}); ok {
|
2023-03-24 10:01:49 +00:00
|
|
|
return datautils.DeepEqual(key, val)
|
2020-01-07 23:13:57 +00:00
|
|
|
}
|
2022-08-18 13:24:59 +00:00
|
|
|
eh.log.V(2).Info("Expected type []interface{}", "value", value, "type", fmt.Sprintf("%T", value))
|
2020-01-07 23:13:57 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eh EqualHandler) validateValueWithMapPattern(key map[string]interface{}, value interface{}) bool {
|
|
|
|
if val, ok := value.(map[string]interface{}); ok {
|
2023-03-24 10:01:49 +00:00
|
|
|
return datautils.DeepEqual(key, val)
|
2020-01-07 23:13:57 +00:00
|
|
|
}
|
2022-08-18 13:24:59 +00:00
|
|
|
eh.log.V(2).Info("Expected type map[string]interface{}", "value", value, "type", fmt.Sprintf("%T", value))
|
2020-01-07 23:13:57 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:29:15 +00:00
|
|
|
func (eh EqualHandler) validateValueWithStringPattern(key string, value interface{}) bool {
|
2021-10-29 08:54:51 +00:00
|
|
|
// We need to check duration first as it's the only type that can be compared to a different type.
|
|
|
|
durationKey, durationValue, err := parseDuration(key, value)
|
|
|
|
if err == nil {
|
|
|
|
return durationKey.Seconds() == durationValue.Seconds()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to extract resource quantity from string.
|
|
|
|
resourceKey, err := resource.ParseQuantity(key)
|
|
|
|
if err == nil {
|
|
|
|
switch typedValue := value.(type) {
|
|
|
|
case string:
|
|
|
|
resourceValue, err := resource.ParseQuantity(typedValue)
|
|
|
|
if err != nil {
|
|
|
|
eh.log.Error(fmt.Errorf("parse error: "), "Failed to parse value type doesn't match key type")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return resourceKey.Equal(resourceValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 23:13:57 +00:00
|
|
|
if val, ok := value.(string); ok {
|
2020-11-29 08:35:33 +00:00
|
|
|
return wildcard.Match(val, key)
|
2020-01-07 23:13:57 +00:00
|
|
|
}
|
2020-03-17 18:05:20 +00:00
|
|
|
|
2022-08-18 13:24:59 +00:00
|
|
|
eh.log.V(2).Info("Expected type string", "value", value, "type", fmt.Sprintf("%T", value))
|
2020-01-07 23:13:57 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:29:15 +00:00
|
|
|
func (eh EqualHandler) validateValueWithFloatPattern(key float64, value interface{}) bool {
|
2020-01-07 23:13:57 +00:00
|
|
|
switch typedValue := value.(type) {
|
|
|
|
case int:
|
|
|
|
// check that float has not fraction
|
|
|
|
if key == math.Trunc(key) {
|
|
|
|
return int(key) == typedValue
|
|
|
|
}
|
2022-08-18 13:24:59 +00:00
|
|
|
eh.log.V(2).Info("Expected type float, found int", "typedValue", typedValue)
|
2020-01-07 23:13:57 +00:00
|
|
|
case int64:
|
|
|
|
// check that float has not fraction
|
|
|
|
if key == math.Trunc(key) {
|
|
|
|
return int64(key) == typedValue
|
|
|
|
}
|
2022-08-18 13:24:59 +00:00
|
|
|
eh.log.V(2).Info("Expected type float, found int", "typedValue", typedValue)
|
2020-01-07 23:13:57 +00:00
|
|
|
case float64:
|
|
|
|
return typedValue == key
|
|
|
|
case string:
|
|
|
|
// extract float from string
|
|
|
|
float64Num, err := strconv.ParseFloat(typedValue, 64)
|
|
|
|
if err != nil {
|
2020-03-17 18:05:20 +00:00
|
|
|
eh.log.Error(err, "Failed to parse float64 from string")
|
2020-01-07 23:13:57 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return float64Num == key
|
|
|
|
default:
|
2022-08-18 13:24:59 +00:00
|
|
|
eh.log.V(2).Info("Expected type float", "value", value, "type", fmt.Sprintf("%T", value))
|
2020-01-07 23:13:57 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:29:15 +00:00
|
|
|
func (eh EqualHandler) validateValueWithBoolPattern(key bool, value interface{}) bool {
|
2020-01-07 23:13:57 +00:00
|
|
|
typedValue, ok := value.(bool)
|
|
|
|
if !ok {
|
2022-08-18 13:24:59 +00:00
|
|
|
eh.log.V(2).Info("Expected type bool", "value", value, "type", fmt.Sprintf("%T", value))
|
2020-01-07 23:13:57 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return key == typedValue
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:29:15 +00:00
|
|
|
func (eh EqualHandler) validateValueWithIntPattern(key int64, value interface{}) bool {
|
2020-01-07 23:13:57 +00:00
|
|
|
switch typedValue := value.(type) {
|
|
|
|
case int:
|
|
|
|
return int64(typedValue) == key
|
|
|
|
case int64:
|
|
|
|
return typedValue == key
|
|
|
|
case float64:
|
|
|
|
// check that float has no fraction
|
|
|
|
if typedValue == math.Trunc(typedValue) {
|
|
|
|
return int64(typedValue) == key
|
|
|
|
}
|
2022-08-18 13:24:59 +00:00
|
|
|
eh.log.V(2).Info("Expected type int, found float", "value", typedValue, "type", fmt.Sprintf("%T", typedValue))
|
2020-01-07 23:13:57 +00:00
|
|
|
return false
|
|
|
|
case string:
|
|
|
|
// extract in64 from string
|
|
|
|
int64Num, err := strconv.ParseInt(typedValue, 10, 64)
|
|
|
|
if err != nil {
|
2020-03-17 18:05:20 +00:00
|
|
|
eh.log.Error(err, "Failed to parse int64 from string")
|
2020-01-07 23:13:57 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return int64Num == key
|
|
|
|
default:
|
2022-08-18 13:24:59 +00:00
|
|
|
eh.log.V(2).Info("Expected type int", "value", value, "type", fmt.Sprintf("%T", value))
|
2020-01-07 23:13:57 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|