Skip to content

docs: Using git credential helpers in CI examples #8989

Closed

Description

@grouville

Some users have been encountering issues when trying out out Git credential helpers in CI

I am moving the conversation to its own issue @pspurlock, and cleaning up the other (everything was screenshotted)

image image image image

After discussing with @nipuna-perera, they do manage to make it work ; It seems to be a configuration issue / patterns to use, which shall become a standard.

Next step on my end:

  • Working repro for GitHub / GitLab / BitBucket
  • Isolate the pattern and document it

Activity

  1. self-assigned this
    on Nov 18, 2024
  2. grouville commented on Nov 18, 2024

    @grouville
    MemberAuthor

    hey @pspurlock

    I have a working implementation in a gitlab-ci.yml:

    image: docker:latest
    
    services:
      - name: docker:${DOCKER_VERSION}-dind
        alias: docker
    
    variables:
      DOCKER_HOST: tcp://docker:2376
      DOCKER_TLS_VERIFY: '1'
      DOCKER_TLS_CERTDIR: '/certs'
      DOCKER_CERT_PATH: '/certs/client'
      DOCKER_DRIVER: overlay2
      DOCKER_VERSION: '24.0.6'
      GIT_USERNAME: "grouville"
      GIT_TOKEN: "${PAT}"
    
    stages:
      - build
    
    dagger_job:
      stage: build
      before_script:
        # Install required packages
        - apk add --no-cache git curl
    
        # Install Dagger
        - curl -L https://dl.dagger.io/dagger/install.sh | sh
        - mv bin/dagger /usr/local/bin
        - chmod +x /usr/local/bin/dagger
    
        # Configure Git credentials with improved script
        - |
          cat > /tmp/git-credential-helper.sh << 'EOF'
          #!/bin/sh
          case "$1" in
              get)
                  if [ -n "$GIT_USERNAME" ] && [ -n "$GIT_TOKEN" ]; then
                      echo "username=$GIT_USERNAME"
                      echo "password=$GIT_TOKEN"
                  fi
                  ;;
              store)
                  # Don't store credentials
                  ;;
              erase)
                  # Don't need to erase credentials
                  ;;
          esac
          exit 0
          EOF
        - chmod +x /tmp/git-credential-helper.sh
    
        # Configure git with the helper and additional settings
        - git config --global credential."https://github.com".helper "/tmp/git-credential-helper.sh"
        - git config --global credential."https://github.com".useHttpPath true
    
      script:
        # Run Dagger commands
        - dagger -m github.com/grouville/daggerverse-private/zip functions
    
        # Graceful shutdown for cache
        - |
          if docker ps --filter name="dagger-engine-*" -q; then
            docker stop -t 300 $(docker ps --filter name="dagger-engine-*" -q)
          fi
    

    Just add the PAT secret as an env var and replace the dagger command as well as the username

    Here, everything is in-memory, so pretty safe. We could also extend to several other providers by adding many credential helpers and with such command:

    git config credential."https://gitlab.com".helper "/tmp/git-credential-helper-gitlab.sh"

    cc @vikram-dagger, where shall i document those examples ?

  3. vikram-dagger commented on Nov 19, 2024

    @vikram-dagger
    Contributor

    cc @vikram-dagger, where shall i document those examples ?

    https://docs.dagger.io/integrations/gitlab would work - thanks for creating this example :)

  4. pspurlock commented on Nov 19, 2024

    @pspurlock

    @grouville Thanks for doing this. I can't try out your example fully because dind requires privileged mode for the runner which we don't allow. However, I did try to mimic it as much as possible and still get the same access denied error.

    For context, we have a gitlab runner deployed alongside a dagger engine/deamon in a cluster. When we run a CI Job, we then use _EXPERIMENTAL_DAGGER_RUNNER_HOST so that the CI Job will connect and use the daemon running in the cluster. So when dagger -m mymodule call ... is ran, which git credential context is being used here? The one running in the CI Job that is running the dagger call command, or the host we are connecting to?

    I have tried to add credentials in both places and neither have worked, but it would help to clarify. I know it isn't a credential issue because I can run the same credential helper commands and then git clone successfully before the dagger command fails.

    Thanks a ton for your quick response/feedback on this.

  5. grouville commented on Nov 19, 2024

    @grouville
    MemberAuthor

    Mmmh, weird 😢 i'd be happy to sync with you to debug that live

    Normally, the source of truth is always the CLI, so the one running in the CI Job that is running the dagger call command. I'll try with the _EXPERIMENTAL_DAGGER_RUNNER_HOST, if there's a bug, but I don't think so.

  6. grouville commented on Nov 19, 2024

    @grouville
    MemberAuthor

    I have tried to add credentials in both places and neither have worked, but it would help to clarify. I know it isn't a credential issue because I can run the same credential helper commands and then git clone successfully before the dagger command fails.

    If you make the same behavior as I did above, basically setting your own credential helper, if you add a step that actually prints it:

    - script:
        # this should print user and pat -- please ensure that 
        - /tmp/git-credential-helper.sh get 
        # Run Dagger command
        - dagger -m github.com/grouville/daggerverse-private/zip functions
  7. grouville commented on Nov 19, 2024

    @grouville
    MemberAuthor

    @grouville Thanks for doing this. I can't try out your example fully because dind requires privileged mode for the runner which we don't allow. However, I did try to mimic it as much as possible and still get the same access denied error.

    For context, we have a gitlab runner deployed alongside a dagger engine/deamon in a cluster. When we run a CI Job, we then use _EXPERIMENTAL_DAGGER_RUNNER_HOST so that the CI Job will connect and use the daemon running in the cluster. So when dagger -m mymodule call ... is ran, which git credential context is being used here? The one running in the CI Job that is running the dagger call command, or the host we are connecting to?

    I have tried to add credentials in both places and neither have worked, but it would help to clarify. I know it isn't a credential issue because I can run the same credential helper commands and then git clone successfully before the dagger command fails.

    Thanks a ton for your quick response/feedback on this.

    Ok, could you please share what your gitconfig looks like ? Even privately, it would help. Are you using a proxy ? Git is a beast, and maybe there's other config interfering in this git cli ? does the clone work directly inside the runner too ?

    Basically, I want to ensure that you don't have a .gitconfig conflicting here:

    #!/bin/bash
    set -e
    
    # Function to show all git config
    show_git_config() {
        echo "Active Git Config:"
        git config --list
    }
    
    # Backup original configs
    OLD_GIT_CONFIG_GLOBAL=$GIT_CONFIG_GLOBAL
    OLD_GIT_CONFIG_SYSTEM=$GIT_CONFIG_SYSTEM
    
    # Get credentials
    creds=$(echo -e "host=github.com\nprotocol=https" | git credential fill)
    username=$(echo "$creds" | grep "username=" | cut -d= -f2)
    password=$(echo "$creds" | grep "password=" | cut -d= -f2)
    
    # Show what configs would be used normally
    echo "=== Before isolation ==="
    show_git_config
    
    # Run isolated command
    echo -e "\n=== Running isolated command ===\n"
    GIT_CONFIG_GLOBAL=/dev/null \
    GIT_CONFIG_SYSTEM=/dev/null \
    git -c credential.helper= \
        -c core.sshCommand=/bin/false \
        -c http.sslVerify=false \
        ls-remote https://${username}:${password}@github.com/username/repo.git HEAD
    
    # Show that no configs were used
    echo -e "\n=== During isolation (should be empty) ==="
    GIT_CONFIG_GLOBAL=/dev/null \
    GIT_CONFIG_SYSTEM=/dev/null \
    git config --list
    
    # Restore original configs
    export GIT_CONFIG_GLOBAL=$OLD_GIT_CONFIG_GLOBAL
    export GIT_CONFIG_SYSTEM=$OLD_GIT_CONFIG_SYSTEM

    And, are you using the http ref ? (prefer to double check)

    Don't hesitate to DM me on Discord (#guillaume_derouville)

    What the dagger engine does is: retrieving from the host where the CLI is located the credentials via the git credential helpers. Under the hood, it does: echo "host=github.com\nprotocol=https\n" | git credential fill to retrieve the credentials and passing it to our git. It does not have the gitconfig context, so this might be the conflicting part.

    Could you please try to have a git run without any config and see if it still works ? Above scripts tries to do that

  8. nipuna-perera commented on Nov 19, 2024

    @nipuna-perera
    Contributor

    FWIW, This is what we do. I've tested this to work on our Jenkins runners.

    sh("git config --global credential.https://github.com.helper \'!f() { sleep 1; echo \"username=\${${username}}\";\n"+
          "echo \"password=\${${password}}\"; }; f\'")

    It's basically doing the helper function inline.

  9. pspurlock commented on Nov 19, 2024

    @pspurlock

    Ok I got it! It wasn't a git-config issue, it WAS a token issue, I just don't know why:

    dagger test:
      extends: .dagger
      stage: test
      variables:
        GIT_USERNAME: gitlab-ci-token
      script:
        - |
          cat > /tmp/git-credential-helper.sh << 'EOF'
          #!/bin/sh
          case "$1" in
              get)
                  if [ -n "$GIT_USERNAME" ]; then
                      echo "protocol=https"
                      echo "username=$GIT_USERNAME"
                      echo "password=$CI_JOB_TOKEN"
                  fi
                  ;;
              store)
                  # Don't store credentials
                  ;;
              erase)
                  # Don't need to erase credentials
                  ;;
          esac
          exit 0
          EOF
        - chmod +x /tmp/git-credential-helper.sh
    
        - git clone https://myproject.com.git || true
        - git config --system credential."https://myproject.com".helper "/tmp/git-credential-helper.sh"
        - echo -e "host=myproject.com\nprotocol=https\n" | git credential fill
        - git clone https://myproject.com.git
        - dagger -v -m https://myproject.com/ruff call lint -h

    The git clones before the dagger calls are my sanity checks -- first clone before the config (to make sure it fails as it should), then one after to make sure it passes after my git config is changed.

    Scenario 1 - Use $CI_JOB_TOKEN

    1st git clone fails, 2nd git clone works. That's what I'd expect. Dagger, however fails.

    Scenario 2 - Use $TEST_DAGGER_TOKEN -- A var I created with a PAT from our private dagger module repo.

    Same as above, except this time Dagger also works successfully! So the only change being made is the token, but the token not working for dagger DOES work when doing a git clone.

    If I had to guess, maybe Dagger is making a call that CI_JOB_TOKEN doesn't have access to? It is limited in scope so it does NOT have full API access. reference

    Thanks again for sticking with me, you guys are awesome.

  10. grouville commented on Nov 19, 2024

    @grouville
    MemberAuthor

    Ok I got it! It wasn't a git-config issue, it WAS a token issue, I just don't know why:

    Thanks for everything 🙏 I would be interested to know what permissions are required though. If at some point you have the time to explore it that would be awesome 👼

  11. abhibacheti commented on Dec 1, 2024

    @abhibacheti

    is it possble to configure this at global level something similar
    git config --global url."https://gitlab-ci-token:[email protected]/".insteadOf "https://gitlab.com/"

  12. grouville commented on Dec 3, 2024

    @grouville
    MemberAuthor

    is it possble to configure this at global level something similar git config --global url."https://gitlab-ci-token:[email protected]/".insteadOf "https://gitlab.com/"

    Hi @abhibacheti, your command rewrites the url to pass the command via the ref ; we currently do not support that, as we just rely on the git credential helper to get the token and pass it as a header in the git query ; However, this would potentially be unlocked with: #9007

  13. skycaptain commented on Dec 4, 2024

    @skycaptain
    Contributor

    I encountered the same issue today. After debugging, I found that the issue lies within this line:

    gs.auth = []string{"-c", "http." + tokenScope(gs.src.Remote) + ".extraheader=Authorization: " + string(dt)}

    The CI_JOB_TOKEN can only be used as a password in the remote URL, e.gg https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.example.com/...

    Alternatively, the job token can be passed in the request using any of these methods:

    • --form "token=$CI_JOB_TOKEN"
    • --header "JOB-TOKEN: $CI_JOB_TOKEN"
    • --data "job_token=$CI_JOB_TOKEN"

    See https://docs.gitlab.com/ee/ci/jobs/ci_job_token.html#to-git-clone-a-private-projects-repository for reference.

    Unlike PATs, CI_JOB_TOKEN do not work with an authorisation header (which explains why PATs work while CI_JOB_TOKEN don’t), which however is what happens in the above function:
    Even when a user calls the module with dagger call -m https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.my.company/my-module.git, Dagger appears to remove the credentials from the remote URL and set them as an auth header instead, i.e. gs.auth contains [-c http.https://gitlab.my.company/my-module.git.extraheader=Authorization: basic [redacted]] as a result.

    I managed to access a private Dagger module after replacing the above line with a similar configuration like the Bitbucket Cloud Git one from the same function.

    Potential solutions might include:

    • Preserve the username/password in the remote URL when explicitly provided by the user
    • Apply one of the above methods when the token starts with glcbt-
  14. pspurlock commented on Dec 4, 2024

    @pspurlock
    • Preserve the username/password in the remote URL when explicitly provided by the user

    Something to keep in mind is when you do this currently dagger will print out your credentials in its pod logs if running daemon on a different host like in a cluster.

  15. skycaptain commented on Dec 5, 2024

    @skycaptain
    Contributor

    Let me correct my earlier statement. The issue isn't that CI_JOB_TOKEN is being passed as an auth header (git/libcurl does this internally anyway). Rather, the problem is that GitLab doesn't accept the hard-coded username x-access-token:

    dt = []byte("basic " + base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("x-access-token:%s", dt))))

    While PATs, PrATs and GrATs allow any non-blank value as a username, CI_JOB_TOKEN requires gitlab-ci-token for the username (although I had issues using a PrAT with Dagger as a workaround as well, maybe I'm still missing something). Here a working example:

    $ git clone -c http.extraHeader="Authorization: Basic $(echo -n gitlab-ci-token:$CI_JOB_TOKEN | base64)" https://gitlab.my.company/my-module.git

    While this doesn't work:

    git clone -c http.extraHeader="Authorization: Basic $(echo -n x-token-auth:$CI_JOB_TOKEN | base64)" https://gitlab.my.company/my-module.git

    A solution would be to preserve the username from the user's credential helper and/or the remote URL (For GitLab CI, using https://gitlab-ci-token:${CI_JOB_TOKEN}@... is more convenient and common than setting up a git credential helper)

  16. jedevc commented on Dec 5, 2024

    @jedevc
    Contributor

    I think preserving the username when explicitly specified makes sense - we should only auto populate it if it wasn't directly specified. cc @grouville


    I would like to avoid a general solution where we put the credentials into the URL - as mentioned above, this appears in logs, in the cache, etc - this could then leak the token, allowing undesired external API access while the token was valid.

  17. skycaptain commented on Dec 5, 2024

    @skycaptain
    Contributor

    I would like to avoid a general solution where we put the credentials into the URL - as mentioned above, this appears in logs, in the cache, etc - this could then leak the token, allowing undesired external API access while the token was valid.

    That's fine, given the concern @pspurlock raised. Nevertheless, you would only need to keep the credentials in the remote URL until they're transformed into an auth header, i.e. they could be removed at the earliest possible point in code. However, they could still leak before then. In my logs, both Dagger (https://xxxxx@...) and GitLab CI (https://gitlab-ci-token:[MASKED]@...) have masked the credentials so far, but your results may vary. I haven't checked the daemon logs yet.

    I strongly recommend adding an example to the docs demonstrating Dagger's solution, since GitLab CI users are familiar with https://gitlab-ci-token:${CI_JOB_TOKEN}@... - this is GitLab's recommended solution that appears throughout their documentation. Perhaps Dagger could also display a warning when users include credentials in a URL.

    While several solutions have been proposed here using a custom credential helper script, I prefer keeping pipeline scripts concise for better readability. Here's a shorter approach; haven't tested though; but what do you think? Any concerns with using the cache helper?

    ...
    .dagger
      ...
      before_script:
        - ...
        # Store the credentials in memory for the duration of the job
        - git config --global credential."${CI_SERVER_URL}".helper "cache --timeout=${CI_JOB_TIMEOUT}"
        - |-
          git credential approve << EOT
          url=${CI_SERVER_URL}
          username=gitlab-ci-token
          password=${CI_JOB_TOKEN}
          EOT
      script:
        - dagger call ...
  18. grouville commented on Dec 5, 2024

    @grouville
    MemberAuthor

    Hi @skycaptain 👋

    Thank you for digging the root cause, I was falling short on time 🙏

    • We already retrieve the username, so totally doable -- it means that users will always have to specify the username, for every provider. If well documented in our docs, would that be ok ? x-access-token for all the other CI providers, gitlab-ci-token for gitlab

    • Regarding your pipeline example, I really like it ; smoother indeed.

    • Regarding the use of the cache git config --global credential."${CI_SERVER_URL}".helper "cache --timeout=${CI_JOB_TIMEOUT}", no objection on my end, this is totally valid

  19. pspurlock commented on Dec 5, 2024

    @pspurlock

    I narrowed it down to just using the .git-credential file since the helper store will ultimately do this anyway. Nice and tidy and ofc its wiped after every ci run:

      - echo -e "https://gitlab-ci-token:[email protected]" > ~/.git-credentials
      - git config --global credential.helper store
      - dagger call ...
    
  20. skycaptain commented on Dec 6, 2024

    @skycaptain
    Contributor

    We already retrieve the username, so totally doable -- it means that users will always have to specify the username, for every provider. If well documented in our docs, would that be ok ? x-access-token for all the other CI providers, gitlab-ci-token for gitlab

    In my experience, users of private repositories are accustomed to specifying both username and token. If the following line from the documentation represents the intended goal, then I would expect Dagger to use whatever username that the user specified:

    If this works, Dagger will be able to use the same credentials to access your private repositories.

    I cannot tell for other CI provider, however for Gitlab CI, I would recommend using gitlab-ci-token and CI_JOB_TOKEN in the docs.

    I narrowed it down to just using the .git-credential file since the helper store will ultimately do this anyway.

    Be careful when using the store helper and .git-credentials in the $HOME directory. When using runner executors other than Docker or Kubernetes, storing credentials outside of the job's workspace may cause unexpected side effects (especially when allowing concurrent jobs on the same machine). Additionally, AFAIK GitLab CI would not clean up this file after a run (at least not until the next run on the same machine). If one would still want to use the store helper, I would probability set up a file CI/CD variable GIT_CREDENTIALS (ensure to have expand variable reference enabled) with the following value https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_FQDN}/ and then use it like this:

    - git config credential.helper "store --file ${GIT_CREDENTIALS}"
    - dagger call ...

    File CI/CD variables are guaranteed to be cleaned up after a run. An additional benefit is that users can securely store credentials for multiple Git hosts in a single location. If defined at as a group variable the same credentials can be use for multiple projects.

    I also removed the --global in this example to avoid modifying the .gitconfig in the user's HOME directory for the reasons mentioned above. I'm hoping that Dagger can still receive the git credential helper config from the local config.

  21. added 4 commits that reference this issue on Dec 7, 2024
    9ef2f86
    e87b0f7
    e3be73e
    edd7693
  22. skycaptain commented on Feb 22, 2025

    @skycaptain
    Contributor

    Update on this issue: v0.16.0/v0.16.1 seems to has broken the above workarounds for using private repos on GitLab. While I'm still trying to set up a minimal reproducible example, I can confirm that accessing modules in private repos worked with Project Access Tokens or Group Access Tokens in v0.15.4, but no longer works in v0.16.0 and later versions. I get the following error:

    Stderr:
    ╭─ Error ──────────────────────────────────────────────────────────────────────╮
    │ Failed to convert result to primitive values: unknown error (failed to get   │
    │ generated context directory: select: failed to generate code: failed to call │
    │ sdk module codegen: select: failed to add client resources from ID: failed   │
    │ to add secret from source client iy4n56jwts31zcnpg86h7qxgh: secret           │
    │ xxh3:6ca97b9b155b5cf7 not found in other store)                              │
    ╰──────────────────────────────────────────────────────────────────────────────╯
    
    
  23. nipuna-perera commented on Feb 22, 2025

    @nipuna-perera
    Contributor
  24. skycaptain commented on Mar 25, 2025

    @skycaptain
    Contributor

    I want to bring attention back to this issue, as it is currently blocking from effectively using modules on GitLab due to the incorrectly hard-coded username used for HTTPS authentication being incompatible with CI_JOB_TOKENs.

    Should we split the "HTTPS with GitLab not working with CI_JOB_TOKEN" or "Dagger not respecting username for HTTPS auth" into a separate issue? The initial intent, as the title suggests, was to provide examples in the docs. However, the discussion revealed an issue with the hard-coded username that prevents us from using Dagger modules as intended.

    Is there any chance we can have this fixed in one of the next releases? Can we somehow contribute?

  25. grouville commented on Mar 27, 2025

    @grouville
    MemberAuthor

    I want to bring attention back to this issue, as it is currently blocking from effectively using modules on GitLab due to the incorrectly hard-coded username used for HTTPS authentication being incompatible with CI_JOB_TOKENs.

    Should we split the "HTTPS with GitLab not working with CI_JOB_TOKEN" or "Dagger not respecting username for HTTPS auth" into a separate issue? The initial intent, as the title suggests, was to provide examples in the docs. However, the discussion revealed an issue with the hard-coded username that prevents us from using Dagger modules as intended.

    Is there any chance we can have this fixed in one of the next releases? Can we somehow contribute?

    Hey @skycaptain, thanks for the ping. Taking a look 👀

  26. locked and limited conversation to collaborators on Mar 24, 2026
  27. converted this issue into a discussion #12180 on Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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