Blob Blame History Raw
From a3b493a8accc338158faa53b9e221067323b75f5 Mon Sep 17 00:00:00 2001
From: Jake Hunsaker <jhunsake@redhat.com>
Date: Thu, 24 Sep 2020 10:06:17 -0400
Subject: [PATCH] [redhat] Ease upload url determination logic

The logic for determining if an archive should be uploaded to the
Customer Portal was too strict, ease it to now properly only block on a
missing case number since username and passwords may now be provided via
env vars.

Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
---
 sos/policies/__init__.py | 6 ++++--
 sos/policies/redhat.py   | 8 ++++++--
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py
index 9a1aac07..215739bd 100644
--- a/sos/policies/__init__.py
+++ b/sos/policies/__init__.py
@@ -1427,8 +1427,8 @@ class LinuxPolicy(Policy):
         """Should be overridden by policies to determine if a password needs to
         be provided for upload or not
         """
-        if ((not self.upload_password and not self._upload_password) and
-                self.upload_user):
+        if not self.get_upload_password() and (self.get_upload_user() !=
+                                               self._upload_user):
             msg = (
                 "Please provide the upload password for %s: "
                 % self.upload_user
@@ -1472,7 +1473,8 @@ class LinuxPolicy(Policy):
             Print a more human-friendly string than vendor URLs
         """
         self.upload_archive = archive
-        self.upload_url = self.get_upload_url()
+        if not self.upload_url:
+            self.upload_url = self.get_upload_url()
         if not self.upload_url:
             raise Exception("No upload destination provided by policy or by "
                             "--upload-url")
diff --git a/sos/policies/redhat.py b/sos/policies/redhat.py
index 34b421f3..f2f847a5 100644
--- a/sos/policies/redhat.py
+++ b/sos/policies/redhat.py
@@ -320,12 +320,16 @@ support representative.
                 "Enter your Red Hat Customer Portal username (empty to use "
                 "public dropbox): ")
             )
+            if not self.upload_user:
+                self.upload_url = RH_FTP_HOST
+                self.upload_user = self._upload_user
 
     def get_upload_url(self):
+        if self.upload_url:
+            return self.upload_url
         if self.commons['cmdlineopts'].upload_url:
             return self.commons['cmdlineopts'].upload_url
-        if (not self.case_id or not self.upload_user or not
-                self.upload_password):
+        if not self.case_id:
             # Cannot use the RHCP. Use anonymous dropbox
             self.upload_user = self._upload_user
             self.upload_directory = self._upload_directory
-- 
2.26.2

From 11cc6f478a9b41ce81b5b74faab5ca42930262ee Mon Sep 17 00:00:00 2001
From: Jake Hunsaker <jhunsake@redhat.com>
Date: Thu, 24 Sep 2020 10:17:25 -0400
Subject: [PATCH] [policy] Use user-provided FTP directory if specified

Fixes an issue whereby we ignore a user-provided FTP directory.

Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
---
 sos/policies/__init__.py | 2 +-
 sos/policies/redhat.py   | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py
index 215739bd..32f271d9 100644
--- a/sos/policies/__init__.py
+++ b/sos/policies/__init__.py
@@ -1677,7 +1677,7 @@ class LinuxPolicy(Policy):
             password = self.get_upload_password()
 
         if not directory:
-            directory = self._upload_directory
+            directory = self.upload_directory or self._upload_directory
 
         try:
             session = ftplib.FTP(url, user, password)
diff --git a/sos/policies/redhat.py b/sos/policies/redhat.py
index f2f847a5..d079406f 100644
--- a/sos/policies/redhat.py
+++ b/sos/policies/redhat.py
@@ -332,7 +332,8 @@ support representative.
         if not self.case_id:
             # Cannot use the RHCP. Use anonymous dropbox
             self.upload_user = self._upload_user
-            self.upload_directory = self._upload_directory
+            if self.upload_directory is None:
+                self.upload_directory = self._upload_directory
             self.upload_password = None
             return RH_FTP_HOST
         else:
-- 
2.26.2

From caa9a2f2a511689080d019ffab61a4de5787d8be Mon Sep 17 00:00:00 2001
From: Jake Hunsaker <jhunsake@redhat.com>
Date: Thu, 24 Sep 2020 10:25:00 -0400
Subject: [PATCH] [policy] Handle additional failure conditions for FTP uploads

Adds a timeout and a timeout handler for FTP connections, rather than
letting the connection attempt continue indefinitely.

Second, adds exception handling for an edge case where the connection to
the FTP server fails, but does not generate an exception from the ftplib
module.

Additionally, correct the type-ing of the error numbers being checked so
that we actually match them.

Resolves: #2245

Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
---
 sos/policies/__init__.py | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py
index 32f271d9..826d022e 100644
--- a/sos/policies/__init__.py
+++ b/sos/policies/__init__.py
@@ -1680,15 +1680,20 @@ class LinuxPolicy(Policy):
             directory = self.upload_directory or self._upload_directory
 
         try:
-            session = ftplib.FTP(url, user, password)
+            session = ftplib.FTP(url, user, password, timeout=15)
+            if not session:
+                raise Exception("connection failed, did you set a user and "
+                                "password?")
             session.cwd(directory)
+        except socket.timeout:
+            raise Exception("timeout hit while connecting to %s" % url)
         except socket.gaierror:
             raise Exception("unable to connect to %s" % url)
         except ftplib.error_perm as err:
             errno = str(err).split()[0]
-            if errno == 503:
+            if errno == '503':
                 raise Exception("could not login as '%s'" % user)
-            if errno == 550:
+            if errno == '550':
                 raise Exception("could not set upload directory to %s"
                                 % directory)
 
-- 
2.26.2

From 21720a0f8c9cf6739e26470b2280e005f0f3e3f1 Mon Sep 17 00:00:00 2001
From: Pavel Moravec <pmoravec@redhat.com>
Date: Thu, 15 Oct 2020 13:45:37 +0200
Subject: [PATCH] [policy] Use FTP server when user isnt set in batch mode

Caling "sos report --upload --case-id=123 --batch" should fallback
to uploading to FTP server as the upload user is unknown and can't
be prompted in batch mode.

Resolves: #2276

Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
---
 sos/policies/redhat.py | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/sos/policies/redhat.py b/sos/policies/redhat.py
index d079406f..3a65b9fa 100644
--- a/sos/policies/redhat.py
+++ b/sos/policies/redhat.py
@@ -324,13 +324,21 @@ support representative.
                 self.upload_url = RH_FTP_HOST
                 self.upload_user = self._upload_user
 
+    def _upload_user_set(self):
+        user = self.get_upload_user()
+        return user and (user != 'anonymous')
+
     def get_upload_url(self):
         if self.upload_url:
             return self.upload_url
         if self.commons['cmdlineopts'].upload_url:
             return self.commons['cmdlineopts'].upload_url
-        if not self.case_id:
-            # Cannot use the RHCP. Use anonymous dropbox
+        # anonymous FTP server should be used as fallback when either:
+        # - case id is not set, or
+        # - upload user isn't set AND batch mode prevents to prompt for it
+        if (not self.case_id) or \
+           ((not self._upload_user_set()) and
+               self.commons['cmdlineopts'].batch):
             self.upload_user = self._upload_user
             if self.upload_directory is None:
                 self.upload_directory = self._upload_directory
-- 
2.26.2