Blame SOURCES/sos-bz1665947-rhui-plugin.patch

89c8f0
From 94b9b90c818eb18f0ca8d78fe063dc5b0677c885 Mon Sep 17 00:00:00 2001
89c8f0
From: Pavel Moravec <pmoravec@redhat.com>
89c8f0
Date: Tue, 22 Jun 2021 12:58:03 +0200
89c8f0
Subject: [PATCH] [rhui] add plugin to RHUI
89c8f0
89c8f0
Add a new/revoked plugin for RHUI (newly based on python3 and pulp-3).
89c8f0
89c8f0
Edditionally, collect /etc/pki/pulp certificates except for RSA keys.
89c8f0
89c8f0
Resolves: #2590
89c8f0
89c8f0
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
89c8f0
---
89c8f0
 sos/report/plugins/pulpcore.py |  7 ++++-
89c8f0
 sos/report/plugins/rhui.py     | 49 ++++++++++++++++++++++++++++++++++
89c8f0
 2 files changed, 55 insertions(+), 1 deletion(-)
89c8f0
 create mode 100644 sos/report/plugins/rhui.py
89c8f0
89c8f0
diff --git a/sos/report/plugins/pulpcore.py b/sos/report/plugins/pulpcore.py
89c8f0
index ccaac3185..77ceacb92 100644
89c8f0
--- a/sos/report/plugins/pulpcore.py
89c8f0
+++ b/sos/report/plugins/pulpcore.py
89c8f0
@@ -77,7 +77,12 @@ def separate_value(line, sep=':'):
89c8f0
     def setup(self):
89c8f0
         self.parse_settings_config()
89c8f0
 
89c8f0
-        self.add_copy_spec("/etc/pulp/settings.py")
89c8f0
+        self.add_copy_spec([
89c8f0
+            "/etc/pulp/settings.py",
89c8f0
+            "/etc/pki/pulp/*"
89c8f0
+        ])
89c8f0
+        # skip collecting certificate keys
89c8f0
+        self.add_forbidden_path("/etc/pki/pulp/*.key")
89c8f0
 
89c8f0
         self.add_cmd_output("rq info -u redis://localhost:6379/8",
89c8f0
                             env={"LC_ALL": "en_US.UTF-8"},
89c8f0
diff --git a/sos/report/plugins/rhui.py b/sos/report/plugins/rhui.py
89c8f0
new file mode 100644
89c8f0
index 000000000..7acd3f49e
89c8f0
--- /dev/null
89c8f0
+++ b/sos/report/plugins/rhui.py
89c8f0
@@ -0,0 +1,49 @@
89c8f0
+# Copyright (C) 2021 Red Hat, Inc., Pavel Moravec <pmoravec@redhat.com>
89c8f0
+
89c8f0
+# This file is part of the sos project: https://github.com/sosreport/sos
89c8f0
+#
89c8f0
+# This copyrighted material is made available to anyone wishing to use,
89c8f0
+# modify, copy, or redistribute it subject to the terms and conditions of
89c8f0
+# version 2 of the GNU General Public License.
89c8f0
+#
89c8f0
+# See the LICENSE file in the source distribution for further information.
89c8f0
+
89c8f0
+from sos.report.plugins import Plugin, RedHatPlugin
89c8f0
+
89c8f0
+
89c8f0
+class Rhui(Plugin, RedHatPlugin):
89c8f0
+
89c8f0
+    short_desc = 'Red Hat Update Infrastructure'
89c8f0
+
89c8f0
+    plugin_name = "rhui"
89c8f0
+    commands = ("rhui-manager",)
89c8f0
+    files = ("/etc/ansible/facts.d/rhui_auth.fact", "/usr/lib/rhui/cds.py")
89c8f0
+
89c8f0
+    def setup(self):
89c8f0
+        self.add_copy_spec([
89c8f0
+            "/etc/rhui/rhui-tools.conf",
89c8f0
+            "/etc/rhui/registered_subscriptions.conf",
89c8f0
+            "/etc/pki/rhui/*",
89c8f0
+            "/var/log/rhui-subscription-sync.log",
89c8f0
+            "/var/cache/rhui/*",
89c8f0
+            "/root/.rhui/*",
89c8f0
+        ])
89c8f0
+        # skip collecting certificate keys
89c8f0
+        self.add_forbidden_path("/etc/pki/rhui/*.key")
89c8f0
+
89c8f0
+        self.add_cmd_output([
89c8f0
+            "rhui-manager status",
89c8f0
+            "rhui-manager cert info",
89c8f0
+            "ls -lR /var/lib/rhui/remote_share",
89c8f0
+        ])
89c8f0
+
89c8f0
+    def postproc(self):
89c8f0
+        # obfuscate admin_pw and secret_key values
89c8f0
+        for prop in ["admin_pw", "secret_key"]:
89c8f0
+            self.do_path_regex_sub(
89c8f0
+                "/etc/ansible/facts.d/rhui_auth.fact",
89c8f0
+                r"(%s\s*=\s*)(.*)" % prop,
89c8f0
+                r"\1********")
89c8f0
+
89c8f0
+
89c8f0
+# vim: set et ts=4 sw=4 :
89c8f0
From bd15dc764c9d4554d8e8f08163228d65ca099985 Mon Sep 17 00:00:00 2001
89c8f0
From: Pavel Moravec <pmoravec@redhat.com>
89c8f0
Date: Thu, 24 Jun 2021 17:53:27 +0200
89c8f0
Subject: [PATCH 1/4] [plugins] Allow add_forbidden_path to apply glob
89c8f0
 recursively
89c8f0
89c8f0
Add option to apply glob.glob to forbidden path recursively.
89c8f0
89c8f0
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
89c8f0
---
89c8f0
 sos/report/plugins/__init__.py | 6 ++++--
89c8f0
 1 file changed, 4 insertions(+), 2 deletions(-)
89c8f0
89c8f0
diff --git a/sos/report/plugins/__init__.py b/sos/report/plugins/__init__.py
89c8f0
index 06923300..6fd1a3b2 100644
89c8f0
--- a/sos/report/plugins/__init__.py
89c8f0
+++ b/sos/report/plugins/__init__.py
89c8f0
@@ -1187,12 +1187,14 @@ class Plugin(object):
89c8f0
             'symlink': "no"
89c8f0
         })
89c8f0
 
89c8f0
-    def add_forbidden_path(self, forbidden):
89c8f0
+    def add_forbidden_path(self, forbidden, recursive=False):
89c8f0
         """Specify a path, or list of paths, to not copy, even if it's part of
89c8f0
         an ``add_copy_spec()`` call
89c8f0
 
89c8f0
         :param forbidden: A filepath to forbid collection from
89c8f0
         :type forbidden: ``str`` or a ``list`` of strings
89c8f0
+
89c8f0
+        :param recursive: Should forbidden glob be applied recursively
89c8f0
         """
89c8f0
         if isinstance(forbidden, str):
89c8f0
             forbidden = [forbidden]
89c8f0
@@ -1202,7 +1204,7 @@ class Plugin(object):
89c8f0
 
89c8f0
         for forbid in forbidden:
89c8f0
             self._log_info("adding forbidden path '%s'" % forbid)
89c8f0
-            for path in glob.glob(forbid):
89c8f0
+            for path in glob.glob(forbid, recursive=recursive):
89c8f0
                 self.forbidden_paths.append(path)
89c8f0
 
89c8f0
     def get_all_options(self):
89c8f0
-- 
89c8f0
2.31.1
89c8f0
89c8f0
89c8f0
From b695201baeb629a6543445d98dbb04f357670621 Mon Sep 17 00:00:00 2001
89c8f0
From: Pavel Moravec <pmoravec@redhat.com>
89c8f0
Date: Thu, 24 Jun 2021 17:57:48 +0200
89c8f0
Subject: [PATCH 2/4] [pulpcore] improve settings.py parsing
89c8f0
89c8f0
- deal with /etc/pulp/settings.py as a one-line string
89c8f0
- parse dbname from it as well
89c8f0
- dont collect any *.key file from whole /etc/pki/pulp dir
89c8f0
89c8f0
Related: #2593
89c8f0
89c8f0
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
89c8f0
---
89c8f0
 sos/report/plugins/pulpcore.py | 23 +++++++++++++++--------
89c8f0
 1 file changed, 15 insertions(+), 8 deletions(-)
89c8f0
89c8f0
diff --git a/sos/report/plugins/pulpcore.py b/sos/report/plugins/pulpcore.py
89c8f0
index 77ceacb9..be526035 100644
89c8f0
--- a/sos/report/plugins/pulpcore.py
89c8f0
+++ b/sos/report/plugins/pulpcore.py
89c8f0
@@ -28,9 +28,10 @@ class PulpCore(Plugin, IndependentPlugin):
89c8f0
         databases_scope = False
89c8f0
         self.dbhost = "localhost"
89c8f0
         self.dbport = 5432
89c8f0
+        self.dbname = "pulpcore"
89c8f0
         self.dbpasswd = ""
89c8f0
         # TODO: read also redis config (we dont expect much customisations)
89c8f0
-        # TODO: read also db user (pulp) and database name (pulpcore)
89c8f0
+        # TODO: read also db user (pulp)
89c8f0
         self.staticroot = "/var/lib/pulp/assets"
89c8f0
         self.uploaddir = "/var/lib/pulp/media/upload"
89c8f0
 
89c8f0
@@ -44,7 +45,10 @@ class PulpCore(Plugin, IndependentPlugin):
89c8f0
             return val
89c8f0
 
89c8f0
         try:
89c8f0
-            for line in open("/etc/pulp/settings.py").read().splitlines():
89c8f0
+            # split the lines to "one option per line" format
89c8f0
+            for line in open("/etc/pulp/settings.py").read() \
89c8f0
+                    .replace(',', ',\n').replace('{', '{\n') \
89c8f0
+                    .replace('}', '\n}').splitlines():
89c8f0
                 # skip empty lines and lines with comments
89c8f0
                 if not line or line[0] == '#':
89c8f0
                     continue
89c8f0
@@ -53,11 +57,14 @@ class PulpCore(Plugin, IndependentPlugin):
89c8f0
                     continue
89c8f0
                 # example HOST line to parse:
89c8f0
                 #         'HOST': 'localhost',
89c8f0
-                if databases_scope and match(r"\s+'HOST'\s*:\s+\S+", line):
89c8f0
+                pattern = r"\s*['|\"]%s['|\"]\s*:\s*\S+"
89c8f0
+                if databases_scope and match(pattern % 'HOST', line):
89c8f0
                     self.dbhost = separate_value(line)
89c8f0
-                if databases_scope and match(r"\s+'PORT'\s*:\s+\S+", line):
89c8f0
+                if databases_scope and match(pattern % 'PORT', line):
89c8f0
                     self.dbport = separate_value(line)
89c8f0
-                if databases_scope and match(r"\s+'PASSWORD'\s*:\s+\S+", line):
89c8f0
+                if databases_scope and match(pattern % 'NAME', line):
89c8f0
+                    self.dbname = separate_value(line)
89c8f0
+                if databases_scope and match(pattern % 'PASSWORD', line):
89c8f0
                     self.dbpasswd = separate_value(line)
89c8f0
                 # if line contains closing '}' database_scope end
89c8f0
                 if databases_scope and '}' in line:
89c8f0
@@ -82,7 +89,7 @@ class PulpCore(Plugin, IndependentPlugin):
89c8f0
             "/etc/pki/pulp/*"
89c8f0
         ])
89c8f0
         # skip collecting certificate keys
89c8f0
-        self.add_forbidden_path("/etc/pki/pulp/*.key")
89c8f0
+        self.add_forbidden_path("/etc/pki/pulp/**/*.key", recursive=True)
89c8f0
 
89c8f0
         self.add_cmd_output("rq info -u redis://localhost:6379/8",
89c8f0
                             env={"LC_ALL": "en_US.UTF-8"},
89c8f0
@@ -104,8 +111,8 @@ class PulpCore(Plugin, IndependentPlugin):
89c8f0
             _query = "select * from %s where pulp_last_updated > NOW() - " \
89c8f0
                      "interval '%s days' order by pulp_last_updated" % \
89c8f0
                      (table, task_days)
89c8f0
-            _cmd = "psql -h %s -p %s -U pulp -d pulpcore -c %s" % \
89c8f0
-                   (self.dbhost, self.dbport, quote(_query))
89c8f0
+            _cmd = "psql -h %s -p %s -U pulp -d %s -c %s" % \
89c8f0
+                   (self.dbhost, self.dbport, self.dbname, quote(_query))
89c8f0
             self.add_cmd_output(_cmd, env=self.env, suggest_filename=table)
89c8f0
 
89c8f0
     def postproc(self):
89c8f0
-- 
89c8f0
2.31.1
89c8f0
89c8f0
89c8f0
From 0286034da44bce43ab368dfc6815da7d74d60719 Mon Sep 17 00:00:00 2001
89c8f0
From: Pavel Moravec <pmoravec@redhat.com>
89c8f0
Date: Thu, 24 Jun 2021 17:59:36 +0200
89c8f0
Subject: [PATCH 3/4] [rhui] call rhui-* commands with proper env and timeout
89c8f0
89c8f0
rhui-manager commands timeout when not being logged in, which
89c8f0
should be reacted by adding proper cmd timeout.
89c8f0
89c8f0
Adding the env.variable ensures potentially unaswered "RHUI Username:"
89c8f0
is also printed/colected.
89c8f0
89c8f0
Further, prevent collecting any *.key file from the whole /etc/pki/rhui
89c8f0
dir.
89c8f0
89c8f0
Related: #2593
89c8f0
89c8f0
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
89c8f0
---
89c8f0
 sos/report/plugins/rhui.py | 7 +++++--
89c8f0
 1 file changed, 5 insertions(+), 2 deletions(-)
89c8f0
89c8f0
diff --git a/sos/report/plugins/rhui.py b/sos/report/plugins/rhui.py
89c8f0
index 7acd3f49..5a152427 100644
89c8f0
--- a/sos/report/plugins/rhui.py
89c8f0
+++ b/sos/report/plugins/rhui.py
89c8f0
@@ -29,13 +29,16 @@ class Rhui(Plugin, RedHatPlugin):
89c8f0
             "/root/.rhui/*",
89c8f0
         ])
89c8f0
         # skip collecting certificate keys
89c8f0
-        self.add_forbidden_path("/etc/pki/rhui/*.key")
89c8f0
+        self.add_forbidden_path("/etc/pki/rhui/**/*.key", recursive=True)
89c8f0
 
89c8f0
+        # call rhui-manager commands with 1m timeout and
89c8f0
+        # with an env. variable ensuring that "RHUI Username:"
89c8f0
+        # even unanswered prompt gets collected
89c8f0
         self.add_cmd_output([
89c8f0
             "rhui-manager status",
89c8f0
             "rhui-manager cert info",
89c8f0
             "ls -lR /var/lib/rhui/remote_share",
89c8f0
-        ])
89c8f0
+        ], timeout=60, env={'PYTHONUNBUFFERED': '1'})
89c8f0
 
89c8f0
     def postproc(self):
89c8f0
         # obfuscate admin_pw and secret_key values
89c8f0
-- 
89c8f0
2.31.1
89c8f0
89c8f0
89c8f0
From a656bd239ab86dfd8973f733ae2c0fbd0c57d416 Mon Sep 17 00:00:00 2001
89c8f0
From: Pavel Moravec <pmoravec@redhat.com>
89c8f0
Date: Thu, 24 Jun 2021 18:01:14 +0200
89c8f0
Subject: [PATCH 4/4] [rhui] fix broken obfuscation
89c8f0
89c8f0
- /etc/ansible/facts.d/rhui_*.fact must be collected by
89c8f0
rhui plugin to let some file to be obfuscated there
89c8f0
- obfuscate also cookies values that can grant login access
89c8f0
89c8f0
Resolves: #2593
89c8f0
89c8f0
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
89c8f0
---
89c8f0
 sos/report/plugins/ansible.py | 3 +++
89c8f0
 sos/report/plugins/rhui.py    | 7 +++++++
89c8f0
 2 files changed, 10 insertions(+)
89c8f0
89c8f0
diff --git a/sos/report/plugins/ansible.py b/sos/report/plugins/ansible.py
89c8f0
index 3e5d3d37..5991b786 100644
89c8f0
--- a/sos/report/plugins/ansible.py
89c8f0
+++ b/sos/report/plugins/ansible.py
89c8f0
@@ -29,4 +29,7 @@ class Ansible(Plugin, RedHatPlugin, UbuntuPlugin):
89c8f0
             "ansible --version"
89c8f0
         ])
89c8f0
 
89c8f0
+        # let rhui plugin collects the RHUI specific files
89c8f0
+        self.add_forbidden_path("/etc/ansible/facts.d/rhui_*.fact")
89c8f0
+
89c8f0
 # vim: set et ts=4 sw=4 :
89c8f0
diff --git a/sos/report/plugins/rhui.py b/sos/report/plugins/rhui.py
89c8f0
index 5a152427..1d479f85 100644
89c8f0
--- a/sos/report/plugins/rhui.py
89c8f0
+++ b/sos/report/plugins/rhui.py
89c8f0
@@ -27,6 +27,7 @@ class Rhui(Plugin, RedHatPlugin):
89c8f0
             "/var/log/rhui-subscription-sync.log",
89c8f0
             "/var/cache/rhui/*",
89c8f0
             "/root/.rhui/*",
89c8f0
+            "/etc/ansible/facts.d/rhui_*.fact",
89c8f0
         ])
89c8f0
         # skip collecting certificate keys
89c8f0
         self.add_forbidden_path("/etc/pki/rhui/**/*.key", recursive=True)
89c8f0
@@ -47,6 +48,12 @@ class Rhui(Plugin, RedHatPlugin):
89c8f0
                 "/etc/ansible/facts.d/rhui_auth.fact",
89c8f0
                 r"(%s\s*=\s*)(.*)" % prop,
89c8f0
                 r"\1********")
89c8f0
+        # obfuscate twoo cookies for login session
89c8f0
+        for cookie in ["csrftoken", "sessionid"]:
89c8f0
+            self.do_path_regex_sub(
89c8f0
+                r"/root/\.rhui/.*/cookies.txt",
89c8f0
+                r"(%s\s+)(\S+)" % cookie,
89c8f0
+                r"\1********")
89c8f0
 
89c8f0
 
89c8f0
 # vim: set et ts=4 sw=4 :
89c8f0
-- 
89c8f0
2.31.1
89c8f0
89c8f0
From 4e5bebffca9936bcdf4d38aad9989970a15dd72b Mon Sep 17 00:00:00 2001
89c8f0
From: Pavel Moravec <pmoravec@redhat.com>
89c8f0
Date: Tue, 3 Aug 2021 21:54:33 +0200
89c8f0
Subject: [PATCH] [rhui] Update the plugin on several places
89c8f0
89c8f0
- obfuscate "rhui_manager_password: xxx" in /root/.rhui/answers.yaml*
89c8f0
- no need to collect or obfuscate anything from /etc/ansible/facts.d
89c8f0
- newly detect the plugin via /etc/rhui/rhui-tools.conf file or rhui-manager
89c8f0
  command (only)
89c8f0
89c8f0
Resolves: #2637
89c8f0
89c8f0
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
89c8f0
---
89c8f0
 sos/report/plugins/rhui.py | 14 ++++++--------
89c8f0
 1 file changed, 6 insertions(+), 8 deletions(-)
89c8f0
89c8f0
diff --git a/sos/report/plugins/rhui.py b/sos/report/plugins/rhui.py
89c8f0
index 1d479f85..52065fb4 100644
89c8f0
--- a/sos/report/plugins/rhui.py
89c8f0
+++ b/sos/report/plugins/rhui.py
89c8f0
@@ -16,8 +16,8 @@ class Rhui(Plugin, RedHatPlugin):
89c8f0
     short_desc = 'Red Hat Update Infrastructure'
89c8f0
 
89c8f0
     plugin_name = "rhui"
89c8f0
-    commands = ("rhui-manager",)
89c8f0
-    files = ("/etc/ansible/facts.d/rhui_auth.fact", "/usr/lib/rhui/cds.py")
89c8f0
+    commands = ("rhui-manager", )
89c8f0
+    files = ("/etc/rhui/rhui-tools.conf", )
89c8f0
 
89c8f0
     def setup(self):
89c8f0
         self.add_copy_spec([
89c8f0
@@ -27,7 +27,6 @@ class Rhui(Plugin, RedHatPlugin):
89c8f0
             "/var/log/rhui-subscription-sync.log",
89c8f0
             "/var/cache/rhui/*",
89c8f0
             "/root/.rhui/*",
89c8f0
-            "/etc/ansible/facts.d/rhui_*.fact",
89c8f0
         ])
89c8f0
         # skip collecting certificate keys
89c8f0
         self.add_forbidden_path("/etc/pki/rhui/**/*.key", recursive=True)
89c8f0
@@ -42,11 +41,10 @@ class Rhui(Plugin, RedHatPlugin):
89c8f0
         ], timeout=60, env={'PYTHONUNBUFFERED': '1'})
89c8f0
 
89c8f0
     def postproc(self):
89c8f0
-        # obfuscate admin_pw and secret_key values
89c8f0
-        for prop in ["admin_pw", "secret_key"]:
89c8f0
-            self.do_path_regex_sub(
89c8f0
-                "/etc/ansible/facts.d/rhui_auth.fact",
89c8f0
-                r"(%s\s*=\s*)(.*)" % prop,
89c8f0
+        # hide rhui_manager_password value in (also rotated) answers file
89c8f0
+        self.do_path_regex_sub(
89c8f0
+                r"/root/\.rhui/answers.yaml.*",
89c8f0
+                r"(\s*rhui_manager_password\s*:)\s*(\S+)",
89c8f0
                 r"\1********")
89c8f0
         # obfuscate twoo cookies for login session
89c8f0
         for cookie in ["csrftoken", "sessionid"]:
89c8f0
-- 
89c8f0
2.31.1
89c8f0