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