inspec/lib/resources/wmi.rb

110 lines
3 KiB
Ruby
Raw Normal View History

2016-03-01 20:01:52 +00:00
# encoding: utf-8
require 'utils/object_traversal'
2016-03-18 22:23:22 +00:00
module Inspec::Resources
2016-03-19 17:50:32 +00:00
# This resource simplifies the access to wmi
# on CLI you would use:
# WMIC /NAMESPACE:\\root\rsop\computer PATH RSOP_SecuritySettingNumeric WHERE "KeyName = 'MinimumPasswordAge' And precedence=1" GET Setting
# We use Get-WmiObject via Powershell to retrieve all values.
2016-03-18 22:23:22 +00:00
class WMI < Inspec.resource(1)
name 'wmi'
supports platform: 'windows'
2016-03-18 22:23:22 +00:00
desc 'request wmi information'
example <<~EXAMPLE
2016-06-18 15:54:04 +00:00
describe wmi({
class: 'RSOP_SecuritySettingNumeric',
2016-03-18 22:23:22 +00:00
namespace: 'root\\rsop\\computer',
filter: 'KeyName = \'MinimumPasswordAge\' And precedence=1'
}) do
its('Setting') { should eq true }
end
EXAMPLE
2016-03-01 20:01:52 +00:00
2016-03-18 22:23:22 +00:00
include ObjectTraverser
attr_accessor :content
2016-03-19 17:50:32 +00:00
2016-06-18 15:54:04 +00:00
def initialize(wmiclass = nil, opts = nil)
@options = opts || {}
if wmiclass.is_a?(Hash)
@options.merge!(wmiclass)
else
Inspec.deprecate(:wmi_non_hash_usage, 'Using `wmi(\'wmisclass\')` is deprecated. Please use`wmi({class: \'wmisclass\'})`')
2016-06-18 15:54:04 +00:00
@options[:class] = wmiclass
end
2016-03-18 22:23:22 +00:00
end
2016-03-01 20:01:52 +00:00
2016-03-18 22:23:22 +00:00
# returns nil, if not existant or value
def method_missing(*keys)
2016-03-19 17:50:32 +00:00
# catch behavior of rspec its implementation
2016-03-18 22:23:22 +00:00
# @see https://github.com/rspec/rspec-its/blob/master/lib/rspec/its.rb#L110
keys.shift if keys.is_a?(Array) && keys[0] == :[]
2016-03-19 17:50:32 +00:00
# map all symbols to strings
2016-06-18 15:54:04 +00:00
keys = keys.map { |x| x.to_s.downcase } if keys.is_a?(Array)
2016-03-19 17:50:32 +00:00
2016-03-18 22:23:22 +00:00
value(keys)
end
2016-03-01 20:01:52 +00:00
2016-03-18 22:23:22 +00:00
def value(key)
2016-06-18 15:54:04 +00:00
extract_value(key, params)
2016-03-18 22:23:22 +00:00
end
2016-03-01 20:01:52 +00:00
2016-06-18 15:54:04 +00:00
def params
2016-03-18 22:23:22 +00:00
return @content if defined?(@content)
@content = {}
2016-06-18 15:54:04 +00:00
# abort if no options are available
return @content unless defined?(@options)
# filter for supported options
args = @options.select { |key, _value| [:class, :namespace, :query, :filter].include?(key) }
2016-03-19 17:50:32 +00:00
2016-06-18 15:54:04 +00:00
# convert to Get-WmiObject arguments
params = ''
args.each { |key, value| params += " -#{key} \"#{value.gsub('"', '`"')}\"" }
2016-06-18 15:54:04 +00:00
# run wmi command and filter empty wmi
script = <<-EOH
Filter Aggregate
{
$arr = @{}
$_.properties | % {
$arr.Add($_.name, $_.value)
}
$arr
}
Get-WmiObject #{params} | Aggregate | ConvertTo-Json
EOH
2016-03-18 22:23:22 +00:00
# run wmi command
2016-06-18 15:54:04 +00:00
cmd = inspec.powershell(script)
2016-03-18 22:23:22 +00:00
@content = JSON.parse(cmd.stdout)
2016-06-18 15:54:04 +00:00
# make all keys case-insensitive
@content = lowercase_keys(@content)
2016-03-19 17:50:32 +00:00
rescue JSON::ParserError => _e
2016-03-18 22:23:22 +00:00
@content
end
2016-03-18 22:23:22 +00:00
def to_s
2016-06-18 15:54:04 +00:00
"WMI with #{@options}"
end
private
def lowercase_keys(content)
if content.is_a?(Hash)
content.keys.each do |key|
new_key = key.to_s.downcase
content[new_key] = content.delete(key)
lowercase_keys(content[new_key])
end
elsif content.respond_to?(:each)
content.each { |item| lowercase_keys(item) }
end
content
2016-03-18 22:23:22 +00:00
end
end
2016-03-01 20:01:52 +00:00
end