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 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,
22 peer_cert=peer_cert, interface=interface,
24 cert_file=cert_file, cache=cache)
26 self.nodemanager = NodeManager(self.config)
28 def sliver_exists(self):
29 sliver_dict = self.nodemanager.GetXIDs()
30 ### xxx slicename is undefined
31 if slicename in sliver_dict.keys():
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)
42 def get_node_key(self):
43 # this call requires no authentication,
44 # so we can generate a random keypair here
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)
54 cert.save_to_file(certfile)
55 registry = self.get_registry()
56 # the registry will scp the key onto the node
59 # override the method in SfaApi
60 def getCredential(self):
62 Get our credential from a remote registry
64 path = self.config.SFA_DATA_DIR
65 config_dir = self.config.config_path
66 cred_filename = path + os.sep + 'node.cred'
68 credential = Credential(filename = cred_filename)
69 return credential.save_to_string(save_parents=True)
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):
79 gid = GID(filename=node_gid_file)
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)
90 def clean_key_cred(self):
92 remove the existing keypair and cred and generate new ones
94 files = ["server.key", "server.cert", "node.cred"]
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):
101 # install the new key pair
102 # GetCredential will take care of generating the new keypair