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