Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for --controls option is not working as expected. #5434

Merged
merged 13 commits into from Mar 19, 2021

Conversation

Vasu1105
Copy link
Contributor

@Vasu1105 Vasu1105 commented Mar 17, 2021

Signed-off-by: Vasu1105 vasundhara.jagdale@chef.io

Description

As mentioned in issue #5215 the control block also gets evaluated and due to which the --controls option is not working as expected.
The reason behind that the filtering of control here https://github.com/inspec/inspec/blob/master/lib/inspec/profile.rb#L228 is happening after evaluating the controls which are happening here https://github.com/inspec/inspec/blob/master/lib/inspec/profile.rb#L220

which calls this

load_with_context(control_eval_context, *args)
and then this
def load_with_context(context, content, source = nil, line = nil)

which then calls this control definition

def control(id, opts = {}, &block)

and describe definition (descrhttps://github.com/inspec/inspec/blob/2f41c16ad444eaf69f7ec77f4ff681f35aad9210/lib/inspec/control_eval_context.rb#L66

both these methods instantiate Inspec::rule

instance_eval(&block)

which basically causing the skipped control is getting evaluated (note not the describe block)

I have added a method in the control_eval_context.rb to filter the controls before evaluating it. I need to add the filter in describe method also because when we are having only describe block without control block the methods creates auto-generated control as described here

# generate a control for them automatically and then execute

so I am making sure those describe blocks are also get filtered when specifying --controls option.

Related Issue

Fixes #5215

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New content (non-breaking change)
  • Breaking change (a content change which would break existing functionality or processes)

Checklist:

  • I have read the CONTRIBUTING document.

@Vasu1105 Vasu1105 requested a review from a team as a code owner March 17, 2021 14:35
@Vasu1105 Vasu1105 requested review from alexpop and Nik08 March 17, 2021 14:35
@netlify
Copy link

netlify bot commented Mar 17, 2021

Deploy preview for chef-inspec canceled.

Built with commit 513c7be

https://app.netlify.com/sites/chef-inspec/deploys/60548abb0c84330007964c14

@@ -178,6 +178,25 @@ def stderr
assert_exit_code 100, out
end

it "executes only specified controls when selecting the controls by literal names" do
inspec("exec " + File.join(profile_path, "filter_table") + " --no-create-lockfile --controls foo")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose the filter_table fixture? I don't think this problem has anything to do with FilterTable. I would suggest either finding a different profile, or creating a new one just for this purpose.

Copy link
Contributor Author

@Vasu1105 Vasu1105 Mar 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes after checking the impact decided to remove this from here. I thought since we are using --controls options in test with this profile that will be good place to include. But yes definitely this is not the right place to have this. Making the required changes.

Version: 0.1.0
Target: local://

✔ foo: a thing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely too brittle of a test - the check marks vary under Windows, for example. I suggest you write tests that look specifically for what you are looking for. For example,

_(out.stdout).wont_include "bar"
_(out.stdout).wont_include "baz"

@Vasu1105 Vasu1105 requested a review from IanMadd as a code owner March 18, 2021 08:43
@Vasu1105 Vasu1105 changed the title [WIP] Fix for when -controls option is not working as expected. Fix for when -controls option is not working as expected. Mar 18, 2021
assert_exit_code 0, out
end

it "executes only specified controls when selecting the controls by literal names" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This selects by regex, not by literal name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Comment on lines 49 to 69
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has a lot of extra material in it that is not being tested. Either it should be removed or it should be tested - fixture files should be minimal. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @clintoncwolfe .Did the required changes.

Copy link
Contributor

@clintoncwolfe clintoncwolfe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of minor notes about the test fixture file, nit-picking really. This works really well. I think users will be very happy with this improvement!

@clintoncwolfe clintoncwolfe added Aspect: Performance Nimble is delightful. Type: Bug Feature not working as expected Component: CLI: exec labels Mar 18, 2021
…option

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
…ing the control block of not included controls in the controls option as it was filtering the controls from the list after evaluating updated the logic so that it gets evaluated after filtering. Removed filter_controls methods as no more using that

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
… 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>
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
…_list_exist?

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
@Vasu1105 Vasu1105 force-pushed the vasundhara/fix-for-controls-option branch from 982ba44 to 428a8a7 Compare March 19, 2021 10:40
…with some random hex which might contain the number that I have used in the rgex due to which test is failing

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
@Vasu1105 Vasu1105 changed the title Fix for when -controls option is not working as expected. Fix for -controls option is not working as expected. Mar 19, 2021
@clintoncwolfe clintoncwolfe merged commit 5378a51 into master Mar 19, 2021
@clintoncwolfe clintoncwolfe deleted the vasundhara/fix-for-controls-option branch March 19, 2021 14:16
@klose4711
Copy link

Thanks for fixing this! 🙂

@Vasu1105 Vasu1105 restored the vasundhara/fix-for-controls-option branch March 25, 2021 14:52
@Vasu1105 Vasu1105 changed the title Fix for -controls option is not working as expected. Fix for --controls option is not working as expected. Oct 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Aspect: Performance Nimble is delightful. Component: CLI: exec Type: Bug Feature not working as expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InSpec CLI exec "--controls=foo" option is not working as expected
3 participants