1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#!/bin/bash
set -pu
function call_set_config_file_create_false {
{{{ set_config_file("$1", "Compression", "no", False) | indent(4) }}}
}
function call_set_config_file {
{{{ set_config_file("$1", "Compression", "no", True) | indent(4) }}}
}
function call_set_config_file_rsyslog {
{{{ set_config_file(path="$1",
parameter="\$DefaultNetstreamDriver", value="gtls",
create=true, separator=" ", separator_regex=" ") }}}
}
is_old_bats=0
setup() {
if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then
BATS_TEST_TMPDIR="$(mktemp -d)" # 1.4.0
# shellcheck disable=SC2034
BATS_TEARDOWN_STARTED= # 1.3.0
is_old_bats=1
else
is_old_bats=0
fi
pushd "${BATS_TEST_TMPDIR}" || exit 1
tmp_file=test.conf
touch "$tmp_file"
}
teardown() {
if (( is_old_bats )); then
if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then
>&2 echo "INTERNAL ERROR"
exit 3
fi
local tmppath xpwd
tmppath="$(readlink -f -- "${BATS_TEST_TMPDIR}")"
if [[ ! "${tmppath}" =~ ^/tmp/ ]] || [[ ! -d "${tmppath}" ]]; then
>&2 echo "INTERNAL ERROR"
exit 3
fi
xpwd="$(readlink -f -- .)"
if [[ "${tmppath}" != "${xpwd}" ]]; then
>&2 echo "INTERNAL ERROR"
exit 3
fi
popd || exit 1
rm -rf -- "${tmppath}"
BATS_TEST_TMPDIR=""
fi
}
@test "set_config_file - Basic value remediation" {
printf "%s\n" "Compression yes" > "$tmp_file"
expected_output="Compression no\n"
call_set_config_file "$tmp_file"
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
@test "set_config_file - No remediation happened" {
printf "%s\n" "Compression no" > "$tmp_file"
expected_output="Compression no\n"
call_set_config_file "$tmp_file"
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
@test "set_config_file - Multiline file remediation" {
printf "%s\n" "Protocol 2" "Compression yes" "Port 22" > "$tmp_file"
expected_output="Protocol 2\nPort 22\nCompression no\n"
call_set_config_file "$tmp_file"
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
@test "set_config_file - No remediation on commented line" {
printf "%s\n" "Protocol 2" "# Compression yes" "Port 22" > "$tmp_file"
expected_output="Protocol 2\n# Compression yes\nPort 22\nCompression no\n"
call_set_config_file "$tmp_file"
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
@test "set_config_file - Create if missing" {
printf "%s\n" "Protocol 2" "Port 22" > "$tmp_file"
expected_output="Protocol 2\nPort 22\nCompression no\n"
call_set_config_file "$tmp_file"
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
@test "set_config_file - Create file if doesn't exist" {
rm "$tmp_file"
expected_output="Compression no\n"
call_set_config_file "$tmp_file"
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
@test "set_config_file - Do not create if missing" {
rm "$tmp_file"
run call_set_config_file_create_false "$tmp_file"
[ "$status" -eq 1 ]
[[ "$output" =~ "Path '$tmp_file' wasn't found" ]]
}
@test "set_config_file - Case insensitive remediation" {
printf "%s\n" "Protocol 2" "COMPRESSION YES" "Port 22" > "$tmp_file"
expected_output="Protocol 2\nPort 22\nCompression no\n"
call_set_config_file "$tmp_file"
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
@test "set_config_file - Case sensitive remediation" {
printf "%s\n" "Protocol 2" "COMPRESSION YES" "Port 22" > "$tmp_file"
expected_output="Protocol 2\nCOMPRESSION YES\nPort 22\nCompression no\n"
{{{ set_config_file("$tmp_file", "Compression", "no", True, "", "", False) | indent(4) }}}
run diff -U2 "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
@test "set_config_file - Basic Bash remediation" {
printf "%s\n" "something=foo" > "$tmp_file"
expected_output="something='va lue'\n"
{{{ bash_shell_file_set("$tmp_file", "something", "va lue") | indent(4) }}}
run diff -U2 "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
@test "set_config_file - Variable remediation - preserve dollar and use double quotes" {
printf "%s\n" "something=bar" > "$tmp_file"
expected_output='something="$value"'"\n"
{{{ bash_shell_file_set("$tmp_file", "something", '$value') | indent(4) }}}
run diff -U2 "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
@test "set_config_file - Basic Bash remediation - don't quote" {
printf "%s\n" "something=foo" > "$tmp_file"
expected_output="something=va lue\n"
{{{ bash_shell_file_set("$tmp_file", "something", "va lue", no_quotes=true) | indent(4) }}}
run diff -U2 "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
@test "set_config_file - Variable remediation - don't quote" {
printf "%s\n" "something=bar" > "$tmp_file"
expected_output='something=$value'"\n"
{{{ bash_shell_file_set("$tmp_file", "something", '$value', no_quotes=true) | indent(4) }}}
run diff -U2 "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
@test "set_config_file - handle escaped dollar" {
printf "%s\n" '$DefaultNetstreamDriver bad' > "$tmp_file"
expected_output='$DefaultNetstreamDriver gtls'"\n"
call_set_config_file_rsyslog "$tmp_file"
run diff -U2 "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
}
|