Skip to content

Commit 561509c

Browse files
committed
Added a shebang to generated bash remediations.
1 parent dc02036 commit 561509c

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/XCCDF_POLICY/xccdf_policy_remediate.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ static int _write_script_header_to_fd(struct xccdf_policy *policy, struct xccdf_
808808
const char *format = sys != NULL ? sys : "";
809809
const char *template = sys != NULL ? " --template " : "";
810810
const char *remediation_type = ansible_script ? "Ansible Playbook" : "Bash Remediation Script";
811+
const char *shebang_with_newline = ansible_script ? "" : "#!/bin/bash\n";
811812

812813
char *fix_header;
813814

@@ -857,6 +858,7 @@ static int _write_script_header_to_fd(struct xccdf_policy *policy, struct xccdf_
857858
}
858859

859860
fix_header = oscap_sprintf(
861+
"%s"
860862
"###############################################################################\n"
861863
"#\n"
862864
"# %s for %s\n"
@@ -879,7 +881,7 @@ static int _write_script_header_to_fd(struct xccdf_policy *policy, struct xccdf_
879881
"%s\n"
880882
"#\n"
881883
"###############################################################################\n\n",
882-
remediation_type, profile_title,
884+
shebang_with_newline, remediation_type, profile_title,
883885
profile_description != NULL ? profile_description : "Not available",
884886
profile_id, benchmark_id, benchmark_version_info, xccdf_version_name,
885887
oscap_version, profile_id, template, format, remediation_type,
@@ -897,6 +899,7 @@ static int _write_script_header_to_fd(struct xccdf_policy *policy, struct xccdf_
897899
const char *xccdf_version_name = xccdf_version_info_get_version(xccdf_version);
898900

899901
fix_header = oscap_sprintf(
902+
"%s"
900903
"###############################################################################\n"
901904
"#\n"
902905
"# %s generated from evaluation of %s\n"
@@ -915,7 +918,7 @@ static int _write_script_header_to_fd(struct xccdf_policy *policy, struct xccdf_
915918
"%s\n"
916919
"#\n"
917920
"###############################################################################\n\n",
918-
remediation_type, profile_title, profile_id, xccdf_version_name,
921+
shebang_with_newline, remediation_type, profile_title, profile_id, xccdf_version_name,
919922
start_time != NULL ? start_time : "Unknown", end_time, oscap_version,
920923
result_id, template, format, remediation_type, remediation_type, how_to_apply
921924
);

tests/API/XCCDF/fix/all.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ function test_generate_fix {
1414

1515
# grep to strip out whitespace and comments
1616
# `tail -n +2` to skip the first line with progress reporting
17-
local GENERATED_FIX=$($OSCAP xccdf generate fix --result-id "$TESTRESULT_ID" "$INPUT" | grep -v -E "^([\t ]*|[\t ]*#.*)$" | tail -n +2)
17+
local GENERATED_FIX RELEVANT_FIX_CONTENTS
18+
GENERATED_FIX=$($OSCAP xccdf generate fix --result-id "$TESTRESULT_ID" "$INPUT")
19+
RELEVANT_FIX_CONTENTS=$(grep -v -E "^([\t ]*|[\t ]*#.*)$" <<< "$GENERATED_FIX" | tail -n +2)
1820
if [ "$?" != "0" ]; then
1921
return 1
2022
fi
2123
echo "$GENERATED_FIX"
2224

23-
if [ "$GENERATED_FIX" == "$EXPECTED_FIX" ]; then
25+
if [ "$RELEVANT_FIX_CONTENTS" == "$EXPECTED_FIX" ]; then
2426
return 0
2527
fi
2628

2729
echo "Generated fix doesn't match expected fix!"
28-
echo "'$GENERATED_FIX' != '$EXPECTED_FIX'"
30+
echo "'$RELEVANT_FIX_CONTENTS' != '$EXPECTED_FIX'"
2931

3032
return 1
3133
}

0 commit comments

Comments
 (0)