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