7

I'm trying to use the library "libgit2sharp" to clone a repository via a SSH key and... I can't find anything... I can clone it via "https" but what I'd like to do is using an SSH key. It's really unclear if it is supported or not.

ssougnez
  • 5,315
  • 11
  • 46
  • 79
  • Possible duplicate of [How to use libgit2sharp with ssh-transport-protocol?](http://stackoverflow.com/questions/24238999/how-to-use-libgit2sharp-with-ssh-transport-protocol) – SushiHangover Nov 20 '16 at 13:30
  • Well, I saw this post before posting but it's not very clear. I checked the PR from it and indeed it looks like they have been working on an SSH support but I can't find it. I checked other PR that goes in that direction but it seems that have issue with external librairies to support SSH and don't say exactly if it is supported or not so... – ssougnez Nov 20 '16 at 13:32
  • Are you building your own libgit2 shared lbrary? – SushiHangover Nov 20 '16 at 13:47
  • Euh no, I'm just using libgit2sharp and I'd like to do a clone with a SSH key. – ssougnez Nov 20 '16 at 13:50

1 Answers1

3

As of now, there is a SSH implementation using libssh2 library. You can find it here LibGit2Sharp - SSH

You should add libgit2sharp-ssh dependency on you Project to be able to use it. It is available as a nugget: https://www.nuget.org/packages/LibGit2Sharp-SSH

Disclaimer: I haven't found a formal usage guide yet, what I know is from putting together bits and pieces from other user questions through LibGit2 forums.

From what I understood, you would need to create a new credential using eitherSshUserKeyCredentials OR SshAgentCredentials to authenticate using SSH, and pass it as part of CloneOptions.

In the sample code I use "git" as user, simply because the remote would be something like git@bitbucket.org:project/reponame.git , in which case "git" is the correct user, otherwise you will get an error saying

$exception  {"username does not match previous request"}LibGit2Sharp.LibGit2SharpException

The code to clone a repo with SSH should be something like that:

public CloneOptions cloningSSHAuthentication(string username, string path_to_public_key_file, string path_to_private_key_file)
    {
        CloneOptions options = new CloneOptions();
        SshUserKeyCredentials credentials = new SshUserKeyCredentials();
        credentials.Username = username;
        credentials.PublicKey = path_to_public_key_file;
        credentials.PrivateKey =  path_to_private_key_file;
        credentials.Passphrase = "ssh_key_password";
        options.CredentialsProvider = new LibGit2Sharp.Handlers.CredentialsHandler((url, usernameFromUrl, types) =>  credentials) ;
        return options;
    }

public CloneOptions cloneSSHAgent(string username){
        CloneOptions options = new CloneOptions();
        SshAgentCredentials credentials = new SshAgentCredentials();
        credentials.Username = username;
        var handler = new LibGit2Sharp.Handlers.CredentialsHandler((url, usernameFromUrl, types) => credentials);
        options.CredentialsProvider = handler;
        return options;

}

public void CloneRepo(string remotePath, string localPath){
    CloneOptions options = cloningSSHAuthentication("git", "C:\\folder\\id_rsa.pub", "C:\\folder\\id_rsa");
    Repository.Clone(remotePath, localPath, options);
}
Anika
  • 398
  • 5
  • 23
  • Does this approach still work? I have tried this approach but am getting the following exception: Failed to authenticate SSH session: Callback returned error at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) in c:\projects\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 137 – Jason Nov 30 '18 at 08:52
  • I think it would be better if you posted it as a separate question, with your code and libgit2sharp version. It has been a long time since I last reviewed that code. – Anika Nov 30 '18 at 11:27