Aron Bordin
Aron Bordin

Reputation: 436

Gitpython ssh password

I'm trying to integrate gitpython with an IDE, but I'm having some problems with push.

remote_repo = self.repo.remotes[remote]
remote_repo.push(self.repo.active_branch.name)

When I run this command, or just

git push --porcelain origin master

the prompt asks my ssh password.

Enter passphrase for key '/home/user/.ssh/id_rsa': 

My problem is:

How can I workaround it and provide an interface to identify if a password is necessary, and if so be able to provide it?

Upvotes: 1

Views: 3761

Answers (2)

Byron
Byron

Reputation: 4316

In case you would like to have full control over how an ssh connection is made, and if you use git 2.3 or newer, you may instantiate git with the GIT_SSH_COMMAND environment variable set. It points to a script which is called in place of ssh. Therefore you are enabled to figure out whether or not a password is required, and to launch additional GUI to obtain the desired input.

In code, it would look something like this:

remote_repo = self.repo.remotes[remote]

# here is where you could choose an executable based on the platform.
# Shell scripts might just not work plainly on windows.
ssh_executable = os.path.join(rw_dir, 'my_ssh_executable.sh')
# This permanently changes all future calls to git to have the given environment variables set
# You can pass additional information in your own environment variables as well.
self.repo.git.update_environment(GIT_SSH_COMMAND=ssh_executable)

# now all calls to git which require SSH interaction call your executable
remote_repo.push(self.repo.active_branch.name)

Please note that this only works for accessing resources via SSH. In case the protocol is HTTPS for example, a password prompt might be given anyway.

Prior to git 2.3, you can use the GIT_SSH environment variable. It works differently as it is expected to contain only the path to the ssh program, to which additional arguments will be passed. Of course, this could be your script as well similar to what was shown above. I'd like to point out the differences between these two environment variables more precisely, but am lacking the personal experience to do so.

Upvotes: 1

VonC
VonC

Reputation: 1323075

The cross-platform solution is for you to launch first the ssh-agent and calling ssh-add.
See also "How can I run ssh-add automatically, without password prompt?" for other alternatives like the keychain.

if [ -z "$SSH_AUTH_SOCK" ] ; then
  eval `ssh-agent -s`
  ssh-add
fi

That will ask you for the passphrase, and store it.

Any subsequent ssh calls which would need the ssh private key (using gitpython or any other tool) wouldn't require to enter the private key passphrase.

Upvotes: 2

Related Questions