Skip to content

✨ Enable pass-through of private Git credentials to containers #9858

Closed

Description

@nipuna-perera

What are you trying to do?

I want to use my host's configured Git credentials within containers in Dagger.

Why is this important to you?

I want to be able to git fetch additional branches to what is already checked out within a git context. This is important for tools like SonarQube where it requires the context of the reference branch to do the analysis. There may be other use cases which require a fetch/pull from remote when working with current git directories. This is easy to do when you don't have to Auth to git (OSS repos). But for Enterprise use cases, it's not ergonomic.

How are you currently working around this?

The current workaround to this is re-authenticating each container with the PAT. An example:

	authCommand := "echo $GHT | gh auth login --with-token"
	credHelper := `git config --global credential.https://github.com.helper '!f() { sleep 1; echo "username=${GIT_USER}"; echo "password=${GHT}"; }; f'`

	cont := dag.Container().From("alpine/git:latest").
		WithExec([]string{"apk", "add", "--no-cache", "github-cli"}).
		WithMountedDirectory("/src", source).
		WithWorkdir("/src").
		WithSecretVariable("GHT", f.PersonalAccessToken).
		WithExec([]string{"sh", "-c", authCommand})

	ghUser, err := cont.WithExec([]string{"gh", "api", "user", "-q", ".login"}).Stdout(ctx)
	if err != nil {
		return nil, fmt.Errorf("error obtaining GitHub user: %w", err)
	}

	cont = cont.WithEnvVariable("GIT_USER", ghUser).
		WithExec([]string{"sh", "-c", credHelper})

	for _, branch := range branches {
		cont = cont.
			WithExec([]string{"git", "fetch", "-v", "origin", branch, "--tags"})
	}

	return cont.Directory("."), nil

This is cumbersome especially given that Dagger engine already has the Git credentials loaded in. For example, dagger passes through proxy, docker and CA certificates to the subsequent containers from the host. The same should happen with git credentials when private Git is involved.

Activity

  1. jedevc commented on Apr 24, 2025

    @jedevc
    Contributor

    Hm, I'm struggling to come up with how this would be ergonomic + secure - if the GitRef.tree is being mounted into a Container, we're not really able to auto-transfer a secret like that.

    It feels a little bit like the automatic-credential injection is part of the problem here - e.g. instead imagine a dag.SSHAgent or a dag.GitPAT function. Then you could easily just grab the credentials from there.

    The problem is this feels terrible from a security perspective. While today, the user ssh agent can be accessed from any WithExec (this is fine, because the keys themselves aren't actually ending up in the container), injecting a PAT like this would be a pretty bad security issue.

    Maybe this is linked to the more general "user-environment credentials" problem?


    Alternatively, maybe this is fixable by continuing to enhance the Git api, so we're not forced to fallback to WithExecs.

  2. nipuna-perera commented on Apr 24, 2025

    @nipuna-perera
    ContributorAuthor

    injecting a PAT like this would be a pretty bad security issue.

    By "this" do you mean what I'm doing above or what you are kind of suggesting?

    Alternatively, maybe this is fixable by continuing to enhance the Git api, so we're not forced to fallback to WithExecs.

    This is basically what I'm looking for :). I am not concerned much with the how. This is not an issue for a public repo where creds aren't required. But here, I know Dagger already has my host git config but I am doing my own auth flow using gh CLI to get that into a container. IF the git config can be pulled into a container I'd be satisfied. I don't think I'm concerned about having it in a specific directory for example with src.AsGit.

    The auth piece in my example is entirely in the context of the container and not a directory.

  3. locked and limited conversation to collaborators on Mar 25, 2026
  4. converted this issue into a discussion #12612 on Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions