How to Squash OpenShift Docker Build Layers to Remove Secrets

Austin Dewey
3 min readJun 26, 2020
Squash..Get it? | Photo by Melanie Hughes on Unsplash

When you build a Docker image, a series of layers get cached to speed up subsequent builds. However, this caching can have adverse effects if an image layer contains secrets, such as credentials. A malicious person or process could find the layer containing your secrets and discover its contents.

Luckily, when performing Docker builds in OpenShift, you can quickly destroy secret layers by setting imageOptimizationPolicy to SkipLayers in your BuildConfig.

Let’s first consider the following Dockerfile:

This Dockerfile installs jq to a UBI8-based image. However, rather than download jq from their releases page, it instead downloads from a local Nexus instance. It is common in the enterprise to download tools from a repository within the corporate network rather than from an upstream repository. However, to download from the corporate repository, you must first authenticate by providing your credentials. Credentials are provided in the example Dockerfile by copying them from a file in the build context (nexus-creds in this case).

Here’s the million-dollar question: How do you remove these credentials from the image so that they cannot be revealed? If you think the answer is to remove the credentials in a RUN command (rm -rf nexus-creds), you are partially correct. That is a necessary step and will remove the credentials from the final container. However, it will not remove the credentials from the COPY layer. The COPY layer and the credentials it contains will still remain in the container registry. As a result, an attacker could steal your credentials by accessing this layer.

Below is partial output of the “oc start-build” command that builds the example Dockerfile. Here, you’ll see each of the layers that got pushed to the internal registry, with the COPY layer being represented by Line 5.

Line 5 shows the COPY layer

So, if removing the credentials in a RUN statement is only partially correct, what else can you do to prevent credentials from being stolen? Red Hat addresses this by allowing you to provide a BuildConfig setting called imageOptimizationPolicy. When setting this to SkipLayers, the OpenShift build will squash each of the layers added to the base image (in other words, it will squash everything down to the FROM command). Since running “rm -rf nexus-creds” after the COPY command removes the credentials from the final layer, you can rest assured that the credentials will be removed entirely from both the image and the registry itself by squashing the layers with imageOptimizationPolicy.

Below is an example of a BuildConfig that uses the imageOptimizationPolicy: SkipLayers setting.

The imageOptimizationPolicy is on Line 19

When this BuildConfig is applied, the resulting image will have only three layers (two from the base image and one from the image we are building off of that base). Below is the ending output of “oc start-build” after the imageOptimizationPolicy setting is applied.

Only three layers this time instead of four! If you were to access your image registry or pull this image down from outside the cluster, there would be no trace of the COPY layer or your credentials for this given image and tag.

Thanks for Reading!

Hopefully, this helps you prevent secrets from being exposed within intermediate layers stored in your image registry. For more information, be sure to check out Red Hat’s documentation on imageOptimizationPolicy and squashing layers in Docker builds.

Originally published at https://austindewey.com on June 26, 2020.

--

--

Austin Dewey

Kubernetes enthusiast and not-terrible guitarist. Co-author of Learn Helm. | https://austindewey.com