Blob Blame History Raw
From df7b06ffaab9f593ae672182a43e43b145fc7739 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= <jcerny@redhat.com>
Date: Tue, 13 Nov 2018 11:15:17 +0100
Subject: [PATCH] Fix trailing whitespaces in Ansible Playbooks

Code which generates Ansible remediations updated to:
* remove trailing whitespaces from the Ansible remediation lines,
* not indent empty Ansible remediation lines.
These issues were found by `ansible-lint`. Also trailing whitespaces
got removed from the generated remediation headers.
---
 src/XCCDF_POLICY/xccdf_policy_remediate.c | 38 ++++++++++++++++-------
 1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/src/XCCDF_POLICY/xccdf_policy_remediate.c b/src/XCCDF_POLICY/xccdf_policy_remediate.c
index acf2c11951..344ff68859 100644
--- a/src/XCCDF_POLICY/xccdf_policy_remediate.c
+++ b/src/XCCDF_POLICY/xccdf_policy_remediate.c
@@ -22,7 +22,8 @@
 #endif
 
 #include <string.h>
+#include <ctype.h>
 #include <errno.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <unistd.h>
@@ -103,6 +104,7 @@ static int _write_remediation_to_fd_and_free(int output_fd, const char* template
 
 		char *current = text;
 		char *next_delim = NULL;
+		char *end = NULL;
 
 		do {
 			next_delim = strchr(current, delim);
@@ -110,17 +112,27 @@ static int _write_remediation_to_fd_and_free(int output_fd, const char* template
 				*next_delim = '\0';
 			}
 
-			// write indentation
-			if (_write_text_to_fd(output_fd, indentation) != 0) {
-				free(text);
-				return 1;
+			// remove all trailing whitespaces
+			end = current + strlen(current) - 1;
+			while (isspace(*end)) {
+				*end = '\0';
+				if (end == current)
+					break;
+				end--;
 			}
 
-			// write rest of line
-			if (_write_text_to_fd(output_fd, current) != 0) {
-				free(text);
-				return 1;
+			if (strlen(current) != 0) {
+				// write indentation
+				if (_write_text_to_fd(output_fd, indentation) != 0) {
+					free(text);
+					return 1;
+				}
+				if (_write_text_to_fd(output_fd, current) != 0) {
+					free(text);
+					return 1;
+				}
 			}
+
 			if (_write_text_to_fd(output_fd, "\n") != 0) {
 				free(text);
 				return 1;
@@ -130,8 +142,12 @@ static int _write_remediation_to_fd_and_free(int output_fd, const char* template
 				// text is NULL terminated to this is guaranteed to point to valid memory
 				current = next_delim + 1;
 			}
+		} while (next_delim != NULL);
+
+		if (_write_text_to_fd(output_fd, "\n") != 0) {
+			free(text);
+			return 1;
 		}
-		while (next_delim != NULL);
 
 		free(text);
 		return 0;
@@ -852,7 +868,7 @@ static int _write_script_header_to_fd(struct xccdf_policy *policy, struct xccdf_
 			"# Benchmark Version:  %s\n#\n"
 			"# XCCDF Version:  %s\n#\n"
 			"# This file was generated by OpenSCAP %s using:\n"
-			"# 	$ oscap xccdf generate fix --profile %s%s%s xccdf-file.xml \n#\n"
+			"# 	$ oscap xccdf generate fix --profile %s%s%s xccdf-file.xml\n#\n"
 			"# This script is generated from an OpenSCAP profile without preliminary evaluation.\n"
 			"# It attempts to fix every selected rule, even if the system is already compliant.\n"
 			"#\n"
@@ -881,7 +897,7 @@ static int _write_script_header_to_fd(struct xccdf_policy *policy, struct xccdf_
 			"# Evaluation Start Time:  %s\n"
 			"# Evaluation End Time:  %s\n#\n"
 			"# This file was generated by OpenSCAP %s using:\n"
-			"# 	$ oscap xccdf generate fix --result-id %s%s%s xccdf-results.xml \n#\n"
+			"# 	$ oscap xccdf generate fix --result-id %s%s%s xccdf-results.xml\n#\n"
 			"# This script is generated from the results of a profile evaluation.\n"
 			"# It attempts to remediate all issues from the selected rules that failed the test.\n"
 			"#\n"