Day 11: Arbitrary Images and the Supply Chain
Goal: You block images from foreign registries and
:latestwith a Kyverno ValidatingPolicy. · Duration: approx. 70 min · Prerequisite: Kyverno installed
Briefing
A cluster that starts any image from anywhere trusts half the world:
a typosquatted kubectl image, a backdoored base image, a
:latest that changes unnoticed. The defense is admission –
a webhook that checks every Pod before it starts. We use
Kyverno. Note: from version 1.20 on, Kyverno only runs
the new CEL-based types; the old ClusterPolicy has been
removed. So we write a ValidatingPolicy.
Prerequisite: Kyverno is installed (
helm install kyverno kyverno/kyverno -n kyverno --create-namespace). Check:kubectl -n kyverno get pods– four controllers are running.
Drill 1: The open state – everything runs
kubectl create namespace supply
kubectl -n supply run bad --image=busybox:latest --command -- sleep infinity
kubectl -n supply get pod bad -o jsonpath='{.status.phase}'; echo
kubectl -n supply delete pod badExpected: Pending/Running – the Pod is
accepted. :latest from an arbitrary registry, no questions
asked.
Save your progress? Sign up for free
Drill 2: The guardrail – ValidatingPolicy
Two rules in CEL: no :latest/missing tag, and only
allowed registries. Scope via a matchCondition on the
namespace:
kubectl apply -f - <<'EOF'
apiVersion: policies.kyverno.io/v1
kind: ValidatingPolicy
metadata:
name: image-guardrails
spec:
validationActions: ["Deny"]
matchConstraints:
resourceRules:
- apiGroups: [""]
apiVersions: ["v1"]
operations: ["CREATE","UPDATE"]
resources: ["pods"]
matchConditions:
- name: only-supply
expression: "request.namespace == 'supply'"
validations:
- expression: >-
object.spec.containers.all(c,
!c.image.endsWith(':latest') && (c.image.contains(':') || c.image.contains('@')))
message: ":latest oder fehlendes Tag ist verboten – nutze ein festes Tag oder Digest"
- expression: >-
object.spec.containers.all(c,
c.image.startsWith('docker.io/library/') || c.image.startsWith('registry.k8s.io/'))
message: "nur Images von docker.io/library oder registry.k8s.io erlaubt"
EOFSave your progress? Sign up for free
Drill 3: Proof – the dangerous images are rejected
:latest:
kubectl -n supply run bad --image=docker.io/library/busybox:latest --command -- sleep infinityExpected:
Error from server: admission webhook "vpol.validate.kyverno.svc-...-image-guardrails"
denied the request: Policy image-guardrails failed: :latest oder fehlendes Tag ist
verboten – nutze ein festes Tag oder Digest
A foreign registry (here a fictional “backdoor” image):
kubectl -n supply run eve --image=ghcr.io/evil/backdoor:1.0 --command -- sleep infinityExpected:
Error from server: admission webhook "..." denied the request: Policy image-guardrails
failed: nur Images von docker.io/library oder registry.k8s.io erlaubt
Save your progress? Sign up for free
Drill 4: Proof – the allowed image runs
kubectl -n supply run good --image=docker.io/library/busybox:1.36 --command -- sleep infinity
kubectl -n supply wait --for=condition=Ready pod/good --timeout=60sExpected:
pod/good condition met
The guardrail does not block across the board – it lets exactly what is allowed through. Clean up:
kubectl delete validatingpolicy image-guardrails
kubectl delete namespace supplySave your progress? Sign up for free
Checkpoint
After re-creating the policy and the namespace, the webhook rejects
:latest:
kubectl create namespace supply
# re-apply the policy from Drill 2, then:
kubectl -n supply run t --image=docker.io/library/busybox:latest --command -- sleep infinity \
2>&1 | grep -o "Policy image-guardrails failed: :latest.*Digest"
kubectl delete namespace supply; kubectl delete validatingpolicy image-guardrailsExpected (excerpt):
Policy image-guardrails failed: :latest ... Digest
Challenge
A registry allowlist and a :latest ban stop sloppiness
and obviously foreign images. But an attacker could tamper with an image
under your allowed name. The hard solution is signature
verification: Kyverno’s ImageValidatingPolicy
verifies cosign signatures and admits only signed images. Sketch which
fields such a policy needs.
Hint:
kubectl explain imagevalidatingpolicy.spec shows, among
others, matchImageReferences and attestors
(the public key(s) or the keyless identity).
Solution
At its core, an ImageValidatingPolicy needs: which image
references are checked (matchImageReferences), against
which trust anchor (attestors – a cosign public key or a
keyless identity with issuer/subject), and the statement that the
signature must be valid. The complete, tested setup with your own
registry and cosign key is in the course “Kyverno & NetworkPolicies
hands-on” (day “Verifying Images”).
Save your progress? Sign up for free
Enterprise Note
- Image guardrails cluster-wide (not just one namespace): allowed
registries, no
:latest, ideally pinning to a digest (@sha256:…). - Signatures mandatory (
ImageValidatingPolicy+ cosign), ideally keyless via a Sigstore identity of your own CI. - Scan images before admitting them (Trivy/Grype) and run your own registry (Harbor) with signatures, SBOM, and retention.
- Roll out policies in
Auditmode first, collect violations, then switch toDeny– see Day 12.