namespace module is gone, plxrn provides PL-specific translations
[sfa.git] / sfa / methods / get_key.py
1 import os
2 import tempfile
3 import commands
4 from sfa.util.faults import *
5 from sfa.util.xrn import hrn_to_urn
6 from sfa.util.method import Method
7 from sfa.util.parameter import Parameter, Mixed
8 from sfa.trust.auth import Auth
9 from sfa.util.table import SfaTable
10 from sfa.trust.certificate import Keypair
11 from sfa.trust.gid import create_uuid
12
13 class get_key(Method):
14     """
15     Generate a new keypair and gid for requesting caller (component).     
16     @return 1 If successful  
17     """
18
19     interfaces = ['registry']
20     
21     accepts = []
22
23     returns = Parameter(int, "1 if successful, faults otherwise")
24     
25     def call(self):
26         # verify that the callers's ip address exist in the db and is an inteface
27         # for a node in the db
28         (ip, port) = self.api.remote_addr
29         interfaces = self.api.plshell.GetInterfaces(self.api.plauth, {'ip': ip}, ['node_id'])
30         if not interfaces:
31             raise NonExistingRecord("no such ip %(ip)s" % locals())
32         nodes = self.api.plshell.GetNodes(self.api.plauth, [interfaces[0]['node_id']], ['node_id', 'hostname'])
33         if not nodes:
34             raise NonExistingRecord("no such node using ip %(ip)s" % locals())
35         node = nodes[0]
36        
37         # look up the sfa record
38         table = SfaTable()
39         records = table.findObjects({'type': 'node', 'pointer': node['node_id']})
40         if not records:
41             raise RecordNotFound("pointer:" + str(node['node_id']))  
42         record = records[0]
43         
44         # generate a new keypair and gid
45         uuid = create_uuid()
46         pkey = Keypair(create=True)
47         urn = hrn_to_urn(record['hrn'], record['type'])
48         gid_object = self.api.auth.hierarchy.create_gid(urn, uuid, pkey)
49         gid = gid_object.save_to_string(save_parents=True)
50         record['gid'] = gid
51         record.set_gid(gid)
52
53         # update the record
54         table.update(record)
55   
56         # attempt the scp the key
57         # and gid onto the node
58         # this will only work for planetlab based components
59         (kfd, key_filename) = tempfile.mkstemp() 
60         (gfd, gid_filename) = tempfile.mkstemp() 
61         pkey.save_to_file(key_filename)
62         gid_object.save_to_file(gid_filename, save_parents=True)
63         host = node['hostname']
64         key_dest="/etc/sfa/node.key"
65         gid_dest="/etc/sfa/node.gid" 
66         scp = "/usr/bin/scp" 
67         #identity = "/etc/planetlab/root_ssh_key.rsa"
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