remote chroot-myplc and remote qemu testbox should almost work
[tests.git] / system / TestKey.py
1 import utils
2 import os, os.path
3 from TestSsh import TestSsh
4
5 class TestKey:
6
7     def __init__ (self,test_plc,key_spec):
8         self.test_plc=test_plc
9         self.key_spec=key_spec
10         self.test_ssh=TestSsh(self.test_plc.test_ssh)
11         
12     def name(self):
13         return self.key_spec['name']
14
15     def publicpath(self):
16         return "keys/%s.pub"%(self.name())
17     def privatepath(self):
18         return "keys/%s.rsa"%(self.name())
19
20     def store_remote_key(self,hostname):
21         #Not tested yet, don't know if needed
22         pub=self.publicpath()
23         priv=self.privatepath()
24         utils.header("Storing key %s in %s into %s "%(self.name(),pub,hostname))
25         dir=os.path.dirname(pub)
26         self.test_ssh.run("mkdir %s"%dir)
27         self.test_ssh.run("cat %s >> %s"%(self.key_spec['key_fields']['key'],pub))
28         self.test_ssh.run("cat %s >> %s"%(self.key_spec['private'],priv))
29         self.test_ssh.run("chmod %s 0400"%priv)
30         self.test_ssh.run("chmod %s 0444"%pub)
31             
32     def store_key(self):
33         pub=self.publicpath()
34         priv=self.privatepath()
35         utils.header("Storing key %s in %s"%(self.name(),pub))
36         dir=os.path.dirname(pub)
37         if not os.path.isdir(dir):
38             os.mkdir(dir)
39         f=open(pub,"w")
40         f.write(self.key_spec['key_fields']['key'])
41         f.close()
42         f=open(priv,"w")
43         f.write(self.key_spec['private'])
44         f.close()
45         os.chmod(priv,0400)
46         os.chmod(pub,0444)
47