inspec/lib/resources/group_policy.rb

55 lines
1.2 KiB
Ruby
Raw Normal View History

# encoding: utf-8
2015-07-15 13:15:18 +00:00
# copyright: 2015, Vulcano Security GmbH
2015-10-06 16:55:44 +00:00
# author: Christoph Hartmann
# author: Dominik Richter
# license: All rights reserved
require 'json'
2015-07-26 10:30:12 +00:00
# return JSON object
2015-09-05 14:07:54 +00:00
def gpo(policy_path, policy_name)
file = ::File.read(::File.join ::File.dirname(__FILE__), 'gpo.json')
gpo_hash = JSON.parse(file)
2015-09-04 07:59:30 +00:00
key = 'Machine--' + policy_path + '--' + policy_name
2015-07-26 10:30:12 +00:00
gpo_hash[key]
end
2015-07-26 10:30:12 +00:00
# Group Policy
2015-10-26 03:04:18 +00:00
class GroupPolicy < Inspec.resource(1)
name 'group_policy'
2015-07-26 10:30:12 +00:00
def get_registry_value(entry)
2015-07-26 10:30:12 +00:00
keys = entry['registry_information'][0]
cmd = "(Get-Item 'Registry::#{keys['path']}').GetValue('#{keys['key']}')"
2015-10-26 03:04:18 +00:00
command_result ||= inspec.command(cmd)
2015-09-05 14:07:54 +00:00
val = { exit_code: command_result.exit_status.to_i, data: command_result.stdout }
2015-07-26 10:30:12 +00:00
val
end
def convert_value(value)
2015-07-26 10:30:12 +00:00
val = value.strip
val = val.to_i if val.match(/^\d+$/)
2015-09-04 07:59:30 +00:00
val
2015-07-26 10:30:12 +00:00
end
2015-07-26 10:30:12 +00:00
# returns nil, if not existant or value
def method_missing(meth)
# map gpo to registry key
entry = gpo(@name, meth.to_s)
2015-07-26 10:30:12 +00:00
# get data
val = get_registry_value(entry)
2015-07-26 10:30:12 +00:00
# verify data
if (val[:exit_code] == 0)
return convert_value(val[:data])
2015-07-26 10:30:12 +00:00
else
2015-09-04 07:59:30 +00:00
return nil
end
2015-07-26 10:30:12 +00:00
end
2015-07-26 10:30:12 +00:00
def to_s
'Group Policy'
end
end