miscell cosmetic + pass the substrate object to TestBonding so it can compute an...
[tests.git] / qaapi / qa / tests / sync_person_key.py
1 #!/usr/bin/python
2 import os, sys
3 from Test import Test
4 from qa import utils
5
6 class sync_person_key(Test):
7     """
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.  
11     """
12
13     def make_keys(self, path, name):
14         
15         if not os.path.isdir(path):
16             os.mkdir(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)
20
21     def call(self, email, plc_name = None):
22         
23         # Determine which plc to talk to 
24         plc = self.config.get_plc(plc_name)
25         api = plc.config.api
26         auth = plc.config.auth 
27
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"
33         
34         # Validate person
35         persons = api.GetPersons(auth, [email], ['person_id', 'key_ids'])
36         if not persons:
37             raise Exception, "No such person %(email)s"
38         person = persons[0]
39
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):
43             # Make new keys
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 " %\
47                 locals())
48             
49         # sync public key  
50         public_key_file = open(public_key_path, 'r')
51         public_key = public_key_file.readline()
52                 
53         key_fields = {'key_type': 'ssh', 'key': public_key}
54         keys = api.GetKeys(auth, person['key_ids'])
55         if not keys:
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() )
60         else:
61             # keys need to be checked and possibly updated
62             key = keys[0]
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())
67             else:
68                 if self.config.verbose:
69                     utils.header("Key in %(public_key_path)s matchs public key in plc" % locals())                      
70
71 if __name__ == '__main__':
72     args = tuple(sys.argv[1:])
73     sync_person_key()(*args)            
74