4 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
5 from sfa.util.nodemanager import NodeManager
7 from sfa.trust.credential import Credential
8 from sfa.trust.certificate import Certificate, Keypair
9 from sfa.trust.gid import GID
11 from sfa.server.sfaapi import SfaApi
14 class PlcComponentApi(SfaApi):
16 This class is the type for the toplevel 'api' object
17 when running the component manager inside a planetlab node.
18 As such it runs an SFA-compliant interface and thus inherits SfaApi
19 However the fact that we run inside a planetlab nodes requires
20 some tweaks as compared with a service running in the infrastructure.
23 def __init__ (self, encoding="utf-8", methods='sfa.methods',
24 config = "/etc/sfa/sfa_config.py",
25 peer_cert = None, interface = None,
26 key_file = None, cert_file = None, cache = None):
27 SfaApi.__init__(self, encoding=encoding, methods=methods,
29 peer_cert=peer_cert, interface=interface,
31 cert_file=cert_file, cache=cache)
33 self.nodemanager = NodeManager(self.config)
35 def sliver_exists(self):
36 sliver_dict = self.nodemanager.GetXIDs()
37 ### xxx slicename is undefined
38 if slicename in sliver_dict.keys():
43 def get_registry(self):
44 addr, port = self.config.SFA_REGISTRY_HOST, self.config.SFA_REGISTRY_PORT
45 url = "http://%(addr)s:%(port)s" % locals()
46 server = xmlrpcprotocol.get_server(url, self.key_file, self.cert_file)
49 def get_node_key(self):
50 # this call requires no authentication,
51 # so we can generate a random keypair here
53 (kfd, keyfile) = tempfile.mkstemp()
54 (cfd, certfile) = tempfile.mkstemp()
55 key = Keypair(create=True)
56 key.save_to_file(keyfile)
57 cert = Certificate(subject=subject)
58 cert.set_issuer(key=key, subject=subject)
61 cert.save_to_file(certfile)
62 registry = self.get_registry()
63 # the registry will scp the key onto the node
66 # override the method in SfaApi
67 def getCredential(self):
69 Get our credential from a remote registry
71 path = self.config.SFA_DATA_DIR
72 config_dir = self.config.config_path
73 cred_filename = path + os.sep + 'node.cred'
75 credential = Credential(filename = cred_filename)
76 return credential.save_to_string(save_parents=True)
78 node_pkey_file = config_dir + os.sep + "node.key"
79 node_gid_file = config_dir + os.sep + "node.gid"
80 cert_filename = path + os.sep + 'server.cert'
81 if not os.path.exists(node_pkey_file) or \
82 not os.path.exists(node_gid_file):
86 gid = GID(filename=node_gid_file)
88 # get credential from registry
89 cert_str = Certificate(filename=cert_filename).save_to_string(save_parents=True)
90 registry = self.get_registry()
91 cred = registry.GetSelfCredential(cert_str, hrn, 'node')
92 # xxx credfile is undefined
93 Credential(string=cred).save_to_file(credfile, save_parents=True)
97 def clean_key_cred(self):
99 remove the existing keypair and cred and generate new ones
101 files = ["server.key", "server.cert", "node.cred"]
103 # xxx KEYDIR is undefined, could be meant to be "/var/lib/sfa/" from sfa_component_setup.py
104 filepath = KEYDIR + os.sep + f
105 if os.path.isfile(filepath):
108 # install the new key pair
109 # GetCredential will take care of generating the new keypair