From ec35c6042aa9853ad2f7f2f0ac7e3f1cc94a56dc Mon Sep 17 00:00:00 2001 From: Nikita Mathur Date: Mon, 24 Jan 2022 19:19:42 +0530 Subject: [PATCH] Fix to capture non indented grub conf values Signed-off-by: Nikita Mathur --- lib/inspec/resources/grub_conf.rb | 2 +- test/fixtures/files/non_indented_grub.conf | 23 ++++++++++++++++++++++ test/helpers/mock_loader.rb | 1 + test/unit/resources/grub_conf_test.rb | 13 ++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/files/non_indented_grub.conf diff --git a/lib/inspec/resources/grub_conf.rb b/lib/inspec/resources/grub_conf.rb index a71ddb4ed..42e9158ac 100644 --- a/lib/inspec/resources/grub_conf.rb +++ b/lib/inspec/resources/grub_conf.rb @@ -162,7 +162,7 @@ module Inspec::Resources current_kernel = file_line.split(" ", 2)[1] lines.drop(index + 1).each do |kernel_line| - if kernel_line =~ /^\s.*/ + if kernel_line =~ /(?:^\s*\w+)/ && !(kernel_line =~ /^title.*/) option_type = kernel_line.split(" ")[0] line_options = kernel_line.split(" ").drop(1) if (menu_entry == conf["default"].to_i && @kernel == "default") || current_kernel == @kernel diff --git a/test/fixtures/files/non_indented_grub.conf b/test/fixtures/files/non_indented_grub.conf new file mode 100644 index 000000000..1330c89b8 --- /dev/null +++ b/test/fixtures/files/non_indented_grub.conf @@ -0,0 +1,23 @@ +# grub.conf generated by anaconda +# +# Note that you do not have to rerun grub after making changes to this file +# NOTICE: You have a /boot partition. This means that +# all kernel and initrd paths are relative to /boot/, eg. +# root (hd0,0) +# kernel /vmlinuz-version ro root=/dev/mapper/VolGroup-lv_root +# initrd /initrd-[generic-]version.img +#boot=/dev/sda +default=0 +timeout=5 +splashimage=(hd0,0)/grub/splash.xpm.gz +hiddenmenu +title CentOS (2.6.32-573.7.1.el6.x86_64) +root (hd0,0) +kernel /vmlinuz-2.6.32-573.7.1.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_GB.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet audit=1 +initrd /initramfs-2.6.32-573.7.1.el6.x86_64.img + +# another section +title CentOS 6 (2.6.32-573.el6.x86_64) +root (hd0,0) +kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_GB.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet audit=1 +initrd /initramfs-2.6.32-573.el6.x86_64.img \ No newline at end of file diff --git a/test/helpers/mock_loader.rb b/test/helpers/mock_loader.rb index b8aaaa906..1914247c1 100644 --- a/test/helpers/mock_loader.rb +++ b/test/helpers/mock_loader.rb @@ -103,6 +103,7 @@ class MockLoader "/etc/inetd.conf" => mockfile.call("inetd.conf"), "/etc/group" => mockfile.call("etcgroup"), "/etc/grub.conf" => mockfile.call("grub.conf"), + "/etc/non_indented_grub.conf" => mockfile.call("non_indented_grub.conf"), "/boot/grub2/grub.cfg" => mockfile.call("grub2.cfg"), "/boot/grub2/grubenv" => mockfile.call("grubenv"), "/boot/grub2/grubenv_invalid" => mockfile.call("grubenv_invalid"), diff --git a/test/unit/resources/grub_conf_test.rb b/test/unit/resources/grub_conf_test.rb index ceebbba64..af684b3a1 100644 --- a/test/unit/resources/grub_conf_test.rb +++ b/test/unit/resources/grub_conf_test.rb @@ -81,4 +81,17 @@ describe "Inspec::Resources::GrubConfig" do _(resource.default).must_equal "0" _(resource.timeout).must_equal "5" end + + it "parses data with no identations correctly with grub1" do + resource = MockLoader.new(:centos6).load_resource( + "grub_conf", + "/etc/non_indented_grub.conf", + "CentOS 6 (2.6.32-573.el6.x86_64)" + ) + + _(resource.kernel).must_include "/vmlinuz-2.6.32-573.el6.x86_64" + _(resource.initrd).must_equal "/initramfs-2.6.32-573.el6.x86_64.img" + _(resource.default).must_equal "0" + _(resource.timeout).must_equal "5" + end end