From 0aebb2a88e7b564b506628d2d9a27a4660e61978 Mon Sep 17 00:00:00 2001 From: Maxim Goncharenko Date: Mon, 20 May 2019 17:07:09 +0300 Subject: [PATCH] Fixed int and float types mismatch --- pkg/engine/validation.go | 28 ++++++++++++++++++++++++++++ pkg/engine/validation_test.go | 14 ++++++++++++++ pkg/webhooks/server.go | 2 +- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/pkg/engine/validation.go b/pkg/engine/validation.go index 35d40393ab..77521deee7 100644 --- a/pkg/engine/validation.go +++ b/pkg/engine/validation.go @@ -151,9 +151,37 @@ func validateMapElement(resourcePart, patternPart interface{}) error { } return checkSingleValue(str, pattern) + case float64: + switch num := resourcePart.(type) { + case float64: + if num != pattern { + return fmt.Errorf("%f not equal %f", num, pattern) + } + case int64: + if float64(num) != pattern { + return fmt.Errorf("%d not equal %f", num, pattern) + } + default: + return fmt.Errorf("expected %T, found %T", patternPart, resourcePart) + } + case int64: + switch num := resourcePart.(type) { + case float64: + if num != float64(pattern) { + return fmt.Errorf("%f not equal %d", num, pattern) + } + case int64: + if float64(num) != float64(num) { + return fmt.Errorf("%d not equal %d", num, pattern) + } + default: + return fmt.Errorf("expected %T, found %T", patternPart, resourcePart) + } default: return fmt.Errorf("validating error: unknown type in map: %T", patternPart) } + + return nil } func getAnchorsFromMap(pattern map[string]interface{}) (map[string]interface{}, error) { diff --git a/pkg/engine/validation_test.go b/pkg/engine/validation_test.go index 93f5ca7360..eac1a884bf 100644 --- a/pkg/engine/validation_test.go +++ b/pkg/engine/validation_test.go @@ -370,3 +370,17 @@ func TestValidate_ServiceTest(t *testing.T) { assert.Assert(t, Validate(policy, rawResource, gvk) != nil) } + +func TestValidate_MapHasFloats(t *testing.T) { + rawPolicy := []byte(`{ "apiVersion": "kubepolicy.nirmata.io/v1alpha1", "kind": "Policy", "metadata": { "name": "policy-deployment-changed" }, "spec": { "rules": [ { "name": "First policy v2", "resource": { "kind": "Deployment", "name": "nginx-*" }, "mutate": { "patches": [ { "path": "/metadata/labels/isMutated", "op": "add", "value": "true" }, { "path": "/metadata/labels/app", "op": "replace", "value": "nginx_is_mutated" } ] }, "validate": { "message": "replicas number is wrong", "pattern": { "metadata": { "labels": { "app": "*" } }, "spec": { "replicas": 3 } } } } ] } }`) + rawResource := []byte(`{ "apiVersion": "apps/v1", "kind": "Deployment", "metadata": { "name": "nginx-deployment", "labels": { "app": "nginx" } }, "spec": { "replicas": 3, "selector": { "matchLabels": { "app": "nginx" } }, "template": { "metadata": { "labels": { "app": "nginx" } }, "spec": { "containers": [ { "name": "nginx", "image": "nginx:1.7.9", "ports": [ { "containerPort": 80 } ] } ] } } } }`) + + var policy kubepolicy.Policy + json.Unmarshal(rawPolicy, &policy) + + gvk := metav1.GroupVersionKind{ + Kind: "Deployment", + } + + assert.NilError(t, Validate(policy, rawResource, gvk)) +} diff --git a/pkg/webhooks/server.go b/pkg/webhooks/server.go index 4842004989..4d85a2ca02 100644 --- a/pkg/webhooks/server.go +++ b/pkg/webhooks/server.go @@ -149,7 +149,7 @@ func (ws *WebhookServer) HandleMutation(request *v1beta1.AdmissionRequest) *v1be if len(policyPatches) > 0 { namespace := engine.ParseNamespaceFromObject(request.Object.Raw) name := engine.ParseNameFromObject(request.Object.Raw) - ws.logger.Printf("Policy %s applied to %s %s/%s", policy.Name, request.Kind.Kind, namespace, name) + ws.logger.Printf("Mutation from policy %s has applied to %s %s/%s", policy.Name, request.Kind.Kind, namespace, name) } }