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