Skip to content

Comments

bake: derive git auth host from remote URL#3648

Open
crazy-max wants to merge 1 commit intodocker:masterfrom
crazy-max:bake-auth-token-domain
Open

bake: derive git auth host from remote URL#3648
crazy-max wants to merge 1 commit intodocker:masterfrom
crazy-max:bake-auth-token-domain

Conversation

@crazy-max
Copy link
Member

@crazy-max crazy-max commented Feb 10, 2026

This PR refactors Bake Git authentication secret handling by introducing a dedicated gitauth helper that centralizes how secrets are built from environment variables. The same helper is now used in both build option creation and remote bake file reads, so the behavior is consistent across those paths while preserving existing support for BUILDX_BAKE_GIT_AUTH_TOKEN and BUILDX_BAKE_GIT_AUTH_HEADER.

It also adds automatic host-based Git auth secret derivation for remote Bake invocations. When a remote URL is in play, Bake now emits both base BuildKit secret IDs and host-scoped IDs (for example GIT_AUTH_TOKEN.<host> / GIT_AUTH_HEADER.<host>) based on the resolved remote URL logic, enabling per-host auth routing automatically without introducing host-suffixed auth env vars.

@crazy-max crazy-max force-pushed the bake-auth-token-domain branch from ac8f506 to 8573243 Compare February 10, 2026 15:12
@crazy-max crazy-max added this to the v0.32.0 milestone Feb 10, 2026
@crazy-max crazy-max force-pushed the bake-auth-token-domain branch from 8573243 to 22217e5 Compare February 10, 2026 15:26
@crazy-max crazy-max requested a review from tonistiigi February 10, 2026 15:38
@crazy-max crazy-max marked this pull request as ready for review February 10, 2026 15:38
@crazy-max

This comment was marked as outdated.

Copy link
Member

@tonistiigi tonistiigi left a comment

Choose a reason for hiding this comment

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

I don't think users should need to set the host in the env variable. We can just get the host from the remote URL and connect these automatically.

@crazy-max crazy-max force-pushed the bake-auth-token-domain branch from 22217e5 to eebe1e5 Compare February 17, 2026 15:28
@crazy-max crazy-max changed the title bake: support host-suffixed git auth env vars bake: derive git auth host from remote URL Feb 17, 2026
@crazy-max crazy-max force-pushed the bake-auth-token-domain branch from eebe1e5 to ac54d55 Compare February 18, 2026 14:02
@crazy-max crazy-max requested a review from tonistiigi February 18, 2026 14:08
bake/bake.go Outdated
}

func isRemoteContext(t build.Inputs, inp *Input) bool {
func remoteContextURL(t build.Inputs, inp *Input) string {
Copy link
Member

Choose a reason for hiding this comment

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

nit: bit cleaner to return string, bool from such functions.

bake/gitauth.go Outdated
return nil
}
secrets := make(buildflags.Secrets, 0, len(hosts)+1)
secrets = append(secrets, &buildflags.Secret{
Copy link
Member

Choose a reason for hiding this comment

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

I still don't get it.

(assuming this isn't breaking existing users badly), this should be only set if the Bake command is using remote input, not based on if the target is using remote URL or not what seems to happen atm.

Additionally, there is no point in adding the main secret key and host key with the same value. Only host key should be set, and only for the host that was "bake remote URL", not any host that happened to be remote URL for a bake target.

Copy link
Member Author

Choose a reason for hiding this comment

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

there is no point in adding the main secret key and host key with the same value.

Ah right, this should only emit host-scoped git auth secrets (no generic main key).

(assuming this isn't breaking existing users badly), this should be only set if the Bake command is using remote input, not based on if the target is using remote URL or not what seems to happen atm.

I kept target remote-context handling for backward compatibility for now. I'm a bit hesitant to switch to strict remote input only immediately, because the current behavior follows context resolution in

buildx/bake/bake.go

Lines 1294 to 1331 in 268f1c7

func updateContext(t *build.Inputs, inp *Input) {
if inp == nil || inp.State == nil {
return
}
for k, v := range t.NamedContexts {
if v.Path == "." {
t.NamedContexts[k] = build.NamedContext{Path: inp.URL}
}
if strings.HasPrefix(v.Path, "cwd://") || strings.HasPrefix(v.Path, "target:") || strings.HasPrefix(v.Path, "docker-image:") {
continue
}
if urlutil.IsRemoteURL(v.Path) {
continue
}
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/"), llb.WithCustomNamef("set context %s to %s", k, v.Path))
t.NamedContexts[k] = build.NamedContext{State: &st, Path: inp.URL}
}
if t.ContextPath == "." {
t.ContextPath = inp.URL
return
}
if strings.HasPrefix(t.ContextPath, "cwd://") {
return
}
if urlutil.IsRemoteURL(t.ContextPath) {
return
}
st := llb.Scratch().File(
llb.Copy(*inp.State, t.ContextPath, "/", &llb.CopyInfo{
CopyDirContentsOnly: true,
}),
llb.WithCustomNamef("set context to %s", t.ContextPath),
)
t.ContextState = &st
t.ContextPath = inp.URL
}

And changing that could break existing flows. If you prefer, I can still change it to strict remote input only behavior.

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
@crazy-max crazy-max force-pushed the bake-auth-token-domain branch from ac54d55 to f43adf9 Compare February 23, 2026 09:08
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.

2 participants