From 10b16ce9e9643fe973f6fa1208d62ba255198ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Thu, 4 Nov 2021 21:40:08 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20issues=20in=20Kyverno=20po?= =?UTF-8?q?licies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- k8s/kyverno-pod-color-2.yaml | 8 +++--- k8s/kyverno-pod-color-3.yaml | 19 ++++++++------ slides/k8s/kyverno.md | 48 +++++++++++++++++++++++------------- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/k8s/kyverno-pod-color-2.yaml b/k8s/kyverno-pod-color-2.yaml index ae98f43f..484abfb3 100644 --- a/k8s/kyverno-pod-color-2.yaml +++ b/k8s/kyverno-pod-color-2.yaml @@ -16,11 +16,11 @@ spec: operator: Equals value: UPDATE - key: "{{ request.oldObject.metadata.labels.color }}" - operator: Equals - value: "*" + operator: NotEquals + value: "" - key: "{{ request.object.metadata.labels.color }}" - operator: Equals - value: "*" + operator: NotEquals + value: "" validate: message: "Once label color has been added, it cannot be changed." deny: diff --git a/k8s/kyverno-pod-color-3.yaml b/k8s/kyverno-pod-color-3.yaml index 1a15e8be..2f7e9794 100644 --- a/k8s/kyverno-pod-color-3.yaml +++ b/k8s/kyverno-pod-color-3.yaml @@ -6,20 +6,23 @@ spec: validationFailureAction: enforce background: false rules: - - name: prevent-color-removal + - name: prevent-color-change match: resources: kinds: - Pod - selector: - matchExpressions: - - key: color - operator: DoesNotExist + preconditions: + - key: "{{ request.operation }}" + operator: Equals + value: UPDATE + - key: "{{ request.oldObject.metadata.labels.color }}" + operator: NotEquals + value: "" + - key: "{{ request.object.metadata.labels.color }}" + operator: Equals + value: "" validate: message: "Once label color has been added, it cannot be removed." deny: conditions: - - key: "{{ request.oldObject.metadata.labels.color }}" - operator: NotIn - value: [] diff --git a/slides/k8s/kyverno.md b/slides/k8s/kyverno.md index 14c23f4a..b0ed828f 100644 --- a/slides/k8s/kyverno.md +++ b/slides/k8s/kyverno.md @@ -100,11 +100,11 @@ - Policy and ClusterPolicy (per-namespace and cluster-scope policies) - - PolicyViolation and ClusterPolicyViolation (used in audit mode) + - PolicyReport and ClusterPolicyReport (used in audit mode) - GenerateRequest (used internally when generating resources asynchronously) -- We will be able to do e.g. `kubectl get policyviolations --all-namespaces` +- We will be able to do e.g. `kubectl get clusterpolicyreports --all-namespaces` (to see policy violations across all namespaces) @@ -266,16 +266,34 @@ - *match* all pods + - add a *precondition* matching pods that have a `color` label +
+ (both in their "before" and "after" states) + - *deny* these pods if their `color` label has changed +- Again, other approaches are possible! + +--- + +## Invalid references + +- We can access the `color` label through `{{ request.object.metadata.labels.color }}` + +- If we reference a label (or any field) that doesn't exist, the policy fails + +- Except in *preconditions*: it then evaluates to an empty string + +- We use a *precondition* to makes sure the label exists in both "old" and "new" objects + +- Then in the *deny* block we can compare the old and new values + + (and reject changes) + - "Old" and "new" versions of the pod can be referenced through `{{ request.oldObject }}` and `{{ request.object }}` -- Our label is available through `{{ request.object.metadata.labels.color }}` - -- Again, other approaches are possible! - --- ## Our second Kyverno policy @@ -351,9 +369,7 @@ (the `AdmissionRequest` object is the thing that gets submitted to the webhook) -- Kyverno lets us access the `AdmissionRequest` object - - (and in particular, `{{ request.object }}` and `{{ request.oldObject }}`) +- We access the `AdmissionRequest` object through `{{ request }}` -- @@ -373,19 +389,17 @@ --- - - ## Background checks - What about the `test-color-0` pod that we create initially? @@ -451,6 +463,8 @@ ] +(Sometimes it takes a little while for the infringement to show up, though.) + --- ## Generating objects