Missing registry keys should not exist

This commit is contained in:
Alex Pop 2016-10-05 11:28:34 +01:00 committed by Dominik Richter
parent de8056eee6
commit 10116724fc
3 changed files with 20 additions and 6 deletions

View file

@ -147,6 +147,11 @@ module Inspec::Resources
script = <<-EOH
Function InSpec-GetRegistryKey($path) {
$reg = Get-Item ('Registry::' + $path)
if ($reg -eq $null) {
Write-Error "InSpec: Failed to find registry key"
exit 1001
}
$properties = New-Object -Type PSObject
$reg.Property | ForEach-Object {
$key = $_
@ -167,11 +172,16 @@ module Inspec::Resources
# cannot rely on exit code for now, successful command returns exit code 1
# return nil if cmd.exit_status != 0, try to parse json
begin
@registry_cache = JSON.parse(cmd.stdout)
# convert keys to lower case
@registry_cache = Hash[@registry_cache.map do |key, value|
[key.downcase, value]
end]
if cmd.exit_status == 1001 && cmd.stderr =~ /InSpec: Failed to find registry key/
# TODO: provide the stderr output
@registry_cache = nil
else
@registry_cache = JSON.parse(cmd.stdout)
# convert keys to lower case
@registry_cache = Hash[@registry_cache.map do |key, value|
[key.downcase, value]
end]
end
rescue JSON::ParserError => _e
@registry_cache = nil
end

View file

@ -147,7 +147,7 @@ class MockLoader
'${Env:PATH}' => cmd.call('$env-PATH'),
# registry key test using winrm 2.0
'2376c7b3d81de9382303356e1efdea99385effb84788562c3e697032d51bf942' => cmd.call('reg_schedule'),
'F2376c7b3d81de9382303356e1efdea99385effb84788562c3e697032d51bf942' => cmd.call('reg_schedule'),
'89b48f91634e7efc40105fc082c5e12693b08c0a7c4a578b1f3a07e34f676c66' => cmd.call('reg_schedule'),
'Auditpol /get /subcategory:\'User Account Management\' /r' => cmd.call('auditpol'),
'/sbin/auditctl -l' => cmd.call('auditctl'),
'/sbin/auditctl -s' => cmd.call('auditctl-s'),

View file

@ -21,6 +21,10 @@ describe registry_key('HKLM\System\Test') do
it { should have_property_value('Binary value', :binary, 'dfa0f066') }
end
describe registry_key('HKLM\Missing\In\Action') do
it { should_not exist }
end
# serverspec compatability
describe windows_registry_key('HKLM\System\Test') do
it { should exist }