[6ff0b91] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | |
---|
| 5 | from BaseHTTPServer import BaseHTTPRequestHandler |
---|
| 6 | |
---|
| 7 | from ZSI import Fault, ParseException, FaultFromNotUnderstood, \ |
---|
| 8 | FaultFromZSIException, FaultFromException, ParsedSoap, SoapWriter |
---|
| 9 | |
---|
| 10 | from M2Crypto import SSL |
---|
[8ecfbad] | 11 | from M2Crypto.SSL.SSLServer import ThreadingSSLServer |
---|
[329f61d] | 12 | import xmlrpclib |
---|
[6ff0b91] | 13 | |
---|
| 14 | from optparse import OptionParser |
---|
| 15 | |
---|
| 16 | from fedd_util import fedd_ssl_context, fedid |
---|
[19cc408] | 17 | from fedd_deter_impl import new_feddservice |
---|
[f4f4117] | 18 | from fedd_services import ns0 |
---|
[19cc408] | 19 | from service_error import * |
---|
[6ff0b91] | 20 | |
---|
[a97394b] | 21 | from threading import * |
---|
[11a08b0] | 22 | from signal import signal, pause, SIGINT, SIGTERM |
---|
[d199ced] | 23 | from select import select, error |
---|
[0ea11af] | 24 | from time import sleep |
---|
[11a08b0] | 25 | import logging |
---|
[72ed6e4] | 26 | from ConfigParser import * |
---|
[a97394b] | 27 | |
---|
[6ff0b91] | 28 | # The SSL server here is based on the implementation described at |
---|
| 29 | # http://www.xml.com/pub/a/ws/2004/01/20/salz.html |
---|
| 30 | |
---|
| 31 | # Turn off the matching of hostname to certificate ID |
---|
| 32 | SSL.Connection.clientPostConnectionCheck = None |
---|
| 33 | |
---|
[72ed6e4] | 34 | class fedd_config_parser(SafeConfigParser): |
---|
| 35 | """ |
---|
| 36 | A SafeConfig parser with a more forgiving get attribute |
---|
| 37 | """ |
---|
[f4f4117] | 38 | |
---|
| 39 | def safe_get(self, sect, opt, method, default=None): |
---|
| 40 | """ |
---|
| 41 | If the option is present, return it, otherwise the default. |
---|
| 42 | """ |
---|
| 43 | if self.has_option(sect, opt): return method(self, sect, opt) |
---|
| 44 | else: return default |
---|
| 45 | |
---|
[72ed6e4] | 46 | def get(self, sect, opt, default=None): |
---|
| 47 | """ |
---|
| 48 | This returns the requested option or a given default. |
---|
| 49 | |
---|
| 50 | It's more like getattr than get. |
---|
| 51 | """ |
---|
[f4f4117] | 52 | |
---|
| 53 | return self.safe_get(sect, opt, SafeConfigParser.get, default) |
---|
| 54 | |
---|
| 55 | def getint(self, sect, opt, default=0): |
---|
| 56 | """ |
---|
| 57 | Returns the selected option as an int or a default. |
---|
| 58 | """ |
---|
| 59 | |
---|
| 60 | return self.safe_get(sect, opt, SafeConfigParser.getint, default) |
---|
| 61 | |
---|
| 62 | def getfloat(self, sect, opt, default=0.0): |
---|
| 63 | """ |
---|
| 64 | Returns the selected option as an int or a default. |
---|
| 65 | """ |
---|
| 66 | |
---|
| 67 | return self.safe_get(sect, opt, SafeConfigParser.getfloat, default) |
---|
| 68 | |
---|
| 69 | def getboolean(self, sect, opt, default=False): |
---|
| 70 | """ |
---|
| 71 | Returns the selected option as a boolean or a default. |
---|
| 72 | """ |
---|
| 73 | |
---|
| 74 | return self.safe_get(sect, opt, SafeConfigParser.getboolean, default) |
---|
[72ed6e4] | 75 | |
---|
[8ecfbad] | 76 | class fedd_server(ThreadingSSLServer): |
---|
[0ea11af] | 77 | """ |
---|
| 78 | Interface the fedd services to the XMLRPC and SOAP interfaces |
---|
| 79 | """ |
---|
[6ff0b91] | 80 | def __init__(self, ME, handler, ssl_ctx, impl): |
---|
[0ea11af] | 81 | """ |
---|
| 82 | Create an SSL server that handles the transport in handler using the |
---|
| 83 | credentials in ssl_ctx, and interfacing to the implementation of fedd |
---|
| 84 | services in fedd. ME is the host port pair on which to bind. |
---|
| 85 | """ |
---|
[8ecfbad] | 86 | ThreadingSSLServer.__init__(self, ME, handler, ssl_ctx) |
---|
[6ff0b91] | 87 | self.impl = impl |
---|
[0b466d1] | 88 | self.soap_methods = impl.soap_services |
---|
| 89 | self.xmlrpc_methods = impl.xmlrpc_services |
---|
| 90 | self.log = logging.getLogger("fedd") |
---|
[6ff0b91] | 91 | |
---|
[329f61d] | 92 | class fedd_soap_handler(BaseHTTPRequestHandler): |
---|
[0ea11af] | 93 | """ |
---|
| 94 | Standard connection between SOAP and the fedd services in impl. |
---|
| 95 | |
---|
| 96 | Much of this is boilerplate from |
---|
| 97 | http://www.xml.com/pub/a/ws/2004/01/20/salz.html |
---|
| 98 | """ |
---|
[6ff0b91] | 99 | server_version = "ZSI/2.0 fedd/0.1 " + BaseHTTPRequestHandler.server_version |
---|
| 100 | |
---|
| 101 | def send_xml(self, text, code=200): |
---|
| 102 | """Send an XML document as reply""" |
---|
| 103 | self.send_response(code) |
---|
| 104 | self.send_header('Content-type', 'text/xml; charset="utf-8"') |
---|
| 105 | self.send_header('Content-Length', str(len(text))) |
---|
| 106 | self.end_headers() |
---|
| 107 | self.wfile.write(text) |
---|
| 108 | self.wfile.flush() |
---|
[dab4d56] | 109 | self.request.socket.close() |
---|
[6ff0b91] | 110 | |
---|
| 111 | def send_fault(self, f, code=500): |
---|
| 112 | """Send a SOAP encoded fault as reply""" |
---|
[0a47d52] | 113 | self.send_xml(f.AsSOAP(processContents="lax"), code) |
---|
[6ff0b91] | 114 | |
---|
| 115 | def check_headers(self, ps): |
---|
| 116 | """Send a fault for any required envelope headers""" |
---|
| 117 | for (uri, localname) in ps.WhatMustIUnderstand(): |
---|
| 118 | self.send_fault(FaultFromNotUnderstood(uri, lname, 'fedd')) |
---|
| 119 | return False |
---|
| 120 | return True |
---|
| 121 | |
---|
| 122 | def check_method(self, ps): |
---|
| 123 | """Confirm that this class implements the namespace and SOAP method""" |
---|
| 124 | root = ps.body_root |
---|
[7aec37d] | 125 | if root.namespaceURI not in self.server.impl.soap_namespaces: |
---|
[6ff0b91] | 126 | self.send_fault(Fault(Fault.Client, |
---|
| 127 | 'Unknown namespace "%s"' % root.namespaceURI)) |
---|
| 128 | return False |
---|
[329f61d] | 129 | |
---|
| 130 | if getattr(root, 'localName', None) == None: |
---|
| 131 | self.send_fault(Fault(Fault.Client, 'No method"')) |
---|
[6ff0b91] | 132 | return False |
---|
| 133 | return True |
---|
| 134 | |
---|
| 135 | def do_POST(self): |
---|
| 136 | """Treat an HTTP POST request as a SOAP service call""" |
---|
| 137 | try: |
---|
| 138 | cl = int(self.headers['content-length']) |
---|
| 139 | data = self.rfile.read(cl) |
---|
| 140 | ps = ParsedSoap(data) |
---|
| 141 | except ParseException, e: |
---|
[0a47d52] | 142 | self.send_fault(Fault(Fault.Client, str(e))) |
---|
[6ff0b91] | 143 | return |
---|
| 144 | except Exception, e: |
---|
| 145 | self.send_fault(FaultFromException(e, 0, sys.exc_info()[2])) |
---|
| 146 | return |
---|
| 147 | if not self.check_headers(ps): return |
---|
| 148 | if not self.check_method(ps): return |
---|
| 149 | try: |
---|
[19cc408] | 150 | resp = self.soap_dispatch(ps.body_root.localName, ps, |
---|
[329f61d] | 151 | fedid(cert=self.request.get_peer_cert())) |
---|
[6ff0b91] | 152 | except Fault, f: |
---|
| 153 | self.send_fault(f) |
---|
| 154 | resp = None |
---|
| 155 | |
---|
| 156 | if resp != None: |
---|
| 157 | sw = SoapWriter() |
---|
| 158 | sw.serialize(resp) |
---|
| 159 | self.send_xml(str(sw)) |
---|
| 160 | |
---|
[0b466d1] | 161 | def log_request(self, code=0, size=0): |
---|
| 162 | """ |
---|
| 163 | Log request to the fedd logger |
---|
| 164 | """ |
---|
| 165 | self.server.log.info("Successful SOAP request code %d" % code) |
---|
| 166 | |
---|
[19cc408] | 167 | def soap_dispatch(self, method, req, fid): |
---|
[0ea11af] | 168 | """ |
---|
| 169 | The connection to the implementation, using the method maps |
---|
| 170 | |
---|
| 171 | The implementation provides a mapping from SOAP method name to the |
---|
| 172 | method in the implementation that provides the service. |
---|
| 173 | """ |
---|
[19cc408] | 174 | if self.server.soap_methods.has_key(method): |
---|
| 175 | try: |
---|
| 176 | return self.server.soap_methods[method](req, fid) |
---|
| 177 | except service_error, e: |
---|
| 178 | de = ns0.faultType_Def( |
---|
| 179 | (ns0.faultType_Def.schema, |
---|
| 180 | "FeddFaultBody")).pyclass() |
---|
| 181 | de._code=e.code |
---|
| 182 | de._errstr=e.code_string() |
---|
| 183 | de._desc=e.desc |
---|
| 184 | if e.is_server_error(): |
---|
| 185 | raise Fault(Fault.Server, e.code_string(), detail=de) |
---|
| 186 | else: |
---|
| 187 | raise Fault(Fault.Client, e.code_string(), detail=de) |
---|
| 188 | else: |
---|
| 189 | raise Fault(Fault.Client, "Unknown method: %s" % method) |
---|
| 190 | |
---|
| 191 | |
---|
[329f61d] | 192 | class fedd_xmlrpc_handler(BaseHTTPRequestHandler): |
---|
[0ea11af] | 193 | """ |
---|
| 194 | Standard connection between XMLRPC and the fedd services in impl. |
---|
| 195 | |
---|
| 196 | Much of this is boilerplate from |
---|
| 197 | http://www.xml.com/pub/a/ws/2004/01/20/salz.html |
---|
| 198 | """ |
---|
[329f61d] | 199 | server_version = "ZSI/2.0 fedd/0.1 " + BaseHTTPRequestHandler.server_version |
---|
| 200 | |
---|
| 201 | def send_xml(self, text, code=200): |
---|
| 202 | """Send an XML document as reply""" |
---|
| 203 | self.send_response(code) |
---|
| 204 | self.send_header('Content-type', 'text/xml; charset="utf-8"') |
---|
| 205 | self.send_header('Content-Length', str(len(text))) |
---|
| 206 | self.end_headers() |
---|
| 207 | self.wfile.write(text) |
---|
| 208 | self.wfile.flush() |
---|
[dab4d56] | 209 | # Make sure to close the socket when we're done |
---|
| 210 | self.request.socket.close() |
---|
[329f61d] | 211 | |
---|
| 212 | def do_POST(self): |
---|
| 213 | """Treat an HTTP POST request as an XMLRPC service call""" |
---|
[058f58e] | 214 | # NB: XMLRPC faults are not HTTP errors, so the code is always 200, |
---|
| 215 | # unless an HTTP error occurs, which we don't handle. |
---|
[329f61d] | 216 | |
---|
| 217 | resp = None |
---|
| 218 | data = None |
---|
[0a47d52] | 219 | method = None |
---|
[329f61d] | 220 | cl = int(self.headers['content-length']) |
---|
| 221 | data = self.rfile.read(cl) |
---|
| 222 | |
---|
| 223 | try: |
---|
[0a47d52] | 224 | params, method = xmlrpclib.loads(data) |
---|
| 225 | except xmlrpclib.ResponseError: |
---|
| 226 | data = xmlrpclib.dumps(xmlrpclib.Fault("Client", |
---|
| 227 | "Malformed request"), methodresponse=True) |
---|
[dab4d56] | 228 | |
---|
[0a47d52] | 229 | if method != None: |
---|
| 230 | try: |
---|
[19cc408] | 231 | resp = self.xmlrpc_dispatch(method, params, |
---|
[0a47d52] | 232 | fedid(cert=self.request.get_peer_cert())) |
---|
[bcbf543] | 233 | data = xmlrpclib.dumps((resp,), encoding='UTF-8', |
---|
| 234 | methodresponse=True) |
---|
[0a47d52] | 235 | except xmlrpclib.Fault, f: |
---|
| 236 | data = xmlrpclib.dumps(f, methodresponse=True) |
---|
| 237 | resp = None |
---|
[dab4d56] | 238 | |
---|
[058f58e] | 239 | self.send_xml(data) |
---|
[329f61d] | 240 | |
---|
[0b466d1] | 241 | def log_request(self, code=0, size=0): |
---|
| 242 | """ |
---|
| 243 | Log request to the fedd logger |
---|
| 244 | """ |
---|
| 245 | self.server.log.info("Successful XMLRPC request code %d" % code) |
---|
| 246 | |
---|
[329f61d] | 247 | |
---|
[19cc408] | 248 | def xmlrpc_dispatch(self, method, req, fid): |
---|
[0ea11af] | 249 | """ |
---|
| 250 | The connection to the implementation, using the method maps |
---|
| 251 | |
---|
| 252 | The implementation provides a mapping from XMLRPC method name to the |
---|
| 253 | method in the implementation that provides the service. |
---|
| 254 | """ |
---|
[19cc408] | 255 | if self.server.xmlrpc_methods.has_key(method): |
---|
| 256 | try: |
---|
| 257 | return self.server.xmlrpc_methods[method](req, fid) |
---|
| 258 | except service_error, e: |
---|
| 259 | raise xmlrpclib.Fault(e.code_string(), e.desc) |
---|
| 260 | else: |
---|
| 261 | raise xmlrpclib.Fault(100, "Unknown method: %s" % method) |
---|
| 262 | |
---|
[6ff0b91] | 263 | class fedd_opts(OptionParser): |
---|
| 264 | """Encapsulate option processing in this class, rather than in main""" |
---|
| 265 | def __init__(self): |
---|
| 266 | OptionParser.__init__(self, usage="%prog [opts] (--help for details)", |
---|
| 267 | version="0.1") |
---|
| 268 | |
---|
[a97394b] | 269 | self.set_defaults(host="localhost", port=23235, transport="soap", |
---|
[11a08b0] | 270 | logfile=None, debug=0) |
---|
[6ff0b91] | 271 | |
---|
| 272 | self.add_option("-d", "--debug", action="count", dest="debug", |
---|
| 273 | help="Set debug. Repeat for more information") |
---|
| 274 | self.add_option("-f", "--configfile", action="store", |
---|
| 275 | dest="configfile", help="Configuration file (required)") |
---|
| 276 | self.add_option("-H", "--host", action="store", type="string", |
---|
| 277 | dest="host", help="Hostname to listen on (default %default)") |
---|
[11a08b0] | 278 | self.add_option("-l", "--logfile", action="store", dest="logfile", |
---|
| 279 | help="File to send log messages to") |
---|
[6ff0b91] | 280 | self.add_option("-p", "--port", action="store", type="int", |
---|
| 281 | dest="port", help="Port to listen on (default %default)") |
---|
[a97394b] | 282 | self.add_option("-s", "--service", action="append", type="string", |
---|
| 283 | dest="services", |
---|
| 284 | help="Service description: host:port:transport") |
---|
[329f61d] | 285 | self.add_option("-x","--transport", action="store", type="choice", |
---|
| 286 | choices=("xmlrpc", "soap"), |
---|
| 287 | help="Transport for request (xmlrpc|soap) (Default: %default)") |
---|
[6ff0b91] | 288 | self.add_option("--trace", action="store_const", dest="tracefile", |
---|
| 289 | const=sys.stderr, help="Print SOAP exchange to stderr") |
---|
| 290 | |
---|
[0ea11af] | 291 | servers_active = True # Sub-servers run while this is True |
---|
| 292 | services = [ ] # Service descriptions |
---|
| 293 | servers = [ ] # fedd_server instances instantiated from services |
---|
| 294 | servers_lock = Lock() # Lock to manipulate servers from sub-server threads |
---|
[11a08b0] | 295 | |
---|
| 296 | def shutdown(sig, frame): |
---|
[0ea11af] | 297 | """ |
---|
| 298 | On a signal, stop running sub-servers. |
---|
| 299 | |
---|
| 300 | This is connected to signals below |
---|
| 301 | """ |
---|
[11a08b0] | 302 | global servers_active, flog |
---|
[d199ced] | 303 | |
---|
[11a08b0] | 304 | servers_active = False |
---|
| 305 | flog.info("Received signal %d, shutting down" % sig); |
---|
| 306 | |
---|
[a97394b] | 307 | def run_server(s): |
---|
[0ea11af] | 308 | """ |
---|
| 309 | Operate a subserver, shutting down when servers_active is false. |
---|
| 310 | |
---|
| 311 | Each server (that is host/port/transport triple) has a thread running this |
---|
| 312 | function, so each can handle requests independently. They all call in to |
---|
| 313 | the same implementation, which must manage its own synchronization. |
---|
| 314 | """ |
---|
[11a08b0] | 315 | global servers_active # Not strictly needed: servers_active is only read |
---|
[0ea11af] | 316 | global servers # List of active servers |
---|
| 317 | global servers_lock # Lock to manipulate servers |
---|
[11a08b0] | 318 | |
---|
[0ea11af] | 319 | while servers_active: |
---|
[d199ced] | 320 | try: |
---|
| 321 | i, o, e = select((s,), (), (), 1.0) |
---|
| 322 | if s in i: s.handle_request() |
---|
| 323 | except error: |
---|
| 324 | # The select call seems to get interrupted by signals as well as |
---|
| 325 | # the main thread. This essentially ignores signals in this |
---|
| 326 | # thread. |
---|
| 327 | pass |
---|
[a97394b] | 328 | |
---|
[0ea11af] | 329 | # Done. Remove us from the list |
---|
| 330 | servers_lock.acquire() |
---|
| 331 | servers.remove(s) |
---|
| 332 | servers_lock.release() |
---|
[a97394b] | 333 | |
---|
[6ff0b91] | 334 | opts, args = fedd_opts().parse_args() |
---|
| 335 | |
---|
[0ea11af] | 336 | # Logging setup |
---|
[11a08b0] | 337 | flog = logging.getLogger("fedd") |
---|
[0ea11af] | 338 | ffmt = logging.Formatter("%(asctime)s %(name)s %(message)s", |
---|
| 339 | '%d %b %y %H:%M:%S') |
---|
[11a08b0] | 340 | |
---|
| 341 | if opts.logfile: fh = logging.FileHandler(opts.logfile) |
---|
| 342 | else: fh = logging.StreamHandler(sys.stdout) |
---|
| 343 | |
---|
| 344 | # The handler will print anything, setting the logger level will affect what |
---|
| 345 | # gets recorded. |
---|
| 346 | fh.setLevel(logging.DEBUG) |
---|
| 347 | |
---|
| 348 | if opts.debug: flog.setLevel(logging.DEBUG) |
---|
| 349 | else: flog.setLevel(logging.INFO) |
---|
| 350 | |
---|
| 351 | fh.setFormatter(ffmt) |
---|
| 352 | flog.addHandler(fh) |
---|
| 353 | |
---|
[72ed6e4] | 354 | |
---|
| 355 | |
---|
[0ea11af] | 356 | # Initialize the implementation |
---|
[6ff0b91] | 357 | if opts.configfile != None: |
---|
| 358 | try: |
---|
[72ed6e4] | 359 | config= fedd_config_parser() |
---|
| 360 | config.read(opts.configfile) |
---|
| 361 | except e: |
---|
| 362 | sys.exit("Cannot parse confgi file: %s" % e) |
---|
[6ff0b91] | 363 | else: |
---|
| 364 | sys.exit("--configfile is required") |
---|
| 365 | |
---|
[72ed6e4] | 366 | try: |
---|
| 367 | impl = new_feddservice(config) |
---|
| 368 | except RuntimeError, e: |
---|
| 369 | str = getattr(e, 'desc', None) or getattr(e,'message', None) or \ |
---|
| 370 | "No message" |
---|
| 371 | sys.exit("Error configuring fedd: %s" % str) |
---|
| 372 | |
---|
[6ff0b91] | 373 | if impl.cert_file == None: |
---|
| 374 | sys.exit("Must supply certificate file (probably in config)") |
---|
| 375 | |
---|
[0ea11af] | 376 | # Create the SSL credentials |
---|
[2106ed1] | 377 | ctx = None |
---|
| 378 | while ctx == None: |
---|
[6ff0b91] | 379 | try: |
---|
| 380 | ctx = fedd_ssl_context(impl.cert_file, impl.trusted_certs, |
---|
| 381 | password=impl.cert_pwd) |
---|
| 382 | except SSL.SSLError, e: |
---|
[2106ed1] | 383 | if str(e) != "bad decrypt" or impl.cert_pwd != None: |
---|
[6ff0b91] | 384 | raise |
---|
| 385 | |
---|
[0ea11af] | 386 | # Walk through the service descriptions and pack them into the services list. |
---|
| 387 | # That list has the form (transport (host, port)). |
---|
[a97394b] | 388 | if opts.services: |
---|
| 389 | for s in opts.services: |
---|
| 390 | h, p, t = s.split(':') |
---|
[329f61d] | 391 | |
---|
[a97394b] | 392 | if not h: h = opts.host |
---|
| 393 | if not p: p = opts.port |
---|
| 394 | if not t: h = opts.transport |
---|
| 395 | |
---|
| 396 | p = int(p) |
---|
| 397 | |
---|
| 398 | services.append((t, (h, p))) |
---|
| 399 | else: |
---|
| 400 | services.append((opts.transport, (opts.host, opts.port))) |
---|
| 401 | |
---|
[0ea11af] | 402 | # Create the servers and put them into a list |
---|
[a97394b] | 403 | for s in services: |
---|
| 404 | if s[0] == "soap": |
---|
| 405 | servers.append(fedd_server(s[1], fedd_soap_handler, ctx, impl)) |
---|
| 406 | elif s[0] == "xmlrpc": |
---|
| 407 | servers.append(fedd_server(s[1], fedd_xmlrpc_handler, ctx, impl)) |
---|
[11a08b0] | 408 | else: flog.warning("Unknown transport: %s" % s[0]) |
---|
| 409 | |
---|
[0ea11af] | 410 | # Make sure that there are no malformed servers in the list |
---|
| 411 | services = [ s for s in services if s ] |
---|
| 412 | |
---|
| 413 | # Catch signals |
---|
[11a08b0] | 414 | signal(SIGINT, shutdown) |
---|
| 415 | signal(SIGTERM, shutdown) |
---|
[a97394b] | 416 | |
---|
[0ea11af] | 417 | # Start the servers |
---|
[a97394b] | 418 | for s in servers: |
---|
[0ea11af] | 419 | Thread(target=run_server, args=(s,)).start() |
---|
| 420 | |
---|
| 421 | # Main thread waits for signals |
---|
| 422 | while servers_active: |
---|
[d199ced] | 423 | sleep(1.0) |
---|
[0ea11af] | 424 | |
---|
| 425 | #Once shutdown starts wait for all the servers to terminate. |
---|
| 426 | while True: |
---|
| 427 | servers_lock.acquire() |
---|
| 428 | if len(servers) == 0: |
---|
| 429 | servers_lock.release() |
---|
| 430 | flog.info("All servers exited. Terminating") |
---|
| 431 | sys.exit(0) |
---|
| 432 | servers_lock.release() |
---|
| 433 | sleep(1) |
---|
[11a08b0] | 434 | |
---|