Skip to content

fix: use git credential helper's username for modules with http auth - #10427

Closed
skycaptain wants to merge 0 commit into
dagger:mainfrom
skycaptain:main
Closed

skycaptain wants to merge 0 commit into
dagger:mainfrom
skycaptain:main

Conversation

@skycaptain

Copy link
Copy Markdown
Contributor

In #8989, an issue emerged with GitLab CI's CI_JOB_TOKENs: Right now, when accessing remote modules via HTTP authentication using dagger call -m gitlab.com/..., the username is hardcoded to x-access-token (except for bitbucket.com). However, as specified in GitLab’s documentation, username gitlab-ci-token is required when using basic authentication with CI_JOB_TOKEN. In the discussion, we recognized that not only receiving the secret but also the username from the git credential helper would solve the issue best.

Several potential solutions came to my mind:

  • Add another option httpAuthUsername to RemoteGitRepository and pass the username alongside httpAuthToken. It would also change the interface of GitRepository, and while this isn't related to the original problem, it makes it easier to set the username when calling a git repo from within a module (currently, the httpAuthHeader arg must be composed manually). This was my first approach until I realized I could solve the issue without extensive code refactoring. However, I was considering adding two helper functions to GitRepository: .WithHttpAuthHeaderFromBasicAuth(username, password) and .WithHttpAuthHeaderFromBearerAuth(token) to simplify httpAuthHeader formatting.
  • Use httpAuthHeader instead of httpAuthToken to leverage existing code with minimal refactoring. While this solution works for the PR, it reveals overlapping logic between core.RemoteGitRepository.setup and core.schema.gitSchema.git. Further refactoring may be beneficial to address this duplication.

Currently, this PR doesn't address another desirable CLI feature: being able to pass credentials within the URL. At present, username and password parameters in URLs are ignored to best of my knowledge. Adding this capability would allow using the format dagger call -m https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/... and also passing repositories as directories from the command line in general. This would eliminate the need to set up a git credential helper, which can be cumbersome in some CI environments. However, during implementation, I discovered that additional refactoring is needed to make this work. Therefore, this PR only addresses the GitLab CI issue from #8989.

Finally, do you have any suggestions for how to write test for these changes?

@skycaptain skycaptain changed the title fix: pass username with dagger modules from remote http ref fix: use git credential helper's username for modules with http auth May 19, 2025
@jedevc
jedevc requested review from grouville and jedevc May 19, 2025 16:50
@grouville

grouville commented May 20, 2025 •

Copy link
Copy Markdown
Member

Thanks for this PR 🙏 Given that all the tests pass, it seems to be a working implementation. I'll dig a bit more tomorrow on the implications (haven't thoroughly checked yet). I'll add a corresponding test too in the meantime.

Privileges on the test repo are mandatory for the implementation of this test ahah, so no worries 😇

Thanks again !

PS: regarding having the PAT inside the ref, I worry that we couldn't ensure 0 leak of information in case the token gets cached

@skycaptain

skycaptain commented May 20, 2025 •

Copy link
Copy Markdown
Contributor Author

[...] I'll dig a bit more tomorrow on the implications (haven't thoroughly checked yet). [...]

Sure! I absolutely understand that a change like this needs to be thought through and tested thoroughly to avoid breaking existing functionality or leaking sensitive data. Let me know, if I can help out.

I'll add a corresponding test too in the meantime.

Thanks!

Comment thread core/schema/git.go
Value: dagql.NewString(remote.String()),
},
{
Name: "httpAuthToken",

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.

I would say we can add a new httpAuthUsername here that does what you suggest.

@grouville grouville May 20, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I’m completely fine adding the httpAuthUsername field. Given this involves an API change, do you think we should include Solomon for the API design review?

Would you prefer extending this PR or handling it as a separate follow-up? I’m impressed by how elegantly this contribution solves the issue, although it means tokens are always sent as headers instead of traditional tokens—this is acceptable, but it does trigger my correctness alert. Still, I’m comfortable moving forward if we agree it’s not critical.

@jedevc

jedevc commented May 20, 2025

Copy link
Copy Markdown
Contributor

LGTM, we need a test though.

@grouville why do we need to make changes to the test repo here? This should just be possible to test just using ExperimentalServiceHost.

There's no tests that cover the behavior of the auto-loading credentials for just plain git clones (only for modules).

func privateRepoSetup(c *dagger.Client, t *testctx.T, tc vcsTestCase) (dagger.WithContainerFunc, func()) {
var socket *dagger.Socket
cleanup := func() {}
if tc.sshKey {
var sockPath string
sockPath, cleanup = setupPrivateRepoSSHAgent(t)
socket = c.Host().UnixSocket(sockPath)
}
return func(ctr *dagger.Container) *dagger.Container {
if socket != nil {
ctr = ctr.
WithUnixSocket("/sock/unix-socket", socket).
WithEnvVariable("SSH_AUTH_SOCK", "/sock/unix-socket")
}
if token := tc.token(); token != "" {
ctr = ctr.
WithExec([]string{
"git", "config", "--global",
"credential.https://" + tc.expectedHost + ".helper",
`!f() { test "$1" = get && echo -e "password=` + token + `\nusername=git"; }; f`,
})
}
return ctr
}, cleanup
}

We should adapt the above to git_test.go and add tests for this through there.

@grouville

grouville commented May 20, 2025 •

Copy link
Copy Markdown
Member

Hey @jedevc, I’m not 100% sure I’ve fully grasped all your points here 🙏👼

Just to confirm my understanding clearly:

  1. On the testing approach, you asked:

@grouville why do we need to make changes to the test repo here? This should just be possible to test just using ExperimentalServiceHost.

My initial intent was testing the specific GitLab edge case (CI_JOB_TOKEN requires explicit username). But your suggestion seems broader: instead of an integration test using a real token, you’d prefer spinning up a local git server with ExperimentalServiceHost and checking credentials locally. Did I get that right?

  1. About “plain git clones”, you wrote:

There's no tests that cover the behavior of the auto-loading credentials for just plain git clones (only for modules).

Could you clarify what you mean by “plain git clones”? Currently, dag.Git() calls within module functions intentionally don’t auto-load credentials unless explicitly passed (see Discord ref). So, your comment made me a bit unsure.

Let me know if I missed something!

@github-actions

github-actions Bot commented Jun 4, 2025

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@grouville

grouville commented Jun 4, 2025 •

Copy link
Copy Markdown
Member

I have a pending PR with the requested changes -- will push them after this week 🙏

@grouville

grouville commented Jun 18, 2025 •

Copy link
Copy Markdown
Member

@skycaptain I tried to push my branch with a few commits on top of your fork's main (as this is the reference of this PR) by force pushing (you only had a commit [that I have on my fork], so it felt safe) -- it worked to update the main branch's HEAD, but I'm getting errors atm, due to branch permissions. This updated your fork's main:
image

Can you please pick the commits from this branch: https://github.com/grouville/dagger/tree/with-authtoken and reopen the PR please ? I don't have the rights 🙏 Your commit is still present cc @jedevc

@skycaptain

Copy link
Copy Markdown
Contributor Author

😮 Yeah, I see that force-pushing somehow removed my commit and might have auto-closed this PR. Since I don't have permission to reopen it, I created a new one: #10605. Since my commit was reverted anyway, starting with a fresh PR containing only our changes would have probably been easier though 😀.

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.

3 participants