added support for urn name format. urn is the default name format used over the wire
[sfa.git] / sfa / methods / get_key.py
1 ### $Id:  $
2 ### $URL:  $
3 import os
4 import tempfile
5 import commands
6 from sfa.util.faults import *
7 from sfa.util.namespace 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.table import SfaTable
12 from sfa.trust.certificate import Keypair
13
14 class get_key(Method):
15     """
16     Generate a new keypair and gid for requesting caller (component).     
17     @return 1 If successful  
18     """
19
20     interfaces = ['registry']
21     
22     accepts = []
23
24     returns = Parameter(int, "1 if successful, faults otherwise")
25     
26     def call(self):
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'])
31         if not interfaces:
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'])
34         if not nodes:
35             raise NonExistingRecord("no such node using ip %(ip)s" % locals())
36         node = nodes[0]
37        
38         # look up the sfa record
39         table = SfaTable()
40         records = table.findObjects({'type': 'node', 'pointer': node['node_id']})
41         if not records:
42             raise RecordNotFound("pointer:" + str(node['node_id']))  
43         record = records[0]
44         
45         # generate a new keypair and gid
46         uuid = create_uuid()
47         pkey = Keypair(create=True)
48         urn = hrn_to_urn(record['hrn'], record['type'])
49         gid_object = self.api.auth.hierarchy.create_gid(urn, uuid, pkey)
50         gid = gid_object.save_to_string(save_parents=True)
51         record['gid'] = gid
52         record.set_gid(gid)
53
54         # update the record
55         table.update(record)
56   
57         # attempt the scp the key
58         # and gid onto the node
59         # this will only work for planetlab based components
60         (kfd, key_filename) = tempfile.mkstemp() 
61         (gfd, gid_filename) = tempfile.mkstemp() 
62         pkey.save_to_file(key_filename)
63         gid_object.save_to_file(gid_filename, save_parents=True)
64         host = node['hostname']
65         key_dest="/etc/sfa/node.key"
66         gid_dest="/etc/sfa/node.gid" 
67         scp = "/usr/bin/scp" 
68         identity = "/etc/sfa/root_ssh_key"
69         scp_options=" -i %(identity)s " % locals()
70         scp_options+="-o StrictHostKeyChecking=no " % locals()
71         scp_key_command="%(scp)s %(scp_options)s %(key_filename)s root@%(host)s:%(key_dest)s" %\
72                          locals()
73         scp_gid_command="%(scp)s %(scp_options)s %(gid_filename)s root@%(host)s:%(gid_dest)s" %\
74                          locals()    
75
76         all_commands = [scp_key_command, scp_gid_command]
77         
78         for command in all_commands:
79             (status, output) = commands.getstatusoutput(command)
80             if status:
81                 raise Exception, output
82
83         for filename in [key_filename, gid_filename]:
84             os.unlink(filename)
85
86         return 1