6 from sfa.util.faults import *
7 from sfa.util.misc import *
8 from sfa.util.method import Method
9 from sfa.util.parameter import Parameter, Mixed
10 from sfa.trust.auth import Auth
11 from sfa.util.genitable import *
12 from sfa.trust.certificate import Keypair
14 class get_key(Method):
16 Generate a new keypair and gid for requesting caller (component).
17 @return 1 If successful
20 interfaces = ['registry']
24 returns = Parameter(int, "1 if successful, faults otherwise")
27 # verify that the callers's ip address exist in the db and is an inteface
28 # for a node in the db
29 (ip, port) = self.api.remote_addr
30 interfaces = self.api.plshell.GetInterfaces(self.api.plauth, {'ip': ip}, ['node_id'])
32 raise NonExistingRecord("no such ip %(ip)s" % locals())
33 nodes = self.api.plshell.GetNodes(self.api.plauth, [interfaces[0]['node_id']], ['node_id', 'hostname'])
35 raise NonExistingRecord("no such node using ip %(ip)s" % locals())
38 # look up the sfa record
40 records = table.findObjects({'type': 'node', 'pointer': node['node_id']})
42 raise RecordNotFound("pointer:" + str(node['node_id']))
45 # generate a new keypair and gid
47 pkey = Keypair(create=True)
48 gid_object = self.api.auth.hierarchy.create_gid(record['hrn'], uuid, pkey)
49 gid = gid_object.save_to_string(save_parents=True)
56 # attempt the scp the key
57 # this will only work for planetlab based compoenents
58 (fd, filename) = tempfile.mkstemp()
59 pkey.save_to_file(filename)
60 host = node['hostname']
61 dest="/etc/sfa/nodekey.key"
62 identity = "/etc/planetlab/root_ssh_key.rsa"
63 scp_options=" -i %(identity)s %(filename)s " % locals()
64 scp_options+="-o StrictHostKeyChecking=no " % locals()
65 scp_command = "/usr/bin/scp %(scp_options)s root@%(host)s:%(dest)s" % locals()
66 (status, output) = commands.getstatusoutput(scp_command)
68 raise Exception, output