Blame SOURCES/sos-bz1886432-policy_fix_failure_conditions_with_upload.patch

c3caec
From 8288b5bb76d01c7fb88c51672bfb5d33e077d2d8 Mon Sep 17 00:00:00 2001
c3caec
From: Jan Jansky <jjansky@redhat.com>
c3caec
Date: Thu, 8 Oct 2020 14:26:03 +0200
c3caec
Subject: [PATCH] [policy] Fix failure conditions with upload
c3caec
c3caec
The logic for determining if an archive should be uploaded to the
c3caec
Customer Portal was too strict, ease it to now properly only block on a
c3caec
missing case number since username and passwords may now be provided via
c3caec
env vars.
c3caec
c3caec
Fixes an issue whereby we ignore a user-provided FTP directory.
c3caec
c3caec
Adds a timeout and a timeout handler for FTP connections, rather than
c3caec
letting the connection attempt continue indefinitely.
c3caec
c3caec
Second, adds exception handling for an edge case where the connection to
c3caec
the FTP server fails, but does not generate an exception from the ftplib
c3caec
module.
c3caec
c3caec
Additionally, correct the type-ing of the error numbers being checked so
c3caec
that we actually match them.
c3caec
c3caec
Caling "sos report --upload --case-id=123 --batch" should fallback
c3caec
to uploading to FTP server as the upload user is unknown and can't
c3caec
be prompted in batch mode.
c3caec
c3caec
Related: #2276
c3caec
Related: #2245
c3caec
Resolves: #2265
c3caec
c3caec
Signed-off-by: Jan Jansky <jjansky@redhat.com>
c3caec
Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
c3caec
---
c3caec
 sos/policies/__init__.py | 19 +++++++++++++------
c3caec
 sos/policies/redhat.py   | 21 +++++++++++++++++----
c3caec
 2 files changed, 30 insertions(+), 10 deletions(-)
c3caec
c3caec
diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py
c3caec
index ed3f0cc..a22c277 100644
c3caec
--- a/sos/policies/__init__.py
c3caec
+++ b/sos/policies/__init__.py
c3caec
@@ -972,7 +972,8 @@ class LinuxPolicy(Policy):
c3caec
         """Should be overridden by policies to determine if a user needs to
c3caec
         be provided or not
c3caec
         """
c3caec
-        if not self.upload_user and not self._upload_user:
c3caec
+        if not self.get_upload_password() and (self.get_upload_user() !=
c3caec
+                                               self._upload_user):
c3caec
             msg = "Please provide upload user for %s: " % self.get_upload_url()
c3caec
             self.upload_user = input(_(msg))
c3caec
 
c3caec
@@ -1029,7 +1030,8 @@ class LinuxPolicy(Policy):
c3caec
 
c3caec
         """
c3caec
         self.upload_archive = archive
c3caec
-        self.upload_url = self.get_upload_url()
c3caec
+        if not self.upload_url:
c3caec
+            self.upload_url = self.get_upload_url()
c3caec
         if not self.upload_url:
c3caec
             raise Exception("No upload destination provided by policy or by "
c3caec
                             "--upload-url")
c3caec
@@ -1187,18 +1189,23 @@ class LinuxPolicy(Policy):
c3caec
             password = self.get_upload_password()
c3caec
 
c3caec
         if not directory:
c3caec
-            directory = self._upload_directory
c3caec
+            directory = self.upload_directory or self._upload_directory
c3caec
 
c3caec
         try:
c3caec
-            session = ftplib.FTP(url, user, password)
c3caec
+            session = ftplib.FTP(url, user, password, timeout=15)
c3caec
+            if not session:
c3caec
+                raise Exception("connection failed, did you set a user and "
c3caec
+                                "password?")
c3caec
             session.cwd(directory)
c3caec
+        except socket.timeout:
c3caec
+            raise Exception("timeout hit while connecting to %s" % url)
c3caec
         except socket.gaierror:
c3caec
             raise Exception("unable to connect to %s" % url)
c3caec
         except ftplib.error_perm as err:
c3caec
             errno = str(err).split()[0]
c3caec
-            if errno == 503:
c3caec
+            if errno == '503':
c3caec
                 raise Exception("could not login as '%s'" % user)
c3caec
-            if errno == 550:
c3caec
+            if errno == '550':
c3caec
                 raise Exception("could not set upload directory to %s"
c3caec
                                 % directory)
c3caec
 
c3caec
diff --git a/sos/policies/redhat.py b/sos/policies/redhat.py
c3caec
index 9fbe743..3412f44 100644
c3caec
--- a/sos/policies/redhat.py
c3caec
+++ b/sos/policies/redhat.py
c3caec
@@ -312,15 +312,28 @@ support representative.
c3caec
                 "Enter your Red Hat Customer Portal username (empty to use "
c3caec
                 "public dropbox): ")
c3caec
             )
c3caec
+            if not self.upload_user:
c3caec
+                self.upload_url = RH_FTP_HOST
c3caec
+                self.upload_user = self._upload_user
c3caec
+
c3caec
+    def _upload_user_set(self):
c3caec
+        user = self.get_upload_user()
c3caec
+        return user and (user != 'anonymous')
c3caec
 
c3caec
     def get_upload_url(self):
c3caec
+        if self.upload_url:
c3caec
+            return self.upload_url
c3caec
         if self.commons['cmdlineopts'].upload_url:
c3caec
             return self.commons['cmdlineopts'].upload_url
c3caec
-        if (not self.case_id or not self.upload_user or not
c3caec
-                self.upload_password):
c3caec
-            # Cannot use the RHCP. Use anonymous dropbox
c3caec
+        # anonymous FTP server should be used as fallback when either:
c3caec
+        # - case id is not set, or
c3caec
+        # - upload user isn't set AND batch mode prevents to prompt for it
c3caec
+        if (not self.case_id) or \
c3caec
+           ((not self._upload_user_set()) and
c3caec
+               self.commons['cmdlineopts'].batch):
c3caec
             self.upload_user = self._upload_user
c3caec
-            self.upload_directory = self._upload_directory
c3caec
+            if self.upload_directory is None:
c3caec
+                self.upload_directory = self._upload_directory
c3caec
             self.upload_password = None
c3caec
             return RH_FTP_HOST
c3caec
         else:
c3caec
-- 
c3caec
1.8.3.1
c3caec