pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone

Blame SOURCES/0176-rpc-avoid-possible-recursion-in-create_connection.patch

483b06
From f5f7cbc63f9235ce040687cdf102aeaef69ab422 Mon Sep 17 00:00:00 2001
483b06
From: Stanislav Laznicka <slaznick@redhat.com>
483b06
Date: Thu, 1 Jun 2017 09:00:15 +0200
483b06
Subject: [PATCH] rpc: avoid possible recursion in create_connection
483b06
483b06
There was a recursion in RPCClient.create_connection() which under rare
483b06
circumstances would not have an ending condition. This commit removes
483b06
it and cleans up the code a bit as well.
483b06
483b06
https://pagure.io/freeipa/issue/6796
483b06
483b06
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
483b06
---
483b06
 ipalib/rpc.py | 140 +++++++++++++++++++++++++++++++---------------------------
483b06
 1 file changed, 74 insertions(+), 66 deletions(-)
483b06
483b06
diff --git a/ipalib/rpc.py b/ipalib/rpc.py
483b06
index b12ce4c5365299332587ad0d2990ca30070217bf..e3b8d67d69c084ad1a43390b5f93061826a27e1d 100644
483b06
--- a/ipalib/rpc.py
483b06
+++ b/ipalib/rpc.py
483b06
@@ -999,77 +999,85 @@ class RPCClient(Connectible):
483b06
             # No session key, do full Kerberos auth
483b06
             pass
483b06
         urls = self.get_url_list(rpc_uri)
483b06
-        serverproxy = None
483b06
+
483b06
+        proxy_kw = {
483b06
+            'allow_none': True,
483b06
+            'encoding': 'UTF-8',
483b06
+            'verbose': verbose
483b06
+        }
483b06
+
483b06
         for url in urls:
483b06
-            kw = {
483b06
-                'allow_none': True,
483b06
-                'encoding': 'UTF-8',
483b06
-                'verbose': verbose
483b06
-            }
483b06
-            if url.startswith('https://'):
483b06
-                if delegate:
483b06
-                    transport_class = DelegatedKerbTransport
483b06
+            # should we get ProtocolError (=> error in HTTP response) and
483b06
+            # 401 (=> Unauthorized), we'll be re-trying with new session
483b06
+            # cookies several times
483b06
+            for _try_num in range(0, 5):
483b06
+                if url.startswith('https://'):
483b06
+                    if delegate:
483b06
+                        transport_class = DelegatedKerbTransport
483b06
+                    else:
483b06
+                        transport_class = KerbTransport
483b06
                 else:
483b06
-                    transport_class = KerbTransport
483b06
-            else:
483b06
-                transport_class = LanguageAwareTransport
483b06
-            kw['transport'] = transport_class(protocol=self.protocol,
483b06
-                                              service='HTTP', ccache=ccache)
483b06
-            self.log.info('trying %s' % url)
483b06
-            setattr(context, 'request_url', url)
483b06
-            serverproxy = self.server_proxy_class(url, **kw)
483b06
-            if len(urls) == 1:
483b06
-                # if we have only 1 server and then let the
483b06
-                # main requester handle any errors. This also means it
483b06
-                # must handle a 401 but we save a ping.
483b06
-                return serverproxy
483b06
-            try:
483b06
-                command = getattr(serverproxy, 'ping')
483b06
+                    transport_class = LanguageAwareTransport
483b06
+                proxy_kw['transport'] = transport_class(
483b06
+                    protocol=self.protocol, service='HTTP', ccache=ccache)
483b06
+                self.log.info('trying %s' % url)
483b06
+                setattr(context, 'request_url', url)
483b06
+                serverproxy = self.server_proxy_class(url, **proxy_kw)
483b06
+                if len(urls) == 1:
483b06
+                    # if we have only 1 server and then let the
483b06
+                    # main requester handle any errors. This also means it
483b06
+                    # must handle a 401 but we save a ping.
483b06
+                    return serverproxy
483b06
                 try:
483b06
-                    command([], {})
483b06
-                except Fault as e:
483b06
-                    e = decode_fault(e)
483b06
-                    if e.faultCode in errors_by_code:
483b06
-                        error = errors_by_code[e.faultCode]
483b06
-                        raise error(message=e.faultString)
483b06
-                    else:
483b06
-                        raise UnknownError(
483b06
-                            code=e.faultCode,
483b06
-                            error=e.faultString,
483b06
-                            server=url,
483b06
-                        )
483b06
-                # We don't care about the response, just that we got one
483b06
-                break
483b06
-            except errors.KerberosError:
483b06
-                # kerberos error on one server is likely on all
483b06
-                raise
483b06
-            except ProtocolError as e:
483b06
-                if hasattr(context, 'session_cookie') and e.errcode == 401:
483b06
-                    # Unauthorized. Remove the session and try again.
483b06
-                    delattr(context, 'session_cookie')
483b06
+                    command = getattr(serverproxy, 'ping')
483b06
                     try:
483b06
-                        delete_persistent_client_session_data(principal)
483b06
-                    except Exception:
483b06
-                        # This shouldn't happen if we have a session but it isn't fatal.
483b06
-                        pass
483b06
-                    return self.create_connection(
483b06
-                        ccache, verbose, fallback, delegate)
483b06
-                if not fallback:
483b06
+                        command([], {})
483b06
+                    except Fault as e:
483b06
+                        e = decode_fault(e)
483b06
+                        if e.faultCode in errors_by_code:
483b06
+                            error = errors_by_code[e.faultCode]
483b06
+                            raise error(message=e.faultString)
483b06
+                        else:
483b06
+                            raise UnknownError(
483b06
+                                code=e.faultCode,
483b06
+                                error=e.faultString,
483b06
+                                server=url,
483b06
+                            )
483b06
+                    # We don't care about the response, just that we got one
483b06
+                    return serverproxy
483b06
+                except errors.KerberosError:
483b06
+                    # kerberos error on one server is likely on all
483b06
                     raise
483b06
-                else:
483b06
-                    self.log.info('Connection to %s failed with %s', url, e)
483b06
-                serverproxy = None
483b06
-            except Exception as e:
483b06
-                if not fallback:
483b06
-                    raise
483b06
-                else:
483b06
-                    self.log.info('Connection to %s failed with %s', url, e)
483b06
-                serverproxy = None
483b06
-
483b06
-        if serverproxy is None:
483b06
-            raise NetworkError(uri=_('any of the configured servers'),
483b06
-                error=', '.join(urls))
483b06
-        return serverproxy
483b06
+                except ProtocolError as e:
483b06
+                    if hasattr(context, 'session_cookie') and e.errcode == 401:
483b06
+                        # Unauthorized. Remove the session and try again.
483b06
+                        delattr(context, 'session_cookie')
483b06
+                        try:
483b06
+                            delete_persistent_client_session_data(principal)
483b06
+                        except Exception:
483b06
+                            # This shouldn't happen if we have a session but
483b06
+                            # it isn't fatal.
483b06
+                            pass
483b06
+                        # try the same url once more with a new session cookie
483b06
+                        continue
483b06
+                    if not fallback:
483b06
+                        raise
483b06
+                    else:
483b06
+                        self.log.info(
483b06
+                            'Connection to %s failed with %s', url, e)
483b06
+                    # try the next url
483b06
+                    break
483b06
+                except Exception as e:
483b06
+                    if not fallback:
483b06
+                        raise
483b06
+                    else:
483b06
+                        self.log.info(
483b06
+                            'Connection to %s failed with %s', url, e)
483b06
+                    # try the next url
483b06
+                    break
483b06
+        # finished all tries but no serverproxy was found
483b06
+        raise NetworkError(uri=_('any of the configured servers'),
483b06
+                           error=', '.join(urls))
483b06
 
483b06
     def destroy_connection(self):
483b06
         conn = getattr(context, self.id, None)
483b06
-- 
483b06
2.9.4
483b06