6 class sync_person_key(Test):
8 Make sure specified users public key on file matches whats
9 recorded at plc. Create a public/private keypair for the
10 specified user if one doesnt exist already.
13 def make_keys(self, path, name):
15 if not os.path.isdir(path):
17 key_path = path + os.sep + name
18 command = "ssh-keygen -f %(key_path)s -t rsa -N ''" % locals()
19 (stdout, stderr) = utils.popen(command)
21 def call(self, email):
23 auth = self.config.auth
24 email_parts = email.split("@")
25 keys_filename = email_parts[0]
26 keys_path = self.config.KEYS_PATH
27 private_key_path = keys_path + os.sep + keys_filename
28 public_key_path = private_key_path + ".pub"
31 persons = api.GetPersons(auth, [email], ['person_id', 'key_ids'])
33 raise Exception, "No such person %(email)s"
36 # make keys if they dont already exist
37 if not os.path.isfile(private_key_path) or \
38 not os.path.isfile(public_key_path):
40 self.make_keys(keys_path, keys_filename)
41 if self.config.verbose:
42 utils.header("Made new key pair %(private_key_path)s %(public_key_path)s " %\
46 public_key_file = open(public_key_path, 'r')
47 public_key = public_key_file.readline()
49 key_fields = {'key_type': 'ssh', 'key': public_key}
50 keys = api.GetKeys(auth, person['key_ids'])
52 # Add current key to db
53 api.AddPersonKey(auth, person['person_id'], key_fields)
54 if self.config.verbose:
55 utils.header("Added public key in %(public_key_path)s to db" % locals() )
57 # keys need to be checked and possibly updated
59 if key['key'] != public_key:
60 api.UpdateKey(auth, key['key_id'], key_fields)
61 if self.config.verbose:
62 utils.header("Updated plc with new public key in %(public_key_path)s " % locals())
64 if self.config.verbose:
65 utils.header("Key in %(public_key_path)s matchs public key in plc" % locals())
67 if __name__ == '__main__':
68 args = tuple(sys.argv[1:])
69 sync_person_key()(*args)