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 |
---|
11 | from M2Crypto.SSL.SSLServer import ThreadingSSLServer |
---|
12 | import xmlrpclib |
---|
13 | |
---|
14 | from fedid import fedid |
---|
15 | from fedd_services import ns0 |
---|
16 | from service_error import * |
---|
17 | |
---|
18 | import logging |
---|
19 | |
---|
20 | # The SSL server here is based on the implementation described at |
---|
21 | # http://www.xml.com/pub/a/ws/2004/01/20/salz.html |
---|
22 | |
---|
23 | # Turn off the matching of hostname to certificate ID |
---|
24 | SSL.Connection.clientPostConnectionCheck = None |
---|
25 | |
---|
26 | class server(ThreadingSSLServer): |
---|
27 | """ |
---|
28 | Interface the fedd services to the XMLRPC and SOAP interfaces |
---|
29 | """ |
---|
30 | def __init__(self, ME, handler, ssl_ctx, impl): |
---|
31 | """ |
---|
32 | Create an SSL server that handles the transport in handler using the |
---|
33 | credentials in ssl_ctx, and interfacing to the implementation of fedd |
---|
34 | services in fedd. ME is the host port pair on which to bind. |
---|
35 | """ |
---|
36 | ThreadingSSLServer.__init__(self, ME, handler, ssl_ctx) |
---|
37 | self.impl = impl |
---|
38 | self.soap_methods = impl.soap_services |
---|
39 | self.xmlrpc_methods = impl.xmlrpc_services |
---|
40 | self.log = logging.getLogger("fedd") |
---|
41 | |
---|
42 | def handle_error(self, request, address): |
---|
43 | """ |
---|
44 | The default SSLServer prints a stack trace here. This is a little |
---|
45 | friendlier. |
---|
46 | """ |
---|
47 | if request or address: |
---|
48 | self.log.warn("[fedd] Error on incoming connection: %s %s" % \ |
---|
49 | (request, address)) |
---|
50 | else: |
---|
51 | self.log.warn("[fedd] Error on incoming connection " + \ |
---|
52 | "(Likely SSL error)") |
---|
53 | |
---|
54 | class soap_handler(BaseHTTPRequestHandler): |
---|
55 | """ |
---|
56 | Standard connection between SOAP and the fedd services in impl. |
---|
57 | |
---|
58 | Much of this is boilerplate from |
---|
59 | http://www.xml.com/pub/a/ws/2004/01/20/salz.html |
---|
60 | """ |
---|
61 | server_version = "ZSI/2.0 fedd/0.1 " + BaseHTTPRequestHandler.server_version |
---|
62 | |
---|
63 | def send_xml(self, text, code=200): |
---|
64 | """Send an XML document as reply""" |
---|
65 | self.send_response(code) |
---|
66 | self.send_header('Content-type', 'text/xml; charset="utf-8"') |
---|
67 | self.send_header('Content-Length', str(len(text))) |
---|
68 | self.end_headers() |
---|
69 | self.wfile.write(text) |
---|
70 | self.wfile.flush() |
---|
71 | self.request.socket.close() |
---|
72 | |
---|
73 | def send_fault(self, f, code=500): |
---|
74 | """Send a SOAP encoded fault as reply""" |
---|
75 | self.send_xml(f.AsSOAP(processContents="lax"), code) |
---|
76 | |
---|
77 | def check_headers(self, ps): |
---|
78 | """Send a fault for any required envelope headers""" |
---|
79 | for (uri, localname) in ps.WhatMustIUnderstand(): |
---|
80 | self.send_fault(FaultFromNotUnderstood(uri, lname, 'fedd')) |
---|
81 | return False |
---|
82 | return True |
---|
83 | |
---|
84 | def check_method(self, ps): |
---|
85 | """Confirm that this class implements the namespace and SOAP method""" |
---|
86 | root = ps.body_root |
---|
87 | if root.namespaceURI not in self.server.impl.soap_namespaces: |
---|
88 | self.send_fault(Fault(Fault.Client, |
---|
89 | 'Unknown namespace "%s"' % root.namespaceURI)) |
---|
90 | return False |
---|
91 | |
---|
92 | if getattr(root, 'localName', None) == None: |
---|
93 | self.send_fault(Fault(Fault.Client, 'No method"')) |
---|
94 | return False |
---|
95 | return True |
---|
96 | |
---|
97 | def do_POST(self): |
---|
98 | """Treat an HTTP POST request as a SOAP service call""" |
---|
99 | try: |
---|
100 | cl = int(self.headers['content-length']) |
---|
101 | data = self.rfile.read(cl) |
---|
102 | ps = ParsedSoap(data) |
---|
103 | except ParseException, e: |
---|
104 | self.send_fault(Fault(Fault.Client, str(e))) |
---|
105 | return |
---|
106 | except Exception, e: |
---|
107 | self.send_fault(FaultFromException(e, 0, sys.exc_info()[2])) |
---|
108 | return |
---|
109 | if not self.check_headers(ps): return |
---|
110 | if not self.check_method(ps): return |
---|
111 | try: |
---|
112 | resp = self.soap_dispatch(ps.body_root.localName, ps, |
---|
113 | fedid(cert=self.request.get_peer_cert())) |
---|
114 | except Fault, f: |
---|
115 | self.send_fault(f) |
---|
116 | resp = None |
---|
117 | |
---|
118 | if resp != None: |
---|
119 | sw = SoapWriter() |
---|
120 | sw.serialize(resp) |
---|
121 | self.send_xml(str(sw)) |
---|
122 | |
---|
123 | def log_request(self, code=0, size=0): |
---|
124 | """ |
---|
125 | Log request to the fedd logger |
---|
126 | """ |
---|
127 | self.server.log.info("Successful SOAP request code %d" % code) |
---|
128 | |
---|
129 | def soap_dispatch(self, method, req, fid): |
---|
130 | """ |
---|
131 | The connection to the implementation, using the method maps |
---|
132 | |
---|
133 | The implementation provides a mapping from SOAP method name to the |
---|
134 | method in the implementation that provides the service. |
---|
135 | """ |
---|
136 | if self.server.soap_methods.has_key(method): |
---|
137 | try: |
---|
138 | return self.server.soap_methods[method](req, fid) |
---|
139 | except service_error, e: |
---|
140 | de = ns0.faultType_Def( |
---|
141 | (ns0.faultType_Def.schema, |
---|
142 | "FeddFaultBody")).pyclass() |
---|
143 | de._code=e.code |
---|
144 | de._errstr=e.code_string() |
---|
145 | de._desc=e.desc |
---|
146 | if e.is_server_error(): |
---|
147 | raise Fault(Fault.Server, e.code_string(), detail=de) |
---|
148 | else: |
---|
149 | raise Fault(Fault.Client, e.code_string(), detail=de) |
---|
150 | else: |
---|
151 | raise Fault(Fault.Client, "Unknown method: %s" % method) |
---|
152 | |
---|
153 | |
---|
154 | class xmlrpc_handler(BaseHTTPRequestHandler): |
---|
155 | """ |
---|
156 | Standard connection between XMLRPC and the fedd services in impl. |
---|
157 | |
---|
158 | Much of this is boilerplate from |
---|
159 | http://www.xml.com/pub/a/ws/2004/01/20/salz.html |
---|
160 | """ |
---|
161 | server_version = "ZSI/2.0 fedd/0.1 " + BaseHTTPRequestHandler.server_version |
---|
162 | |
---|
163 | def send_xml(self, text, code=200): |
---|
164 | """Send an XML document as reply""" |
---|
165 | self.send_response(code) |
---|
166 | self.send_header('Content-type', 'text/xml; charset="utf-8"') |
---|
167 | self.send_header('Content-Length', str(len(text))) |
---|
168 | self.end_headers() |
---|
169 | self.wfile.write(text) |
---|
170 | self.wfile.flush() |
---|
171 | # Make sure to close the socket when we're done |
---|
172 | self.request.socket.close() |
---|
173 | |
---|
174 | def do_POST(self): |
---|
175 | """Treat an HTTP POST request as an XMLRPC service call""" |
---|
176 | # NB: XMLRPC faults are not HTTP errors, so the code is always 200, |
---|
177 | # unless an HTTP error occurs, which we don't handle. |
---|
178 | |
---|
179 | resp = None |
---|
180 | data = None |
---|
181 | method = None |
---|
182 | cl = int(self.headers['content-length']) |
---|
183 | data = self.rfile.read(cl) |
---|
184 | |
---|
185 | try: |
---|
186 | params, method = xmlrpclib.loads(data) |
---|
187 | except xmlrpclib.ResponseError: |
---|
188 | data = xmlrpclib.dumps(xmlrpclib.Fault("Client", |
---|
189 | "Malformed request"), methodresponse=True) |
---|
190 | |
---|
191 | if method != None: |
---|
192 | try: |
---|
193 | resp = self.xmlrpc_dispatch(method, params, |
---|
194 | fedid(cert=self.request.get_peer_cert())) |
---|
195 | data = xmlrpclib.dumps((resp,), encoding='UTF-8', |
---|
196 | methodresponse=True) |
---|
197 | except xmlrpclib.Fault, f: |
---|
198 | data = xmlrpclib.dumps(f, methodresponse=True) |
---|
199 | resp = None |
---|
200 | |
---|
201 | self.send_xml(data) |
---|
202 | |
---|
203 | def log_request(self, code=0, size=0): |
---|
204 | """ |
---|
205 | Log request to the fedd logger |
---|
206 | """ |
---|
207 | self.server.log.info("Successful XMLRPC request code %d" % code) |
---|
208 | |
---|
209 | |
---|
210 | def xmlrpc_dispatch(self, method, req, fid): |
---|
211 | """ |
---|
212 | The connection to the implementation, using the method maps |
---|
213 | |
---|
214 | The implementation provides a mapping from XMLRPC method name to the |
---|
215 | method in the implementation that provides the service. |
---|
216 | """ |
---|
217 | if self.server.xmlrpc_methods.has_key(method): |
---|
218 | try: |
---|
219 | return self.server.xmlrpc_methods[method](req, fid) |
---|
220 | except service_error, e: |
---|
221 | raise xmlrpclib.Fault(e.code_string(), e.desc) |
---|
222 | else: |
---|
223 | raise xmlrpclib.Fault(100, "Unknown method: %s" % method) |
---|