Changeset c922f23


Ignore:
Timestamp:
Nov 19, 2008 7:24:16 PM (16 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master, version-1.30, version-2.00, version-3.01, version-3.02
Children:
058f58e
Parents:
dab4d56
Message:

Start the move to unified service calling routines

Location:
fedd
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • fedd/fedd_access.py

    rdab4d56 rc922f23  
    5151            "dynamic_projects_cert_pwd", "dynamic_projects_trusted_certs")
    5252    id_list_attrs = ("restricted",)
     53
     54    proxy_request, proxy_xmlrpc_request = make_service_callers('RequestAccess',
     55            'getfeddPortType', RequestAccessRequestMessage,
     56            'RequestAccessRequestBody')
    5357
    5458    def __init__(self, config=None):
     
    387391            self.log.warning(("[read_state]: No saved state: " + \
    388392                    "Unpickling failed: %s") % e)
    389 
    390 
    391     def proxy_xmlrpc_request(self, dt, req):
    392         """Send an XMLRPC proxy request.  Called if the SOAP RPC fails"""
    393 
    394         # No retry loop here.  Proxy servers must correctly authenticate
    395         # themselves without help
    396         try:
    397             ctx = fedd_ssl_context(self.proxy_cert_file,
    398                     self.proxy_trusted_certs, password=self.proxy_cert_pwd)
    399         except SSL.SSLError:
    400             raise service_error(service_error.server_config,
    401                     "Server certificates misconfigured")
    402 
    403         # Of all the dumbass things.  The XMLRPC library in use here won't
    404         # properly encode unicode strings, so we make a copy of req with the
    405         # unicode objects converted.  We also convert the destination testbed
    406         # to a basic string if it isn't one already.
    407         if isinstance(dt, str): url = dt
    408         else: url = str(dt)
    409 
    410         r = strip_unicode(copy.deepcopy(req))
    411        
    412         transport = SSL_Transport(ctx)
    413         port = xmlrpclib.ServerProxy(url, transport=transport)
    414 
    415         # Reconstruct the full request message
    416         try:
    417             resp = port.RequestAccess(
    418                     { "RequestAccessRequestBody": r})
    419             resp, method = xmlrpclib.loads(resp)
    420         except xmlrpclib.Fault, f:
    421             se = service_error(None, f.faultString, f.faultCode)
    422             raise se
    423         except xmlrpclib.Error, e:
    424             raise service_error(service_error.proxy,
    425                     "Remote XMLRPC Fault: %s" % e)
    426        
    427         if resp[0].has_key('RequestAccessResponseBody'):
    428             return resp[0]['RequestAccessResponseBody']
    429         else:
    430             raise service_error(service_error.proxy,
    431                     "Bad proxy response")
    432 
    433     def proxy_request(self, dt, req):
    434         """
    435         Send req on to the real destination in dt and return the response
    436 
    437         Req is just the requestType object.  This function re-wraps it.  It
    438         also rethrows any faults.
    439         """
    440         # No retry loop here.  Proxy servers must correctly authenticate
    441         # themselves without help
    442         try:
    443             ctx = fedd_ssl_context(self.proxy_cert_file,
    444                     self.proxy_trusted_certs, password=self.proxy_cert_pwd)
    445         except SSL.SSLError:
    446             raise service_error(service_error.server_config,
    447                     "Server certificates misconfigured")
    448 
    449         loc = feddServiceLocator();
    450         port = loc.getfeddPortType(dt,
    451                 transport=M2Crypto.httpslib.HTTPSConnection,
    452                 transdict={ 'ssl_context' : ctx })
    453 
    454         # Reconstruct the full request message
    455         msg = RequestAccessRequestMessage()
    456         msg.set_element_RequestAccessRequestBody(
    457                 pack_soap(msg, "RequestAccessRequestBody", req))
    458         try:
    459             resp = port.RequestAccess(msg)
    460         except ZSI.ParseException, e:
    461             raise service_error(service_error.proxy,
    462                     "Bad format message (XMLRPC??): %s" %
    463                     str(e))
    464         r = unpack_soap(resp)
    465 
    466         if r.has_key('RequestAccessResponseBody'):
    467             return r['RequestAccessResponseBody']
    468         else:
    469             raise service_error(service_error.proxy,
    470                     "Bad proxy response")
    471393
    472394    def permute_wildcards(self, a, p):
     
    779701            try:
    780702                # Proxy the request using SOAP
    781                 return self.proxy_request(dt, req)
     703                self.log.debug("Sending proxy message to %s" % dt)
     704                resp = self.proxy_request(dt, req, feddServiceLocator,
     705                        self.proxy_cert_file, self.proxy_cert_pwd,
     706                        self.proxy_trusted_certs)
     707                if resp.has_key('RequestAccessResponseBody'):
     708                    return resp['RequestAccessResponseBody']
     709                elif resp.has_key('Fedd_FaultBody'):
     710                    raise service_error(resp['FeddFaultBody']['code'],
     711                            resp['FeddFaultBody']['desc'])
    782712            except service_error, e:
    783713                if e.code == service_error.proxy: p_fault = None
     
    791721            # out.
    792722            if p_fault == None:
    793                 return self.proxy_xmlrpc_request(dt, req)
     723                resp = self.proxy_xmlrpc_request(dt, req, self.proxy_cert_file,
     724                    self.proxy_cert_pwd, self.proxy_trusted_certs)
     725                if resp.has_key('RequestAccessResponseBody'):
     726                    return resp['RequestAccessResponseBody']
    794727            else:
    795728                # Build the fault
  • fedd/fedd_util.py

    rdab4d56 rc922f23  
    55import tempfile
    66import logging
     7import copy
    78
    89from M2Crypto import SSL, X509, EVP
     10from M2Crypto.m2xmlrpclib import SSL_Transport
     11import M2Crypto.httpslib
    912from pyasn1.codec.der import decoder
    1013
    11 from xmlrpclib import Binary
     14from fedd_services import *
     15from fedd_internal_services import *
     16from service_error import *
     17from xmlrpclib import ServerProxy, dumps, loads, Fault, Error, Binary
    1218
    1319
     
    413419        decap_fedids = (('fedid', lambda x: fedid(bits=x.data)),)
    414420
    415         #p = decapsulate_binaries(params[0], input_binaries)
    416421        p = apply_to_tags(params[0], decap_fedids)
    417422        msg = method(p, fid)
    418423
    419424        if msg != None:
    420             return encapsulate_binaries({ body_name: msg }, ('fedid',))
     425            return make_unicode(encapsulate_binaries({ body_name: msg }, ('fedid',)))
    421426        else:
    422427            return None
     
    424429    return handler
    425430
     431def make_service_callers(service_name, port_name, request_message,
     432        request_body_name):
     433
     434    def call_xmlrpc_service(self, url, req, cert_file=None, cert_pwd=None,
     435            trusted_certs=None):
     436        """Send an XMLRPC request.  """
     437        decap_fedids = (('fedid', lambda x: fedid(bits=x.data)),)
     438
     439        # No retry loop here.  Proxy servers must correctly authenticate
     440        # themselves without help
     441        try:
     442            ctx = fedd_ssl_context(cert_file, trusted_certs, password=cert_pwd)
     443        except SSL.SSLError:
     444            raise service_error(service_error.server_config,
     445                    "Server certificates misconfigured")
     446
     447        # Of all the dumbass things.  The XMLRPC library in use here won't
     448        # properly encode unicode strings, so we make a copy of req with the
     449        # unicode objects converted.  We also convert the url to a basic string
     450        # if it isn't one already.
     451        r = strip_unicode(copy.deepcopy(req))
     452        url = str(url)
     453       
     454        transport = SSL_Transport(ctx)
     455        port = ServerProxy(url, transport=transport)
     456        try:
     457            remote_method = getattr(port, service_name, None)
     458            resp = remote_method(encapsulate_binaries({ request_body_name: r},
     459                ('fedid',)))
     460        except Fault, f:
     461            raise service_error(None, f.faultString, f.faultCode)
     462        except Error, e:
     463            raise service_error(service_error.proxy,
     464                    "Remote XMLRPC Fault: %s" % e)
     465
     466        return apply_to_tags(resp, decap_fedids)
     467
     468    def call_soap_service(self, url, req, loc_const, cert_file=None, cert_pwd=None,
     469            trusted_certs=None):
     470        """
     471        Send req on to the real destination in dt and return the response
     472
     473        Req is just the requestType object.  This function re-wraps it.  It
     474        also rethrows any faults.
     475        """
     476        # No retry loop here.  Proxy servers must correctly authenticate
     477        # themselves without help
     478        try:
     479            ctx = fedd_ssl_context(cert_file, trusted_certs, password=cert_pwd)
     480        except SSL.SSLError:
     481            raise service_error(service_error.server_config,
     482                    "Server certificates misconfigured")
     483
     484        loc = loc_const()
     485        get_port = getattr(loc, port_name, None)
     486        if not get_port:
     487            raise service_error(service_error.internal,
     488                    "Cannot get port %s from locator" % port_name)
     489        port = get_port(url,
     490                transport=M2Crypto.httpslib.HTTPSConnection,
     491                transdict={ 'ssl_context' : ctx })
     492        remote_method = getattr(port, service_name, None)
     493        if not remote_method:
     494            raise service_error(service_error.internal,
     495                    "Cannot get service from SOAP port")
     496
     497        # Reconstruct the full request message
     498        msg = request_message()
     499        set_element = getattr(msg, "set_element_%s" % request_body_name, None)
     500        if not set_element:
     501            raise service_error(service_error.internal,
     502                    "Cannot get element setting method for %s" % \
     503                            request_body_name)
     504        set_element(pack_soap(msg, request_body_name, req))
     505        try:
     506            resp = remote_method(msg)
     507        except ZSI.ParseException, e:
     508            raise service_error(service_error.proxy,
     509                    "Bad format message (XMLRPC??): %s" %
     510                    str(e))
     511        r = unpack_soap(resp)
     512        return r
     513
     514    return (call_soap_service, call_xmlrpc_service)
    426515
    427516def set_log_level(config, sect, log):
Note: See TracChangeset for help on using the changeset viewer.