-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-public-ssh-keys-for-gitolite.py
executable file
·127 lines (91 loc) · 3.47 KB
/
extract-public-ssh-keys-for-gitolite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
usageHelp = r"""extract-public-ssh-keys-for-gitolite.py [OPTIONS]
Extract public SSH keys for a set of listed users'
<userid>/.ssh/authorized_keys files and create gitolite user public key files
to put into gitolite-admin/keydir/.
This script is to be run as sudo to gather up all of the public SSH files for
a set of users already on a machine and create gitolite-compatiable public key
files of the form <userid>@<some-machine-name>.pub run using something like:
$ sudo extract-public-ssh-keys-for-gitolite.py \
--homedir=/home \
--userids=<usr0>,<usr1>,... \
--keydir=~/gitolite-admin/keydir
"""
import sys
import os
import subprocess
import commands
import re
from optparse import OptionParser
def getPublicSSHKeysList(authorizedKeysFile):
if not os.path.exists(authorizedKeysFile):
print "Warning: the file '"+authorizedKeysFile+"' does not exist!"
return []
authorizedKeysFileContentsStr = open(authorizedKeysFile, "r").read()
#print "\nauthorizedKeysFileContentsStr:\n", authorizedKeysFileContentsStr
authorizedKeysFileContentsList = authorizedKeysFileContentsStr.strip().split("\n")
return authorizedKeysFileContentsList
def getMachineOriginName(authorizedKeyLine):
return authorizedKeyLine.split(" ")[-1]
def getNormalizedMachineOriginName(machineOriginName):
normalizedMachineOriginName = ""
for c in machineOriginName:
if c == '@':
cr = '_'
elif c == '.':
cr = '_'
else:
cr = c
normalizedMachineOriginName += cr
return normalizedMachineOriginName
def writePublicSSHKeyFile(userid, authorizedKeyLine, normalizedMachineOriginName, keydir):
publicKeyFile = keydir+"/"+userid+"@"+normalizedMachineOriginName+".pub"
print "Writing file "+publicKeyFile
open(publicKeyFile, "w").write(authorizedKeyLine+"\n")
#
# Script body
#
if __name__ == '__main__':
#
# A) Get command-line options
#
from optparse import OptionParser
clp = OptionParser(usage=usageHelp)
clp.add_option(
"--homedir", dest="homeDir", type="string",
default="/home",
help="Home base directory. The default is '/home'.")
clp.add_option(
"--userids", dest="userids", type="string",
default="",
help="List of userids separated by comas '<usr0>,<usr1>,...' (required)")
clp.add_option(
"--keydir", dest="keyDir", type="string",
default="",
help="Base directory where the generated public SSH key files will be" \
+ " written to (required).")
(options, args) = clp.parse_args()
# Assert the commandline
if not options.homeDir:
print "\nError: --homedir=<homeDir> is required!"
sys.exit(1)
if not options.userids:
print "\nError: --userids=<usr0>,<usr1>,... is required!"
sys.exit(2)
if not options.keyDir:
print "\nError: --keydir=<keyDir> is required!"
sys.exit(3)
#
# B) Look over all of the users, extract their public keys and make the
# public key files.
#
useridsList = options.userids.split(",")
for userid in useridsList:
print "\nExtracting public SSH keys for userid: "+userid
authorizedKeysFile = options.homeDir+"/"+userid+"/.ssh/authorized_keys"
authorizedKeysList = getPublicSSHKeysList(authorizedKeysFile)
for authorizedKeyLine in authorizedKeysList:
machineOriginName = getMachineOriginName(authorizedKeyLine)
normalizedMachineOriginName = getNormalizedMachineOriginName(machineOriginName)
writePublicSSHKeyFile(userid, authorizedKeyLine, normalizedMachineOriginName,
options.keyDir)