You can control which images are allowed to run on your cluster using the ImagePolicy admission plug-in (currently considered beta). It allows you to control:
-
The source of images: which registries can be used to pull images
-
Image resolution: force pods to run with immutable digests to ensure the image does not change due to a re-tag
-
Container image label restrictions: force an image to have or not have particular labels
-
Image annotation restrictions: force an image in the integrated container registry to have or not have particular annotations
To enable this feature, configure the plug-in in master-config.yaml
:
admissionConfig:
pluginConfig:
openshift.io/ImagePolicy:
configuration:
kind: ImagePolicyConfig
apiVersion: v1
resolveImages: AttemptRewrite (1)
executionRules: (2)
- name: execution-denied
# Reject all images that have the annotation images.openshift.io/deny-execution set to true.
# This annotation may be set by infrastructure that wishes to flag particular images as dangerous
onResources: (3)
- resource: pods
- resource: builds
reject: true (4)
matchImageAnnotations: (5)
- key: images.openshift.io/deny-execution
value: "true"
skipOnResolutionFailure: true (6)
- name: allow-images-from-internal-registry
# allows images from the internal registry and tries to resolve them
onResources:
- resource: pods
- resource: builds
matchIntegratedRegistry: true
- name: allow-images-from-dockerhub
onResources:
- resource: pods
- resource: builds
matchRegistries:
- docker.io
resolutionRules: (7)
- targetResource:
resource: pods
localNames: true
- targetResource: (8)
group: batch
resource: jobs
localNames: true (9)
-
Try to resolve images to an immutable image digest and update the image pull specification in the pod.
-
Array of rules to evaluate against incoming resources. If you only have reject==true rules, the default is allow all. If you have any accept rule, the default is deny all.
-
Indicates which resources to enforce rules upon. If nothing is specified, the default is pods.
-
Indicates that if this rule matches, the pod should be rejected.
-
List of annotations to match on the image object’s metadata.
-
If you are not able to resolve the image, do not fail the pod.
-
Array of rules allowing use of image streams in Kubernetes resources. By default, this will allow pods, replication controllers, replica sets, deployments, and jobs to use image streams.
-
Identifies the group and resource for image stream resolution. If resource is
*
, this rule will apply to all resources in that group. -
LocalNames
will allow single segment names to be interpreted as namespace-local image stream tags, but only if the target image stream tag has theresolveLocalNames
field set.
Note
|
If you normally rely on infrastructure images being pulled using a default
registry prefix (such as docker.io or registry.access.redhat.com), those
images will not match to any |
-
Use the
openshift/image-policy-check
to test your configuration.For example, use the information above, then test like this:
oc import-image openshift/image-policy-check:latest --confirm
-
Create a pod using this YAML. The pod should be created.
apiVersion: v1 kind: Pod metadata: generateName: test-pod spec: containers: - image: docker.io/openshift/image-policy-check:latest name: first
-
Create another pod pointing to a different registry. The pod should be rejected.
apiVersion: v1 kind: Pod metadata: generateName: test-pod spec: containers: - image: different-registry/openshift/image-policy-check:latest name: first
-
Create a pod pointing to the internal registry using the imported image. The pod should be created and if you look at the image specification, you should see a digest in place of the tag.
apiVersion: v1 kind: Pod metadata: generateName: test-pod spec: containers: - image: <internal registry IP>:5000/<namespace>/image-policy-check:latest name: first
-
Create a pod pointing to the internal registry using the imported image. The pod should be created and if you look at the image specification, you should see the tag unmodified.
apiVersion: v1 kind: Pod metadata: generateName: test-pod spec: containers: - image: <internal registry IP>:5000/<namespace>/image-policy-check:v1 name: first
-
Get the digest from
oc get istag/image-policy-check:latest
and use it foroc annotate images/<digest> images.openshift.io/deny-execution=true
. For example:$ oc annotate images/sha256:09ce3d8b5b63595ffca6636c7daefb1a615a7c0e3f8ea68e5db044a9340d6ba8 images.openshift.io/deny-execution=true
-
Create this pod again, and you should see the pod rejected:
apiVersion: v1 kind: Pod metadata: generateName: test-pod spec: containers: - image: <internal registry IP>:5000/<namespace>/image-policy-check:latest name: first