#Fetch private Git repo with Access Token

1 messages · Page 1 of 1 (latest)

jolly mountain
#

Hello, I would like to fetch a private Git repo with a access token instead of the sshAuthSocket. How would this be possible in Node JS.

I have seen a example in go but i cannot really convert it to Node JS because i dont really understand the Go code.

Thx for your help.

main kraken
jolly mountain
main kraken
#

Then main bit is executing in a shell to expand that contents of the token secret variable:

    token = client.setSecret("GH_ACCESS_TOKEN", process.env.GH_ACCESS_TOKEN!?.toString() || "")

    src
      .withSecretVariable("GH_ACCESS_TOKEN", token)
      .withExec(["sh", "-c", "git config --global url.\"https://$GH_ACCESS_TOKEN@github.com/.insteadOf\" https://github.com/"])
      .withExec(["git", "clone", "https://github.com/myuser/myrepo"])

This solution is based on a git config to force a different url (like a web url rewrite): https://gist.github.com/taoyuan/bfa3ff87e4b5611b5cbe. Then when you use git clone https://github.com/myuser/myrepo, it'll rewrite to add the token under the scene.

jolly mountain
#

Ah I understand. For now I have been using const src = client.git(git).branch(branch).tree(); this gave me a Directory type. is there a way to replicate this functionality with the solution you provided?

jolly mountain
main kraken
jolly mountain
main kraken
#

Since there's multiple auth options, I think it's better to use withXXX. I'm going to propose the ssh options to move from tree() into those as well.

#

So it would add:

  • withAuthToken(token: SecretID!)
  • withAuthHeader(header: SecretID!)
  • withSSHSocket(socket: SocketID!, knownHosts: String)
    You'd use only one of these really.
pastel pond
#

This is fantastic! I'll be looking forward to this too as we use internal PAT auth for our enterprise gh.

jolly mountain
main kraken
#

I'm having a hard time getting it to work with a github private repo:

❯ docker run --rm -it -e GIT_TERMINAL_PROMPT=0 bitnami/git git -c "http.extraheader=Authorization: basic $(echo x-access-token:xxxxx) clone https://github.com/helderco/myrepo
Cloning into 'myrepo'...
fatal: could not read Username for 'https://github.com': terminal prompts disabled

@jolly mountain is your private repo in GitHub?

jolly mountain
main kraken
#

In both github and gitlab, it works directly on the URL:

git clone https://x-access-token:<token>@github.com/helderco/<repo>
git clone https://x-access-token:<token>@gitlab.com/helderco/<repo>

But not with http.extraheader. At least it used to work, I'm not sure if I'm missing something, but from docs seems that github pushes more towards using a credential helper and doesn't support this anymore, however, it does accept via URL (isn't it worse security wise?), which perhaps is due to a lower level networking thing.

main kraken
pastel pond
#

Yeah afaik GH fully supports token based https clones. They deprecated the user/pass

coral hedge
inner plover
#

This feels related: https://github.com/dagger/dagger/issues/4243

Just a friendly ping to see if there was any progress on this. I just ran into this trying to pull a centralized vulnerability ignore file from our platform repo. We do this in github actions currently with a sparse checkout of just a .snyk file... drop it into the target repo then run our scan. I wanted to check back prior to trying to sort out the socket path. The withAuthToken seems great... we could just hook into our usual system of environment variables.

GitHub

We should allow users to provide a username and secret for pulling a git repo. { git(url: "https://github.com/dagger/dagger", auth: {username: "jlongtine", secret: "my key ...