Skip to content

Instantly share code, notes, and snippets.

@zachfi
Created January 12, 2015 15:19
Show Gist options
  • Select an option

  • Save zachfi/d1b53a6a3566560dd60a to your computer and use it in GitHub Desktop.

Select an option

Save zachfi/d1b53a6a3566560dd60a to your computer and use it in GitHub Desktop.
Gitolite keys from LDAP
#! /usr/bin/env python
from pyldap import PyLDAP
from config import get_option, load_config
from os import mkdir
from os.path import isdir
def main():
opt, users = get_option()
config = load_config(opt.config, 'LDAP')
ldap = PyLDAP(
config['url'],
config['binddn'],
config['bindpw'],
config['basedn'],
)
ldap.bind()
keydir = 'keys.d'
if not isdir(keydir):
mkdir(keydir)
for user in users:
userDN = ldap.userDN(user)
mail = ldap.objectAttribute(userDN, 'mail')
keys = ldap.objectAttribute(userDN, 'sshPublicKey')
if keys is None:
continue
else:
for i, k in enumerate(keys):
filename = "{dir}/{mail}@{index}.pub".format(
dir='keys.d',
mail=mail,
index=i)
with open(filename, 'w') as f:
f.write(k)
ldap.unbind
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment