[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Freeipa-devel] [PATCH] 0336 rpcserver: Consolidate __call__ in xmlclient and jsonclient_kerb



See commit message & ticket.

https://fedorahosted.org/freeipa/ticket/4069

--
PetrĀ³
From f2eaff6f9965331d83a2213161100ae21cd9a26a Mon Sep 17 00:00:00 2001
From: Petr Viktorin <pviktori redhat com>
Date: Tue, 10 Dec 2013 17:36:32 +0100
Subject: [PATCH] rpcserver: Consolidate __call__ in xmlclient and
 jsonclient_kerb

The two classes had very similar __call__ methods, but the JSON
server lacked error handling.

Create a common class for the __call__ method.

https://fedorahosted.org/freeipa/ticket/4069
---
 ipaserver/rpcserver.py | 110 ++++++++++++++++++++-----------------------------
 1 file changed, 45 insertions(+), 65 deletions(-)

diff --git a/ipaserver/rpcserver.py b/ipaserver/rpcserver.py
index a37d3cd0f25eed8b19e4980fa30a496c261ebde4..992b89583b920355b9137db0f760378624141f5e 100644
--- a/ipaserver/rpcserver.py
+++ b/ipaserver/rpcserver.py
@@ -596,7 +596,50 @@ def finalize_kerberos_acquisition(self, who, ccache_name, environ, start_respons
         return ['']
 
 
-class xmlserver(WSGIExecutioner, HTTP_Status, KerberosSession):
+class KerberosWSGIExecutioner(WSGIExecutioner, HTTP_Status, KerberosSession):
+    """Base class for xmlserver and jsonserver_kerb
+    """
+
+    def _on_finalize(self):
+        super(KerberosWSGIExecutioner, self)._on_finalize()
+        self.kerb_session_on_finalize()
+
+    def __call__(self, environ, start_response):
+        self.debug('KerberosWSGIExecutioner.__call__:')
+        user_ccache=environ.get('KRB5CCNAME')
+
+        headers = [('Content-Type', '%s; charset=utf-8' % self.content_type)]
+
+        if user_ccache is None:
+
+            status = HTTP_STATUS_SERVER_ERROR
+            response_headers = [('Content-Type', 'text/html; charset=utf-8')]
+
+            self.log.error(
+                '%s: %s', status,
+                'KerberosWSGIExecutioner.__call__: '
+                'KRB5CCNAME not defined in HTTP request environment')
+
+            return self.marshal(None, CCacheError())
+        try:
+            self.create_context(ccache=user_ccache)
+            response = super(KerberosWSGIExecutioner, self).__call__(
+                environ, start_response)
+            session_data = getattr(context, 'session_data', None)
+            if (session_data is None and self.env.context != 'lite'):
+                self.finalize_kerberos_acquisition(
+                    'xmlserver', user_ccache, environ, start_response, headers)
+        except PublicError, e:
+            status = HTTP_STATUS_SUCCESS
+            response = status
+            start_response(status, headers)
+            return self.marshal(None, e)
+        finally:
+            destroy_context()
+        return response
+
+
+class xmlserver(KerberosWSGIExecutioner):
     """
     Execution backend plugin for XML-RPC server.
 
@@ -606,41 +649,6 @@ class xmlserver(WSGIExecutioner, HTTP_Status, KerberosSession):
     content_type = 'text/xml'
     key = '/xml'
 
-    def _on_finalize(self):
-        self.__system = {
-            'system.listMethods': self.listMethods,
-            'system.methodSignature': self.methodSignature,
-            'system.methodHelp': self.methodHelp,
-        }
-        super(xmlserver, self)._on_finalize()
-        self.kerb_session_on_finalize()
-
-    def __call__(self, environ, start_response):
-        '''
-        '''
-
-        self.debug('WSGI xmlserver.__call__:')
-        user_ccache=environ.get('KRB5CCNAME')
-        headers = [('Content-Type', 'text/xml; charset=utf-8')]
-        if user_ccache is None:
-            self.internal_error(environ, start_response,
-                                'xmlserver.__call__: KRB5CCNAME not defined in HTTP request environment')
-            return self.marshal(None, CCacheError())
-        try:
-            self.create_context(ccache=user_ccache)
-            response = super(xmlserver, self).__call__(environ, start_response)
-            if getattr(context, 'session_data', None) is None and \
-              self.env.context != 'lite':
-                self.finalize_kerberos_acquisition('xmlserver', user_ccache, environ, start_response, headers)
-        except PublicError, e:
-            status = HTTP_STATUS_SUCCESS
-            response = status
-            start_response(status, headers)
-            return self.marshal(None, e)
-        finally:
-            destroy_context()
-        return response
-
     def listMethods(self, *params):
         return tuple(name.decode('UTF-8') for name in self.Command)
 
@@ -769,41 +777,13 @@ def __call__(self, environ, start_response):
         return response
 
 
-class jsonserver_kerb(jsonserver, KerberosSession):
+class jsonserver_kerb(jsonserver, KerberosWSGIExecutioner):
     """
     JSON RPC server protected with kerberos auth.
     """
 
     key = '/json'
 
-    def _on_finalize(self):
-        super(jsonserver_kerb, self)._on_finalize()
-        self.kerb_session_on_finalize()
-
-    def __call__(self, environ, start_response):
-        '''
-        '''
-
-        self.debug('WSGI jsonserver_kerb.__call__:')
-
-        user_ccache=environ.get('KRB5CCNAME')
-        if user_ccache is None:
-            self.internal_error(environ, start_response,
-                                'jsonserver_kerb.__call__: KRB5CCNAME not defined in HTTP request environment')
-            return self.marshal(None, CCacheError())
-        self.create_context(ccache=user_ccache)
-
-        try:
-            response = super(jsonserver_kerb, self).__call__(environ, start_response)
-            if (getattr(context, 'session_data', None) is None and
-                    self.env.context != 'lite'):
-                self.finalize_kerberos_acquisition('jsonserver', user_ccache,
-                                                environ, start_response)
-        finally:
-            destroy_context()
-
-        return response
-
 
 class login_kerberos(Backend, KerberosSession, HTTP_Status):
     key = '/session/login_kerberos'
-- 
1.8.3.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]