[9460b1e] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import copy |
---|
| 4 | |
---|
[1b57352] | 5 | from socket import error as socket_error |
---|
[10a7053] | 6 | from socket import sslerror |
---|
[1b57352] | 7 | |
---|
[9460b1e] | 8 | import M2Crypto.httpslib |
---|
[6a0c9f4] | 9 | from M2Crypto import SSL |
---|
[9460b1e] | 10 | from M2Crypto.m2xmlrpclib import SSL_Transport |
---|
[1b57352] | 11 | from M2Crypto.SSL import SSLError |
---|
[1dcaff4] | 12 | from M2Crypto.BIO import BIOError |
---|
[a94cb0a] | 13 | from ZSI import ParseException, FaultException, SoapWriter |
---|
[9460b1e] | 14 | |
---|
[c2c153b] | 15 | # Underlying SOAP comms use this and we need to catch their exceptions |
---|
| 16 | import httplib |
---|
| 17 | |
---|
[e83f2f2] | 18 | from proof import proof |
---|
[f069052] | 19 | from service_error import service_error |
---|
[9460b1e] | 20 | from xmlrpclib import ServerProxy, dumps, loads, Fault, Error, Binary |
---|
[c179764] | 21 | try: |
---|
| 22 | import fedd_services |
---|
| 23 | import fedd_internal_services |
---|
[9556f2a] | 24 | service_port_name = 'getfeddPortType' |
---|
| 25 | internal_service_port_name = 'getfeddInternalPortType' |
---|
[c179764] | 26 | except ImportError: |
---|
| 27 | import fedd_client |
---|
[9556f2a] | 28 | import fedd_internal_client |
---|
| 29 | fedd_services = fedd_client |
---|
| 30 | fedd_internal_services = fedd_internal_client |
---|
| 31 | service_port_name = 'getfeddPort' |
---|
[9b76f1a] | 32 | internal_service_port_name = 'getfedd_internal_port' |
---|
[ec4fb42] | 33 | from util import fedd_ssl_context |
---|
[51cc9df] | 34 | from fedid import fedid |
---|
[6a0c9f4] | 35 | import parse_detail |
---|
[9460b1e] | 36 | |
---|
[6a0c9f4] | 37 | # Turn off the matching of hostname to certificate ID |
---|
| 38 | SSL.Connection.clientPostConnectionCheck = None |
---|
[9460b1e] | 39 | |
---|
| 40 | # Used by the remote_service_base class. |
---|
| 41 | def to_binary(o): |
---|
| 42 | """ |
---|
| 43 | A function that converts an object into an xmlrpclib.Binary using |
---|
| 44 | either its internal packing method, or the standard Binary constructor. |
---|
| 45 | """ |
---|
| 46 | pack = getattr(o, 'pack_xmlrpc', None) |
---|
| 47 | if callable(pack): return Binary(pack()) |
---|
| 48 | else: return Binary(o) |
---|
| 49 | |
---|
| 50 | # Classes that encapsulate the process of making and dealing with requests to |
---|
| 51 | # WSDL-generated and XMLRPC remote accesses. |
---|
| 52 | |
---|
| 53 | class remote_service_base: |
---|
| 54 | """ |
---|
| 55 | This invisible base class encapsulates the functions used to massage the |
---|
| 56 | dictionaries used to pass parameters into and out of the RPC formats. It's |
---|
| 57 | mostly a container for the static methods to do that work, but defines some |
---|
| 58 | maps sued by sub classes on apply_to_tags |
---|
| 59 | """ |
---|
| 60 | # A map used to convert fedid fields to fedid objects (when the field is |
---|
| 61 | # already a string) |
---|
[e15435c] | 62 | fedid_to_object = {'fedid': lambda x: fedid(bits=x)} |
---|
[9460b1e] | 63 | # A map used by apply_to_tags to convert fedids from xmlrpclib.Binary |
---|
| 64 | # objects to fedid objects in one sweep. |
---|
[e15435c] | 65 | decap_fedids = {'fedid': lambda x: fedid(bits=x.data), |
---|
| 66 | 'credential': lambda x: x.data} |
---|
[9460b1e] | 67 | # A map used to encapsulate fedids into xmlrpclib.Binary objects |
---|
[e15435c] | 68 | encap_fedids = {'fedid': to_binary, 'credential': to_binary} |
---|
[9460b1e] | 69 | |
---|
[e83f2f2] | 70 | # fields that are never unicoded, because they represent non strings. |
---|
| 71 | do_not_unicode = set(['credential']) |
---|
| 72 | |
---|
[9460b1e] | 73 | @staticmethod |
---|
| 74 | def pack_soap(container, name, contents): |
---|
| 75 | """ |
---|
| 76 | Convert the dictionary in contents into a tree of ZSI classes. |
---|
| 77 | |
---|
| 78 | The holder classes are constructed from factories in container and |
---|
| 79 | assigned to either the element or attribute name. This is used to |
---|
| 80 | recursively create the SOAP message. |
---|
| 81 | """ |
---|
| 82 | if getattr(contents, "__iter__", None) != None: |
---|
| 83 | attr =getattr(container, "new_%s" % name, None) |
---|
| 84 | if attr: obj = attr() |
---|
| 85 | else: |
---|
| 86 | raise TypeError("%s does not have a new_%s attribute" % \ |
---|
| 87 | (container, name)) |
---|
| 88 | for e, v in contents.iteritems(): |
---|
| 89 | assign = getattr(obj, "set_element_%s" % e, None) or \ |
---|
| 90 | getattr(obj, "set_attribute_%s" % e, None) |
---|
| 91 | if isinstance(v, type(dict())): |
---|
| 92 | assign(remote_service_base.pack_soap(obj, e, v)) |
---|
| 93 | elif getattr(v, "__iter__", None) != None: |
---|
| 94 | assign([ remote_service_base.pack_soap(obj, e, val ) \ |
---|
| 95 | for val in v]) |
---|
| 96 | elif getattr(v, "pack_soap", None) != None: |
---|
| 97 | assign(v.pack_soap()) |
---|
| 98 | else: |
---|
| 99 | assign(v) |
---|
| 100 | return obj |
---|
| 101 | else: return contents |
---|
| 102 | |
---|
| 103 | @staticmethod |
---|
| 104 | def unpack_soap(element): |
---|
| 105 | """ |
---|
| 106 | Convert a tree of ZSI SOAP classes intro a hash. The inverse of |
---|
| 107 | pack_soap |
---|
| 108 | |
---|
| 109 | Elements or elements that are empty are ignored. |
---|
| 110 | """ |
---|
| 111 | methods = [ m for m in dir(element) \ |
---|
| 112 | if m.startswith("get_element") or m.startswith("get_attribute")] |
---|
| 113 | if len(methods) > 0: |
---|
| 114 | rv = { } |
---|
| 115 | for m in methods: |
---|
| 116 | if m.startswith("get_element_"): |
---|
| 117 | n = m.replace("get_element_","",1) |
---|
| 118 | else: |
---|
| 119 | n = m.replace("get_attribute_", "", 1) |
---|
| 120 | sub = getattr(element, m)() |
---|
| 121 | if sub != None: |
---|
| 122 | if isinstance(sub, basestring): |
---|
| 123 | rv[n] = sub |
---|
| 124 | elif getattr(sub, "__iter__", None) != None: |
---|
| 125 | if len(sub) > 0: rv[n] = \ |
---|
| 126 | [remote_service_base.unpack_soap(e) \ |
---|
| 127 | for e in sub] |
---|
| 128 | else: |
---|
| 129 | rv[n] = remote_service_base.unpack_soap(sub) |
---|
| 130 | return rv |
---|
| 131 | else: |
---|
| 132 | return element |
---|
| 133 | |
---|
| 134 | @staticmethod |
---|
| 135 | def apply_to_tags(e, map): |
---|
| 136 | """ |
---|
| 137 | Map is an iterable of ordered pairs (tuples) that map a key to a |
---|
| 138 | function. |
---|
| 139 | This function walks the given message and replaces any object with a |
---|
| 140 | key in the map with the result of applying that function to the object. |
---|
| 141 | """ |
---|
| 142 | if isinstance(e, dict): |
---|
| 143 | for k in e.keys(): |
---|
[e15435c] | 144 | if k in map: |
---|
| 145 | fcn = map[k] |
---|
| 146 | if isinstance(e[k], list): |
---|
| 147 | e[k] = [ fcn(b) for b in e[k]] |
---|
| 148 | else: |
---|
| 149 | e[k] = fcn(e[k]) |
---|
| 150 | elif isinstance(e[k], dict): |
---|
| 151 | remote_service_base.apply_to_tags(e[k], map) |
---|
| 152 | elif isinstance(e[k], list): |
---|
| 153 | for ee in e[k]: |
---|
| 154 | remote_service_base.apply_to_tags(ee, map) |
---|
[9460b1e] | 155 | # Other types end the recursion - they should be leaves |
---|
| 156 | return e |
---|
| 157 | |
---|
| 158 | @staticmethod |
---|
| 159 | def strip_unicode(obj): |
---|
| 160 | """Walk through a message and convert all strings to non-unicode |
---|
| 161 | strings""" |
---|
| 162 | if isinstance(obj, dict): |
---|
| 163 | for k in obj.keys(): |
---|
| 164 | obj[k] = remote_service_base.strip_unicode(obj[k]) |
---|
| 165 | return obj |
---|
| 166 | elif isinstance(obj, basestring) and not isinstance(obj, str): |
---|
| 167 | return str(obj) |
---|
| 168 | elif getattr(obj, "__iter__", None): |
---|
| 169 | return [ remote_service_base.strip_unicode(x) for x in obj] |
---|
| 170 | else: |
---|
| 171 | return obj |
---|
| 172 | |
---|
| 173 | @staticmethod |
---|
| 174 | def make_unicode(obj): |
---|
| 175 | """Walk through a message and convert all strings to unicode""" |
---|
| 176 | if isinstance(obj, dict): |
---|
| 177 | for k in obj.keys(): |
---|
[e83f2f2] | 178 | if k not in remote_service_base.do_not_unicode: |
---|
| 179 | obj[k] = remote_service_base.make_unicode(obj[k]) |
---|
[9460b1e] | 180 | return obj |
---|
| 181 | elif isinstance(obj, basestring) and not isinstance(obj, unicode): |
---|
| 182 | return unicode(obj) |
---|
| 183 | elif getattr(obj, "__iter__", None): |
---|
| 184 | return [ remote_service_base.make_unicode(x) for x in obj] |
---|
| 185 | else: |
---|
| 186 | return obj |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | class soap_handler(remote_service_base): |
---|
| 191 | """ |
---|
| 192 | Encapsulate the handler code to unpack and pack SOAP requests and responses |
---|
| 193 | and call the given method. |
---|
| 194 | |
---|
| 195 | The code to decapsulate and encapsulate parameters encoded in SOAP is the |
---|
| 196 | same modulo a few parameters. This is a functor that calls a fedd service |
---|
| 197 | trhough a soap interface. The parameters are the typecode of the request |
---|
| 198 | parameters, the method to call (usually a bound instance of a method on a |
---|
| 199 | fedd service providing class), the constructor of a response packet and the |
---|
| 200 | name of the body element of that packet. The handler takes a ParsedSoap |
---|
| 201 | object (the request) and returns an instance of the class created by |
---|
| 202 | constructor containing the response. Failures of the constructor or badly |
---|
| 203 | created constructors will result in None being returned. |
---|
| 204 | """ |
---|
[f069052] | 205 | def __init__(self, service_name, method, typecode=None, |
---|
| 206 | constructor=None, body_name=None): |
---|
[9460b1e] | 207 | self.method = method |
---|
[f069052] | 208 | |
---|
| 209 | response_class_name = "%sResponseMessage" % service_name |
---|
| 210 | request_class_name = "%sRequestMessage" % service_name |
---|
| 211 | |
---|
| 212 | if body_name: self.body_name = body_name |
---|
| 213 | else: self.body_name = "%sResponseBody" % service_name |
---|
| 214 | |
---|
| 215 | if constructor: self.constructor = constructor |
---|
| 216 | else: |
---|
| 217 | self.constructor = self.get_class(response_class_name) |
---|
| 218 | if not self.constructor: |
---|
| 219 | raise service_error(service_error.internal, |
---|
| 220 | "Cannot find class for %s" % response_class_name) |
---|
| 221 | |
---|
| 222 | if typecode: self.typecode = typecode |
---|
| 223 | else: |
---|
| 224 | req = self.get_class(request_class_name) |
---|
| 225 | if req: |
---|
| 226 | self.typecode = req.typecode |
---|
| 227 | else: |
---|
| 228 | raise service_error(service_error.internal, |
---|
| 229 | "Cannot find class for %s" % request_class_name) |
---|
| 230 | |
---|
| 231 | if not self.typecode: |
---|
| 232 | raise service_error(service_error.internal, |
---|
| 233 | "Cannot get typecode for %s" % class_name) |
---|
| 234 | |
---|
| 235 | def get_class(self, class_name): |
---|
| 236 | return getattr(fedd_services, class_name, None) or \ |
---|
| 237 | getattr(fedd_internal_services, class_name, None) |
---|
[9460b1e] | 238 | |
---|
| 239 | def __call__(self, ps, fid): |
---|
| 240 | req = ps.Parse(self.typecode) |
---|
| 241 | # Convert the message to a dict with the fedid strings converted to |
---|
| 242 | # fedid objects |
---|
| 243 | req = self.apply_to_tags(self.unpack_soap(req), self.fedid_to_object) |
---|
| 244 | |
---|
| 245 | msg = self.method(req, fid) |
---|
| 246 | |
---|
| 247 | resp = self.constructor() |
---|
| 248 | set_element = getattr(resp, "set_element_%s" % self.body_name, None) |
---|
| 249 | if set_element and callable(set_element): |
---|
| 250 | try: |
---|
| 251 | set_element(self.pack_soap(resp, self.body_name, msg)) |
---|
| 252 | return resp |
---|
| 253 | except (NameError, TypeError): |
---|
| 254 | return None |
---|
| 255 | else: |
---|
| 256 | return None |
---|
| 257 | |
---|
| 258 | class xmlrpc_handler(remote_service_base): |
---|
| 259 | """ |
---|
| 260 | Generate the handler code to unpack and pack XMLRPC requests and responses |
---|
| 261 | and call the given method. |
---|
| 262 | |
---|
| 263 | The code to marshall and unmarshall XMLRPC parameters to and from a fedd |
---|
| 264 | service is largely the same. This helper creates such a handler. The |
---|
| 265 | parameters are the method name, and the name of the body struct that |
---|
| 266 | contains the response. A handler is created that takes the params response |
---|
| 267 | from an xmlrpclib.loads on the incoming rpc and a fedid and responds with |
---|
| 268 | a hash representing the struct ro be returned to the other side. On error |
---|
| 269 | None is returned. Fedid fields are decapsulated from binary and converted |
---|
| 270 | to fedid objects on input and encapsulated as Binaries on output. |
---|
| 271 | """ |
---|
[f069052] | 272 | def __init__(self, service_name, method): |
---|
[9460b1e] | 273 | self.method = method |
---|
[f069052] | 274 | self.body_name = "%sResponseBody" % service_name |
---|
[9460b1e] | 275 | |
---|
| 276 | def __call__(self, params, fid): |
---|
| 277 | msg = None |
---|
| 278 | |
---|
| 279 | p = self.apply_to_tags(params[0], self.decap_fedids) |
---|
| 280 | try: |
---|
| 281 | msg = self.method(p, fid) |
---|
| 282 | except service_error, e: |
---|
[89d33df] | 283 | raise Fault(e.code, "%s: %s" % (e.code_string(), e.desc)) |
---|
[9460b1e] | 284 | if msg != None: |
---|
| 285 | return self.make_unicode(self.apply_to_tags(\ |
---|
| 286 | { self.body_name: msg }, self.encap_fedids)) |
---|
| 287 | else: |
---|
| 288 | return None |
---|
| 289 | |
---|
| 290 | class service_caller(remote_service_base): |
---|
[f069052] | 291 | def __init__(self, service_name, request_message=None, |
---|
[114d24b] | 292 | request_body_name=None, tracefile=None, strict=True, |
---|
[11867dde] | 293 | log=None, max_retries=None): |
---|
[9460b1e] | 294 | self.service_name = service_name |
---|
[f069052] | 295 | |
---|
[9556f2a] | 296 | if getattr(fedd_services.feddBindingSOAP, service_name, None): |
---|
| 297 | self.locator = fedd_services.feddServiceLocator |
---|
| 298 | self.port_name = service_port_name |
---|
| 299 | elif getattr(fedd_internal_services.feddInternalBindingSOAP, |
---|
| 300 | service_name, None): |
---|
| 301 | self.locator = fedd_internal_services.feddInternalServiceLocator |
---|
| 302 | self.port_name = internal_service_port_name |
---|
[f069052] | 303 | |
---|
| 304 | if request_message: self.request_message = request_message |
---|
| 305 | else: |
---|
| 306 | request_message_name = "%sRequestMessage" % service_name |
---|
[8445caf] | 307 | self.request_message = \ |
---|
| 308 | getattr(fedd_services, request_message_name, None) or \ |
---|
| 309 | getattr(fedd_internal_services, request_message_name, |
---|
| 310 | None) |
---|
[6a8a9ec] | 311 | if not self.request_message and strict: |
---|
[f069052] | 312 | raise service_error(service_error.internal, |
---|
| 313 | "Cannot find class for %s" % request_message_name) |
---|
| 314 | |
---|
[6a8a9ec] | 315 | if request_body_name is not None: |
---|
| 316 | self.request_body_name = request_body_name |
---|
| 317 | else: |
---|
| 318 | self.request_body_name = "%sRequestBody" % service_name |
---|
[f069052] | 319 | |
---|
[9460b1e] | 320 | self.tracefile = tracefile |
---|
| 321 | self.__call__ = self.call_service |
---|
[11867dde] | 322 | if max_retries is not None: self.max_retries = max_retries |
---|
| 323 | else: self.max_retries = 5 |
---|
[114d24b] | 324 | self.log = log |
---|
[9460b1e] | 325 | |
---|
[a94cb0a] | 326 | def serialize_soap(self, req): |
---|
| 327 | """ |
---|
| 328 | Return a string containing the message that would be sent to call this |
---|
| 329 | service with the given request. |
---|
| 330 | """ |
---|
| 331 | msg = self.request_message() |
---|
| 332 | set_element = getattr(msg, "set_element_%s" % self.request_body_name, |
---|
| 333 | None) |
---|
| 334 | if not set_element: |
---|
| 335 | raise service_error(service_error.internal, |
---|
| 336 | "Cannot get element setting method for %s" % \ |
---|
| 337 | self.request_body_name) |
---|
| 338 | set_element(self.pack_soap(msg, self.request_body_name, req)) |
---|
| 339 | sw = SoapWriter() |
---|
| 340 | sw.serialize(msg) |
---|
| 341 | return unicode(sw) |
---|
| 342 | |
---|
[9460b1e] | 343 | def call_xmlrpc_service(self, url, req, cert_file=None, cert_pwd=None, |
---|
| 344 | trusted_certs=None, context=None, tracefile=None): |
---|
| 345 | """Send an XMLRPC request. """ |
---|
| 346 | |
---|
| 347 | |
---|
| 348 | # If a context is given, use it. Otherwise construct one from |
---|
| 349 | # components. The construction shouldn't call out for passwords. |
---|
| 350 | if context: |
---|
| 351 | ctx = context |
---|
| 352 | else: |
---|
| 353 | try: |
---|
| 354 | ctx = fedd_ssl_context(cert_file, trusted_certs, |
---|
| 355 | password=cert_pwd) |
---|
[a6a3f93] | 356 | except SSL.SSLError, e: |
---|
[9460b1e] | 357 | raise service_error(service_error.server_config, |
---|
[a6a3f93] | 358 | "Certificates misconfigured: %s" % e) |
---|
[9460b1e] | 359 | |
---|
| 360 | # Of all the dumbass things. The XMLRPC library in use here won't |
---|
| 361 | # properly encode unicode strings, so we make a copy of req with |
---|
| 362 | # the unicode objects converted. We also convert the url to a |
---|
| 363 | # basic string if it isn't one already. |
---|
| 364 | r = self.strip_unicode(copy.deepcopy(req)) |
---|
[8e6fe4d] | 365 | if self.request_body_name: |
---|
| 366 | r = self.apply_to_tags(\ |
---|
| 367 | { self.request_body_name: r}, self.encap_fedids) |
---|
| 368 | else: |
---|
| 369 | r = self.apply_to_tags(r, self.encap_fedids) |
---|
| 370 | |
---|
[2ee4226] | 371 | url = str(url) |
---|
[8e6fe4d] | 372 | ok = False |
---|
| 373 | retries = 0 |
---|
| 374 | |
---|
| 375 | while not ok and retries < self.max_retries: |
---|
| 376 | try: |
---|
[2ee4226] | 377 | transport = SSL_Transport(ctx) |
---|
| 378 | port = ServerProxy(url, transport=transport) |
---|
[8e6fe4d] | 379 | remote_method = getattr(port, self.service_name, None) |
---|
| 380 | resp = remote_method(r) |
---|
[c5b28bf] | 381 | ok = True |
---|
[8e6fe4d] | 382 | except socket_error, e: |
---|
| 383 | raise service_error(service_error.connect, |
---|
| 384 | "Cannot connect to %s: %s" % (url, e[1])) |
---|
[1dcaff4] | 385 | except BIOError, e: |
---|
[114d24b] | 386 | if self.log: |
---|
| 387 | self.log.warn("BIO error contacting %s: %s" % (url, e)) |
---|
[1dcaff4] | 388 | retries += 1 |
---|
[10a7053] | 389 | except sslerror, e: |
---|
| 390 | if self.log: |
---|
| 391 | self.log.warn("SSL (socket) error contacting %s: %s" % |
---|
| 392 | (url, e)) |
---|
| 393 | retries += 1 |
---|
[8e6fe4d] | 394 | except SSLError, e: |
---|
[114d24b] | 395 | if self.log: |
---|
| 396 | self.log.warn("SSL error contacting %s: %s" % (url, e)) |
---|
[8e6fe4d] | 397 | retries += 1 |
---|
[c2c153b] | 398 | except httplib.HTTPException, e: |
---|
[114d24b] | 399 | if self.log: |
---|
| 400 | self.log.warn("HTTP error contacting %s: %s" % (url, e)) |
---|
[c2c153b] | 401 | retries +=1 |
---|
[8e6fe4d] | 402 | except Fault, f: |
---|
| 403 | raise service_error(f.faultCode, f.faultString) |
---|
| 404 | except Error, e: |
---|
| 405 | raise service_error(service_error.protocol, |
---|
| 406 | "Remote XMLRPC Fault: %s" % e) |
---|
| 407 | |
---|
[8cce763] | 408 | if retries >= self.max_retries : |
---|
| 409 | raise service_error(service_error.connect, "Too many SSL failures") |
---|
[9460b1e] | 410 | |
---|
| 411 | return self.apply_to_tags(resp, self.decap_fedids) |
---|
| 412 | |
---|
| 413 | def call_soap_service(self, url, req, cert_file=None, cert_pwd=None, |
---|
| 414 | trusted_certs=None, context=None, tracefile=None): |
---|
| 415 | """ |
---|
| 416 | Send req on to the real destination in dt and return the response |
---|
| 417 | |
---|
| 418 | Req is just the requestType object. This function re-wraps it. It |
---|
| 419 | also rethrows any faults. |
---|
| 420 | """ |
---|
| 421 | |
---|
| 422 | tf = tracefile or self.tracefile or None |
---|
| 423 | |
---|
[6a8a9ec] | 424 | if not self.request_body_name: |
---|
| 425 | raise service_error(service_error.internal, |
---|
| 426 | "Call to soap service without a configured request body"); |
---|
| 427 | |
---|
[8e6fe4d] | 428 | ok = False |
---|
| 429 | retries = 0 |
---|
| 430 | while not ok and retries < self.max_retries: |
---|
| 431 | try: |
---|
[2ee4226] | 432 | # Reconstruct the full request message |
---|
| 433 | msg = self.request_message() |
---|
| 434 | set_element = getattr(msg, "set_element_%s" % \ |
---|
| 435 | self.request_body_name, |
---|
| 436 | None) |
---|
| 437 | if not set_element: |
---|
| 438 | raise service_error(service_error.internal, |
---|
| 439 | "Cannot get element setting method for %s" % \ |
---|
| 440 | self.request_body_name) |
---|
| 441 | set_element(self.pack_soap(msg, self.request_body_name, req)) |
---|
| 442 | # If a context is given, use it. Otherwise construct one from |
---|
| 443 | # components. The construction shouldn't call out for |
---|
| 444 | # passwords. |
---|
| 445 | if context: |
---|
| 446 | if self.log: |
---|
| 447 | self.log.debug("Context passed in to call_soap") |
---|
| 448 | ctx = context |
---|
| 449 | else: |
---|
| 450 | if self.log: |
---|
| 451 | self.log.debug( |
---|
| 452 | "Constructing context in call_soap: %s" % \ |
---|
| 453 | cert_file) |
---|
| 454 | try: |
---|
| 455 | ctx = fedd_ssl_context(cert_file, trusted_certs, |
---|
| 456 | password=cert_pwd) |
---|
| 457 | except SSL.SSLError, e: |
---|
| 458 | if self.log: |
---|
| 459 | self.log.debug("Certificate error: %s" % e) |
---|
| 460 | raise service_error(service_error.server_config, |
---|
[a6a3f93] | 461 | "Certificates misconfigured: %s" % e) |
---|
[2ee4226] | 462 | loc = self.locator() |
---|
| 463 | get_port = getattr(loc, self.port_name, None) |
---|
| 464 | if not get_port: |
---|
| 465 | raise service_error(service_error.internal, |
---|
| 466 | "Cannot get port %s from locator" % self.port_name) |
---|
| 467 | port = get_port(url, |
---|
| 468 | transport=M2Crypto.httpslib.HTTPSConnection, |
---|
| 469 | transdict={ 'ssl_context' : ctx }, |
---|
| 470 | tracefile=tf) |
---|
| 471 | remote_method = getattr(port, self.service_name, None) |
---|
| 472 | if not remote_method: |
---|
| 473 | raise service_error(service_error.internal, |
---|
| 474 | "Cannot get service from SOAP port") |
---|
| 475 | |
---|
[bdf00d5] | 476 | fail_exc = None |
---|
[2ee4226] | 477 | if self.log: |
---|
| 478 | self.log.debug("Calling %s (retry %d)" % \ |
---|
| 479 | (self.service_name, retries)) |
---|
[8e6fe4d] | 480 | resp = remote_method(msg) |
---|
| 481 | ok = True |
---|
| 482 | except socket_error, e: |
---|
| 483 | raise service_error(service_error.connect, |
---|
| 484 | "Cannot connect to %s: %s" % (url, e[1])) |
---|
[1dcaff4] | 485 | except BIOError, e: |
---|
[114d24b] | 486 | if self.log: |
---|
| 487 | self.log.warn("BIO error contacting %s: %s" % (url, e)) |
---|
[bdf00d5] | 488 | fail_exc = e |
---|
[1dcaff4] | 489 | retries += 1 |
---|
[10a7053] | 490 | except sslerror, e: |
---|
| 491 | if self.log: |
---|
| 492 | self.log.warn("SSL (socket) error contacting %s: %s" % |
---|
| 493 | (url, e)) |
---|
| 494 | retries += 1 |
---|
[8e6fe4d] | 495 | except SSLError, e: |
---|
[114d24b] | 496 | if self.log: |
---|
| 497 | self.log.warn("SSL error contacting %s: %s" % (url, e)) |
---|
[bdf00d5] | 498 | fail_exc = e |
---|
[8e6fe4d] | 499 | retries += 1 |
---|
[c2c153b] | 500 | except httplib.HTTPException, e: |
---|
[114d24b] | 501 | if self.log: |
---|
| 502 | self.log.warn("HTTP error contacting %s: %s" % (url, e)) |
---|
[bdf00d5] | 503 | fail_exc = e |
---|
[c2c153b] | 504 | retries +=1 |
---|
[8e6fe4d] | 505 | except ParseException, e: |
---|
| 506 | raise service_error(service_error.protocol, |
---|
| 507 | "Bad format message (XMLRPC??): %s" % e) |
---|
| 508 | except FaultException, e: |
---|
[85bba36] | 509 | # If the method isn't implemented we get a FaultException |
---|
| 510 | # without a detail (which would be a FeddFault). If that's the |
---|
| 511 | # case construct a service_error out of the SOAP fields of the |
---|
| 512 | # fault, if they're present. |
---|
| 513 | if e.fault.detail: |
---|
| 514 | det = e.fault.detail[0] |
---|
| 515 | ee = self.unpack_soap(det).get('FeddFaultBody', { }) |
---|
| 516 | else: |
---|
| 517 | ee = { 'code': service_error.internal, |
---|
| 518 | 'desc': e.fault.string or "Something Weird" } |
---|
[8e6fe4d] | 519 | if ee: |
---|
[e83f2f2] | 520 | if 'proof' in ee: |
---|
| 521 | pl = [ proof.from_dict(p) for p in ee['proof']] |
---|
| 522 | else: |
---|
| 523 | pl = None |
---|
[814b5e5] | 524 | raise service_error(ee.get('code', 'no code'), |
---|
[e83f2f2] | 525 | ee.get('desc','no desc'), proof=pl) |
---|
[8e6fe4d] | 526 | else: |
---|
| 527 | raise service_error(service_error.internal, |
---|
| 528 | "Unexpected fault body") |
---|
| 529 | |
---|
[bdf00d5] | 530 | if retries >= self.max_retries and fail_exc and not ok: |
---|
| 531 | raise service_error(service_error.connect, |
---|
| 532 | "Too many failures: %s" % fail_exc) |
---|
[8e6fe4d] | 533 | |
---|
[9460b1e] | 534 | # Unpack and convert fedids to objects |
---|
| 535 | r = self.apply_to_tags(self.unpack_soap(resp), self.fedid_to_object) |
---|
[8e6fe4d] | 536 | |
---|
[9460b1e] | 537 | # Make sure all strings are unicode |
---|
| 538 | r = self.make_unicode(r) |
---|
| 539 | return r |
---|
| 540 | |
---|
| 541 | def call_service(self, url, req, cert_file=None, cert_pwd=None, |
---|
| 542 | trusted_certs=None, context=None, tracefile=None): |
---|
| 543 | p_fault = None # Any SOAP failure (sent unless XMLRPC works) |
---|
| 544 | resp = None |
---|
| 545 | try: |
---|
| 546 | # Try the SOAP request |
---|
| 547 | resp = self.call_soap_service(url, req, |
---|
| 548 | cert_file, cert_pwd, trusted_certs, context, tracefile) |
---|
| 549 | return resp |
---|
| 550 | except service_error, e: |
---|
| 551 | if e.code == service_error.protocol: p_fault = None |
---|
| 552 | else: raise |
---|
| 553 | except FaultException, f: |
---|
| 554 | p_fault = f.fault.detail[0] |
---|
| 555 | |
---|
| 556 | |
---|
| 557 | # If we could not get a valid SOAP response to the request above, |
---|
| 558 | # try the same address using XMLRPC and let any faults flow back |
---|
| 559 | # out. |
---|
| 560 | if p_fault == None: |
---|
| 561 | resp = self.call_xmlrpc_service(url, req, cert_file, |
---|
| 562 | cert_pwd, trusted_certs, context, tracefile) |
---|
| 563 | return resp |
---|
| 564 | else: |
---|
| 565 | # Build the fault |
---|
| 566 | ee = unpack_soap(p_fault).get('FeddFaultBody', { }) |
---|
| 567 | if ee: |
---|
| 568 | raise service_error(ee['code'], ee['desc']) |
---|
| 569 | else: |
---|
| 570 | raise service_error(service_error.internal, |
---|
| 571 | "Unexpected fault body") |
---|