e09dbee5509b65c0a2c78ea3a31f20e8adf3a7ca
[sfa.git] / sfa / plc / plccomponentapi.py
1 import os
2 import tempfile
3
4 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
5 from sfa.util.nodemanager import NodeManager
6
7 from sfa.trust.credential import Credential
8 from sfa.trust.certificate import Certificate, Keypair
9 from sfa.trust.gid import GID
10
11 from sfa.server.sfaapi import SfaApi
12
13 ####################
14 class PlcComponentApi(SfaApi):
15
16     def __init__ (self, encoding="utf-8", methods='sfa.methods', 
17                   config = "/etc/sfa/sfa_config.py", 
18                   peer_cert = None, interface = None, 
19                   key_file = None, cert_file = None, cache = None):
20         SfaApi.__init__(self, encoding=encoding, methods=methods, 
21                         config=config, 
22                         peer_cert=peer_cert, interface=interface, 
23                         key_file=key_file, 
24                         cert_file=cert_file, cache=cache)
25
26         self.nodemanager = NodeManager(self.config)
27
28     def sliver_exists(self):
29         sliver_dict = self.nodemanager.GetXIDs()
30         ### xxx slicename is undefined
31         if slicename in sliver_dict.keys():
32             return True
33         else:
34             return False
35
36     def get_registry(self):
37         addr, port = self.config.SFA_REGISTRY_HOST, self.config.SFA_REGISTRY_PORT
38         url = "http://%(addr)s:%(port)s" % locals()
39         server = xmlrpcprotocol.get_server(url, self.key_file, self.cert_file)
40         return server
41
42     def get_node_key(self):
43         # this call requires no authentication,
44         # so we can generate a random keypair here
45         subject="component"
46         (kfd, keyfile) = tempfile.mkstemp()
47         (cfd, certfile) = tempfile.mkstemp()
48         key = Keypair(create=True)
49         key.save_to_file(keyfile)
50         cert = Certificate(subject=subject)
51         cert.set_issuer(key=key, subject=subject)
52         cert.set_pubkey(key)
53         cert.sign()
54         cert.save_to_file(certfile)
55         registry = self.get_registry()
56         # the registry will scp the key onto the node
57         registry.get_key()        
58
59     # override the method in SfaApi
60     def getCredential(self):
61         """
62         Get our credential from a remote registry
63         """
64         path = self.config.SFA_DATA_DIR
65         config_dir = self.config.config_path
66         cred_filename = path + os.sep + 'node.cred'
67         try:
68             credential = Credential(filename = cred_filename)
69             return credential.save_to_string(save_parents=True)
70         except IOError:
71             node_pkey_file = config_dir + os.sep + "node.key"
72             node_gid_file = config_dir + os.sep + "node.gid"
73             cert_filename = path + os.sep + 'server.cert'
74             if not os.path.exists(node_pkey_file) or \
75                not os.path.exists(node_gid_file):
76                 self.get_node_key()
77
78             # get node's hrn
79             gid = GID(filename=node_gid_file)
80             hrn = gid.get_hrn()
81             # get credential from registry
82             cert_str = Certificate(filename=cert_filename).save_to_string(save_parents=True)
83             registry = self.get_registry()
84             cred = registry.GetSelfCredential(cert_str, hrn, 'node')
85             # xxx credfile is undefined
86             Credential(string=cred).save_to_file(credfile, save_parents=True)            
87
88             return cred
89
90     def clean_key_cred(self):
91         """
92         remove the existing keypair and cred  and generate new ones
93         """
94         files = ["server.key", "server.cert", "node.cred"]
95         for f in files:
96             # xxx KEYDIR is undefined, could be meant to be "/var/lib/sfa/" from sfa_component_setup.py
97             filepath = KEYDIR + os.sep + f
98             if os.path.isfile(filepath):
99                 os.unlink(f)
100
101         # install the new key pair
102         # GetCredential will take care of generating the new keypair
103         # and credential
104         self.get_node_key()
105         self.getCredential()