Blame SOURCES/oscap-anaconda-addon-1.2.2-absent_appstream-PR_184.patch

f4bcdd
From 8eacfad08b3c27aa9510f2c3337356581bd9bebd Mon Sep 17 00:00:00 2001
f4bcdd
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
f4bcdd
Date: Mon, 3 Jan 2022 17:31:49 +0100
f4bcdd
Subject: [PATCH 1/3] Add oscap sanity check before attempting remediation
f4bcdd
f4bcdd
If something is obviously wrong with the scanner, then don't attempt to remediate
f4bcdd
and try to show relevant information in a dialog window.
f4bcdd
---
f4bcdd
 org_fedora_oscap/common.py   | 39 ++++++++++++++++++++++++++++--------
f4bcdd
 org_fedora_oscap/ks/oscap.py | 11 ++++++++++
f4bcdd
 tests/test_common.py         |  8 ++++++++
f4bcdd
 3 files changed, 50 insertions(+), 8 deletions(-)
f4bcdd
f4bcdd
diff --git a/org_fedora_oscap/common.py b/org_fedora_oscap/common.py
f4bcdd
index 884bbc8..05829ce 100644
f4bcdd
--- a/org_fedora_oscap/common.py
f4bcdd
+++ b/org_fedora_oscap/common.py
f4bcdd
@@ -139,7 +139,8 @@ def execute(self, ** kwargs):
f4bcdd
             proc = subprocess.Popen(self.args, stdout=subprocess.PIPE,
f4bcdd
                                     stderr=subprocess.PIPE, ** kwargs)
f4bcdd
         except OSError as oserr:
f4bcdd
-            msg = "Failed to run the oscap tool: %s" % oserr
f4bcdd
+            msg = ("Failed to execute command '{command_string}': {oserr}"
f4bcdd
+                   .format(command_string=command_string, oserr=oserr))
f4bcdd
             raise OSCAPaddonError(msg)
f4bcdd
 
f4bcdd
         (stdout, stderr) = proc.communicate()
f4bcdd
@@ -215,6 +216,34 @@ def _run_oscap_gen_fix(profile, fpath, template, ds_id="", xccdf_id="",
f4bcdd
     return proc.stdout
f4bcdd
 
f4bcdd
 
f4bcdd
+def do_chroot(chroot):
f4bcdd
+    """Helper function doing the chroot if requested."""
f4bcdd
+    if chroot and chroot != "/":
f4bcdd
+        os.chroot(chroot)
f4bcdd
+        os.chdir("/")
f4bcdd
+
f4bcdd
+
f4bcdd
+def assert_scanner_works(chroot, executable="oscap"):
f4bcdd
+    args = [executable, "--version"]
f4bcdd
+    command = " ".join(args)
f4bcdd
+
f4bcdd
+    try:
f4bcdd
+        proc = subprocess.Popen(
f4bcdd
+            args, preexec_fn=lambda: do_chroot(chroot),
f4bcdd
+            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
f4bcdd
+        (stdout, stderr) = proc.communicate()
f4bcdd
+        stderr = stderr.decode(errors="replace")
f4bcdd
+    except OSError as exc:
f4bcdd
+        msg = _(f"Basic invocation '{command}' fails: {str(exc)}")
f4bcdd
+        raise OSCAPaddonError(msg)
f4bcdd
+    if proc.returncode != 0:
f4bcdd
+        msg = _(
f4bcdd
+            f"Basic scanner invocation '{command}' exited "
f4bcdd
+            "with non-zero error code {proc.returncode}: {stderr}")
f4bcdd
+        raise OSCAPaddonError(msg)
f4bcdd
+    return True
f4bcdd
+
f4bcdd
+
f4bcdd
 def run_oscap_remediate(profile, fpath, ds_id="", xccdf_id="", tailoring="",
f4bcdd
                         chroot=""):
f4bcdd
     """
f4bcdd
@@ -244,12 +273,6 @@ def run_oscap_remediate(profile, fpath, ds_id="", xccdf_id="", tailoring="",
f4bcdd
     if not profile:
f4bcdd
         return ""
f4bcdd
 
f4bcdd
-    def do_chroot():
f4bcdd
-        """Helper function doing the chroot if requested."""
f4bcdd
-        if chroot and chroot != "/":
f4bcdd
-            os.chroot(chroot)
f4bcdd
-            os.chdir("/")
f4bcdd
-
f4bcdd
     # make sure the directory for the results exists
f4bcdd
     results_dir = os.path.dirname(RESULTS_PATH)
f4bcdd
     if chroot:
f4bcdd
@@ -274,7 +297,7 @@ def do_chroot():
f4bcdd
     args.append(fpath)
f4bcdd
 
f4bcdd
     proc = SubprocessLauncher(args)
f4bcdd
-    proc.execute(preexec_fn=do_chroot)
f4bcdd
+    proc.execute(preexec_fn=lambda: do_chroot(chroot))
f4bcdd
     proc.log_messages()
f4bcdd
 
f4bcdd
     if proc.returncode not in (0, 2):
f4bcdd
diff --git a/org_fedora_oscap/ks/oscap.py b/org_fedora_oscap/ks/oscap.py
f4bcdd
index 65d74cf..da1600f 100644
f4bcdd
--- a/org_fedora_oscap/ks/oscap.py
f4bcdd
+++ b/org_fedora_oscap/ks/oscap.py
f4bcdd
@@ -488,6 +488,17 @@ def execute(self, storage, ksdata, users, payload):
f4bcdd
             # selected
f4bcdd
             return
f4bcdd
 
f4bcdd
+        try:
f4bcdd
+            common.assert_scanner_works(
f4bcdd
+                chroot=conf.target.system_root, executable="oscap")
f4bcdd
+        except Exception as exc:
f4bcdd
+            msg_lines = [_(
f4bcdd
+                "The 'oscap' scanner doesn't work in the installed system: {error}"
f4bcdd
+                .format(error=str(exc)))]
f4bcdd
+            msg_lines.append(_("As a result, the installed system can't be hardened."))
f4bcdd
+            self._terminate("\n".join(msg_lines))
f4bcdd
+            return
f4bcdd
+
f4bcdd
         target_content_dir = utils.join_paths(conf.target.system_root,
f4bcdd
                                               common.TARGET_CONTENT_DIR)
f4bcdd
         utils.ensure_dir_exists(target_content_dir)
f4bcdd
diff --git a/tests/test_common.py b/tests/test_common.py
f4bcdd
index 9f7a16a..4f25379 100644
f4bcdd
--- a/tests/test_common.py
f4bcdd
+++ b/tests/test_common.py
f4bcdd
@@ -77,6 +77,14 @@ def _run_oscap(mock_subprocess, additional_args):
f4bcdd
     return expected_args, kwargs
f4bcdd
 
f4bcdd
 
f4bcdd
+def test_oscap_works():
f4bcdd
+    assert common.assert_scanner_works(chroot="/")
f4bcdd
+    with pytest.raises(common.OSCAPaddonError, match="No such file"):
f4bcdd
+        common.assert_scanner_works(chroot="/", executable="i_dont_exist")
f4bcdd
+    with pytest.raises(common.OSCAPaddonError, match="non-zero"):
f4bcdd
+        common.assert_scanner_works(chroot="/", executable="false")
f4bcdd
+
f4bcdd
+
f4bcdd
 def test_run_oscap_remediate_profile_only(mock_subprocess, monkeypatch):
f4bcdd
     return run_oscap_remediate_profile(
f4bcdd
         mock_subprocess, monkeypatch,
f4bcdd
f4bcdd
From b54cf2bddba56e5b776fb60514a5e29d47c74cac Mon Sep 17 00:00:00 2001
f4bcdd
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
f4bcdd
Date: Mon, 3 Jan 2022 17:42:31 +0100
f4bcdd
Subject: [PATCH 2/3] Don't raise exceptions in execute()
f4bcdd
f4bcdd
Those result in tracebacks during the installation,
f4bcdd
while a dialog window presents a more useful form of user interaction.
f4bcdd
---
f4bcdd
 org_fedora_oscap/ks/oscap.py | 18 ++++++++++++------
f4bcdd
 1 file changed, 12 insertions(+), 6 deletions(-)
f4bcdd
f4bcdd
diff --git a/org_fedora_oscap/ks/oscap.py b/org_fedora_oscap/ks/oscap.py
f4bcdd
index da1600f..d3f0dbe 100644
f4bcdd
--- a/org_fedora_oscap/ks/oscap.py
f4bcdd
+++ b/org_fedora_oscap/ks/oscap.py
f4bcdd
@@ -513,8 +513,9 @@ def execute(self, storage, ksdata, users, payload):
f4bcdd
             ret = util.execInSysroot("yum", ["-y", "--nogpg", "install",
f4bcdd
                                              self.raw_postinst_content_path])
f4bcdd
             if ret != 0:
f4bcdd
-                raise common.ExtractionError("Failed to install content "
f4bcdd
-                                             "RPM to the target system")
f4bcdd
+                msg = _(f"Failed to install content RPM to the target system.")
f4bcdd
+                self._terminate(msg)
f4bcdd
+                return
f4bcdd
         elif self.content_type == "scap-security-guide":
f4bcdd
             # nothing needed
f4bcdd
             pass
f4bcdd
@@ -525,10 +526,15 @@ def execute(self, storage, ksdata, users, payload):
f4bcdd
         if os.path.exists(self.preinst_tailoring_path):
f4bcdd
             shutil.copy2(self.preinst_tailoring_path, target_content_dir)
f4bcdd
 
f4bcdd
-        common.run_oscap_remediate(self.profile_id, self.postinst_content_path,
f4bcdd
-                                   self.datastream_id, self.xccdf_id,
f4bcdd
-                                   self.postinst_tailoring_path,
f4bcdd
-                                   chroot=conf.target.system_root)
f4bcdd
+        try:
f4bcdd
+            common.run_oscap_remediate(self.profile_id, self.postinst_content_path,
f4bcdd
+                                       self.datastream_id, self.xccdf_id,
f4bcdd
+                                       self.postinst_tailoring_path,
f4bcdd
+                                       chroot=conf.target.system_root)
f4bcdd
+        except Exception as exc:
f4bcdd
+            msg = _(f"Something went wrong during the final hardening: {str(exc)}.")
f4bcdd
+            self._terminate(msg)
f4bcdd
+            return
f4bcdd
 
f4bcdd
     def clear_all(self):
f4bcdd
         """Clear all the stored values."""
f4bcdd
f4bcdd
From 00d770d1b7f8e1f0734e93da227f1c3e445033c8 Mon Sep 17 00:00:00 2001
f4bcdd
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
f4bcdd
Date: Mon, 3 Jan 2022 17:44:12 +0100
f4bcdd
Subject: [PATCH 3/3] Change the error feedback based on the installation mode
f4bcdd
f4bcdd
The original approach was confusing, because non-interactive installs run without any user input,
f4bcdd
and the message assumed that the user is able to answer installer's questions.
f4bcdd
---
f4bcdd
 org_fedora_oscap/ks/oscap.py | 5 +++--
f4bcdd
 1 file changed, 3 insertions(+), 2 deletions(-)
f4bcdd
f4bcdd
diff --git a/org_fedora_oscap/ks/oscap.py b/org_fedora_oscap/ks/oscap.py
f4bcdd
index d3f0dbe..ef34448 100644
f4bcdd
--- a/org_fedora_oscap/ks/oscap.py
f4bcdd
+++ b/org_fedora_oscap/ks/oscap.py
f4bcdd
@@ -372,13 +372,14 @@ def postinst_tailoring_path(self):
f4bcdd
                                 self.tailoring_path)
f4bcdd
 
f4bcdd
     def _terminate(self, message):
f4bcdd
-        message += "\n" + _("The installation should be aborted.")
f4bcdd
-        message += " " + _("Do you wish to continue anyway?")
f4bcdd
         if flags.flags.automatedInstall and not flags.flags.ksprompt:
f4bcdd
             # cannot have ask in a non-interactive kickstart
f4bcdd
             # installation
f4bcdd
+            message += "\n" + _("Aborting the installation.")
f4bcdd
             raise errors.CmdlineError(message)
f4bcdd
 
f4bcdd
+        message += "\n" + _("The installation should be aborted.")
f4bcdd
+        message += " " + _("Do you wish to continue anyway?")
f4bcdd
         answ = errors.errorHandler.ui.showYesNoQuestion(message)
f4bcdd
         if answ == errors.ERROR_CONTINUE:
f4bcdd
             # prevent any futher actions here by switching to the dry