Skip to content

Use custom ssh key cannot push/fetch/pull #690

Closed

Description

Environment: GitPython (2.1.7), python 2.7.14, CentOS 6.9, git 2.15.0-rc1

I using this following code to clone:

ssh_key = os.path.join('.',Config.git.ssh_key)
ssh_key_command = 'ssh -i %s'%ssh_key
os.environ['GIT_SSH_COMMAND'] = 'ssh -i ./data/id_rsa'
repo = Repo.clone_from(Config.git.source,'wkdir')

It's work, but when I using:

r = Repo('wkdir')
with r.git.custom_environment(GIT_SSH_COMMAND='ssh -i ./data/id_rsa'):
	r.remotes.origin.fetch()

It say that:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/local/lib/python2.7/site-packages/git/remote.py", line 779, in fetch
    res = self._get_fetch_info_from_stderr(proc, progress)
  File "/usr/local/lib/python2.7/site-packages/git/remote.py", line 665, in _get_fetch_info_from_stderr
    proc.wait(stderr=stderr_text)
  File "/usr/local/lib/python2.7/site-packages/git/cmd.py", line 418, in wait
    raise GitCommandError(self.args, status, errstr)
git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
  cmdline: git fetch -v origin
  stderr: 'fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.'

I make sure I have full permission with remote repository, I also trying:

#!/bin/sh
ID_RSA=$PWD/data/hksd
exec /usr/bin/ssh -o StrictHostKeyChecking=no -i $ID_RSA "$@"

and

with r.git.custom_environment(GIT_SSH='id_rsa.sh'):
	r.remotes.origin.fetch()

It's doesn't work, too.

Activity

  1. qneill commented on Oct 18, 2017

    @qneill

    Setting GIT_PYTHON_TRACE=full in the environment may shed some light on this. See

    http://gitpython.readthedocs.io/en/stable/tutorial.html#git-command-debugging-and-customization

  2. ghost added a commit that references this issue on Oct 22, 2017
  3. Byron commented on Dec 11, 2017

    @Byron
    Member

    Are you sure the key in question does not have a passphrase?
    I could imagine that anything run from within a TTY is somehow able to use a cached password and/or connect to the ssh-agent, which GitPython by default wouldn't do (unless maybe $SSH_AGENT_PID and $SSH_AUTH_SOCK are passed as well)

  4. Byron commented on Dec 12, 2017

    @Byron
    Member

    I think something that could shed light on the issue is if the script instead executes ssh -v -i <path to keyfile>. That way, you see exactly what ssh tries to do, and why it fails (if it does fail at all).

  5. Byron commented on Jan 7, 2018

    @Byron
    Member

    Thanks for the update!
    It's probably a good idea to use absolute directories, yes. I will remember that for the future.
    Since the solution was posted here, I am closing this issue.
    Please feel free to post another comment or reopen the issue if there is something you think GitPython should do better.

  6. Byron commented on Jan 8, 2018

    @Byron
    Member

    I believe the current working directory of the git program invocations is the .git folder. Thus you can use these directories to construct the path to the key file, or use the correct relative path, such as ../ssh-keys/id-rsa.
    Either way, it is something the application has to figure out, and I don't think GitPython can be of any help there.
    However, if you believe the documentation could be improved, please feel free to submit a PR with the changes that would have helped you :).

  7. ghost added a commit that references this issue on Jan 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions