Access Github by specifying the SSH key in GitPython

I needed to access the repository on Github from within the program, so I used GitPython. Most of them could be solved by looking at Tutorial, but it was described because there was a part that took some time to operate by specifying the SSH key. To do.

Specify SSH key in Git

In the case we worked on this time, the following assumptions and requirements exist.

--I have two Github accounts. Hereinafter, they will be referred to as account A and account B, respectively. --Each account has a different key. Hereafter, the key for account A will be ~ / .ssh / id_rsa, and the key for account B will be ~ / .ssh / id_rsa_new. --Clone the repository of account B in the program, make some changes and then push. Since it is a public repository, anyone can clone it.

In this case, it depends on the environment, so it cannot be said unconditionally, but in many cases there is no problem in accessing the repository of account A. Account A is tied to the default SSH key (~ / .ssh / id_rsa), and Git uses this key by default.

On the other hand, if you use account B, you need to tell Git to use ~ / .ssh / id_rsa_new. One way to do this is to set ~ / .ssh / config to:

~/.ssh/config


host github-new
  user git
  hostname github.com
  identityfile ~/.ssh/id_rsa_new
  identitiesonly yes

You can then use the value specified in host instead of github.com (specified in hostname) to access it using ~ / .ssh / id_rsa_new.

~/.ssh/id_rsa_Clone using new


$ git clone git@github-new:<AccountB>/<repository>.git

This is fine if you just run it in your own environment, but this program needs to run in an environment other than your own. Therefore, it is better to be able to specify it as a run-time option rather than setting it.

Git has environment variables GIT_SSH, GIT_SSH_COMMAND (GIT_SSH_COMMAND is 2.3 or later). These can specify the SSH commands used by Git. You can solve the problem by specifying the same options as the above configuration file in the SSH command to be executed.

GIT_SSH,GIT_SSH_COMMAND


# GIT_SSH
$ GIT_SSH='~/ssh_cmd' git push origin master
$ cat ~/ssh_cmd
ssh -i ~/.ssh/id_rsa_new -oIdentitiesOnly=yes "$@"

# GIT_SSH_COMMAND
$ GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa_new -oIdentitiesOnly=yes' git push origin master

(Reference) http://git-scm.com/docs/git#_other

Specify the SSH key in GitPython

GitPython defines a context manager that supports GIT_SSH, GIT_SSH_COMMAND.

(Reference) http://gitpython.readthedocs.org/en/stable/tutorial.html#handling-remotes

I couldn't assume that the Git version of the execution environment is 2.3 or higher, so I used GIT_SSH this time. Also, since I already had the contents of the SSH key in the settings, I wrote the key and command to a file and specified it.

GIT with Git Python_SSH,GIT_SSH_COMMAND


import git

#Export ssh key
with open('./id_rsa_tmp', 'w') as f:
    f.write('<SSH Key>')
    os.chmod('./id_rsa_tmp', 0o600)
#Export ssh commands
with open('./ssh_cmd', 'w') as f:
    f.write('ssh -i ../id_rsa_tmp -oIdentitiesOnly=yes "$@"')
    os.chmod('./ssh_cmd', 0o777)

#Repository Clone
repo = git.Repo.clone_from('accountB_repo')

#Repository Push
ssh_executable = '../ssh_cmd'
with repo.git.custom_environment(GIT_SSH=ssh_executable):
    repo.remote().push('master')

Please note that when you run repo.gitcustom_environment, you are in the working directory of the repository. In the above code, the cloned repository (ʻaccountB_repo), key (ʻid_rsa_tmp) and command (ssh_cmd) are in the same directory. When repo.gitcustom_environment is executed, it is under the ʻaccountB_repo` directory, so specify the keys and commands while being aware that they are in the parent directory.

Key and command specification


with open('./ssh_cmd', 'w') as f:
    #The key is in the parent directory
    f.write('ssh -i ../id_rsa_tmp -oIdentitiesOnly=yes "$@"')

#The command is in the parent directory
ssh_executable = '../ssh_cmd'
with repo.git.custom_environment(GIT_SSH=ssh_executable):
...

Recommended Posts

Access Github by specifying the SSH key in GitPython
Extract the status code error in the 400,500 range of the apache access log by specifying the time range.
Access the Twitter API in Python
Sort by specifying conditions in CASTable
How to sort by specifying a column in the Python Numpy array.
Install by specifying the version with pip
Read the file line by line in Python
Read the file line by line in Python
Read the file by specifying the character code.
Password management technology in the GitHub era
Import by directly specifying the directory path
Log in to the remote server with SSH
Access files in the same directory as the executable
Sort the elements of the array by specifying the conditions
BeautifulSoup trick: Decide the Tag by specifying the path
Make any key the primary key in Django's model
Search by the value of the instance in the list
Access the variables defined in the script from the REPL