223ae774cb2edfb1fe84897a6e4d7eb97624b6c6
[sfa.git] / sfa / server / sfaserver.py
1 ##
2 # This module implements a general-purpose server layer for sfa.
3 # The same basic server should be usable on the registry, component, or
4 # other interfaces.
5 #
6 # TODO: investigate ways to combine this with existing PLC server?
7 ##
8
9 import threading
10
11 from sfa.server.threadedserver import ThreadedServer, SecureXMLRpcRequestHandler
12
13 from sfa.util.sfalogging import logger
14 from sfa.trust.certificate import Keypair, Certificate
15
16 ##
17 # Implements an HTTPS XML-RPC server. Generally it is expected that SFA
18 # functions will take a credential string, which is passed to
19 # decode_authentication. Decode_authentication() will verify the validity of
20 # the credential, and verify that the user is using the key that matches the
21 # GID supplied in the credential.
22
23
24 class SfaServer(threading.Thread):
25
26     ##
27     # Create a new SfaServer object.
28     #
29     # @param ip the ip address to listen on
30     # @param port the port to listen on
31     # @param key_file private key filename of registry
32     # @param cert_file certificate filename containing public key
33     #   (could be a GID file)
34
35     def __init__(self, ip, port, key_file, cert_file, interface):
36         threading.Thread.__init__(self)
37         self.key = Keypair(filename=key_file)
38         self.cert = Certificate(filename=cert_file)
39         #self.server = SecureXMLRPCServer((ip, port), SecureXMLRpcRequestHandler, key_file, cert_file)
40         self.server = ThreadedServer(
41             (ip, int(port)), SecureXMLRpcRequestHandler, key_file, cert_file)
42         self.server.interface = interface
43         self.trusted_cert_list = None
44         self.register_functions()
45         logger.info("Starting SfaServer, interface=%s" % interface)
46
47     ##
48     # Register functions that will be served by the XMLRPC server. This
49     # function should be overridden by each descendant class.
50
51     def register_functions(self):
52         self.server.register_function(self.noop)
53
54     ##
55     # Sample no-op server function. The no-op function decodes the credential
56     # that was passed to it.
57
58     def noop(self, cred, anything):
59         return anything
60
61     ##
62     # Execute the server, serving requests forever.
63
64     def run(self):
65         self.server.serve_forever()