inspec/lib/resources/group_policy.rb

60 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
# license: All rights reserved
require 'json'
2015-07-26 10:30:12 +00:00
include Serverspec::Type
2015-07-26 10:30:12 +00:00
# return JSON object
def gpo (policy_path, policy_name)
file = ::File.read(::File.join ::File.dirname(__FILE__), "gpo.json")
gpo_hash = JSON.parse(file)
2015-07-26 10:30:12 +00:00
key = "Machine--" + policy_path + "--" + policy_name
gpo_hash[key]
end
2015-07-26 10:30:12 +00:00
# Group Policy
class GroupPolicy < Serverspec::Type::Base
def getRegistryValue(entry)
keys = entry['registry_information'][0]
cmd = "(Get-Item 'Registry::#{keys['path']}').GetValue('#{keys['key']}')"
command_result ||= @runner.run_command(cmd)
val = { :exit_code => command_result.exit_status.to_i, :data => command_result.stdout }
val
end
2015-07-26 10:30:12 +00:00
def convertValue (value)
val = value.strip
val = val.to_i if val.match(/^\d+$/)
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 = getRegistryValue(entry)
2015-07-26 10:30:12 +00:00
# verify data
if (val[:exit_code] == 0)
val = convertValue(val[:data])
else
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
2015-07-26 10:30:12 +00:00
end
module Serverspec::Type
def group_policy(policy_path)
GroupPolicy.new(policy_path)
end
end