Skip to content

pkg build: add --secret to expose buildkit secrets - #4223

Open
europaul wants to merge 1 commit into
linuxkit:masterfrom
europaul:feat/pkg-build-secret
Open

europaul wants to merge 1 commit into
linuxkit:masterfrom
europaul:feat/pkg-build-secret

Conversation

@europaul

@europaul europaul commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

- What I did

Added a repeatable --secret flag to linuxkit pkg build (and so pkg push), using the buildx/buildctl syntax: id=X,env=VAR or id=X,src=PATH.

The motivation is Dockerfile git sources such as ADD https://github.com/org/repo.git#ref. buildkitd fetches these itself, running git with a cleared environment and HOME=/dev/null, so the caller's gitconfig and credential helpers never apply. The only way to authenticate them is the GIT_AUTH_TOKEN[.<host>] session secret, and linuxkit attached no secrets provider, so these fetches and their recursive submodules were always anonymous. GitHub intermittently refuses anonymous fetches from busy CI IPs, which fails package builds with could not read Username for 'https://github.com'. This hits packages with many submodules (edk2) especially hard. The flag also makes secrets available to RUN --mount=type=secret.

- How I did it

Plumbed the flag through the build options the same way --ssh is plumbed: pkg_build.go → pkglib.WithSecrets → spec.ImageBuildOptions.Secrets. dockerimpl.go then attaches buildkit's secrets provider, using the vendored build.ParseSecret parser from buildctl.

Unlike buildx, a secret sourced from an environment variable that is unset or empty is an error. Otherwise buildkit would send an empty git token and fail with an unrelated auth error. In GitHub Actions, secrets that are unavailable to a job, such as on fork PRs, expand to an empty string.

Secrets don't affect the package hash or tag.

- How to verify it

A package whose Dockerfile does ADD https://github.com/<private-repo>.git#<ref>:

  • linuxkit pkg build . fails with could not read Username for 'https://github.com'
  • GITHUB_TOKEN=... linuxkit pkg build --secret id=GIT_AUTH_TOKEN.github.com,env=GITHUB_TOKEN . succeeds
  • RUN --mount=type=secret,id=probe sees a second --secret id=probe,src=<file>
  • an unset or empty env= source fails with environment variable X is unset or empty

- Description for the changelog

linuxkit pkg build accepts --secret to expose buildkit secrets to builds, e.g. to authenticate Dockerfile git sources.

🤖 Generated with Claude Code

Dockerfile git sources such as `ADD https://github.com/org/repo.git#ref`
are fetched by buildkitd, which ignores any gitconfig and only
authenticates with the GIT_AUTH_TOKEN[.<host>] session secret. linuxkit
attached no secrets provider, so these fetches, and their submodules,
were always anonymous. GitHub intermittently refuses anonymous fetches,
failing package builds.

Add a repeatable --secret flag using the buildx/buildctl syntax
(id=X,env=VAR or id=X,src=PATH) and attach buildkit's secrets provider,
like --ssh does for agent forwarding. This also makes secrets available
to RUN --mount=type=secret.

Unlike buildx, an env source that is unset or empty is an error: buildkit
would otherwise send an empty git token and fail with an unrelated auth
error.

Signed-off-by: Paul Gaiduk <paulg@zededa.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
if typ == "env" && env == "" {
env = src
}
if env == "" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A bare --secret id=GIT_AUTH_TOKEN (no env=/src=) skips this check, but buildkit's secretsprovider.NewStore falls back to os.LookupEnv(id) and uses the variable if it is set, even when empty. So on a fork PR where GIT_AUTH_TOKEN expands to "" this still sends an empty token. Could you also handle the case where env and src are both empty and the variable named by id is set but empty? (If it is unset, NewStore treats id as a file path and fails on the stat, which is fine.)

@eriknordmark

Copy link
Copy Markdown
Contributor

@deitch could you take a look at this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants