Skip to content

bake: derive git auth host from remote URL#3648

Merged
crazy-max merged 1 commit intodocker:masterfrom
crazy-max:bake-auth-token-domain
Feb 24, 2026
Merged

bake: derive git auth host from remote URL#3648
crazy-max merged 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.

@crazy-max crazy-max force-pushed the bake-auth-token-domain branch from ac54d55 to f43adf9 Compare February 23, 2026 09:08
bake/bake.go Outdated
Env: "BUILDX_BAKE_GIT_AUTH_HEADER",
})
}
if remoteURL, ok := remoteContextURL(bi, inp); ok {
Copy link
Member

Choose a reason for hiding this comment

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

The change in remote.go seems right, but this one still seems to use target.ContextPath as ther remote URL, what may not be correct. A target context URL and the bake remote definition URL may not be the same and these env secrets should be based on the remote definition URL only iiuc.

Copy link
Member Author

Choose a reason for hiding this comment

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

but this one still seems to use target.ContextPath as ther remote URL, what may not be correct

Ok I was following the 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
}
where ContextPath is set to inp.URL but not needed. I'm changing this.

@crazy-max crazy-max force-pushed the bake-auth-token-domain branch from f43adf9 to a5b81ef Compare February 24, 2026 16:31
@crazy-max crazy-max requested a review from tonistiigi February 24, 2026 16:32
bake/bake.go Outdated
Env: "BUILDX_BAKE_GIT_AUTH_HEADER",
})
}
if inp != nil && urlutil.IsRemoteURL(inp.URL) && !strings.HasPrefix(bi.ContextPath, "cwd://") {
Copy link
Member

Choose a reason for hiding this comment

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

This should do some kind of 1) both are URLs 2) both URLs use same host , check instead of just !cwd://

@crazy-max crazy-max force-pushed the bake-auth-token-domain branch from a5b81ef to 26c7ae6 Compare February 24, 2026 17:09
@crazy-max crazy-max requested a review from tonistiigi February 24, 2026 17:10
@crazy-max crazy-max merged commit 989bab0 into docker:master Feb 24, 2026
159 checks passed
@crazy-max crazy-max deleted the bake-auth-token-domain branch February 24, 2026 17:35
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