2016-03-01 20:01:52 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
|
|
|
|
2016-03-18 22:14:41 +00:00
|
|
|
require 'utils/object_traversal'
|
|
|
|
|
2016-03-01 20:01:52 +00:00
|
|
|
class WMI < Inspec.resource(1)
|
|
|
|
name 'wmi'
|
|
|
|
desc 'request wmi information'
|
|
|
|
example "
|
|
|
|
describe wmi('RSOP_SecuritySettingNumeric', {
|
|
|
|
namespace: 'root\\rsop\\computer',
|
2016-03-18 22:14:41 +00:00
|
|
|
filter: 'KeyName = \'MinimumPasswordAge\' And precedence=1'
|
2016-03-01 20:01:52 +00:00
|
|
|
}) do
|
|
|
|
its('Setting') { should eq true }
|
|
|
|
end
|
|
|
|
"
|
|
|
|
|
2016-03-18 22:14:41 +00:00
|
|
|
include ObjectTraverser
|
|
|
|
|
2016-03-01 20:01:52 +00:00
|
|
|
# This would be the same as:
|
|
|
|
# WMIC /NAMESPACE:\\root\rsop\computer PATH RSOP_SecuritySettingNumeric WHERE "KeyName = 'MinimumPasswordAge' And precedence=1" GET Setting
|
|
|
|
attr_accessor :content
|
2016-03-18 22:14:41 +00:00
|
|
|
def initialize(wmiclass, opts = {})
|
2016-03-01 20:01:52 +00:00
|
|
|
@wmiclass = wmiclass
|
|
|
|
@wminamespace = opts[:namespace]
|
|
|
|
@wmifilter = opts[:filter]
|
|
|
|
|
|
|
|
# verify that this resource is only supported on Windows
|
|
|
|
return skip_resource 'The `windows_feature` resource is not supported on your OS.' unless inspec.os.windows?
|
|
|
|
end
|
|
|
|
|
|
|
|
# returns nil, if not existant or value
|
2016-03-18 22:14:41 +00:00
|
|
|
def method_missing(*keys)
|
|
|
|
# catch bahavior of rspec its implementation
|
|
|
|
# @see https://github.com/rspec/rspec-its/blob/master/lib/rspec/its.rb#L110
|
|
|
|
keys.shift if keys.is_a?(Array) && keys[0] == :[]
|
|
|
|
value(keys)
|
2016-03-01 20:01:52 +00:00
|
|
|
end
|
|
|
|
|
2016-03-18 22:14:41 +00:00
|
|
|
def value(key)
|
|
|
|
extract_value(key, info)
|
2016-03-01 20:01:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def info
|
|
|
|
return @content if defined?(@content)
|
|
|
|
@content = {}
|
2016-03-18 22:14:41 +00:00
|
|
|
|
|
|
|
# optional params
|
|
|
|
cmd_namespace = "-namespace #{@wminamespace}" unless @wminamespace.nil?
|
|
|
|
cmd_filter = "-filter \"#{@wmifilter}\"" unless @wmifilter.nil?
|
|
|
|
|
|
|
|
# run wmi command
|
|
|
|
cmd = inspec.command("Get-WmiObject -class #{@wmiclass} #{cmd_namespace} #{cmd_filter} | ConvertTo-Json")
|
2016-03-01 20:01:52 +00:00
|
|
|
@content = JSON.parse(cmd.stdout)
|
|
|
|
rescue JSON::ParserError => e
|
|
|
|
@content
|
|
|
|
end
|
2016-03-18 22:14:41 +00:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
"WMI #{@wmiclass} where #{@wmifilter}"
|
|
|
|
end
|
|
|
|
|
2016-03-01 20:01:52 +00:00
|
|
|
end
|