Fixes 5215: While using --controls options the control block was also getting evaluated as filtering of the control was happening after evaluating so added the filter logic in the control_eval_context. Also when we have describe block outside control block we are we

generating a control for them automatically and then execute due that also becomes a control and has to add same logic to filter the control in that mehtod

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
This commit is contained in:
Vasu1105 2021-03-18 21:37:48 +05:30
parent df8d589e25
commit a5932b7da7
3 changed files with 63 additions and 9 deletions

View file

@ -53,13 +53,9 @@ module Inspec
def control(id, opts = {}, &block)
opts[:skip_only_if_eval] = @skip_only_if_eval
id_exist_in_list = @conf["profile"].include_controls_list.any? do |inclusion|
# Try to see if the inclusion is a regex, and if it matches
inclusion == id || (inclusion.is_a?(Regexp) && inclusion =~ id)
end
if id_exist_in_list || @conf["profile"].include_controls_list.empty?
if control_exist_in_controls_list?(id)
register_control(Inspec::Rule.new(id, profile_id, resources_dsl, opts, &block))
elsif control_list_empty?
register_control(Inspec::Rule.new(id, profile_id, resources_dsl, opts, &block))
end
end
@ -75,11 +71,16 @@ module Inspec
id = "(generated from #{loc} #{SecureRandom.hex})"
res = nil
rule = Inspec::Rule.new(id, profile_id, resources_dsl, {}) do
res = describe(*args, &block)
end
register_control(rule, &block)
if control_exist_in_controls_list?(id)
register_control(rule, &block)
elsif control_list_empty?
register_control(rule, &block)
end
res
end
@ -183,5 +184,23 @@ module Inspec
"#{File.basename(path)}:#{line}"
end
end
def profile_config_exist?
!@conf.empty? && @conf.key?("profile") && !@conf["profile"].include_controls_list.empty?
end
def control_list_empty?
!@conf.empty? && @conf.key?("profile") && @conf["profile"].include_controls_list.empty? || @conf.empty?
end
def control_exist_in_controls_list?(id)
if profile_config_exist?
id_exist_in_list = @conf["profile"].include_controls_list.any? do |inclusion|
# Try to see if the inclusion is a regex, and if it matches
inclusion == id || (inclusion.is_a?(Regexp) && inclusion =~ id)
end
end
id_exist_in_list
end
end
end

View file

@ -30,4 +30,40 @@ control "11_pass2" do
describe 'a thing' do
it { should cmp 'a thing' }
end
end
describe 'a thing' do
it { should cmp 'a thing' }
end
describe.one do
describe 'ConfigurationA' do
it { should cmp 'a thing' }
end
describe 'ConfigurationB' do
it { should cmp 'a thing' }
end
end
title '/ profile'
# you add controls here
control 'tmp-1.0' do # A unique ID for this control
impact 0.7 # The criticality, if this control fails.
title 'Create / directory' # A human-readable title
desc 'An optional description...' # Describe why this is needed
desc 'label', 'An optional description with a label' # Pair a part of the description with a label
tag data: 'temp data' # A tag allows you to associate key information
tag 'security' # to the test
ref 'Document A-12', url: 'http://...' # Additional references
describe file('/') do # The actual test
it { should be_directory }
end
end
# you can also use plain tests
describe file('/') do
it { should be_directory }
end

View file

@ -192,7 +192,6 @@ Test Summary: 0 successful, 0 failures, 0 skipped
inspec("exec " + File.join(profile_path, "controls-option-test") + " --no-create-lockfile --controls '/^11/'")
_(out.stdout).must_include "11_pass"
_(out.stdout).must_include "11_pass2"
_(out.stdout).wont_include "foo"
_(out.stdout).wont_include "bar"
_(out.stdout).wont_include "baz"
_(stderr).must_equal ""