The Rub

Automatically Simple Since 2002

Generate gitolite Key Directory from Existing authorized_keys Files

04 November 2011

One thing that Gitolite leaves to the administrator is SSH key management. However, all of the tools are available to automate key management if there is an existing key infrastructure.

In this case, SSH keys already existed on a NFS-backed shared home directory. The following is a script to traverse the home directory for public keys and add them to gitolite.


# Script to generate a gitolite style key directory from existing
# authorized_keys files. Supports multiple key files.
# IN: /home/*/.ssh/authorized_keys files
# OUT:
#     keydir/
#     keydir/user1
#     keydir/user1/1/
#     keydir/user1/2/
#     keydir/user1/3/
#     keydir/user1/4/
#     keydir/user2
#     keydir/user2/1/
#     keydir/user3
#     keydir/user3/1/
#     keydir/user3/2/
#   - If a .ssh directory is not readable, it will be silently ignored.

git pull

rm -rf ${keydir}
for file in `ls /home/*/.ssh/authorized_keys`; do
    user=`echo $file | cut -d / -f 3`
    cat ${file} | sort | uniq | while read line; do

        # Skip lines that do not begin with "ssh-"
        if [ ! $(echo ${line} | cut -c 1-4) = "ssh-" ]; then

        # Add the keys
        mkdir -p ${keydir}/${user}/${count}
        echo ${line} > ${keydir}/${user}/${count}/${user}.pub
        count=`expr $count + 1`

git add keydir
git commit -a -m "Update keydir" && git push