Scraping Github SSH keys
Did you know that you can download the SSH public keys for github.com users via the github API without even being logged in? It’s true! All you need is their github username!
This can come in really handy when you want to add a bunch of users to your private gitolite instance. All you have to do is go to https://api.github.com/users/NAME/keys. You can do that in your browser right now with your own github user name.
Gitolite stores keys in the gitolite-admin/keydir/ sub-directory. The name of the file corresponds to the username in the gitolite conf. So alice.pub is the ssh pubkey for a user named "alice" in gitolite-admin/conf/gitolite.conf. If you have multiple ssh keys for the same user, for example "alice", you can put additional keys in sub-directories like gitolite-admin/keydir/more-keys/alice.pub
So I automated that, by adding this script to my admin repo as gitolite-admin/github-fetch-keys.py, next to a file of github usernames separated by newlines in gitolite-admin/github-users. I recommend you commit both to your gitolite-admin repo.
The script automatically creates sub-directories in gitolite-admin/keydir/imported/ so the imported github keys don't clash with your manually added ones or each other. The keys are saved in gitolite-admin/keydir/imported/$ID/$USERNAME.pub. After running the script, you still need to git-add, commit, and push the new keys, but otherwise it's totally automated. Enjoy!
#!/usr/bin/python3.6 # Copyright 2019 Robert Pfeiffer # Download ssh keys from github into gitolite import requests import os with open("github-users") as usernames: os.makedirs("keydir/imported/", exist_ok=True) os.chdir("keydir/imported/") for line in usernames: split_line=line.split() if len(split_line)==1: github_name = local_name = split_line[0] elif len(split_line)==2: local_name, github_name = split_line else: print(f'cannot parse user line "{line}"') continue response=requests.get(f"https://api.github.com/users/{github_name}/keys") nkeys=len(response.json()) print (f"user {local_name} aka {github_name} has {nkeys} keys.") for key_dict in response.json(): key_id=key_dict["id"] os.makedirs(f"ID_{key_id}/", exist_ok=True) with open(f"ID_{key_id}/{local_name}.pub","w") as key_file: key_file.write(key_dict["key"]) print(f" + saved ID_{key_id}/{local_name}.pub")















