Allowed registries with a ValidatingAdmissionPolicy
Author: Fabrice JAMMES (LinkedIn).
Objective
Only accept the pods whose images come from an allowed registry, with a ValidatingAdmissionPolicy: an admission rule written in CEL, run by the API server itself, without any webhook.
The CKS curriculum lists “Allowlist registries” in the Supply chain security domain. A ValidatingAdmissionPolicy (GA since Kubernetes 1.30) has two parts:
| Object | Role |
|---|---|
ValidatingAdmissionPolicy | What is checked: the resources and the CEL expressions |
ValidatingAdmissionPolicyBinding | Where and how: the namespaces, and the action (Deny, Warn, Audit) |
The lab works on a cluster shared by the trainees: the policy and its binding are cluster-wide objects, so their names end with your account, and the binding only applies to your namespace.
Prerequisites
Exercise 1: the policy, in warning mode
Write a ValidatingAdmissionPolicy named allowed-registries-$(whoami) which checks that the images of all the containers and init containers of a pod start with registry.k8s.io/. Bind it to your namespace only, with the Warn action.
Then run a pod nginx with the image nginx:1.27. What happens?
Tip
In CEL, object is the pod sent to the API server. list.all(x, condition) is true if the condition is true for each element, and has(object.spec.initContainers) tests if a field is set. The namespace of an object is selected with its label kubernetes.io/metadata.name.
Exercise 2: deny
Change the action of the binding to Deny. Then try these images: nginx:1.27, docker.io/library/nginx:1.27 and registry.k8s.io/pause:3.10.
Warning
The cluster is shared: a binding without namespaceSelector applies to all the namespaces. With Deny, it would reject the pods of the other trainees, and the system pods (Traefik, the CNI…) as soon as they restart, since their images come from Docker Hub. Before switching to Deny, check that the binding only selects your namespace:
The trainer finds a binding without selector, and deletes it, with:
Exercise 3: a Deployment
Create a Deployment web with the image nginx:1.27. Is it created? Does it run? Where is the error?
Exercise 4: init containers and other namespaces
- Create a pod whose container uses
registry.k8s.io/pause:3.10and whose init container usesbusybox:1.36. - Run
nginx:1.27in thedefaultnamespace, with--dry-run=server.
Questions
- Why
registry.k8s.io/with the trailing/, and notregistry.k8s.io? - Who can bypass this policy?
- What are the other ways to allow only some registries?