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, plc_name = None):
23 # Determine which plc to talk to
24 plc = self.config.get_plc(plc_name)
26 auth = plc.config.auth
28 email_parts = email.split("@")
29 keys_filename = email_parts[0]
30 keys_path = self.config.KEYS_PATH
31 private_key_path = keys_path + os.sep + keys_filename
32 public_key_path = private_key_path + ".pub"
35 persons = api.GetPersons(auth, [email], ['person_id', 'key_ids'])
37 raise Exception, "No such person %(email)s"
40 # make keys if they dont already exist
41 if not os.path.isfile(private_key_path) or \
42 not os.path.isfile(public_key_path):
44 self.make_keys(keys_path, keys_filename)
45 if self.config.verbose:
46 utils.header("Made new key pair %(private_key_path)s %(public_key_path)s " %\
50 public_key_file = open(public_key_path, 'r')
51 public_key = public_key_file.readline()
53 key_fields = {'key_type': 'ssh', 'key': public_key}
54 keys = api.GetKeys(auth, person['key_ids'])
56 # Add current key to db
57 api.AddPersonKey(auth, person['person_id'], key_fields)
58 if self.config.verbose:
59 utils.header("Added public key in %(public_key_path)s to db" % locals() )
61 # keys need to be checked and possibly updated
63 if key['key'] != public_key:
64 api.UpdateKey(auth, key['key_id'], key_fields)
65 if self.config.verbose:
66 utils.header("Updated plc with new public key in %(public_key_path)s " % locals())
68 if self.config.verbose:
69 utils.header("Key in %(public_key_path)s matchs public key in plc" % locals())
71 if __name__ == '__main__':
72 args = tuple(sys.argv[1:])
73 sync_person_key()(*args)