#!/usr/local/bin/python import sys from BaseHTTPServer import BaseHTTPRequestHandler from ZSI import Fault, ParseException, FaultFromNotUnderstood, \ FaultFromZSIException, FaultFromException, ParsedSoap, SoapWriter from M2Crypto import SSL from M2Crypto.SSL.SSLServer import ThreadingSSLServer import xmlrpclib from optparse import OptionParser from fedd_util import fedd_ssl_context from fedid import fedid from fedd_deter_impl import new_feddservice from fedd_services import ns0 from service_error import * from threading import * from signal import signal, pause, SIGINT, SIGTERM from select import select, error from time import sleep import logging from ConfigParser import * # The SSL server here is based on the implementation described at # http://www.xml.com/pub/a/ws/2004/01/20/salz.html # Turn off the matching of hostname to certificate ID SSL.Connection.clientPostConnectionCheck = None class fedd_config_parser(SafeConfigParser): """ A SafeConfig parser with a more forgiving get attribute """ def safe_get(self, sect, opt, method, default=None): """ If the option is present, return it, otherwise the default. """ if self.has_option(sect, opt): return method(self, sect, opt) else: return default def get(self, sect, opt, default=None): """ This returns the requested option or a given default. It's more like getattr than get. """ return self.safe_get(sect, opt, SafeConfigParser.get, default) def getint(self, sect, opt, default=0): """ Returns the selected option as an int or a default. """ return self.safe_get(sect, opt, SafeConfigParser.getint, default) def getfloat(self, sect, opt, default=0.0): """ Returns the selected option as an int or a default. """ return self.safe_get(sect, opt, SafeConfigParser.getfloat, default) def getboolean(self, sect, opt, default=False): """ Returns the selected option as a boolean or a default. """ return self.safe_get(sect, opt, SafeConfigParser.getboolean, default) class fedd_server(ThreadingSSLServer): """ Interface the fedd services to the XMLRPC and SOAP interfaces """ def __init__(self, ME, handler, ssl_ctx, impl): """ Create an SSL server that handles the transport in handler using the credentials in ssl_ctx, and interfacing to the implementation of fedd services in fedd. ME is the host port pair on which to bind. """ ThreadingSSLServer.__init__(self, ME, handler, ssl_ctx) self.impl = impl self.soap_methods = impl.soap_services self.xmlrpc_methods = impl.xmlrpc_services self.log = logging.getLogger("fedd") class fedd_soap_handler(BaseHTTPRequestHandler): """ Standard connection between SOAP and the fedd services in impl. Much of this is boilerplate from http://www.xml.com/pub/a/ws/2004/01/20/salz.html """ server_version = "ZSI/2.0 fedd/0.1 " + BaseHTTPRequestHandler.server_version def send_xml(self, text, code=200): """Send an XML document as reply""" self.send_response(code) self.send_header('Content-type', 'text/xml; charset="utf-8"') self.send_header('Content-Length', str(len(text))) self.end_headers() self.wfile.write(text) self.wfile.flush() self.request.socket.close() def send_fault(self, f, code=500): """Send a SOAP encoded fault as reply""" self.send_xml(f.AsSOAP(processContents="lax"), code) def check_headers(self, ps): """Send a fault for any required envelope headers""" for (uri, localname) in ps.WhatMustIUnderstand(): self.send_fault(FaultFromNotUnderstood(uri, lname, 'fedd')) return False return True def check_method(self, ps): """Confirm that this class implements the namespace and SOAP method""" root = ps.body_root if root.namespaceURI not in self.server.impl.soap_namespaces: self.send_fault(Fault(Fault.Client, 'Unknown namespace "%s"' % root.namespaceURI)) return False if getattr(root, 'localName', None) == None: self.send_fault(Fault(Fault.Client, 'No method"')) return False return True def do_POST(self): """Treat an HTTP POST request as a SOAP service call""" try: cl = int(self.headers['content-length']) data = self.rfile.read(cl) ps = ParsedSoap(data) except ParseException, e: self.send_fault(Fault(Fault.Client, str(e))) return except Exception, e: self.send_fault(FaultFromException(e, 0, sys.exc_info()[2])) return if not self.check_headers(ps): return if not self.check_method(ps): return try: resp = self.soap_dispatch(ps.body_root.localName, ps, fedid(cert=self.request.get_peer_cert())) except Fault, f: self.send_fault(f) resp = None if resp != None: sw = SoapWriter() sw.serialize(resp) self.send_xml(str(sw)) def log_request(self, code=0, size=0): """ Log request to the fedd logger """ self.server.log.info("Successful SOAP request code %d" % code) def soap_dispatch(self, method, req, fid): """ The connection to the implementation, using the method maps The implementation provides a mapping from SOAP method name to the method in the implementation that provides the service. """ if self.server.soap_methods.has_key(method): try: return self.server.soap_methods[method](req, fid) except service_error, e: de = ns0.faultType_Def( (ns0.faultType_Def.schema, "FeddFaultBody")).pyclass() de._code=e.code de._errstr=e.code_string() de._desc=e.desc if e.is_server_error(): raise Fault(Fault.Server, e.code_string(), detail=de) else: raise Fault(Fault.Client, e.code_string(), detail=de) else: raise Fault(Fault.Client, "Unknown method: %s" % method) class fedd_xmlrpc_handler(BaseHTTPRequestHandler): """ Standard connection between XMLRPC and the fedd services in impl. Much of this is boilerplate from http://www.xml.com/pub/a/ws/2004/01/20/salz.html """ server_version = "ZSI/2.0 fedd/0.1 " + BaseHTTPRequestHandler.server_version def send_xml(self, text, code=200): """Send an XML document as reply""" self.send_response(code) self.send_header('Content-type', 'text/xml; charset="utf-8"') self.send_header('Content-Length', str(len(text))) self.end_headers() self.wfile.write(text) self.wfile.flush() # Make sure to close the socket when we're done self.request.socket.close() def do_POST(self): """Treat an HTTP POST request as an XMLRPC service call""" # NB: XMLRPC faults are not HTTP errors, so the code is always 200, # unless an HTTP error occurs, which we don't handle. resp = None data = None method = None cl = int(self.headers['content-length']) data = self.rfile.read(cl) try: params, method = xmlrpclib.loads(data) except xmlrpclib.ResponseError: data = xmlrpclib.dumps(xmlrpclib.Fault("Client", "Malformed request"), methodresponse=True) if method != None: try: resp = self.xmlrpc_dispatch(method, params, fedid(cert=self.request.get_peer_cert())) data = xmlrpclib.dumps((resp,), encoding='UTF-8', methodresponse=True) except xmlrpclib.Fault, f: data = xmlrpclib.dumps(f, methodresponse=True) resp = None self.send_xml(data) def log_request(self, code=0, size=0): """ Log request to the fedd logger """ self.server.log.info("Successful XMLRPC request code %d" % code) def xmlrpc_dispatch(self, method, req, fid): """ The connection to the implementation, using the method maps The implementation provides a mapping from XMLRPC method name to the method in the implementation that provides the service. """ if self.server.xmlrpc_methods.has_key(method): try: return self.server.xmlrpc_methods[method](req, fid) except service_error, e: raise xmlrpclib.Fault(e.code_string(), e.desc) else: raise xmlrpclib.Fault(100, "Unknown method: %s" % method) class fedd_opts(OptionParser): """Encapsulate option processing in this class, rather than in main""" def __init__(self): OptionParser.__init__(self, usage="%prog [opts] (--help for details)", version="0.1") self.set_defaults(host="localhost", port=23235, transport="soap", logfile=None, debug=0) self.add_option("-d", "--debug", action="count", dest="debug", help="Set debug. Repeat for more information") self.add_option("-f", "--configfile", action="store", dest="configfile", help="Configuration file (required)") self.add_option("-H", "--host", action="store", type="string", dest="host", help="Hostname to listen on (default %default)") self.add_option("-l", "--logfile", action="store", dest="logfile", help="File to send log messages to") self.add_option("-p", "--port", action="store", type="int", dest="port", help="Port to listen on (default %default)") self.add_option("-s", "--service", action="append", type="string", dest="services", help="Service description: host:port:transport") self.add_option("-x","--transport", action="store", type="choice", choices=("xmlrpc", "soap"), help="Transport for request (xmlrpc|soap) (Default: %default)") self.add_option("--trace", action="store_const", dest="tracefile", const=sys.stderr, help="Print SOAP exchange to stderr") servers_active = True # Sub-servers run while this is True services = [ ] # Service descriptions servers = [ ] # fedd_server instances instantiated from services servers_lock = Lock() # Lock to manipulate servers from sub-server threads def shutdown(sig, frame): """ On a signal, stop running sub-servers. This is connected to signals below """ global servers_active, flog servers_active = False flog.info("Received signal %d, shutting down" % sig); def run_server(s): """ Operate a subserver, shutting down when servers_active is false. Each server (that is host/port/transport triple) has a thread running this function, so each can handle requests independently. They all call in to the same implementation, which must manage its own synchronization. """ global servers_active # Not strictly needed: servers_active is only read global servers # List of active servers global servers_lock # Lock to manipulate servers while servers_active: try: i, o, e = select((s,), (), (), 1.0) if s in i: s.handle_request() except error: # The select call seems to get interrupted by signals as well as # the main thread. This essentially ignores signals in this # thread. pass # Done. Remove us from the list servers_lock.acquire() servers.remove(s) servers_lock.release() opts, args = fedd_opts().parse_args() # Logging setup flog = logging.getLogger("fedd") ffmt = logging.Formatter("%(asctime)s %(name)s %(message)s", '%d %b %y %H:%M:%S') if opts.logfile: fh = logging.FileHandler(opts.logfile) else: fh = logging.StreamHandler(sys.stdout) # The handler will print anything, setting the logger level will affect what # gets recorded. fh.setLevel(logging.DEBUG) if opts.debug: flog.setLevel(logging.DEBUG) else: flog.setLevel(logging.INFO) fh.setFormatter(ffmt) flog.addHandler(fh) # Initialize the implementation if opts.configfile != None: try: config= fedd_config_parser() config.read(opts.configfile) except e: sys.exit("Cannot parse confgi file: %s" % e) else: sys.exit("--configfile is required") try: impl = new_feddservice(config) except RuntimeError, e: str = getattr(e, 'desc', None) or getattr(e,'message', None) or \ "No message" sys.exit("Error configuring fedd: %s" % str) if impl.cert_file == None: sys.exit("Must supply certificate file (probably in config)") # Create the SSL credentials ctx = None while ctx == None: try: ctx = fedd_ssl_context(impl.cert_file, impl.trusted_certs, password=impl.cert_pwd) except SSL.SSLError, e: if str(e) != "bad decrypt" or impl.cert_pwd != None: raise # Walk through the service descriptions and pack them into the services list. # That list has the form (transport (host, port)). if opts.services: for s in opts.services: h, p, t = s.split(':') if not h: h = opts.host if not p: p = opts.port if not t: h = opts.transport p = int(p) services.append((t, (h, p))) else: services.append((opts.transport, (opts.host, opts.port))) # Create the servers and put them into a list for s in services: if s[0] == "soap": servers.append(fedd_server(s[1], fedd_soap_handler, ctx, impl)) elif s[0] == "xmlrpc": servers.append(fedd_server(s[1], fedd_xmlrpc_handler, ctx, impl)) else: flog.warning("Unknown transport: %s" % s[0]) # Make sure that there are no malformed servers in the list services = [ s for s in services if s ] # Catch signals signal(SIGINT, shutdown) signal(SIGTERM, shutdown) # Start the servers for s in servers: Thread(target=run_server, args=(s,)).start() # Main thread waits for signals while servers_active: sleep(1.0) #Once shutdown starts wait for all the servers to terminate. while True: servers_lock.acquire() if len(servers) == 0: servers_lock.release() flog.info("All servers exited. Terminating") sys.exit(0) servers_lock.release() sleep(1)