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

KeypairFromMemory not working (_pygit2.GitError: err -7 (no message provided)) #800

Closed
mew1033 opened this issue Jun 8, 2018 · 3 comments
Labels

Comments

@mew1033
Copy link

mew1033 commented Jun 8, 2018

I'm follwing the git clone via ssh example here: http://www.pygit2.org/recipes/git-clone-ssh.html (Just the second half). It's working fine when my keys are stored in files, but when I try to use the public key stored in memory option, it fails. Here's what I've done:

>>>
>>> git_host = 'githost.com'
>>> org = 'myorg'
>>> repo = 'testgitpull'
>>>
>>> keypair = pygit2.Keypair("git", 'TESTGIT.pub', 'TESTGIT', "icanhazgit")
>>> callbacks = pygit2.RemoteCallbacks(credentials=keypair)
>>> r = pygit2.clone_repository("ssh://git@%s/%s/%s" % (git_host, org, repo), "testgitpull", callbacks=callbacks)
>>>
>>> print r
pygit2.Repository('/home/mew1033/testgit/testgitpull/.git/')
>>>
>>>


(I deleted the folder here so that it could clone again)


>>>
>>> with open('TESTGIT.pub', 'r') as f:
...     pub = f.read()
...
>>> with open('TESTGIT', 'r') as f:
...     priv = f.read()
...
>>>
>>>
>>> keypair = pygit2.KeypairFromMemory("git", pub, priv, "icanhazgit")
>>> callbacks = pygit2.RemoteCallbacks(credentials=keypair)
>>> r = pygit2.clone_repository("ssh://git@%s/%s/%s" % (git_host, org, repo), "testgitpull", callbacks=callbacks)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/pygit2/__init__.py", line 263, in clone_repository
    check_error(err)
  File "/usr/lib64/python2.7/site-packages/pygit2/errors.py", line 64, in check_error
    raise GitError(message)
_pygit2.GitError: err -7 (no message provided)
>>>
>>>
@mew1033 mew1033 changed the title KeypairFromMemory not working KeypairFromMemory not working (_pygit2.GitError: err -7 (no message provided)) Jun 8, 2018
@kinkerl
Copy link
Contributor

kinkerl commented Jun 25, 2018

i have the exact same problem. however, for reasons i do not understand, this works:

class MyRemoteCallbacks(pygit2.RemoteCallbacks):
    def credentials(self, url, username_from_url, allowed_types):
        if allowed_types & pygit2.credentials.GIT_CREDTYPE_USERNAME:
            return pygit2.Username("git")
        elif allowed_types & pygit2.credentials.GIT_CREDTYPE_SSH_MEMORY:
             return pygit2.KeypairFromMemory("git", pub, priv, "icanhazgit")
        else:
            return None
 r = pygit2.clone_repository("ssh://git@%s/%s/%s" % (git_host, org, repo), "testgitpull", callbacks=MyRemoteCallbacks)

(some variables in this example are not correct when adapding to your example but you get the general idea i suppose)

@jdavid jdavid added the bug label Sep 9, 2018
@nelsonhp
Copy link

nelsonhp commented Feb 6, 2019

@kinkerl THANK YOU!

I was hitting this issue as well for some time. I tried all options I could find, but had no luck with any solutions provided anywhere else. The solution provided by kinkerl (above) works on install of library with specs as:

Ubuntu 18.04
python3.6
pygit2==0.27.4
libgit2==0.27.4
libssh2-dev==0.1.8

This is a more complete example for this solution:

WARNING: This is not actual code. It has been modified to
be less verbose and not carry object information from actual
source. This code has not been tested, and may need to be
modified for actual use. This is just a more verbose example
of how to use the previously mentioned methods. Use at your
own risk!

"""
Author: Hunter Prendergast
Based on code by: Dennis Schwertel
Date: FEB 6, 2019
License: GPLv2 (To conform to license of pygit2)
"""
from functools import partial
import pygit2

class MyRemoteCallbacks(pygit2.RemoteCallbacks):

    def __init__(self, privk=None, pubk=None, passwd=None, credentials=None, certificate=None):
        """Overloading of initialization to allow for partial object creation."""
        if credentials is not None:
            self.credentials = credentials
        if certificate is not None:
            self.certificate = certificate
        self.privk = privk
        self.pubk = pubk
        self.passwd = passwd

    def credentials(self, url, username_from_url, allowed_types):
        if allowed_types & pygit2.credentials.GIT_CREDTYPE_USERNAME:
            return pygit2.Username("git")
        elif allowed_types & pygit2.credentials.GIT_CREDTYPE_SSH_MEMORY:
            return pygit2.KeypairFromMemory("git",
                                            self.pubk,
                                            self.privk,
                                            self.passwd)
        else:
            return None


def callback(privk, pubk, passwd): 
    return partial(MyRemoteCallbacks, privk=privk, pubk=pubk, passwd=passwd)

def clone(url, path, privk, pubk, passwd):
    return pygit2.clone_repository(url, path, callbacks=callback(
        privk, pubk, passwd))

Files are read in using standard python method:

with open("some_file","r") as f:
    data = f.read()

Newlines have not been stripped from end of file(s).

Url is taken directly from gitlab/github for ssh clone. EX:

git@github.com:libgit2/pygit2.git

Keys generated with following command:

ssh-keygen -t rsa -b 4096 -P <password> -C <comment> -f <path> -q -o

Hopefully this helps some one else who is still staring at a screen at 2:30
in the morning.

@jdavid
Copy link
Member

jdavid commented May 2, 2020

Just added a unit test proving that this works.

Had to build libssh2 1.9.0, since the tests failed with 1.8.0

Open a new bug if you still have issues, but try first with libssh2 1.9.0, older versions not supported.

@jdavid jdavid closed this as completed May 2, 2020
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Oct 22, 2020
Includes a build fix for the updated libgit2 1.1.x, figured it may as well
be updated at the same time.

1.3.0 (2020-09-18)
-------------------------

- New ``Repository.add_submodule(...)``
  `#1011 <https://github.com/libgit2/pygit2/pull/1011>`_

- New ``Repository.applies(...)``
  `#1019 <https://github.com/libgit2/pygit2/pull/1019>`_

- New ``Repository.revparse(...)`` and ``Repository.revparse_ext(...)``
  `#1022 <https://github.com/libgit2/pygit2/pull/1022>`_

- New optional ``flags`` and ``file_flags`` arguments in
  ``Repository.merge_commits`` and ``Repository.merge_trees``
  `#1008 <https://github.com/libgit2/pygit2/pull/1008>`_

- New ``Reference.raw_target``, ``Repository.raw_listall_branches(...)`` and
  ``Repository.raw_listall_references()``; allow bytes in
  ``Repository.lookup_branch(...)`` and ``Repository.diff(...)``
  `#1029 <https://github.com/libgit2/pygit2/pull/1029>`_

- New ``GIT_BLAME_FIRST_PARENT`` and ``GIT_BLAME_USE_MAILMAP`` constants
  `#1031 <https://github.com/libgit2/pygit2/pull/1031>`_

- New ``IndexEntry`` supports ``repr()``, ``str()``, ``==`` and ``!=``
  `#1009 <https://github.com/libgit2/pygit2/pull/1009>`_

- New ``Object`` supports ``repr()``
  `#1022 <https://github.com/libgit2/pygit2/pull/1022>`_

- New accept tuples of strings (not only lists) in a number of places
  `#1025 <https://github.com/libgit2/pygit2/pull/1025>`_

- Fix compatibility with old macOS 10.9
  `#1026 <https://github.com/libgit2/pygit2/issues/1026>`_
  `#1027 <https://github.com/libgit2/pygit2/pull/1027>`_

- Fix check argument type in ``Repository.apply(...)``
  `#1033 <https://github.com/libgit2/pygit2/issues/1033>`_

- Fix raise exception if error in ``Repository.listall_submodules()`` commit 32133974

- Fix a couple of refcount errors in ``OdbBackend.refresh()`` and
  ``Worktree_is_prunable`` commit fed0c19c

- Unit tests
  `#800 <https://github.com/libgit2/pygit2/issues/800>`_
  `#1015 <https://github.com/libgit2/pygit2/pull/1015>`_

- Documentation
  `#705 <https://github.com/libgit2/pygit2/pull/705>`_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants