Blame SOURCES/scap-security-guide-0.1.53-fix-empty-bash-wrapping-PR_6173.patch

973b04
From 08d5fb8355020856282eecfcdd09e96d9850cd62 Mon Sep 17 00:00:00 2001
973b04
From: Watson Sato <wsato@redhat.com>
973b04
Date: Fri, 9 Oct 2020 09:30:35 +0200
973b04
Subject: [PATCH] Do not platform wrap empty Bash remediation
973b04
973b04
The fix text for a rule can end up empty if a Jinja macro or conditional
973b04
doesn't render any text.
973b04
In these cases, avoid wrapping empty lines in an if-else, as this causes
973b04
syntax error.
973b04
---
973b04
 ssg/build_remediations.py | 15 +++++++++------
973b04
 1 file changed, 9 insertions(+), 6 deletions(-)
973b04
973b04
diff --git a/ssg/build_remediations.py b/ssg/build_remediations.py
973b04
index f269d4d2d6..572db61701 100644
973b04
--- a/ssg/build_remediations.py
973b04
+++ b/ssg/build_remediations.py
973b04
@@ -273,6 +273,13 @@ def parse_from_file_with_jinja(self, env_yaml):
973b04
         self.local_env_yaml.update(env_yaml)
973b04
         result = super(BashRemediation, self).parse_from_file_with_jinja(self.local_env_yaml)
973b04
 
973b04
+        # Avoid platform wrapping empty fix text
973b04
+        # Remediations can be empty when a Jinja macro or conditional
973b04
+        # renders no fix text for a product
973b04
+        stripped_fix_text = result.contents.strip()
973b04
+        if stripped_fix_text == "":
973b04
+            return result
973b04
+
973b04
         rule_platforms = set()
973b04
         if self.associated_rule:
973b04
             # There can be repeated inherited platforms and rule platforms
973b04
@@ -301,15 +308,11 @@ def parse_from_file_with_jinja(self, env_yaml):
973b04
 
973b04
             all_conditions = " && ".join(platform_conditionals)
973b04
             wrapped_fix_text.append("if {0}; then".format(all_conditions))
973b04
-
973b04
-            # Avoid adding extra blank line
973b04
-            if not result.contents.startswith("\n"):
973b04
-                wrapped_fix_text.append("")
973b04
-
973b04
+            wrapped_fix_text.append("")
973b04
             # It is possible to indent the original body of the remediation with textwrap.indent(),
973b04
             # however, it is not supported by python2, and there is a risk of breaking remediations
973b04
             # For example, remediations with a here-doc block could be affected.
973b04
-            wrapped_fix_text.append("{0}".format(result.contents))
973b04
+            wrapped_fix_text.append("{0}".format(stripped_fix_text))
973b04
             wrapped_fix_text.append("")
973b04
             wrapped_fix_text.append("else")
973b04
             wrapped_fix_text.append("    >&2 echo 'Remediation is not applicable, nothing was done'")