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

Add nil check for sshd config file #2217

Merged
merged 2 commits into from
Oct 6, 2017
Merged

Conversation

jquick
Copy link
Contributor

@jquick jquick commented Oct 5, 2017

This fixes #1778. There was a issue where if the user did not have read permissions on /etc/ssh/sshd_config it would error out on the empty? check. The fix here is to also look for nil on the file content. Along with this I refactored the inspec file empty? check as it does not exist and was also erroring during my testing. Output with the changes:
screen shot 2017-10-05 at 10 43 37 am

Signed-off-by: Jared Quick [email protected]

@jquick jquick requested a review from a team as a code owner October 5, 2017 14:46
@jquick jquick force-pushed the jq/add_nil_check_sshd_config branch from ef0a84d to 3a284e4 Compare October 5, 2017 14:47
@jquick jquick changed the title Add nil check for sshd config file WIP: Add nil check for sshd config file Oct 5, 2017
@jquick jquick force-pushed the jq/add_nil_check_sshd_config branch 2 times, most recently from 7847e82 to 3f13e43 Compare October 5, 2017 15:00
@jquick jquick changed the title WIP: Add nil check for sshd config file Add nil check for sshd config file Oct 5, 2017
Copy link
Contributor

@adamleff adamleff left a comment

Choose a reason for hiding this comment

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

@jquick this is a great first pass at this. We need to make sure we're always using InSpec resource, and not the ruby stdlib, when running commands or reading files... otherwise, we'll break the experience for remote scanned targets.

@@ -63,7 +63,7 @@ def read_content
end

@content = file.content
if @content.empty? && !file.empty?
if @content.nil? || (@content.empty? && File.size?(@conf_path))
Copy link
Contributor

Choose a reason for hiding this comment

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

We can't use the Ruby File class because this will always operate locally on the machine executing InSpec. If the user is scanning a remote target with --target, this call will still happen on the local machine. That's why the call to file was being done (lowercase file referring to the variable storing the inspec.file resource that will work both remotely and locally).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no inspec.file.empty? currently. Should we create one that does a size check accordingly?

Copy link
Contributor

Choose a reason for hiding this comment

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

Since we basically just hand over logic on files to Train, it should probably be implemented there. Can we do file.size == 0 for now and address that in a future PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good to me

@@ -109,6 +109,8 @@ def md.directory?
'/proc/net/bonding/bond0' => mockfile.call('bond0'),
'/etc/ssh/ssh_config' => mockfile.call('ssh_config'),
'/etc/ssh/sshd_config' => mockfile.call('sshd_config'),
'/etc/ssh/sshd_config_does_not_exist' => mockfile.call('sshd_config_does_not_exist'),
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should create a fileempty object, much like the empty object you see in the command mocks, so we don't have to create empty mock files. Like this, but for files: https://github.com/chef/inspec/blob/master/test/helper.rb#L339

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created the emptyfile object for the empty file mock. For this one I want it to point to file it cannot find.

it 'check bad path' do
resource = load_resource('sshd_config', '/etc/ssh/sshd_config_does_not_exist')
_(resource.send(:read_content)).must_equal "Can't find file \"/etc/ssh/sshd_config_does_not_exist\""
assert_nil(resource.Protocol)
Copy link
Contributor

Choose a reason for hiding this comment

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

We have not used assert_nil anywhere in the codebase yet and instead use must_be_nil. We should probably be consistent.

This fixes #1778. There was a issue where if the user did not have read
permissions on /etc/ssh/sshd_config it would error out on the empty?
check. The fix here is to also look for nil on the file content. Along
with this I refactored the inspec file empty? check as it does not exist
and was also erroring during my testing.

Signed-off-by: Jared Quick <[email protected]>
@jquick jquick force-pushed the jq/add_nil_check_sshd_config branch from 3f13e43 to 46bb47f Compare October 5, 2017 15:16
Copy link
Contributor

@adamleff adamleff left a comment

Choose a reason for hiding this comment

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

Great fix, @jquick - thanks!

@adamleff adamleff requested a review from a team October 5, 2017 17:08
@adamleff adamleff added the Type: Bug Feature not working as expected label Oct 5, 2017
@jquick jquick force-pushed the jq/add_nil_check_sshd_config branch 2 times, most recently from 1c5c55e to 822ceae Compare October 5, 2017 20:52
@jquick jquick force-pushed the jq/add_nil_check_sshd_config branch from 822ceae to 1e86bbb Compare October 5, 2017 20:57
Copy link
Contributor

@chris-rock chris-rock left a comment

Choose a reason for hiding this comment

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

Thank you @jquick for your contribution! This is a great improvement.

@chris-rock chris-rock merged commit 7bb7767 into master Oct 6, 2017
@chris-rock chris-rock deleted the jq/add_nil_check_sshd_config branch October 6, 2017 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Feature not working as expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

undefined method `empty?' for nil:NilClass with sshd_conf
4 participants