From a5d049e20bf28e80fdd62cea523a4a963e67b366 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Wed, 18 Mar 2015 08:00:45 -0700 Subject: [PATCH] Do not display InsecurePlatformWarning The current requests module issues InsecurePlatformWarning on what seems like every request. Perform duplicate suppresion on all warning messages. Additionally, never display this message to the user because there is almost nothing the user can do about it, and annoying them is not going to help. Log the message once. Also move the InsecureRequestWarning suppression to use the same new system. Log it once if ssl verification is disabled. Display the warning if it is not disabled. Change-Id: Icc84596dbc5f0e36a1af5c56215380b0f405613f --- gertty/app.py | 15 +++++++++++++++ gertty/requestsexceptions.py | 29 +++++++++++++++++++++++++++++ gertty/sync.py | 3 --- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 gertty/requestsexceptions.py diff --git a/gertty/app.py b/gertty/app.py index 58c6797..99f0122 100644 --- a/gertty/app.py +++ b/gertty/app.py @@ -36,6 +36,7 @@ from gertty import mywid from gertty import palette from gertty import sync from gertty import search +from gertty import requestsexceptions from gertty.view import change_list as view_change_list from gertty.view import project_list as view_project_list from gertty.view import change as view_change @@ -207,6 +208,7 @@ class App(object): self.sync_pipe = self.loop.watch_pipe(self.refresh) self.error_queue = Queue.Queue() self.error_pipe = self.loop.watch_pipe(self._errorPipeInput) + self.logged_warnings = set() warnings.showwarning = self._showWarning @@ -469,7 +471,20 @@ class App(object): def _showWarning(self, message, category, filename, lineno, file=None, line=None): + # Don't display repeat warnings + if str(message) in self.logged_warnings: + return m = warnings.formatwarning(message, category, filename, lineno, line) + self.log.warning(m) + self.logged_warnings.add(str(message)) + # Log this warning, but never display it to the user; it is + # nearly un-actionable. + if category == requestsexceptions.InsecurePlatformWarning: + return + # Disable InsecureRequestWarning when certificate validation is disabled + if not self.config.verify_ssl: + if category == requestsexceptions.InsecureRequestWarning: + return self.error_queue.put(('Warning', m)) os.write(self.error_pipe, 'error\n') diff --git a/gertty/requestsexceptions.py b/gertty/requestsexceptions.py new file mode 100644 index 0000000..aefb002 --- /dev/null +++ b/gertty/requestsexceptions.py @@ -0,0 +1,29 @@ +# Copyright 2015 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +try: + from requests.packages.urllib3.exceptions import InsecurePlatformWarning +except ImportError: + try: + from urllib3.exceptions import InsecurePlatformWarning + except ImportError: + InsecurePlatformWarning = None + +try: + from requests.packages.urllib3.exceptions import InsecureRequestWarning +except ImportError: + try: + from urllib3.exceptions import InsecureRequestWarning + except ImportError: + InsecureRequestWarning = None diff --git a/gertty/sync.py b/gertty/sync.py index cd0882d..b4673fb 100644 --- a/gertty/sync.py +++ b/gertty/sync.py @@ -887,9 +887,6 @@ class Sync(object): self.log = logging.getLogger('gertty.sync') self.queue = MultiQueue([HIGH_PRIORITY, NORMAL_PRIORITY, LOW_PRIORITY]) self.result_queue = Queue.Queue() - # Disable InsecureRequestWarning when certificate validation is disabled - if not self.app.config.verify_ssl: - requests.packages.urllib3.disable_warnings() self.session = requests.Session() if self.app.config.auth_type == 'basic': authclass = requests.auth.HTTPBasicAuth