-
Notifications
You must be signed in to change notification settings - Fork 681
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
xinetd_conf resource: fix false positives when config file or directory doesn't exist #2302
Conversation
Thanks for the PR, @eramoto! There's actually a more graceful way for us to handle this now with raising exceptions, but I discovered a bug while building my reply to you. Let me get this bug fixed first (See #2307) and then I'll describe the change I believe will address this in a better way and in a manner in which we plan to write resources going forward. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @eramoto! I've provided some feedback on a way to handle this a bit more cleanly and what will become a more standard way of us handling resource failures in the future using Ruby exceptions.
lib/resources/xinetd.rb
Outdated
@@ -53,12 +53,14 @@ def read_content(path = @conf_path) | |||
return @contents[path] if @contents.key?(path) | |||
file = inspec.file(path) | |||
if !file.file? | |||
return skip_resource "Can't find file \"#{path}\"" | |||
skip_resource "Can't find file \"#{path}\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With #2307 now merged, a cleaner way to handle this would be to raise an actual Ruby exception. We have two to primary ones to choose from:
Inspec::Exceptions::ResourceSkipped
: will skip the resource in the outputInspec::Exceptions::ResourceFailed
: will fail the resource in the output
One could make the argument that a failure is more appropriate here than a skip, but in this case, we should aim to keep consistency since that could be considered a breaking change. So, we can change this line to:
raise Inspec::Exceptions::ResourceSkipped, "Can't find file: #{path}"
... and then you can remove the return nil
below it since the raise
will stop the resource from continuing on.
lib/resources/xinetd.rb
Outdated
end | ||
|
||
@contents[path] = file.content | ||
if @contents[path].nil? || @contents[path].empty? | ||
return skip_resource "Can't read file \"#{path}\"" | ||
skip_resource "Can't read file \"#{path}\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above - change to a raised exception, and then we can remove the return nil
Not return 'nil' when xinetd_conf resource checks an empty file or a non-existent file/directory. So the following test succeeds, where /empty_file is a empty file. describe xinetd_conf("/empty_file") do it { should be_disabled } end Signed-off-by: ERAMOTO Masaya <[email protected]>
When the following double check is done for a unreadable file, the second check wrong succeeds. This fixes to register the file which passes all validation tests. describe xinetd_conf("/unreadable_file") do it { should_not be_disabled } it { should be_enabled } end Signed-off-by: ERAMOTO Masaya <[email protected]>
a4130af
to
b0155f6
Compare
Hi @adamleff , |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nicely done, @eramoto!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for discovering and fixing that bug @eramoto
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
There are the similar issues as PR inspec#2302. Almost resources return false positives when a file does not exist or is not read. Signed-off-by: ERAMOTO Masaya <[email protected]>
* Create file-check functionality into utility file There are the similar issues as PR #2302. Almost resources return false positives when a file does not exist or is not read. * Replace to file-check functionality * Fix dh_params and x509_certificate resources If a file is empty, OpenSSL::PKey::DH and OpenSSL::X509::Certificate have raised an exception and have skipped the inspection. Thus x509_certificate and dh_params resources are not allowed to read a empty file. * to_s of shadow expects filters is not nil * Remove workaround of sshd_config Removes the workaround of sshd_config since Travis CI fails due to a bug of dev-sec/ssh-baseline and the PR #100 will fix it. * Use init block variable in methods Signed-off-by: ERAMOTO Masaya <[email protected]>
Not return 'nil' when xinetd_conf resource checks an empty file or a
non-existent file. So the following test succeeds, where /empty_file is
empty file.
describe xinetd_conf("/empty_file") do
it { should be_disabled }
end
Signed-off-by: ERAMOTO Masaya [email protected]