Skip to content

Commit

Permalink
modify test scenarios to use the new common.sh script efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtapolasek committed Sep 20, 2024
1 parent 09b5b00 commit c3b668f
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@

source common.sh

{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}

# Removes argument from kernel command line in /boot/loader/entries/*.conf

for file in /boot/loader/entries/*.conf ; do
if grep -q '^.*{{{ ESCAPED_ARG_NAME }}}=.*' "$file" ; then
sed -i 's/\(^.*\){{{ARG_NAME}}}=[^[:space:]]*\(.*\)/\1 \2/' "$file"
fi
# ensure that grubenv is not referenced
if grep -q '\$kernelopts' "$file"; then
sed -i 's/^\(options.*\)\$kernelopts\(.*\)$/\1\2/' "$file"
fi
done
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

source common.sh

{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}

# Removes argument from kernel command line in /etc/default/grub
if grep -q '^GRUB_CMDLINE_LINUX=.*{{{ARG_NAME}}}=.*"' '/etc/default/grub' ; then
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ARG_NAME}}}=[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
fi

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

source common.sh

{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}

# Removes the argument from kernel command line in /etc/default/grub
if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*{{{ARG_NAME}}}=.*"' '/etc/default/grub' ; then
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\){{{ARG_NAME}}}=[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,14 @@
{{%- set ARG_NAME_VALUE= ARG_NAME ~ "=correct_value" %}}
{{%- endif %}}


source common.sh

# Removes audit argument from kernel command line in /boot/grub2/grubenv
file="/boot/grub2/grubenv"
if grep -q '^.*{{{ARG_NAME}}}=.*' "$file" ; then
sed -i 's/\(^.*\){{{ARG_NAME}}}=[^[:space:]]*\(.*\)/\1 \2/' "$file"
fi

{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}
# ensure that the grubenv entry is not referenced
# also in RHEL 8, after performing previous steps, the only option is $kernelopts
# so add the arg=value there so that the test passes
for entry in /boot/loader/entries/*.conf; do
if ! grep -q '\$kernelopts' "$entry"; then
sed -i 's/^(options.*)$/\1 \$kernelopts/' "$entry"
if grep -q '\$kernelopts' "$entry"; then
sed -i 's/^\(options.*\)\$kernelopts\(.*\)$/\1\2 mock {{{ ARG_NAME_VALUE }}}/' "$entry"
fi
done

# remediate with grubby, this ill ensure expanded options are in /boot/loader/entries/*.conf
{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@

source common.sh

# Removes audit argument from kernel command line in /boot/grub2/grubenv
file="/boot/grub2/grubenv"
if grep -q '^.*{{{ARG_NAME}}}=.*' "$file" ; then
sed -i 's/\(^.*\){{{ARG_NAME}}}=[^[:space:]]*\(.*\)/\1 \2/' "$file"
fi

for entry in /boot/loader/entries/*.conf; do
if ! grep -q '\$kernelopts' "$entry"; then
sed -i 's/^(options.*)$/\1 \$kernelopts/' "$entry"
sed -i 's/^\(options.*\)$/\1 \$kernelopts/' "$entry"
fi
done

Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,14 @@

source common.sh

# configure the argument in kernel command line in /boot/grub2/grubenv
#file="/boot/grub2/grubenv"
#if grep -q '^.*{{{ARG_NAME}}}=.*' "$file"; then
# modify the GRUB command-line if the arg already exists
# sed -i 's/\(^.*\){{{ARG_NAME}}}=[^[:space:]]*\(.*\)/\1 {{{ARG_NAME_VALUE}}} \2/' "$file"
#else
# no arg is present, append it
# sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ARG_NAME_VALUE}}}/' "$file"
#fi

{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}

# Correct the form of default kernel command line in GRUB /etc/default/grub and applies value through Grubby
if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*{{{ ESCAPED_ARG_NAME }}}=.*"' '/etc/default/grub' ; then
# modify the GRUB command-line if an arg=value already exists
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\){{{ ARG_NAME }}}=[^[:space:]]*\(.*"\)/\1 {{{ ARG_NAME_VALUE }}} \2/' '/etc/default/grub'
else
# no audit=arg is present, append it
# no arg is present, append it
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\)"/\1 {{{ ARG_NAME_VALUE }}}"/' '/etc/default/grub'
fi

Expand All @@ -39,5 +30,3 @@ sed -i 's/\(^.*GRUB_DISABLE_RECOVERY=\).*/\1true/' '/etc/default/grub'
if ! grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*$' '/etc/default/grub'; then
echo 'GRUB_CMDLINE_LINUX_DEFAULT="{{{ ARG_NAME_VALUE }}}"' >> /etc/default/grub
fi

{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
{{%- set ARG_NAME_VALUE= ARG_NAME ~ "=correct_value" %}}
{{%- endif %}}

source common.sh

# Clean up
rm -f /etc/default/grub.d/*
echo "GRUB_CMDLINE_LINUX=\"\"" > /etc/default/grub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

source common.sh

# Removes argument from kernel command line in /boot/loader/entries/*.conf
{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}

# Breaks argument from kernel command line in /boot/loader/entries/*.conf
for file in /boot/loader/entries/*.conf ; do
if grep -q '^.*{{{ ESCAPED_ARG_NAME }}}=.*' "$file" ; then
# modify the GRUB command-line if an ={{{ARG_NAME}}} arg already exists
Expand All @@ -15,4 +16,8 @@ for file in /boot/loader/entries/*.conf ; do
# no {{{ARG_NAME}}}=arg is present, append it
sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ARG_NAME}}}=wrong/' "$file"
fi
# ensure that grubenv is not referenced
if grep -q '\$kernelopts' "$file"; then
sed -i 's/^\(options.*\)\$kernelopts\(.*\)$/\1\2/' "$file"
fi
done
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*{{{ ESCAPED_ARG_NAME }}}=.*"' '/etc/d
# modify the GRUB command-line if an arg=value already exists
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\){{{ ARG_NAME }}}=[^[:space:]]*\(.*"\)/\1 {{{ ARG_NAME }}}={{{ ARG_NAME_VALUE_WRONG }}} \2/' '/etc/default/grub'
else
# no audit=arg is present, append it
# no arg is present, append it
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\)"/\1 {{{ ARG_NAME }}}={{{ ARG_NAME_VALUE_WRONG }}}"/' '/etc/default/grub'
fi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

source common.sh

{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}

# Break the argument in kernel command line in /boot/grub2/grubenv
file="/boot/grub2/grubenv"
if grep -q '^.*{{{ARG_NAME}}}=.*' "$file" ; then
Expand All @@ -19,6 +21,6 @@ fi
# othervise contents of grubenv are ignored
for entry in /boot/loader/entries/*.conf; do
if ! grep -q '\$kernelopts' "$entry"; then
sed -i 's/^(options.*)$/\1 \$kernelopts/' "$entry"
sed -i 's/^\(options.*\)$/\1 \$kernelopts/' "$entry"
fi
done

0 comments on commit c3b668f

Please sign in to comment.