How do I set ssh credentials for libgit2/libssh2?

1.9k views Asked by At

I am attempting to push references from within pygit2/libgit2:

    push_refspec = git_repo.lookup_reference('HEAD').resolve().name
    logger.info("Pushing " + push_refspec)
    git_remote.push(push_refspec)

However, I get an error about missing credentials:

    _pygit2.GitError: Cannot set up SSH connection without credentials

This ultimately comes from libssh2 via a call from libgit2 (transports/ssh.c):

if (user && pass) {
    if (git_cred_userpass_plaintext_new(&t->cred, user, pass) < 0)
        goto on_error;
} else if (t->owner->cred_acquire_cb) {
    if (t->owner->cred_acquire_cb(
            &t->cred, t->owner->url, user,
            GIT_CREDTYPE_USERPASS_PLAINTEXT |
            GIT_CREDTYPE_SSH_KEY |
            GIT_CREDTYPE_SSH_CUSTOM,
            t->owner->cred_acquire_payload) < 0)
        goto on_error;

    if (!t->cred) {
        giterr_set(GITERR_SSH, "Callback failed to initialize SSH credentials");
        goto on_error;
    }
} else {
    giterr_set(GITERR_SSH, "Cannot set up SSH connection without credentials");
    goto on_error;
}

A push from the repository using the Git client works just fine. I tried GIT_SSL_NO_VERIFY=true which (unsurprisingly) didn't work.

What extra information do I need to set to get this to work?

1

There are 1 answers

0
Ben Straub On BEST ANSWER

It looks like pygit2 doesn't support libgit2's credentialed network operations just yet. There's some API discussion happening, but nothing is finalized.

EDIT: Looks like this has been implemented since this answer was accepted. The documentation has some notes on this.