1 | #!/usr/bin/python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # This program is free software; you can redistribute it and/or modify |
---|
4 | # it under the terms of the GNU Lesser General Public License as published by the |
---|
5 | # Free Software Foundation; either version 3, or (at your option) any later |
---|
6 | # version. |
---|
7 | # |
---|
8 | # This program is distributed in the hope that it will be useful, but |
---|
9 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
10 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
11 | # for more details. |
---|
12 | |
---|
13 | """Pythonic simple SOAP Client implementation""" |
---|
14 | |
---|
15 | from __future__ import unicode_literals |
---|
16 | import sys |
---|
17 | if sys.version > '3': |
---|
18 | unicode = str |
---|
19 | |
---|
20 | try: |
---|
21 | import cPickle as pickle |
---|
22 | except ImportError: |
---|
23 | import pickle |
---|
24 | import copy |
---|
25 | import hashlib |
---|
26 | import logging |
---|
27 | import os |
---|
28 | import tempfile |
---|
29 | import warnings |
---|
30 | |
---|
31 | from . import __author__, __copyright__, __license__, __version__, TIMEOUT |
---|
32 | from .simplexml import SimpleXMLElement, TYPE_MAP, REVERSE_TYPE_MAP, Struct |
---|
33 | from .transport import get_http_wrapper, set_http_wrapper, get_Http |
---|
34 | # Utility functions used throughout wsdl_parse, moved aside for readability |
---|
35 | from .helpers import Alias, fetch, sort_dict, make_key, process_element, \ |
---|
36 | postprocess_element, get_message, preprocess_schema, \ |
---|
37 | get_local_name, get_namespace_prefix, TYPE_MAP, urlsplit |
---|
38 | from .wsse import UsernameToken |
---|
39 | |
---|
40 | log = logging.getLogger(__name__) |
---|
41 | |
---|
42 | class SoapFault(RuntimeError): |
---|
43 | def __init__(self, faultcode, faultstring, detail=None): |
---|
44 | self.faultcode = faultcode |
---|
45 | self.faultstring = faultstring |
---|
46 | self.detail = detail |
---|
47 | RuntimeError.__init__(self, faultcode, faultstring, detail) |
---|
48 | |
---|
49 | def __unicode__(self): |
---|
50 | return '%s: %s' % (self.faultcode, self.faultstring) |
---|
51 | |
---|
52 | if sys.version > '3': |
---|
53 | __str__ = __unicode__ |
---|
54 | else: |
---|
55 | def __str__(self): |
---|
56 | return self.__unicode__().encode('ascii', 'ignore') |
---|
57 | |
---|
58 | def __repr__(self): |
---|
59 | return "SoapFault(faultcode = %s, faultstring %s, detail = %s)" % (repr(self.faultcode), |
---|
60 | repr(self.faultstring), |
---|
61 | repr(self.detail)) |
---|
62 | |
---|
63 | |
---|
64 | # soap protocol specification & namespace |
---|
65 | soap_namespaces = dict( |
---|
66 | soap11='http://schemas.xmlsoap.org/soap/envelope/', |
---|
67 | soap='http://schemas.xmlsoap.org/soap/envelope/', |
---|
68 | soapenv='http://schemas.xmlsoap.org/soap/envelope/', |
---|
69 | soap12='http://www.w3.org/2003/05/soap-env', |
---|
70 | soap12env="http://www.w3.org/2003/05/soap-envelope", |
---|
71 | ) |
---|
72 | |
---|
73 | |
---|
74 | class SoapClient(object): |
---|
75 | """Simple SOAP Client (simil PHP)""" |
---|
76 | def __init__(self, location=None, action=None, namespace=None, |
---|
77 | cert=None, exceptions=True, proxy=None, ns=None, |
---|
78 | soap_ns=None, wsdl=None, wsdl_basedir='', cache=False, cacert=None, |
---|
79 | sessions=False, soap_server=None, timeout=TIMEOUT, |
---|
80 | http_headers=None, trace=False, |
---|
81 | username=None, password=None, |
---|
82 | key_file=None, plugins=None, strict=True, |
---|
83 | ): |
---|
84 | """ |
---|
85 | :param http_headers: Additional HTTP Headers; example: {'Host': 'ipsec.example.com'} |
---|
86 | """ |
---|
87 | self.certssl = cert |
---|
88 | self.keyssl = key_file |
---|
89 | self.location = location # server location (url) |
---|
90 | self.action = action # SOAP base action |
---|
91 | self.namespace = namespace # message |
---|
92 | self.exceptions = exceptions # lanzar execpiones? (Soap Faults) |
---|
93 | self.xml_request = self.xml_response = '' |
---|
94 | self.http_headers = http_headers or {} |
---|
95 | self.plugins = plugins or [] |
---|
96 | self.strict = strict |
---|
97 | # extract the base directory / url for wsdl relative imports: |
---|
98 | if wsdl and wsdl_basedir == '': |
---|
99 | # parse the wsdl url, strip the scheme and filename |
---|
100 | url_scheme, netloc, path, query, fragment = urlsplit(wsdl) |
---|
101 | wsdl_basedir = os.path.dirname(netloc + path) |
---|
102 | |
---|
103 | self.wsdl_basedir = wsdl_basedir |
---|
104 | |
---|
105 | # shortcut to print all debugging info and sent / received xml messages |
---|
106 | if trace: |
---|
107 | if trace is True: |
---|
108 | level = logging.DEBUG # default logging level |
---|
109 | else: |
---|
110 | level = trace # use the provided level |
---|
111 | logging.basicConfig(level=level) |
---|
112 | log.setLevel(level) |
---|
113 | |
---|
114 | if not soap_ns and not ns: |
---|
115 | self.__soap_ns = 'soap' # 1.1 |
---|
116 | elif not soap_ns and ns: |
---|
117 | self.__soap_ns = 'soapenv' # 1.2 |
---|
118 | else: |
---|
119 | self.__soap_ns = soap_ns |
---|
120 | |
---|
121 | # SOAP Server (special cases like oracle, jbossas6 or jetty) |
---|
122 | self.__soap_server = soap_server |
---|
123 | |
---|
124 | # SOAP Header support |
---|
125 | self.__headers = {} # general headers |
---|
126 | self.__call_headers = None # Struct to be marshalled for RPC Call |
---|
127 | |
---|
128 | # check if the Certification Authority Cert is a string and store it |
---|
129 | if cacert and cacert.startswith('-----BEGIN CERTIFICATE-----'): |
---|
130 | fd, filename = tempfile.mkstemp() |
---|
131 | f = os.fdopen(fd, 'w+b', -1) |
---|
132 | log.debug("Saving CA certificate to %s" % filename) |
---|
133 | f.write(cacert) |
---|
134 | cacert = filename |
---|
135 | f.close() |
---|
136 | self.cacert = cacert |
---|
137 | |
---|
138 | # Create HTTP wrapper |
---|
139 | Http = get_Http() |
---|
140 | self.http = Http(timeout=timeout, cacert=cacert, proxy=proxy, sessions=sessions) |
---|
141 | if username and password: |
---|
142 | if hasattr(self.http, 'add_credentials'): |
---|
143 | self.http.add_credentials(username, password) |
---|
144 | if cert and key_file: |
---|
145 | if hasattr(self.http, 'add_certificate'): |
---|
146 | self.http.add_certificate(key=key_file, cert=cert, domain='') |
---|
147 | |
---|
148 | |
---|
149 | # namespace prefix, None to use xmlns attribute or False to not use it: |
---|
150 | self.__ns = ns |
---|
151 | if not ns: |
---|
152 | self.__xml = """<?xml version="1.0" encoding="UTF-8"?> |
---|
153 | <%(soap_ns)s:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
---|
154 | xmlns:xsd="http://www.w3.org/2001/XMLSchema" |
---|
155 | xmlns:%(soap_ns)s="%(soap_uri)s"> |
---|
156 | <%(soap_ns)s:Header/> |
---|
157 | <%(soap_ns)s:Body> |
---|
158 | <%(method)s xmlns="%(namespace)s"> |
---|
159 | </%(method)s> |
---|
160 | </%(soap_ns)s:Body> |
---|
161 | </%(soap_ns)s:Envelope>""" |
---|
162 | else: |
---|
163 | self.__xml = """<?xml version="1.0" encoding="UTF-8"?> |
---|
164 | <%(soap_ns)s:Envelope xmlns:%(soap_ns)s="%(soap_uri)s" xmlns:%(ns)s="%(namespace)s"> |
---|
165 | <%(soap_ns)s:Header/> |
---|
166 | <%(soap_ns)s:Body><%(ns)s:%(method)s></%(ns)s:%(method)s></%(soap_ns)s:Body></%(soap_ns)s:Envelope>""" |
---|
167 | |
---|
168 | # parse wsdl url |
---|
169 | self.services = wsdl and self.wsdl_parse(wsdl, cache=cache) |
---|
170 | self.service_port = None # service port for late binding |
---|
171 | |
---|
172 | def __getattr__(self, attr): |
---|
173 | """Return a pseudo-method that can be called""" |
---|
174 | if not self.services: # not using WSDL? |
---|
175 | return lambda *args, **kwargs: self.call(attr, *args, **kwargs) |
---|
176 | else: # using WSDL: |
---|
177 | return lambda *args, **kwargs: self.wsdl_call(attr, *args, **kwargs) |
---|
178 | |
---|
179 | def call(self, method, *args, **kwargs): |
---|
180 | """Prepare xml request and make SOAP call, returning a SimpleXMLElement. |
---|
181 | |
---|
182 | If a keyword argument called "headers" is passed with a value of a |
---|
183 | SimpleXMLElement object, then these headers will be inserted into the |
---|
184 | request. |
---|
185 | """ |
---|
186 | #TODO: method != input_message |
---|
187 | # Basic SOAP request: |
---|
188 | soap_uri = soap_namespaces[self.__soap_ns] |
---|
189 | xml = self.__xml % dict(method=method, # method tag name |
---|
190 | namespace=self.namespace, # method ns uri |
---|
191 | ns=self.__ns, # method ns prefix |
---|
192 | soap_ns=self.__soap_ns, # soap prefix & uri |
---|
193 | soap_uri=soap_uri) |
---|
194 | request = SimpleXMLElement(xml, namespace=self.__ns and self.namespace, |
---|
195 | prefix=self.__ns) |
---|
196 | |
---|
197 | request_headers = kwargs.pop('headers', None) |
---|
198 | |
---|
199 | # serialize parameters |
---|
200 | if kwargs: |
---|
201 | parameters = list(kwargs.items()) |
---|
202 | else: |
---|
203 | parameters = args |
---|
204 | if parameters and isinstance(parameters[0], SimpleXMLElement): |
---|
205 | body = request('Body', ns=list(soap_namespaces.values()),) |
---|
206 | # remove default body parameter (method name) |
---|
207 | delattr(body, method) |
---|
208 | # merge xmlelement parameter ("raw" - already marshalled) |
---|
209 | body.import_node(parameters[0]) |
---|
210 | elif parameters: |
---|
211 | # marshall parameters: |
---|
212 | use_ns = None if (self.__soap_server == "jetty" or self.qualified is False) else True |
---|
213 | for k, v in parameters: # dict: tag=valor |
---|
214 | if hasattr(v, "namespaces") and use_ns: |
---|
215 | ns = v.namespaces.get(None, True) |
---|
216 | else: |
---|
217 | ns = use_ns |
---|
218 | getattr(request, method).marshall(k, v, ns=ns) |
---|
219 | elif self.__soap_server in ('jbossas6',): |
---|
220 | # JBossAS-6 requires no empty method parameters! |
---|
221 | delattr(request("Body", ns=list(soap_namespaces.values()),), method) |
---|
222 | |
---|
223 | # construct header and parameters (if not wsdl given) except wsse |
---|
224 | if self.__headers and not self.services: |
---|
225 | self.__call_headers = dict([(k, v) for k, v in self.__headers.items() |
---|
226 | if not k.startswith('wsse:')]) |
---|
227 | # always extract WS Security header and send it (backward compatible) |
---|
228 | if 'wsse:Security' in self.__headers and not self.plugins: |
---|
229 | warnings.warn("Replace wsse:Security with UsernameToken plugin", |
---|
230 | DeprecationWarning) |
---|
231 | self.plugins.append(UsernameToken()) |
---|
232 | |
---|
233 | if self.__call_headers: |
---|
234 | header = request('Header', ns=list(soap_namespaces.values()),) |
---|
235 | for k, v in self.__call_headers.items(): |
---|
236 | ##if not self.__ns: |
---|
237 | ## header['xmlns'] |
---|
238 | if isinstance(v, SimpleXMLElement): |
---|
239 | # allows a SimpleXMLElement to be constructed and inserted |
---|
240 | # rather than a dictionary. marshall doesn't allow ns: prefixes |
---|
241 | # in dict key names |
---|
242 | header.import_node(v) |
---|
243 | else: |
---|
244 | header.marshall(k, v, ns=self.__ns, add_children_ns=False) |
---|
245 | if request_headers: |
---|
246 | header = request('Header', ns=list(soap_namespaces.values()),) |
---|
247 | for subheader in request_headers.children(): |
---|
248 | header.import_node(subheader) |
---|
249 | |
---|
250 | # do pre-processing using plugins (i.e. WSSE signing) |
---|
251 | for plugin in self.plugins: |
---|
252 | plugin.preprocess(self, request, method, args, kwargs, |
---|
253 | self.__headers, soap_uri) |
---|
254 | |
---|
255 | self.xml_request = request.as_xml() |
---|
256 | self.xml_response = self.send(method, self.xml_request) |
---|
257 | response = SimpleXMLElement(self.xml_response, namespace=self.namespace, |
---|
258 | jetty=self.__soap_server in ('jetty',)) |
---|
259 | if self.exceptions and response("Fault", ns=list(soap_namespaces.values()), error=False): |
---|
260 | detailXml = response("detail", ns=list(soap_namespaces.values()), error=False) |
---|
261 | detail = None |
---|
262 | |
---|
263 | if detailXml and detailXml.children(): |
---|
264 | if self.services is not None: |
---|
265 | operation = self.get_operation(method) |
---|
266 | fault_name = detailXml.children()[0].get_name() |
---|
267 | # if fault not defined in WSDL, it could be an axis or other |
---|
268 | # standard type (i.e. "hostname"), try to convert it to string |
---|
269 | fault = operation['faults'].get(fault_name) or unicode |
---|
270 | detail = detailXml.children()[0].unmarshall(fault, strict=False) |
---|
271 | else: |
---|
272 | detail = repr(detailXml.children()) |
---|
273 | |
---|
274 | raise SoapFault(unicode(response.faultcode), |
---|
275 | unicode(response.faultstring), |
---|
276 | detail) |
---|
277 | |
---|
278 | # do post-processing using plugins (i.e. WSSE signature verification) |
---|
279 | for plugin in self.plugins: |
---|
280 | plugin.postprocess(self, response, method, args, kwargs, |
---|
281 | self.__headers, soap_uri) |
---|
282 | |
---|
283 | return response |
---|
284 | |
---|
285 | def send(self, method, xml): |
---|
286 | """Send SOAP request using HTTP""" |
---|
287 | if self.location == 'test': return |
---|
288 | # location = '%s' % self.location #?op=%s" % (self.location, method) |
---|
289 | http_method = str('POST') |
---|
290 | location = str(self.location) |
---|
291 | |
---|
292 | if self.services: |
---|
293 | soap_action = str(self.action) |
---|
294 | else: |
---|
295 | soap_action = str(self.action) + method |
---|
296 | |
---|
297 | headers = { |
---|
298 | 'Content-type': 'text/xml; charset="UTF-8"', |
---|
299 | 'Content-length': str(len(xml)), |
---|
300 | } |
---|
301 | |
---|
302 | if self.action is not None: |
---|
303 | headers['SOAPAction'] = '"' + soap_action + '"' |
---|
304 | |
---|
305 | headers.update(self.http_headers) |
---|
306 | log.info("POST %s" % location) |
---|
307 | log.debug('\n'.join(["%s: %s" % (k, v) for k, v in headers.items()])) |
---|
308 | log.debug(xml) |
---|
309 | |
---|
310 | if sys.version < '3': |
---|
311 | # Ensure http_method, location and all headers are binary to prevent |
---|
312 | # UnicodeError inside httplib.HTTPConnection._send_output. |
---|
313 | |
---|
314 | # httplib in python3 do the same inside itself, don't need to convert it here |
---|
315 | headers = dict((str(k), str(v)) for k, v in headers.items()) |
---|
316 | |
---|
317 | response, content = self.http.request( |
---|
318 | location, http_method, body=xml, headers=headers) |
---|
319 | self.response = response |
---|
320 | self.content = content |
---|
321 | |
---|
322 | log.debug('\n'.join(["%s: %s" % (k, v) for k, v in response.items()])) |
---|
323 | log.debug(content) |
---|
324 | return content |
---|
325 | |
---|
326 | def get_operation(self, method): |
---|
327 | # try to find operation in wsdl file |
---|
328 | soap_ver = self.__soap_ns.startswith('soap12') and 'soap12' or 'soap11' |
---|
329 | if not self.service_port: |
---|
330 | for service_name, service in self.services.items(): |
---|
331 | for port_name, port in [port for port in service['ports'].items()]: |
---|
332 | if port['soap_ver'] == soap_ver: |
---|
333 | self.service_port = service_name, port_name |
---|
334 | break |
---|
335 | else: |
---|
336 | raise RuntimeError('Cannot determine service in WSDL: ' |
---|
337 | 'SOAP version: %s' % soap_ver) |
---|
338 | else: |
---|
339 | port = self.services[self.service_port[0]]['ports'][self.service_port[1]] |
---|
340 | if not self.location: |
---|
341 | self.location = port['location'] |
---|
342 | operation = port['operations'].get(method) |
---|
343 | if not operation: |
---|
344 | raise RuntimeError('Operation %s not found in WSDL: ' |
---|
345 | 'Service/Port Type: %s' % |
---|
346 | (method, self.service_port)) |
---|
347 | return operation |
---|
348 | |
---|
349 | def wsdl_call(self, method, *args, **kwargs): |
---|
350 | """Pre and post process SOAP call, input and output parameters using WSDL""" |
---|
351 | return self.wsdl_call_with_args(method, args, kwargs) |
---|
352 | |
---|
353 | def wsdl_call_with_args(self, method, args, kwargs): |
---|
354 | """Pre and post process SOAP call, input and output parameters using WSDL""" |
---|
355 | soap_uri = soap_namespaces[self.__soap_ns] |
---|
356 | operation = self.get_operation(method) |
---|
357 | |
---|
358 | # get i/o type declarations: |
---|
359 | input = operation['input'] |
---|
360 | output = operation['output'] |
---|
361 | header = operation.get('header') |
---|
362 | if 'action' in operation: |
---|
363 | self.action = operation['action'] |
---|
364 | |
---|
365 | if 'namespace' in operation: |
---|
366 | self.namespace = operation['namespace'] or '' |
---|
367 | self.qualified = operation['qualified'] |
---|
368 | |
---|
369 | # construct header and parameters |
---|
370 | if header: |
---|
371 | self.__call_headers = sort_dict(header, self.__headers) |
---|
372 | method, params = self.wsdl_call_get_params(method, input, args, kwargs) |
---|
373 | |
---|
374 | # call remote procedure |
---|
375 | response = self.call(method, *params) |
---|
376 | # parse results: |
---|
377 | resp = response('Body', ns=soap_uri).children().unmarshall(output, strict=self.strict) |
---|
378 | return resp and list(resp.values())[0] # pass Response tag children |
---|
379 | |
---|
380 | def wsdl_call_get_params(self, method, input, args, kwargs): |
---|
381 | """Build params from input and args/kwargs""" |
---|
382 | params = inputname = inputargs = None |
---|
383 | all_args = {} |
---|
384 | if input: |
---|
385 | inputname = list(input.keys())[0] |
---|
386 | inputargs = input[inputname] |
---|
387 | |
---|
388 | if input and args: |
---|
389 | # convert positional parameters to named parameters: |
---|
390 | d = {} |
---|
391 | for idx, arg in enumerate(args): |
---|
392 | key = list(inputargs.keys())[idx] |
---|
393 | if isinstance(arg, dict): |
---|
394 | if key not in arg: |
---|
395 | raise KeyError('Unhandled key %s. use client.help(method)' % key) |
---|
396 | d[key] = arg[key] |
---|
397 | else: |
---|
398 | d[key] = arg |
---|
399 | all_args.update({inputname: d}) |
---|
400 | |
---|
401 | if input and (kwargs or all_args): |
---|
402 | if kwargs: |
---|
403 | all_args.update({inputname: kwargs}) |
---|
404 | valid, errors, warnings = self.wsdl_validate_params(input, all_args) |
---|
405 | if not valid: |
---|
406 | raise ValueError('Invalid Args Structure. Errors: %s' % errors) |
---|
407 | # sort and filter parameters according to wsdl input structure |
---|
408 | tree = sort_dict(input, all_args) |
---|
409 | root = list(tree.values())[0] |
---|
410 | params = [] |
---|
411 | # make a params tuple list suitable for self.call(method, *params) |
---|
412 | for k, v in root.items(): |
---|
413 | # fix referenced namespaces as info is lost when calling call |
---|
414 | root_ns = root.namespaces[k] |
---|
415 | if not root.references[k] and isinstance(v, Struct): |
---|
416 | v.namespaces[None] = root_ns |
---|
417 | params.append((k, v)) |
---|
418 | # TODO: check style and document attributes |
---|
419 | if self.__soap_server in ('axis', ): |
---|
420 | # use the operation name |
---|
421 | method = method |
---|
422 | else: |
---|
423 | # use the message (element) name |
---|
424 | method = inputname |
---|
425 | #elif not input: |
---|
426 | #TODO: no message! (see wsmtxca.dummy) |
---|
427 | else: |
---|
428 | params = kwargs and kwargs.items() |
---|
429 | |
---|
430 | return (method, params) |
---|
431 | |
---|
432 | def wsdl_validate_params(self, struct, value): |
---|
433 | """Validate the arguments (actual values) for the parameters structure. |
---|
434 | Fail for any invalid arguments or type mismatches.""" |
---|
435 | errors = [] |
---|
436 | warnings = [] |
---|
437 | valid = True |
---|
438 | |
---|
439 | # Determine parameter type |
---|
440 | if type(struct) == type(value): |
---|
441 | typematch = True |
---|
442 | if not isinstance(struct, dict) and isinstance(value, dict): |
---|
443 | typematch = True # struct can be a dict or derived (Struct) |
---|
444 | else: |
---|
445 | typematch = False |
---|
446 | |
---|
447 | if struct == str: |
---|
448 | struct = unicode # fix for py2 vs py3 string handling |
---|
449 | |
---|
450 | if not isinstance(struct, (list, dict, tuple)) and struct in TYPE_MAP.keys(): |
---|
451 | if not type(value) == struct and value is not None: |
---|
452 | try: |
---|
453 | struct(value) # attempt to cast input to parameter type |
---|
454 | except: |
---|
455 | valid = False |
---|
456 | errors.append('Type mismatch for argument value. parameter(%s): %s, value(%s): %s' % (type(struct), struct, type(value), value)) |
---|
457 | |
---|
458 | elif isinstance(struct, list) and len(struct) == 1 and not isinstance(value, list): |
---|
459 | # parameter can have a dict in a list: [{}] indicating a list is allowed, but not needed if only one argument. |
---|
460 | next_valid, next_errors, next_warnings = self.wsdl_validate_params(struct[0], value) |
---|
461 | if not next_valid: |
---|
462 | valid = False |
---|
463 | errors.extend(next_errors) |
---|
464 | warnings.extend(next_warnings) |
---|
465 | |
---|
466 | # traverse tree |
---|
467 | elif isinstance(struct, dict): |
---|
468 | if struct and value: |
---|
469 | for key in value: |
---|
470 | if key not in struct: |
---|
471 | valid = False |
---|
472 | errors.append('Argument key %s not in parameter. parameter: %s, args: %s' % (key, struct, value)) |
---|
473 | else: |
---|
474 | next_valid, next_errors, next_warnings = self.wsdl_validate_params(struct[key], value[key]) |
---|
475 | if not next_valid: |
---|
476 | valid = False |
---|
477 | errors.extend(next_errors) |
---|
478 | warnings.extend(next_warnings) |
---|
479 | for key in struct: |
---|
480 | if key not in value: |
---|
481 | warnings.append('Parameter key %s not in args. parameter: %s, value: %s' % (key, struct, value)) |
---|
482 | elif struct and not value: |
---|
483 | warnings.append('parameter keys not in args. parameter: %s, args: %s' % (struct, value)) |
---|
484 | elif not struct and value: |
---|
485 | valid = False |
---|
486 | errors.append('Args keys not in parameter. parameter: %s, args: %s' % (struct, value)) |
---|
487 | else: |
---|
488 | pass |
---|
489 | elif isinstance(struct, list): |
---|
490 | struct_list_value = struct[0] |
---|
491 | for item in value: |
---|
492 | next_valid, next_errors, next_warnings = self.wsdl_validate_params(struct_list_value, item) |
---|
493 | if not next_valid: |
---|
494 | valid = False |
---|
495 | errors.extend(next_errors) |
---|
496 | warnings.extend(next_warnings) |
---|
497 | elif not typematch: |
---|
498 | valid = False |
---|
499 | errors.append('Type mismatch. parameter(%s): %s, value(%s): %s' % (type(struct), struct, type(value), value)) |
---|
500 | |
---|
501 | return (valid, errors, warnings) |
---|
502 | |
---|
503 | def help(self, method): |
---|
504 | """Return operation documentation and invocation/returned value example""" |
---|
505 | operation = self.get_operation(method) |
---|
506 | input = operation.get('input') |
---|
507 | input = input and input.values() and list(input.values())[0] |
---|
508 | if isinstance(input, dict): |
---|
509 | input = ", ".join("%s=%s" % (k, repr(v)) for k, v in input.items()) |
---|
510 | elif isinstance(input, list): |
---|
511 | input = repr(input) |
---|
512 | output = operation.get('output') |
---|
513 | if output: |
---|
514 | output = list(operation['output'].values())[0] |
---|
515 | headers = operation.get('headers') or None |
---|
516 | return "%s(%s)\n -> %s:\n\n%s\nHeaders: %s" % ( |
---|
517 | method, |
---|
518 | input or '', |
---|
519 | output and output or '', |
---|
520 | operation.get('documentation', ''), |
---|
521 | headers, |
---|
522 | ) |
---|
523 | |
---|
524 | soap_ns_uris = { |
---|
525 | 'http://schemas.xmlsoap.org/wsdl/soap/': 'soap11', |
---|
526 | 'http://schemas.xmlsoap.org/wsdl/soap12/': 'soap12', |
---|
527 | } |
---|
528 | wsdl_uri = 'http://schemas.xmlsoap.org/wsdl/' |
---|
529 | xsd_uri = 'http://www.w3.org/2001/XMLSchema' |
---|
530 | xsi_uri = 'http://www.w3.org/2001/XMLSchema-instance' |
---|
531 | |
---|
532 | def _url_to_xml_tree(self, url, cache, force_download): |
---|
533 | """Unmarshall the WSDL at the given url into a tree of SimpleXMLElement nodes""" |
---|
534 | # Open uri and read xml: |
---|
535 | xml = fetch(url, self.http, cache, force_download, self.wsdl_basedir, self.http_headers) |
---|
536 | # Parse WSDL XML: |
---|
537 | wsdl = SimpleXMLElement(xml, namespace=self.wsdl_uri) |
---|
538 | |
---|
539 | # Extract useful data: |
---|
540 | self.namespace = "" |
---|
541 | self.documentation = unicode(wsdl('documentation', error=False)) or '' |
---|
542 | |
---|
543 | # some wsdl are split down in several files, join them: |
---|
544 | imported_wsdls = {} |
---|
545 | for element in wsdl.children() or []: |
---|
546 | if element.get_local_name() in ('import'): |
---|
547 | wsdl_namespace = element['namespace'] |
---|
548 | wsdl_location = element['location'] |
---|
549 | if wsdl_location is None: |
---|
550 | log.warning('WSDL location not provided for %s!' % wsdl_namespace) |
---|
551 | continue |
---|
552 | if wsdl_location in imported_wsdls: |
---|
553 | log.warning('WSDL %s already imported!' % wsdl_location) |
---|
554 | continue |
---|
555 | imported_wsdls[wsdl_location] = wsdl_namespace |
---|
556 | log.debug('Importing wsdl %s from %s' % (wsdl_namespace, wsdl_location)) |
---|
557 | # Open uri and read xml: |
---|
558 | xml = fetch(wsdl_location, self.http, cache, force_download, self.wsdl_basedir, self.http_headers) |
---|
559 | # Parse imported XML schema (recursively): |
---|
560 | imported_wsdl = SimpleXMLElement(xml, namespace=self.xsd_uri) |
---|
561 | # merge the imported wsdl into the main document: |
---|
562 | wsdl.import_node(imported_wsdl) |
---|
563 | # warning: do not process schemas to avoid infinite recursion! |
---|
564 | |
---|
565 | return wsdl |
---|
566 | |
---|
567 | def _xml_tree_to_services(self, wsdl, cache, force_download): |
---|
568 | """Convert SimpleXMLElement tree representation of the WSDL into pythonic objects""" |
---|
569 | # detect soap prefix and uri (xmlns attributes of <definitions>) |
---|
570 | xsd_ns = None |
---|
571 | soap_uris = {} |
---|
572 | for k, v in wsdl[:]: |
---|
573 | if v in self.soap_ns_uris and k.startswith('xmlns:'): |
---|
574 | soap_uris[get_local_name(k)] = v |
---|
575 | if v == self.xsd_uri and k.startswith('xmlns:'): |
---|
576 | xsd_ns = get_local_name(k) |
---|
577 | |
---|
578 | elements = {} # element: type def |
---|
579 | messages = {} # message: element |
---|
580 | port_types = {} # port_type_name: port_type |
---|
581 | bindings = {} # binding_name: binding |
---|
582 | services = {} # service_name: service |
---|
583 | |
---|
584 | # check axis2 namespace at schema types attributes (europa.eu checkVat) |
---|
585 | if "http://xml.apache.org/xml-soap" in dict(wsdl[:]).values(): |
---|
586 | # get the sub-namespace in the first schema element (see issue 8) |
---|
587 | if wsdl('types', error=False): |
---|
588 | schema = wsdl.types('schema', ns=self.xsd_uri) |
---|
589 | attrs = dict(schema[:]) |
---|
590 | self.namespace = attrs.get('targetNamespace', self.namespace) |
---|
591 | if not self.namespace or self.namespace == "urn:DefaultNamespace": |
---|
592 | self.namespace = wsdl['targetNamespace'] or self.namespace |
---|
593 | |
---|
594 | imported_schemas = {} |
---|
595 | global_namespaces = {None: self.namespace} |
---|
596 | |
---|
597 | # process current wsdl schema (if any, or many if imported): |
---|
598 | # <wsdl:definitions> |
---|
599 | # <wsdl:types> |
---|
600 | # <xs:schema> |
---|
601 | # <xs:element> |
---|
602 | # <xs:complexType>...</xs:complexType> |
---|
603 | # or |
---|
604 | # <xs:.../> |
---|
605 | # </xs:element> |
---|
606 | # </xs:schema> |
---|
607 | # </wsdl:types> |
---|
608 | # </wsdl:definitions> |
---|
609 | |
---|
610 | for types in wsdl('types', error=False) or []: |
---|
611 | # avoid issue if schema is not given in the main WSDL file |
---|
612 | schemas = types('schema', ns=self.xsd_uri, error=False) |
---|
613 | for schema in schemas or []: |
---|
614 | preprocess_schema(schema, imported_schemas, elements, self.xsd_uri, |
---|
615 | self.__soap_server, self.http, cache, |
---|
616 | force_download, self.wsdl_basedir, |
---|
617 | global_namespaces=global_namespaces) |
---|
618 | |
---|
619 | # 2nd phase: alias, postdefined elements, extend bases, convert lists |
---|
620 | postprocess_element(elements, []) |
---|
621 | |
---|
622 | for message in wsdl.message: |
---|
623 | for part in message('part', error=False) or []: |
---|
624 | element = {} |
---|
625 | element_name = part['element'] |
---|
626 | if not element_name: |
---|
627 | # some implementations (axis) uses type instead |
---|
628 | element_name = part['type'] |
---|
629 | type_ns = get_namespace_prefix(element_name) |
---|
630 | type_uri = part.get_namespace_uri(type_ns) |
---|
631 | part_name = part['name'] or None |
---|
632 | if type_uri == self.xsd_uri: |
---|
633 | element_name = get_local_name(element_name) |
---|
634 | fn = REVERSE_TYPE_MAP.get(element_name, None) |
---|
635 | element = {part_name: fn} |
---|
636 | # emulate a true Element (complexType) for rpc style |
---|
637 | if (message['name'], part_name) not in messages: |
---|
638 | od = Struct() |
---|
639 | od.namespaces[None] = type_uri |
---|
640 | messages[(message['name'], part_name)] = {message['name']: od} |
---|
641 | else: |
---|
642 | od = messages[(message['name'], part_name)].values()[0] |
---|
643 | od.namespaces[part_name] = type_uri |
---|
644 | od.references[part_name] = False |
---|
645 | od.update(element) |
---|
646 | else: |
---|
647 | element_name = get_local_name(element_name) |
---|
648 | fn = elements.get(make_key(element_name, 'element', type_uri)) |
---|
649 | if not fn: |
---|
650 | # some axis servers uses complexType for part messages (rpc) |
---|
651 | fn = elements.get(make_key(element_name, 'complexType', type_uri)) |
---|
652 | od = Struct() |
---|
653 | od[part_name] = fn |
---|
654 | od.namespaces[None] = type_uri |
---|
655 | od.namespaces[part_name] = type_uri |
---|
656 | od.references[part_name] = False |
---|
657 | element = {message['name']: od} |
---|
658 | else: |
---|
659 | element = {element_name: fn} |
---|
660 | messages[(message['name'], part_name)] = element |
---|
661 | |
---|
662 | for port_type_node in wsdl.portType: |
---|
663 | port_type_name = port_type_node['name'] |
---|
664 | port_type = port_types[port_type_name] = {} |
---|
665 | operations = port_type['operations'] = {} |
---|
666 | |
---|
667 | for operation_node in port_type_node.operation: |
---|
668 | op_name = operation_node['name'] |
---|
669 | op = operations[op_name] = {} |
---|
670 | op['style'] = operation_node['style'] |
---|
671 | op['parameter_order'] = (operation_node['parameterOrder'] or "").split(" ") |
---|
672 | op['documentation'] = unicode(operation_node('documentation', error=False)) or '' |
---|
673 | |
---|
674 | if operation_node('input', error=False): |
---|
675 | op['input_msg'] = get_local_name(operation_node.input['message']) |
---|
676 | ns = get_namespace_prefix(operation_node.input['message']) |
---|
677 | op['namespace'] = operation_node.get_namespace_uri(ns) |
---|
678 | |
---|
679 | if operation_node('output', error=False): |
---|
680 | op['output_msg'] = get_local_name(operation_node.output['message']) |
---|
681 | |
---|
682 | #Get all fault message types this operation may return |
---|
683 | fault_msgs = op['fault_msgs'] = {} |
---|
684 | faults = operation_node('fault', error=False) |
---|
685 | if faults is not None: |
---|
686 | for fault in operation_node('fault', error=False): |
---|
687 | fault_msgs[fault['name']] = get_local_name(fault['message']) |
---|
688 | |
---|
689 | for binding_node in wsdl.binding: |
---|
690 | port_type_name = get_local_name(binding_node['type']) |
---|
691 | if port_type_name not in port_types: |
---|
692 | # Invalid port type |
---|
693 | continue |
---|
694 | port_type = port_types[port_type_name] |
---|
695 | binding_name = binding_node['name'] |
---|
696 | soap_binding = binding_node('binding', ns=list(soap_uris.values()), error=False) |
---|
697 | transport = soap_binding and soap_binding['transport'] or None |
---|
698 | style = soap_binding and soap_binding['style'] or None # rpc |
---|
699 | |
---|
700 | binding = bindings[binding_name] = { |
---|
701 | 'name': binding_name, |
---|
702 | 'operations': copy.deepcopy(port_type['operations']), |
---|
703 | 'port_type_name': port_type_name, |
---|
704 | 'transport': transport, |
---|
705 | 'style': style, |
---|
706 | } |
---|
707 | |
---|
708 | for operation_node in binding_node.operation: |
---|
709 | op_name = operation_node['name'] |
---|
710 | op_op = operation_node('operation', ns=list(soap_uris.values()), error=False) |
---|
711 | action = op_op and op_op['soapAction'] |
---|
712 | |
---|
713 | op = binding['operations'].setdefault(op_name, {}) |
---|
714 | op['name'] = op_name |
---|
715 | op['style'] = op.get('style', style) |
---|
716 | if action is not None: |
---|
717 | op['action'] = action |
---|
718 | |
---|
719 | # input and/or output can be not present! |
---|
720 | input = operation_node('input', error=False) |
---|
721 | body = input and input('body', ns=list(soap_uris.values()), error=False) |
---|
722 | parts_input_body = body and body['parts'] or None |
---|
723 | |
---|
724 | # parse optional header messages (some implementations use more than one!) |
---|
725 | parts_input_headers = [] |
---|
726 | headers = input and input('header', ns=list(soap_uris.values()), error=False) |
---|
727 | for header in headers or []: |
---|
728 | hdr = {'message': header['message'], 'part': header['part']} |
---|
729 | parts_input_headers.append(hdr) |
---|
730 | |
---|
731 | if 'input_msg' in op: |
---|
732 | headers = {} # base header message structure |
---|
733 | for input_header in parts_input_headers: |
---|
734 | header_msg = get_local_name(input_header.get('message')) |
---|
735 | header_part = get_local_name(input_header.get('part')) |
---|
736 | # warning: some implementations use a separate message! |
---|
737 | hdr = get_message(messages, header_msg or op['input_msg'], header_part) |
---|
738 | if hdr: |
---|
739 | headers.update(hdr) |
---|
740 | else: |
---|
741 | pass # not enough info to search the header message: |
---|
742 | op['input'] = get_message(messages, op['input_msg'], parts_input_body, op['parameter_order']) |
---|
743 | op['header'] = headers |
---|
744 | |
---|
745 | try: |
---|
746 | element = list(op['input'].values())[0] |
---|
747 | ns_uri = element.namespaces[None] |
---|
748 | qualified = element.qualified |
---|
749 | except (AttributeError, KeyError) as e: |
---|
750 | # TODO: fix if no parameters parsed or "variants" |
---|
751 | ns_uri = op['namespace'] |
---|
752 | qualified = None |
---|
753 | if ns_uri: |
---|
754 | op['namespace'] = ns_uri |
---|
755 | op['qualified'] = qualified |
---|
756 | |
---|
757 | # Remove temporary property |
---|
758 | del op['input_msg'] |
---|
759 | |
---|
760 | else: |
---|
761 | op['input'] = None |
---|
762 | op['header'] = None |
---|
763 | |
---|
764 | output = operation_node('output', error=False) |
---|
765 | body = output and output('body', ns=list(soap_uris.values()), error=False) |
---|
766 | parts_output_body = body and body['parts'] or None |
---|
767 | if 'output_msg' in op: |
---|
768 | op['output'] = get_message(messages, op['output_msg'], parts_output_body) |
---|
769 | # Remove temporary property |
---|
770 | del op['output_msg'] |
---|
771 | else: |
---|
772 | op['output'] = None |
---|
773 | |
---|
774 | if 'fault_msgs' in op: |
---|
775 | faults = op['faults'] = {} |
---|
776 | for msg in op['fault_msgs'].values(): |
---|
777 | msg_obj = get_message(messages, msg, parts_output_body) |
---|
778 | tag_name = list(msg_obj)[0] |
---|
779 | faults[tag_name] = msg_obj |
---|
780 | |
---|
781 | # useless? never used |
---|
782 | parts_output_headers = [] |
---|
783 | headers = output and output('header', ns=list(soap_uris.values()), error=False) |
---|
784 | for header in headers or []: |
---|
785 | hdr = {'message': header['message'], 'part': header['part']} |
---|
786 | parts_output_headers.append(hdr) |
---|
787 | |
---|
788 | |
---|
789 | |
---|
790 | |
---|
791 | for service in wsdl("service", error=False) or []: |
---|
792 | service_name = service['name'] |
---|
793 | if not service_name: |
---|
794 | continue # empty service? |
---|
795 | |
---|
796 | serv = services.setdefault(service_name, {}) |
---|
797 | ports = serv['ports'] = {} |
---|
798 | serv['documentation'] = service['documentation'] or '' |
---|
799 | for port in service.port: |
---|
800 | binding_name = get_local_name(port['binding']) |
---|
801 | |
---|
802 | if not binding_name in bindings: |
---|
803 | continue # unknown binding |
---|
804 | |
---|
805 | binding = ports[port['name']] = copy.deepcopy(bindings[binding_name]) |
---|
806 | address = port('address', ns=list(soap_uris.values()), error=False) |
---|
807 | location = address and address['location'] or None |
---|
808 | soap_uri = address and soap_uris.get(address.get_prefix()) |
---|
809 | soap_ver = soap_uri and self.soap_ns_uris.get(soap_uri) |
---|
810 | |
---|
811 | binding.update({ |
---|
812 | 'location': location, |
---|
813 | 'service_name': service_name, |
---|
814 | 'soap_uri': soap_uri, |
---|
815 | 'soap_ver': soap_ver, |
---|
816 | }) |
---|
817 | |
---|
818 | # create an default service if none is given in the wsdl: |
---|
819 | if not services: |
---|
820 | services[''] = {'ports': {'': None}} |
---|
821 | |
---|
822 | elements = list(e for e in elements.values() if type(e) is type) + sorted(e for e in elements.values() if not(type(e) is type)) |
---|
823 | e = None |
---|
824 | self.elements = [] |
---|
825 | for element in elements: |
---|
826 | if e!= element: self.elements.append(element) |
---|
827 | e = element |
---|
828 | |
---|
829 | return services |
---|
830 | |
---|
831 | def wsdl_parse(self, url, cache=False): |
---|
832 | """Parse Web Service Description v1.1""" |
---|
833 | |
---|
834 | log.debug('Parsing wsdl url: %s' % url) |
---|
835 | # Try to load a previously parsed wsdl: |
---|
836 | force_download = False |
---|
837 | if cache: |
---|
838 | # make md5 hash of the url for caching... |
---|
839 | filename_pkl = '%s.pkl' % hashlib.md5(url).hexdigest() |
---|
840 | if isinstance(cache, basestring): |
---|
841 | filename_pkl = os.path.join(cache, filename_pkl) |
---|
842 | if os.path.exists(filename_pkl): |
---|
843 | log.debug('Unpickle file %s' % (filename_pkl, )) |
---|
844 | f = open(filename_pkl, 'r') |
---|
845 | pkl = pickle.load(f) |
---|
846 | f.close() |
---|
847 | # sanity check: |
---|
848 | if pkl['version'][:-1] != __version__.split(' ')[0][:-1] or pkl['url'] != url: |
---|
849 | warnings.warn('version or url mismatch! discarding cached wsdl', RuntimeWarning) |
---|
850 | log.debug('Version: %s %s' % (pkl['version'], __version__)) |
---|
851 | log.debug('URL: %s %s' % (pkl['url'], url)) |
---|
852 | force_download = True |
---|
853 | else: |
---|
854 | self.namespace = pkl['namespace'] |
---|
855 | self.documentation = pkl['documentation'] |
---|
856 | return pkl['services'] |
---|
857 | |
---|
858 | # always return an unicode object: |
---|
859 | REVERSE_TYPE_MAP['string'] = str |
---|
860 | |
---|
861 | wsdl = self._url_to_xml_tree(url, cache, force_download) |
---|
862 | services = self._xml_tree_to_services(wsdl, cache, force_download) |
---|
863 | |
---|
864 | # dump the full service/port/operation map |
---|
865 | #log.debug(pprint.pformat(services)) |
---|
866 | |
---|
867 | # Save parsed wsdl (cache) |
---|
868 | if cache: |
---|
869 | f = open(filename_pkl, "wb") |
---|
870 | pkl = { |
---|
871 | 'version': __version__.split(' ')[0], |
---|
872 | 'url': url, |
---|
873 | 'namespace': self.namespace, |
---|
874 | 'documentation': self.documentation, |
---|
875 | 'services': services, |
---|
876 | } |
---|
877 | pickle.dump(pkl, f) |
---|
878 | f.close() |
---|
879 | |
---|
880 | return services |
---|
881 | |
---|
882 | def __setitem__(self, item, value): |
---|
883 | """Set SOAP Header value - this header will be sent for every request.""" |
---|
884 | self.__headers[item] = value |
---|
885 | |
---|
886 | def close(self): |
---|
887 | """Finish the connection and remove temp files""" |
---|
888 | self.http.close() |
---|
889 | if self.cacert.startswith(tempfile.gettempdir()): |
---|
890 | log.debug('removing %s' % self.cacert) |
---|
891 | os.unlink(self.cacert) |
---|
892 | |
---|
893 | def __repr__(self): |
---|
894 | s = 'SOAP CLIENT' |
---|
895 | s += '\n ELEMENTS' |
---|
896 | for e in self.elements: |
---|
897 | if isinstance(e, type): |
---|
898 | e = e.__name__ |
---|
899 | elif isinstance(e, Alias): |
---|
900 | e = e.xml_type |
---|
901 | elif isinstance(e, Struct) and e.key[1]=='element': |
---|
902 | e = repr(e) |
---|
903 | else: |
---|
904 | continue |
---|
905 | s += '\n %s' % e |
---|
906 | for service in self.services: |
---|
907 | s += '\n SERVICE (%s)' % service |
---|
908 | ports = self.services[service]['ports'] |
---|
909 | for port in ports: |
---|
910 | port = ports[port] |
---|
911 | if port['soap_ver'] == None: continue |
---|
912 | s += '\n PORT (%s)' % port['name'] |
---|
913 | s += '\n Location: %s' % port['location'] |
---|
914 | s += '\n Soap ver: %s' % port['soap_ver'] |
---|
915 | s += '\n Soap URI: %s' % port['soap_uri'] |
---|
916 | s += '\n OPERATIONS' |
---|
917 | operations = port['operations'] |
---|
918 | for operation in sorted(operations): |
---|
919 | operation = self.get_operation(operation) |
---|
920 | input = operation.get('input') |
---|
921 | input = input and input.values() and list(input.values())[0] |
---|
922 | input_str = '' |
---|
923 | if isinstance(input, dict): |
---|
924 | if 'parameters' not in input or input['parameters']!=None: |
---|
925 | for k, v in input.items(): |
---|
926 | if isinstance(v, type): |
---|
927 | v = v.__name__ |
---|
928 | elif isinstance(v, Alias): |
---|
929 | v = v.xml_type |
---|
930 | elif isinstance(v, Struct): |
---|
931 | v = v.key[0] |
---|
932 | input_str += '%s: %s, ' % (k, v) |
---|
933 | output = operation.get('output') |
---|
934 | if output: |
---|
935 | output = list(operation['output'].values())[0] |
---|
936 | s += '\n %s(%s)' % ( |
---|
937 | operation['name'], |
---|
938 | input_str[:-2] |
---|
939 | ) |
---|
940 | s += '\n > %s' % output |
---|
941 | |
---|
942 | return s |
---|
943 | |
---|
944 | def parse_proxy(proxy_str): |
---|
945 | """Parses proxy address user:pass@host:port into a dict suitable for httplib2""" |
---|
946 | proxy_dict = {} |
---|
947 | if proxy_str is None: |
---|
948 | return |
---|
949 | if '@' in proxy_str: |
---|
950 | user_pass, host_port = proxy_str.split('@') |
---|
951 | else: |
---|
952 | user_pass, host_port = '', proxy_str |
---|
953 | if ':' in host_port: |
---|
954 | host, port = host_port.split(':') |
---|
955 | proxy_dict['proxy_host'], proxy_dict['proxy_port'] = host, int(port) |
---|
956 | if ':' in user_pass: |
---|
957 | proxy_dict['proxy_user'], proxy_dict['proxy_pass'] = user_pass.split(':') |
---|
958 | return proxy_dict |
---|
959 | |
---|
960 | |
---|
961 | if __name__ == '__main__': |
---|
962 | pass |
---|