clone a git repository with SSH and libgit2sharp
Asked Answered
G

1

8

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.

Graven answered 20/11, 2016 at 2:43 Comment(4)
Possible duplicate of How to use libgit2sharp with ssh-transport-protocol?Martinez
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...Graven
Are you building your own libgit2 shared lbrary?Martinez
Euh no, I'm just using libgit2sharp and I'd like to do a clone with a SSH key.Graven
F
4

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 [email protected]: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);
}
Fulfillment answered 21/3, 2017 at 20:44 Comment(2)
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 137Papillote
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.Fulfillment

© 2022 - 2024 — McMap. All rights reserved.