[afa43a8] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | # Encode the set of fixed keys as a colon-separated user/key pairs. This also |
---|
| 4 | # includes a command line utility to manipulate the DB. |
---|
| 5 | |
---|
[159a447] | 6 | def read_key_db(file): |
---|
[afa43a8] | 7 | """ |
---|
| 8 | Read the set of fixed keys fom the file |
---|
| 9 | """ |
---|
| 10 | keys = set() |
---|
| 11 | |
---|
| 12 | f = open(file, "r") |
---|
| 13 | for line in f: |
---|
| 14 | u, k = line.rstrip().split(':', 2) |
---|
| 15 | keys.add((u, k)) |
---|
| 16 | f.close() |
---|
| 17 | return keys |
---|
| 18 | |
---|
[159a447] | 19 | def write_key_db(file, keys): |
---|
[afa43a8] | 20 | """ |
---|
| 21 | Write the set of keys to the given file |
---|
| 22 | """ |
---|
| 23 | |
---|
| 24 | f = open(file, 'w') |
---|
| 25 | for t in keys: |
---|
| 26 | print >>f, "%s:%s" % t |
---|
| 27 | f.close() |
---|
| 28 | |
---|
[159a447] | 29 | def read_project_db(file): |
---|
| 30 | """ |
---|
| 31 | Read the set of fixed keys fom the file |
---|
| 32 | """ |
---|
| 33 | projects = set() |
---|
| 34 | |
---|
| 35 | f = open(file, "r") |
---|
| 36 | for line in f: |
---|
| 37 | projects.add(line.rstrip()) |
---|
| 38 | f.close() |
---|
| 39 | return projects |
---|
| 40 | |
---|
| 41 | def write_project_db(file, projects): |
---|
| 42 | """ |
---|
| 43 | Write the set of keys to the given file |
---|
| 44 | """ |
---|
| 45 | |
---|
| 46 | f = open(file, 'w') |
---|
| 47 | for p in projects: |
---|
| 48 | print >>f, "%s" % p |
---|
| 49 | f.close() |
---|
| 50 | |
---|
| 51 | |
---|
[afa43a8] | 52 | # This is the command line utility |
---|
| 53 | if __name__ =='__main__': |
---|
| 54 | import sys |
---|
| 55 | import MySQLdb |
---|
| 56 | from optparse import OptionParser |
---|
| 57 | |
---|
[159a447] | 58 | actions = set(("initall", "init", "add", "delete")) |
---|
[afa43a8] | 59 | |
---|
| 60 | def read_key_file(file): |
---|
| 61 | """ |
---|
| 62 | Read a single line from a keyfile and trim the whitespace |
---|
| 63 | """ |
---|
| 64 | f = open(file, 'r') |
---|
| 65 | key = f.readline().rstrip() |
---|
| 66 | f.close() |
---|
| 67 | return key |
---|
| 68 | |
---|
| 69 | class key_opts(OptionParser): |
---|
[f8582c9] | 70 | """ |
---|
| 71 | Options to the command line, pretty self describing |
---|
| 72 | """ |
---|
[afa43a8] | 73 | def __init__(self): |
---|
[159a447] | 74 | OptionParser.__init__(self, usage="%prog " + \ |
---|
| 75 | "(init|add|delete|initall) " + \ |
---|
[afa43a8] | 76 | "[opts] (--help for details)", version="0.1") |
---|
[159a447] | 77 | self.add_option('-t','--type', dest='type', type='choice', |
---|
| 78 | choices=['keys','projects'], help="database type") |
---|
[afa43a8] | 79 | self.add_option('-u','--user', dest='user', default=None, |
---|
| 80 | action='store', help='user to add/delete') |
---|
[159a447] | 81 | self.add_option('-p','--project', dest='project', default=None, |
---|
| 82 | action='store', help='project to add/delete') |
---|
[afa43a8] | 83 | self.add_option('-k','--key', dest='key', default=None, |
---|
| 84 | action='store', help='key to add/delete (string)') |
---|
| 85 | self.add_option('-K','--keyfile', dest='keyfile', default=None, |
---|
| 86 | action='store', help='key to add/delete (file)') |
---|
| 87 | self.add_option('-d', '--database', dest='file', default=None, |
---|
| 88 | action='store', help='database file') |
---|
| 89 | self.add_option('-f', '--file', dest='file', action='store', |
---|
| 90 | help='database file (synonym for --database)') |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | parser = key_opts() |
---|
| 94 | action = sys.argv[1] |
---|
| 95 | |
---|
| 96 | # Check the action |
---|
| 97 | if action in actions: del sys.argv[1] |
---|
| 98 | else: sys.exit("Bad action, must be one of %s" % ", ".join(actions)) |
---|
| 99 | |
---|
[f8582c9] | 100 | # Parse and check the the options for consistency |
---|
[afa43a8] | 101 | (opts, args) = parser.parse_args() |
---|
| 102 | if not opts.file: |
---|
| 103 | sys.exit("Must specify DB file") |
---|
[159a447] | 104 | if not opts.type: |
---|
| 105 | sys.exit("Must specify database type (--type)") |
---|
| 106 | elif opts.type == 'keys': |
---|
| 107 | if action != "init" and action != "initall" and action != "addall": |
---|
| 108 | if opts.user and (opts.key or opts.keyfile): |
---|
| 109 | user = opts.user |
---|
| 110 | if opts.key: |
---|
| 111 | key = opts.key |
---|
| 112 | elif opts.keyfile: |
---|
| 113 | try: |
---|
| 114 | key = read_key_file(opts.keyfile) |
---|
| 115 | except IOError, e: |
---|
| 116 | sys.exit("Error reading keyfile: %s" % e) |
---|
| 117 | else: |
---|
| 118 | sys.exit("Must specify user and key") |
---|
| 119 | elif opts.type == 'projects': |
---|
| 120 | if action != "init" and action != "initall" and action != "addall": |
---|
| 121 | if opts.project: project = opts.project |
---|
| 122 | else: sys.exit("Must specify project") |
---|
[afa43a8] | 123 | else: |
---|
[159a447] | 124 | sys.exit("Invalid --type field (how'd you do that?)") |
---|
[afa43a8] | 125 | |
---|
[159a447] | 126 | if opts.type == 'keys': |
---|
| 127 | if action != 'init' and action != 'initall': |
---|
| 128 | keys = read_key_db(opts.file) |
---|
[afa43a8] | 129 | else: |
---|
[159a447] | 130 | keys = set() |
---|
[afa43a8] | 131 | |
---|
[159a447] | 132 | if action == 'initall': |
---|
| 133 | # Add all users from the Emulab DB |
---|
| 134 | try: |
---|
| 135 | db = MySQLdb.connect(db="tbdb") |
---|
| 136 | except: |
---|
| 137 | sys.exit("Cannot access the Emulab database") |
---|
| 138 | |
---|
| 139 | c = db.cursor() |
---|
| 140 | c.execute("SELECT uid, pubkey FROM user_pubkeys") |
---|
| 141 | for u, k in c.fetchall(): |
---|
| 142 | keys.add((u, k)) |
---|
| 143 | c.close() |
---|
| 144 | db.close() |
---|
| 145 | elif action == 'add': |
---|
[afa43a8] | 146 | keys.add((user, key)) |
---|
[159a447] | 147 | elif action == 'delete': |
---|
[afa43a8] | 148 | if (user, key) in keys: |
---|
| 149 | keys.remove((user, key)) |
---|
| 150 | else: |
---|
[159a447] | 151 | print >>sys.stderr, "Cannot delete (%s, %s): not in db" % \ |
---|
[afa43a8] | 152 | (user, key) |
---|
[f8582c9] | 153 | # init action falls through to write an empty DB |
---|
[159a447] | 154 | write_key_db(opts.file, keys) |
---|
| 155 | else: |
---|
| 156 | if action != 'init' and action != 'initall': |
---|
| 157 | projects = read_project_db(opts.file) |
---|
[afa43a8] | 158 | else: |
---|
[159a447] | 159 | projects = set() |
---|
| 160 | |
---|
| 161 | if action == 'initall': |
---|
| 162 | # Add all projects from the Emulab DB |
---|
| 163 | try: |
---|
| 164 | db = MySQLdb.connect(db="tbdb") |
---|
| 165 | except: |
---|
| 166 | sys.exit("Cannot access the Emulab database") |
---|
| 167 | |
---|
| 168 | c = db.cursor() |
---|
| 169 | c.execute("SELECT pid FROM projects") |
---|
| 170 | for p in c.fetchall(): |
---|
| 171 | projects.add(p) |
---|
| 172 | c.close() |
---|
| 173 | db.close() |
---|
| 174 | elif action == 'add': |
---|
| 175 | projects.add(project) |
---|
| 176 | elif action == 'delete': |
---|
| 177 | if project in projects: |
---|
| 178 | projects.remove(project) |
---|
| 179 | else: |
---|
| 180 | print >>sys.stderr, "Cannot delete %s: not in db" % project |
---|
[f8582c9] | 181 | |
---|
| 182 | # init action falls through to write an empty DB |
---|
[159a447] | 183 | write_project_db(opts.file, projects) |
---|
| 184 | |
---|
[afa43a8] | 185 | sys.exit(0) |
---|