Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use checksum in ADD from ENV #3287

Merged
merged 1 commit into from
Nov 21, 2022

Conversation

giggsoff
Copy link
Contributor

We cannot use checksum from environment variables (for example with ADD --checksum=${HASH} ${LINK} /tmp/foo), which made this option useless in cases when we define url using environment variables and adjust them using some logic around.
Let's add checksum field to Expand function to fill it before sending out.

PR contains implementation and test

Comment on lines +66 to +68
ENV DIGEST=%s
ENV LINK=%s
ADD --checksum=${DIGEST} ${LINK} /tmp/foo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using environment variables and adjust them using some logic around.

I'm a bit confused by this part, as both ENV and ADD --checksum=<some_env> are hard-coded in the Dockerfile, or was that just to avoid having to include the digest twice?

I seem to recall we added substitution for some other flags; were those flags accepting ENV, ARG (or both?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably also update the documentation to mention that variable substitution is supported for this option (https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#verifying-a-remote-file-checksum-add---checksumchecksum-http-src-dest)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused by this part, as both ENV and ADD --checksum=<some_env> are hard-coded in the Dockerfile, or was that just to avoid having to include the digest twice?

It is just small sample I use as test here. Having digest as an ENV will make Dockefile looks better as we will have all ENVs in one place where we put the link, the checksum (version, options, something else...). Also in case of complex behavior when we use different versions (different links) for different target arches we should have control on checksum field, so we should use variables.
For example:

FROM foo-build-base AS foo-build-amd64
ENV FOO_VERSION=2
ENV FOO_DIGEST=sha256:...
ENV FOO_OPTIONS=...
ENV FOO_PATCHES=...

FROM foo-build-base AS foo-build-arm64
ENV FOO_VERSION=1
ENV FOO_DIGEST=sha256:...
ENV FOO_OPTIONS=...
ENV FOO_PATCHES=...

FROM foo-build-${TARGETARCH} AS foo-build
ADD --checksum=${FOO_DIGEST} ${FOO_LINK}/${FOO_VERSION}.tar.gz /foo.tar.gz
RUN build_foo...

Do you want me to modify the test somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably also update the documentation to mention that variable substitution is supported for this option (https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#verifying-a-remote-file-checksum-add---checksumchecksum-http-src-dest)

It seems to me that this is a separate PR with improvements in the documentation. I can see a general mention of substitution support for the ADD command here: https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#environment-replacement. But going into details, we expand source, destination, chown, but they are not mentioned either. And mentioning that the checksum field supports substitution would give the wrong impression (at least to me) that other fields don't.

@@ -254,6 +253,12 @@ func (c *AddCommand) Expand(expander SingleWordExpander) error {
}
c.Chown = expandedChown

expandedChecksum, err := expander(c.Checksum)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better if you keep the Checksum as digest.Digest. In here after expansion, you should call digest.Parse() to convert the string to Digest and error if the value is invalid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately it is not possible to store AddCommand's Checksum as digest.Digest (I can only try to do validation without type cast inside Expand, but it looks quite misplaced) as we set it before expansion. Sorry if I misunderstood your suggestion.
I modified the code and now convert (and check) during filling of copyConfig.

We cannot use checksum from environment variables (for example with
`ADD --checksum=${DIGEST} ${LINK} /tmp/foo`), which made this
option useless in cases when we define url using environment variables
and adjust them using some logic around.
Let's add checksum field to Expand function to fill it before sending
out.

Signed-off-by: Petr Fedchenkov <giggsoff@gmail.com>
@tonistiigi tonistiigi merged commit b20bc6a into moby:master Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants