Blame SOURCES/oscap-anaconda-addon-2.0.1-absent_appstream-PR_185.patch

a0903e
From c92205d5a5c788eeac84a6e67956a3e0540ab565 Mon Sep 17 00:00:00 2001
a0903e
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
a0903e
Date: Mon, 3 Jan 2022 17:31:49 +0100
a0903e
Subject: [PATCH 1/2] Add oscap sanity check before attempting remediation
a0903e
a0903e
If something is obviously wrong with the scanner, then don't attempt to remediate
a0903e
and try to show relevant information in a dialog window.
a0903e
---
a0903e
 org_fedora_oscap/common.py               | 39 +++++++++++++++++++-----
a0903e
 org_fedora_oscap/service/installation.py | 11 +++++++
a0903e
 tests/test_common.py                     |  8 +++++
a0903e
 tests/test_installation.py               |  3 +-
a0903e
 4 files changed, 52 insertions(+), 9 deletions(-)
a0903e
a0903e
diff --git a/org_fedora_oscap/common.py b/org_fedora_oscap/common.py
a0903e
index c432168..eeb27fc 100644
a0903e
--- a/org_fedora_oscap/common.py
a0903e
+++ b/org_fedora_oscap/common.py
a0903e
@@ -171,7 +171,8 @@ def execute(self, ** kwargs):
a0903e
             proc = subprocess.Popen(self.args, stdout=subprocess.PIPE,
a0903e
                                     stderr=subprocess.PIPE, ** kwargs)
a0903e
         except OSError as oserr:
a0903e
-            msg = "Failed to run the oscap tool: %s" % oserr
a0903e
+            msg = ("Failed to execute command '{command_string}': {oserr}"
a0903e
+                   .format(command_string=command_string, oserr=oserr))
a0903e
             raise OSCAPaddonError(msg)
a0903e
 
a0903e
         (stdout, stderr) = proc.communicate()
a0903e
@@ -247,6 +248,34 @@ def _run_oscap_gen_fix(profile, fpath, template, ds_id="", xccdf_id="",
a0903e
     return proc.stdout
a0903e
 
a0903e
 
a0903e
+def do_chroot(chroot):
a0903e
+    """Helper function doing the chroot if requested."""
a0903e
+    if chroot and chroot != "/":
a0903e
+        os.chroot(chroot)
a0903e
+        os.chdir("/")
a0903e
+
a0903e
+
a0903e
+def assert_scanner_works(chroot, executable="oscap"):
a0903e
+    args = [executable, "--version"]
a0903e
+    command = " ".join(args)
a0903e
+
a0903e
+    try:
a0903e
+        proc = subprocess.Popen(
a0903e
+            args, preexec_fn=lambda: do_chroot(chroot),
a0903e
+            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
a0903e
+        (stdout, stderr) = proc.communicate()
a0903e
+        stderr = stderr.decode(errors="replace")
a0903e
+    except OSError as exc:
a0903e
+        msg = _(f"Basic invocation '{command}' fails: {str(exc)}")
a0903e
+        raise OSCAPaddonError(msg)
a0903e
+    if proc.returncode != 0:
a0903e
+        msg = _(
a0903e
+            f"Basic scanner invocation '{command}' exited "
a0903e
+            "with non-zero error code {proc.returncode}: {stderr}")
a0903e
+        raise OSCAPaddonError(msg)
a0903e
+    return True
a0903e
+
a0903e
+
a0903e
 def run_oscap_remediate(profile, fpath, ds_id="", xccdf_id="", tailoring="",
a0903e
                         chroot=""):
a0903e
     """
a0903e
@@ -276,12 +305,6 @@ def run_oscap_remediate(profile, fpath, ds_id="", xccdf_id="", tailoring="",
a0903e
     if not profile:
a0903e
         return ""
a0903e
 
a0903e
-    def do_chroot():
a0903e
-        """Helper function doing the chroot if requested."""
a0903e
-        if chroot and chroot != "/":
a0903e
-            os.chroot(chroot)
a0903e
-            os.chdir("/")
a0903e
-
a0903e
     # make sure the directory for the results exists
a0903e
     results_dir = os.path.dirname(RESULTS_PATH)
a0903e
     if chroot:
a0903e
@@ -306,7 +329,7 @@ def do_chroot():
a0903e
     args.append(fpath)
a0903e
 
a0903e
     proc = SubprocessLauncher(args)
a0903e
-    proc.execute(preexec_fn=do_chroot)
a0903e
+    proc.execute(preexec_fn=lambda: do_chroot(chroot))
a0903e
     proc.log_messages()
a0903e
 
a0903e
     if proc.returncode not in (0, 2):
a0903e
diff --git a/org_fedora_oscap/service/installation.py b/org_fedora_oscap/service/installation.py
a0903e
index 2da8559..d909c44 100644
a0903e
--- a/org_fedora_oscap/service/installation.py
a0903e
+++ b/org_fedora_oscap/service/installation.py
a0903e
@@ -239,6 +239,17 @@ def name(self):
a0903e
 
a0903e
     def run(self):
a0903e
         """Run the task."""
a0903e
+        try:
a0903e
+            common.assert_scanner_works(
a0903e
+                chroot=self._sysroot, executable="oscap")
a0903e
+        except Exception as exc:
a0903e
+            msg_lines = [_(
a0903e
+                "The 'oscap' scanner doesn't work in the installed system: {error}"
a0903e
+                .format(error=str(exc)))]
a0903e
+            msg_lines.append(_("As a result, the installed system can't be hardened."))
a0903e
+            terminate("\n".join(msg_lines))
a0903e
+            return
a0903e
+
a0903e
         common.run_oscap_remediate(
a0903e
             self._policy_data.profile_id,
a0903e
             self._target_content_path,
a0903e
diff --git a/tests/test_common.py b/tests/test_common.py
a0903e
index 9f7a16a..4f25379 100644
a0903e
--- a/tests/test_common.py
a0903e
+++ b/tests/test_common.py
a0903e
@@ -77,6 +77,14 @@ def _run_oscap(mock_subprocess, additional_args):
a0903e
     return expected_args, kwargs
a0903e
 
a0903e
 
a0903e
+def test_oscap_works():
a0903e
+    assert common.assert_scanner_works(chroot="/")
a0903e
+    with pytest.raises(common.OSCAPaddonError, match="No such file"):
a0903e
+        common.assert_scanner_works(chroot="/", executable="i_dont_exist")
a0903e
+    with pytest.raises(common.OSCAPaddonError, match="non-zero"):
a0903e
+        common.assert_scanner_works(chroot="/", executable="false")
a0903e
+
a0903e
+
a0903e
 def test_run_oscap_remediate_profile_only(mock_subprocess, monkeypatch):
a0903e
     return run_oscap_remediate_profile(
a0903e
         mock_subprocess, monkeypatch,
a0903e
diff --git a/tests/test_installation.py b/tests/test_installation.py
a0903e
index 5749a94..f819c3b 100644
a0903e
--- a/tests/test_installation.py
a0903e
+++ b/tests/test_installation.py
a0903e
@@ -115,4 +115,5 @@ def test_remediate_system_task(sysroot_path, content_path, tailoring_path):
a0903e
     )
a0903e
 
a0903e
     assert task.name == "Remediate the system"
a0903e
-    task.run()
a0903e
+    with pytest.raises(installation.NonCriticalInstallationError, match="No such file"):
a0903e
+        task.run()
a0903e
a0903e
From ea2dbf5017445875b1c0e4ee27899c8dde292c98 Mon Sep 17 00:00:00 2001
a0903e
From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= <matyc@redhat.com>
a0903e
Date: Mon, 3 Jan 2022 17:42:31 +0100
a0903e
Subject: [PATCH 2/2] Don't raise exceptions in execute()
a0903e
a0903e
Those result in tracebacks during the installation,
a0903e
while a dialog window presents a more useful form of user interaction.
a0903e
---
a0903e
 org_fedora_oscap/service/installation.py | 27 ++++++++++++++----------
a0903e
 1 file changed, 16 insertions(+), 11 deletions(-)
a0903e
a0903e
diff --git a/org_fedora_oscap/service/installation.py b/org_fedora_oscap/service/installation.py
a0903e
index d909c44..290da40 100644
a0903e
--- a/org_fedora_oscap/service/installation.py
a0903e
+++ b/org_fedora_oscap/service/installation.py
a0903e
@@ -210,9 +210,9 @@ def run(self):
a0903e
             )
a0903e
 
a0903e
             if ret != 0:
a0903e
-                raise common.ExtractionError(
a0903e
-                    "Failed to install content RPM to the target system"
a0903e
-                )
a0903e
+                msg = _(f"Failed to install content RPM to the target system.")
a0903e
+                terminate(msg)
a0903e
+                return
a0903e
         else:
a0903e
             pattern = utils.join_paths(common.INSTALLATION_CONTENT_DIR, "*")
a0903e
             utils.universal_copy(pattern, target_content_dir)
a0903e
@@ -250,11 +250,16 @@ def run(self):
a0903e
             terminate("\n".join(msg_lines))
a0903e
             return
a0903e
 
a0903e
-        common.run_oscap_remediate(
a0903e
-            self._policy_data.profile_id,
a0903e
-            self._target_content_path,
a0903e
-            self._policy_data.datastream_id,
a0903e
-            self._policy_data.xccdf_id,
a0903e
-            self._target_tailoring_path,
a0903e
-            chroot=self._sysroot
a0903e
-        )
a0903e
+        try:
a0903e
+            common.run_oscap_remediate(
a0903e
+                self._policy_data.profile_id,
a0903e
+                self._target_content_path,
a0903e
+                self._policy_data.datastream_id,
a0903e
+                self._policy_data.xccdf_id,
a0903e
+                self._target_tailoring_path,
a0903e
+                chroot=self._sysroot
a0903e
+            )
a0903e
+        except Exception as exc:
a0903e
+            msg = _(f"Something went wrong during the final hardening: {str(exc)}.")
a0903e
+            terminate(msg)
a0903e
+            return