Skip to content

fix(core/git): ensure HTTP auth is propagated to submodules - #10855

Merged
grouville merged 6 commits into
dagger:mainfrom
grouville:git-submodules
Aug 19, 2025
Merged

grouville merged 6 commits into
dagger:mainfrom
grouville:git-submodules

Conversation

@grouville

@grouville grouville commented Aug 8, 2025 •

Copy link
Copy Markdown
Member

Fixes https://discord.com/channels/707636530424053791/1397694731496067194/1397694731496067194

Previously, HTTP(S) authentication worked only for the parent repository clone. Submodule clones failed because the auth header (http.:///.extraheader) passed via CLI args (-c) wasn’t inherited by nested Git processes (git submodule update). By injecting the header through the GIT_CONFIG_* environment variables instead, the auth configuration propagates to submodules automatically, fixing the inheritance issue and ensuring consistent authentication.

Update 2025-08-12

Since opening this PR, I’ve made a few adjustments:

  • Implemented shallow fetch fallback:

Now we always attempt git submodule update with --depth=1 first. If the server doesn’t support shallow clones (fatal: dumb http transport does not support shallow capabilities), we retry without the --depth argument. This preserves performance benefits where supported while ensuring compatibility.

  • Added smart-HTTP test helper:

Extracted the previously inline smart-HTTP setup into a reusable helper (gitSmartHTTPServiceDirAuth). This simplifies writing tests involving authenticated smart-HTTP git clones, which are necessary to properly support shallow fetches.

  • Extended submodule auth tests:

Expanded TestSubmoduleAuth to verify behavior for both smart and dumb HTTP protocols using the same repository setup. This clearly demonstrates the new fallback mechanism works correctly in both cases.

  • Reduced PR scope:

Removed unrelated refactors and kept the changes tightly focused on submodule auth and shallow-fetch behavior.

@grouville
grouville marked this pull request as ready for review August 8, 2025 00:30
@grouville
grouville requested a review from jedevc August 8, 2025 00:31
Comment thread core/integration/git_test.go Resolved
Comment thread core/git.go Resolved
Comment thread core/git.go Resolved
@grouville
grouville force-pushed the git-submodules branch 4 times, most recently from 513f112 to 8ac538e Compare August 9, 2025 01:52
Comment thread core/git.go Resolved
Comment thread core/git.go Resolved
Comment thread core/git.go Resolved
Comment thread core/git.go Resolved
Comment thread core/integration/git_test.go Resolved
Comment thread core/git.go Resolved
Comment thread core/integration/git_test.go Resolved
@grouville
grouville marked this pull request as draft August 12, 2025 08:04
@grouville
grouville requested review from jedevc and marcosnils August 12, 2025 20:34
@grouville
grouville marked this pull request as ready for review August 12, 2025 20:34
Comment thread core/integration/git_test.go Resolved
Comment thread core/integration/services_test.go Resolved
Comment thread core/integration/git_test.go Resolved
Comment thread util/gitutil/config_env_test.go Resolved
Comment thread util/gitutil/config_env_test.go Resolved
Comment thread util/gitutil/config_env_test.go Resolved
@grouville

Copy link
Copy Markdown
Member Author

Failing because of: #10878

Comment thread core/git.go Resolved
@grouville
grouville force-pushed the git-submodules branch 2 times, most recently from c80d112 to ca6b0fe Compare August 19, 2025 05:39

@jedevc jedevc left a comment

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.

Nice! 🎉

Thanks for splitting out to the helpers, now that's a really nice interface we can use in the future as well!

@jedevc jedevc added this to the v0.18.17 milestone Aug 19, 2025
@jedevc jedevc added the needs/changelog Pull requests that require a changelog entry label Aug 19, 2025
@jedevc

jedevc commented Aug 19, 2025

Copy link
Copy Markdown
Contributor

@grouville just needs a brief oneline changelog, then feel free to merge!

…auth

Previously we passed `http.<remote>.extraheader` via `-c` CLI args. That only
applies to the top-level `git` process, so `git submodule update --init --recursive`
did not inherit the `Authorization` header and submodule fetches failed under
HTTP(S) auth.

This change injects the header through `GIT_CONFIG_*` environment variables for
every git invocation, appending a host-scoped key:
  http.<scheme>://<host>/.extraheader = Authorization: <value>
Using env-config makes nested `git` processes inherit the same auth, which fixes
submodule clones that resolve relative to the parent’s host.

Notes:
- `AuthHeader` continues to be the header *value* (e.g., `Basic …` / `Bearer …`);
  we add the `Authorization: ` prefix at injection time.
- `AuthToken` still builds `basic <base64(user:pass)>` with host defaults
  (`x-access-token`, or `x-token-auth` for Bitbucket).
- We scope by `scheme://host` to avoid leaking headers to other hosts.

Limitation:
- Cross-host submodules are still unauthenticated; addressing that requires API
  support (see dagger#10863).

Signed-off-by: Guillaume de Rouville <[email protected]>
Introduce gitSmartHTTPServiceDirAuth — a tiny lighttpd+git-http-backend
helper (optional Basic auth) that serves a directory of bare repos.

Add TestSubmoduleAuth:
- Creates bare /srv/submodule.git and /srv/parent.git
- Adds submodule via absolute file:// then rewrites .gitmodules to a
  relative URL (../submodule.git) to mirror real repos
- Serves over smart-HTTP and clones parent with HTTPAuthToken
- Verifies both parent and submodule files are fetched
- Negative case without auth fails

Why smart-HTTP? `git submodule update --depth=1` performs a shallow
fetch; dumb HTTP can’t advertise shallow capability, so the test must
use smart-HTTP.

This test reproduces the original bug (submodules didn’t inherit
http.extraheader) and now passes with env-based injection.

Signed-off-by: Guillaume de Rouville <[email protected]>
…HTTP

git submodule update --init --recursive now attempts a shallow update
first (--depth=1) and, if the fetch fails with an error indicating the
server “does not support shallow” (i.e., dumb HTTP), retries without the
depth flag.

Signed-off-by: Guillaume de Rouville <[email protected]>
Follow-up addressing maintainer review:

- core/git: Capitalize HTTP Authorization scheme as "Basic" (fix typo)
- core/integration/git_test: Replace uuid.NewString() with identity.NewID() + check on exact error message ("git error", "Authentication failed")
- core/integration/services_test: Replace previous lighttpd CGI setup with simpler nginx+fcgiwrap solution for smart-HTTP git backend
- util/gitutil/config_env_test.go: Introduce parseKeyLine helper function to avoid repeated parsing logic

Signed-off-by: Guillaume de Rouville <[email protected]>
Following Justin's feedback, this commit encapsulates HTTPS authorization header and configuration logic within gitutil itself. This approach simplifies callers by abstracting internal details, ensuring consistent handling of authentication across git operations, including nested commands such as submodule fetches.

Key reasoning:
- Centralizes the logic for building HTTP authorization headers and ensures submodule clones inherit authentication.

Signed-off-by: Guillaume de Rouville <[email protected]>
Signed-off-by: Guillaume de Rouville <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs/changelog Pull requests that require a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants