Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SSH Git URLs for authenticated connections to private repositories #1851

Open
jnicholls opened this issue Jul 28, 2015 · 64 comments
Open
Labels
A-crate-dependencies Area: [dependencies] of any kind A-git Area: anything dealing with git C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-needs-mentor Status: Issue or feature is accepted, but needs a team member to commit to helping and reviewing.

Comments

@jnicholls
Copy link

Right now there is no way for cargo to connect to a private repository, whether or HTTPS or SSH. I believe if the git Cargo.toml dependency option would support a non-URL string such as [email protected]:user/repo then the Git client in cargo would likely successfully connect.

This is a major hole in cargo right now; I'm forced to submodule everything which is tedious.

Thanks in advance.

@alexcrichton
Copy link
Member

This is actually supported, Cargo just doesn't prompt for a password (see #1306). You can use git credentials to store information (which Cargo reads).

@jnicholls
Copy link
Author

I've tried this before only a few weeks ago and it was not working as
advertised, despite that issue having been closed and me running the latest
cargo. I'll try again.

On Tuesday, July 28, 2015, Alex Crichton [email protected] wrote:

This is actually supported, Cargo just doesn't prompt for a password (see
#1306 #1306). You can use git
credentials http://git-scm.com/docs/gitcredentials to store information
(which Cargo reads).


Reply to this email directly or view it on GitHub
#1851 (comment).

Sent from Gmail Mobile

@alexcrichton
Copy link
Member

Ok, but if you run into any problems feel free to open an issue! It's a difficult code path to test and it's not exercised that much, so there may be a bug or two lurking.

@jnicholls
Copy link
Author

This is in fact an issue. I have my osxkeychain git credential helper setup, and my credentials are cached. All fresh terminals authenticate seamlessly to HTTPS GitHub URLs of private repositories. However, cargo continues to report Unable to update https://github.com/blah/blah both on stable and nightly cargo. The credential helper is setup in the global config (~/.gitconfig)

[credential]
        helper = osxkeychain

Please advise. Thanks in advance!

EDIT: I have also tried a file store helper, which also does not work.

[credential]
        helper = store

Do I need to setup credentials in some special way other than using a credential helper?

@alexcrichton
Copy link
Member

Sounds like some investigation is warranted!

@alexcrichton alexcrichton reopened this Jul 31, 2015
@jnicholls
Copy link
Author

As stated in the OP, SSH URLs in the form of [email protected]:user/repo are not supported by Cargo. When parsing the .toml file, it does not understand that as being a legitimate URL (because it isn't).

This is a suggestion. Apparently one can use ssh://[email protected]/repo and Cargo is happy with that, since it is in fact a legitimate URL and libgit2 understands it just fine.

@alexcrichton
Copy link
Member

Specifically, it sounds like you had a submodule which used a URL of the form [email protected]:user/repo and when Cargo tried to check that out it failed because it wasn't able to parse the URL and set the right credentials?

@jnicholls
Copy link
Author

That's correct! Whether it is specified directly as the git argument of a [dependency] entry, or if it's present as a submodule url in .gitmodules, it will not work.

@simon-nicholls
Copy link

I actually thought this was working until I switched locations.

I set up a project with a git ssh://[email protected]:/repo/blah.git dependency whilst on OSX, and thought it was picking up my .ssh config just fine - certainly my id_rsa, but perhaps also a custom ssh port number.

Now that I'm on Linux, the same project ignores all .ssh config for git dependencies, though it does work when including the password, port number, and what I had for breakfast in the URL.

@jnicholls
Copy link
Author

Are you running through vagrant or something? In either case, is ssh-agent
running?

I've had no problems on OS X or Linux on vagrant with ssh-agent enabled in
my Vagrantfile.

On Saturday, December 5, 2015, Si [email protected] wrote:

I actually thought this was working until I switched locations.

I set up a project with a git ssh://[email protected]:/repo/blah.git dependency
whilst on OSX, and thought it was picking up my .ssh config just fine -
certainly my id_rsa, but perhaps also a custom ssh port number.

Now that I'm on Linux, the same project ignores all .ssh config for git
dependencies, though it does work when including the password, port number,
and what I had for breakfast in the URL.


Reply to this email directly or view it on GitHub
#1851 (comment).

Sent from Gmail Mobile

@simon-nicholls
Copy link

Physical machines, unfortunately in different countries.

My best guess is that ssh-agent was running on the Mac without my realising it. As soon as I use ssh-agent here on Linux, it works as expected. I still need to be explicit about the port, but that may also have been the case on the Mac. The repo is just a personal one, so I was relying on ssh auth negotiation beforehand.

@alexcrichton
Copy link
Member

@simon-nicholls you may also be running into #2078, right now Cargo doesn't take a look at .ssh/config

@simon-nicholls
Copy link

Thanks. That clears things up for me.

@Binero
Copy link

Binero commented Apr 5, 2016

I would like to bump this issue. I am trying to connect to a remote, private ssh repository on Gitlab. I change the scp syntax [email protected]:project/supercool.git into ssh://gitlab.com/project/supercool.git. When running cargo update it gives me an error saying it cannot authenticate. I do have the key inside my ~/.ssh, and it is registered using ssh-add. It is also part of my GNOME Keyring, which my git credential helper is set to.

I can clone the repository manually, as well as ssh into the server.

@jnicholls
Copy link
Author

@Binero change your URL to ssh://[email protected]/project/supercool.git (notice the git@ user addition) and try again?

@Binero
Copy link

Binero commented Apr 6, 2016

@jnicholls Gold. Worked nicely.

@mkollaro
Copy link

mkollaro commented Sep 1, 2016

I'm having a similar issue:

reponame = { git = "ssh://[email protected]:22/mkollaro/reponame.git" }

but cargo fails with:

 $ cargo test --verbose
    Updating git repository `ssh://[email protected]:22/mkollaro/reponame.git`
error: Unable to update ssh://[email protected]:22/mkollaro/reponame.git

Caused by:
  failed to fetch into /home/mkollaro/.cargo/git/db/reponame-e51b056051cf84
Caused by:
  failed to authenticate when downloading repository
attempted ssh-agent authentication, but none of the usernames `git` succeeded

Caused by:
  [23/-1] error authenticating: no auth sock variable

Doing a git clone with the very same URL works fine. I'm using cargo 0.12.0-nightly (6b98d1f 2016-07-04).

Btw, I had to add an explicit port number because it's complaining about an invalid port number without it - maybe another bug?

@alexcrichton
Copy link
Member

@mkollaro that actually looks like it's a different error perhaps?

I believe git clone will transitively invoke ssh itself which will read ~/.ssh/config for keys and such. Cargo, however, which uses libgit2 which uses libssh2, will not read ~/.ssh/config yet (not implemented). The only authentication method method supported by Cargo right now is connecting to the ssh-agent. Do you have the agent running with your keys added?

@Zteve
Copy link

Zteve commented Mar 23, 2017

On Mac OS X (Sierra) I had to create a .ssh/config file like this:

Host *
   UseKeychain yes
   AddKeysToAgent yes
   IdentityFile ~/.ssh/id_rsa

with the (private) rsa file pointed to, and then issue the command:

ssh-add -K ~/.ssh/id_rsa

which (finally!) allowed an entry like:

git = "ssh://[email protected]/skipjaq/loda.git"

to work perfectly. (Now I only have to cure the coding bugs.)

I do not know how often I will have to repeat the ssh-add command, but it appears this ought to hold at least until the next reboot, which (on MAC at least) is pretty rare.

This anomaly is apparently a feature of ssh-agent on Mac OS X Sierra.

@pronebird
Copy link

@Zteve I am seeing quite weird behavior when https://github.com/username/XXXX.git rejects to work, while switching prefix to ssh:// makes it work.

@Zteve
Copy link

Zteve commented Aug 1, 2017

@pronebird Yes, the scheme https: fails for me also. I can only get it to work if I use the ssh: scheme with the git@ prefix on the host part.

Have you tried it with https://[email protected]/username/XXXX.git? I haven't.

@pronebird
Copy link

pronebird commented Aug 1, 2017

@Zteve tried with https://git@... but unfortunately it doesn't work. Only ssh:// works for private repos.

@gibfahn
Copy link

gibfahn commented Dec 5, 2020

I ended up writing down all the different ways to auth and the gotchas here: https://fahn.co/posts/cargo-auth-for-private-git-repos.html

It got a bit long 😅 . Not sure if any of it is worth adding to the Cargo docs.

@xuqingkuang
Copy link

xuqingkuang commented Apr 29, 2021

It seems git changed the behavior of URL, all of upon solutions doesn't work anymore.

$ cargo install --git 'ssh://git.lab.com/rust/example-module.git'

    Updating git repository `ssh://git.lab.com/rust/example-module.git`
Enter passphrase for key '/home/xqkuang/.ssh/id_rsa': 
error: failed to fetch into: /home/xqkuang/.cargo/git/db/trpc-rust-ec60571bf845afd2

Caused by:
  process didn't exit successfully: `git fetch --force --update-head-ok 'ssh://git.lab.com/rust/example-module.git' 'refs/heads/master:refs/remotes/origin/master' 'HEAD:refs/remotes/origin/HEAD'` (exit code: 128)
  --- stderr
  fatal: remote error: Git:Project error, please check URL.

$ git fetch --force --update-head-ok 'ssh://git.lab.com/rust/example-module.git' 

'refs/heads/master:refs/remotes/origin/master' 'HEAD:refs/remotes/origin/HEAD'
Enter passphrase for key '/home/xqkuang/.ssh/id_rsa': 
fatal: remote error: Git:Project error, please check url.

But changing the url argument of git fetch to the correct URL [email protected]:rust/example-module.git is working.

$ git fetch --force --update-head-ok '[email protected]:rust/example-module.git' 

'refs/heads/master:refs/remotes/origin/master' 'HEAD:refs/remotes/origin/HEAD'
Enter passphrase for key '/home/xqkuang/.ssh/id_rsa': 
remote: Finding sources: 100% (3124/3124)
remote: Total 3124 (delta 1718), reused 3055 (delta 1718)
Receiving objects: 100% (3124/3124), 567.77 KiB | 19.58 MiB/s, done.
Resolving deltas: 100% (1718/1718), done.
From git.lab.com:rust/example-module
 * [new branch]      master     -> origin/master
 * [new ref]                    -> origin/HEAD

Is it possible for the cargo to translate the URL to the correct one?

@mathstuf
Copy link
Contributor

I think reporting to Git developers that something changed would be better here. Can you, perhaps, bisect where GIt changed this behavior?

@xuqingkuang
Copy link

I think reporting to Git developers that something changed would be better here. Can you, perhaps, bisect where GIt changed this behavior?

The problem is git fetch supports the URL schema like [email protected]:rust/example-module.git, but not the cargo schema ssh://git.lab.com/rust/example-module.git

I just think cargo should adapt the URL resolve mechanism of git, and solve the problem.

@mathstuf
Copy link
Contributor

Git supports ssh:// just fine; I use it all the time. Something else is up here.

@mathstuf
Copy link
Contributor

I think I see the issue. You have ssh://host/ vs. user@host:. Try ssh://user@host/ instead.

oowekyala added a commit to lf-lang/lingua-franca that referenced this issue Jul 13, 2021
Now we use an ssh connection to access the github repo.
Be aware that you need a credentials helper for this to
work as the repo is private and cargo will not prompt for
a password.

rust-lang/cargo#1851
@onatm
Copy link

onatm commented Sep 28, 2021

Note to all other hipster fish users.

it doesn't seem cargo install --git ssh://[email protected]/org/crate command work on fish. You need to switch to bash, add your ssh key and then you'll be able to download the crate

@mathstuf
Copy link
Contributor

It seems very odd that the shell would affect something like that. Are there any other symptoms?

@onatm
Copy link

onatm commented Oct 13, 2021

It seems very odd that the shell would affect something like that. Are there any other symptoms?

There isn't much except the error output:

❯ cargo install --git ssh://[email protected]/xxx/xxxxx --branch main
    Updating git repository `ssh://[email protected]/xxx/xxxxx`
error: failed to fetch into: /Users/onat.mercan/.cargo/git/db/xxxxx-db26b348fdd70ca7

Caused by:
  failed to authenticate when downloading repository

  * attempted ssh-agent authentication, but no usernames succeeded: `git`

  if the git CLI succeeds then `net.git-fetch-with-cli` may help here
  https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli

Caused by:
  no authentication available

@mathstuf
Copy link
Contributor

It seems like something is wrong with the libgit2 used internally. I'm not sure what it thinks using the remote url's username has to do with contacting the local ssh-agent.

The only thing fish could be doing is some magic on that argument, but it looks like it got through successfully. I would recommend some strace to see what is happening under the hood and whether any strings are being manip'd improperly.

@sfackler
Copy link
Member

Your bash config is probably setting up an ssh-agent and your fish config is not.

@onatm
Copy link

onatm commented Oct 13, 2021

Your bash config is probably setting up an ssh-agent and your fish config is not.

It's the other way around. My bash config is not setting up ssh-agent but fish does.

The only thing fish could be doing is some magic on that argument, but it looks like it got through successfully. I would recommend some strace to see what is happening under the hood and whether any strings are being manip'd improperly.

I am on Mac and I'll try to figure out if I could get any meaningful trace with dtruss. I tried verbose display option to get more info from cargo but the output is the same. I'll also try to debug a local cargo version.

@sfackler
Copy link
Member

If one shell has an ssh-agent and the other doesn't, that is the difference. It is not related to fish or bash specifically.

@onatm
Copy link

onatm commented Oct 13, 2021

I found out the reason for the failure. It is due to missing identity on ssh-agent. I am not sure how I managed to clone and work on private repos so far without having an identity on ssh-agent. Thank you @sfackler and @mathstuf

@weihanglo weihanglo added S-needs-mentor Status: Issue or feature is accepted, but needs a team member to commit to helping and reviewing. and removed S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review labels Apr 18, 2023
@evbo
Copy link

evbo commented Jul 17, 2023

just curious to know is this issue technically resolved per?:
#1851 (comment)

@sagoez
Copy link

sagoez commented Dec 13, 2023

I'm getting this error not locally but in github actions, has anybody found any workaround for that case?

@kaimast
Copy link

kaimast commented Dec 13, 2023

I'm getting this error not locally but in github actions, has anybody found any workaround for that case?

Did you make the SSH key available to your GitHub action? If not, take a look at this and this.

Edit: Added second link

@Cypher1
Copy link

Cypher1 commented Apr 10, 2024

I now appear to have an https:// style git url failing to install because cargo attempts to use the SSH version of the repo's url. Not sure how this happens though, or if the SSH version of the url has been cached or stored somewhere without my knowledge.

@mathstuf
Copy link
Contributor

I now appear to have an https:// style git url failing to install because cargo attempts to use the SSH version of the repo's url.

Check for insteadOf configurations in Git that can rewrite URLs.

@Cypher1
Copy link

Cypher1 commented Apr 10, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-crate-dependencies Area: [dependencies] of any kind A-git Area: anything dealing with git C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-needs-mentor Status: Issue or feature is accepted, but needs a team member to commit to helping and reviewing.
Projects
None yet
Development

No branches or pull requests