Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions shared/macros/10-bash.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2253,23 +2253,23 @@ for f in $(echo -n "{{{ files }}}"); do
fi

# find key in section and change value
if grep -qzosP "[[:space:]]*\[{{{ section }}}\]([^\n\[]*\n+)+?[[:space:]]*{{{ key }}}" "$f"; then
if ! grep -qPz "{{{ key }}}={{{ value }}}" "$f"; then
if grep -qzosP "(?m)^[[:space:]]*\[{{{ section }}}\]([^\n\[]*\n+)+?[[:space:]]*{{{ key }}}" "$f"; then
if ! grep -qzosP "(?m)^[[:space:]]*{{{ key }}}[[:space:]]*=[[:space:]]*{{{ value }}}" "$f"; then
{{% if no_quotes %}}
sed -i "s/{{{ key }}}[^(\n)]*/{{{ key }}}={{{ value | replace("/", "\/") }}}/" "$f"
sed -i "/^[[:space:]]*{{{ key }}}/s/\([[:blank:]]*=[[:blank:]]*\).*/\1{{{ value | replace("/", "\/") }}}/" "$f"
{{% else %}}
sed -i 's/{{{ key }}}[^(\n)]*/{{{ key }}}="{{{ value | replace("/", "\/") }}}"/' "$f"
sed -i '/^[[:space:]]*{{{ key }}}/s/\([[:blank:]]*=[[:blank:]]*\).*/\1"{{{ value | replace("/", "\/") }}}"/' "$f"
{{% endif %}}
fi

found=true

# find section and add key = value to it
elif grep -qs "[[:space:]]*\[{{{ section }}}\]" "$f"; then
elif grep -qs "^[[:space:]]*\[{{{ section }}}\]" "$f"; then
{{% if no_quotes %}}
sed -i "/[[:space:]]*\[{{{ section }}}\]/a {{{ key }}}={{{ value | replace("/", "\/") }}}" "$f"
sed -i "/^[[:space:]]*\[{{{ section }}}\]/a {{{ key }}}={{{ value | replace("/", "\/") }}}" "$f"
{{% else %}}
sed -i '/[[:space:]]*\[{{{ section }}}\]/a {{{ key }}}="{{{ value | replace ("/", "\/") }}}"' "$f"
sed -i '/^[[:space:]]*\[{{{ section }}}\]/a {{{ key }}}="{{{ value | replace ("/", "\/") }}}"' "$f"
{{% endif %}}
found=true
fi
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
SECTION="{{{ SECTION }}}"
PARAM="{{{ PARAM }}}"
VALUE="{{{ VALUE }}}"
MASTER_CFG_FILE="{{{ MASTER_CFG_FILE }}}"

# This setup tests if remediation is "tricked" by a commented-out correct value.
# It sets an active bad value and a commented-out good value.
{{% if NO_QUOTES %}}
echo -e "[$SECTION]\n$PARAM=badval\n#$PARAM=$VALUE" > "$MASTER_CFG_FILE"
{{% else %}}
echo -e "[$SECTION]\n$PARAM=\"badval\"\n#$PARAM=\"$VALUE\"" > "$MASTER_CFG_FILE"
{{% endif %}}
6 changes: 3 additions & 3 deletions tests/unit/bash/test_bash_ensure_ini_config.bats.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ teardown() {

@test "bash_ensure_ini_config - Basic value remediation" {
printf "[pam]\npam_cert_auth = false\n" > sssd_test/sssd.conf
expected_output="[pam]\npam_cert_auth=true\n"
expected_output="[pam]\npam_cert_auth = true\n"

call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

Expand All @@ -57,7 +57,7 @@ teardown() {
@test "bash_ensure_ini_config - Value remediation in multiple files" {
printf "[pam]\npam_cert_auth = false\n" > sssd_test/sssd.conf
printf "[pam]\npam_cert_auth = false\n" > pam_cert_auth.conf
expected_output="[pam]\npam_cert_auth=true\n"
expected_output="[pam]\npam_cert_auth = true\n"

call_bash_ensure_ini_config "sssd_test/sssd.conf pam_cert_auth.conf" "pam" "pam_cert_auth" "true"

Expand All @@ -70,7 +70,7 @@ teardown() {

@test "bash_ensure_ini_config - No remediation happened" {
printf "[pam]\npam_cert_auth = true\n" > sssd_test/sssd.conf
expected_output="[pam]\npam_cert_auth=true\n"
expected_output="[pam]\npam_cert_auth = true\n"

call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

Expand Down
Loading