2015-09-20 15:42:09 +00:00
|
|
|
# encoding: utf-8
|
2015-10-06 16:55:44 +00:00
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
2015-09-20 15:42:09 +00:00
|
|
|
|
|
|
|
# check for a Windows feature
|
|
|
|
# Usage:
|
|
|
|
# describe windows_feature('DHCP Server') do
|
|
|
|
# it{ should be_installed }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# deprecated serverspec syntax:
|
|
|
|
# describe windows_feature('IIS-Webserver') do
|
|
|
|
# it{ should be_installed.by("dism") }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# describe windows_feature('Web-Webserver') do
|
|
|
|
# it{ should be_installed.by("powershell") }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This implementation uses the Get-WindowsFeature commandlet:
|
|
|
|
# Get-WindowsFeature | Where-Object {$_.Name -eq 'XPS Viewer' -or $_.DisplayName -eq 'XPS Viewe
|
|
|
|
# r'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json
|
|
|
|
# {
|
|
|
|
# "Name": "XPS-Viewer",
|
|
|
|
# "DisplayName": "XPS Viewer",
|
|
|
|
# "Description": "The XPS Viewer is used to read, set permissions for, and digitally sign XPS documents.",
|
|
|
|
# "Installed": false,
|
|
|
|
# "InstallState": 0
|
|
|
|
# }
|
2016-03-08 18:06:55 +00:00
|
|
|
module Inspec::Resources
|
|
|
|
class WindowsFeature < Inspec.resource(1)
|
|
|
|
name 'windows_feature'
|
|
|
|
desc 'Use the windows_feature InSpec audit resource to test features on Microsoft Windows.'
|
|
|
|
example "
|
|
|
|
describe windows_feature('dhcp') do
|
|
|
|
it { should be_installed }
|
|
|
|
end
|
|
|
|
"
|
2015-09-20 15:42:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def initialize(feature)
|
|
|
|
@feature = feature
|
|
|
|
@cache = nil
|
2015-09-20 15:42:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# verify that this resource is only supported on Windows
|
2016-08-03 17:18:24 +00:00
|
|
|
return skip_resource 'The `windows_feature` resource is not supported on your OS.' if !inspec.os.windows?
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2015-09-20 15:42:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# returns true if the package is installed
|
|
|
|
def installed?(_provider = nil, _version = nil)
|
|
|
|
info[:installed] == true
|
|
|
|
end
|
2015-09-20 15:42:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# returns the package description
|
|
|
|
def info
|
|
|
|
return @cache if !@cache.nil?
|
|
|
|
features_cmd = "Get-WindowsFeature | Where-Object {$_.Name -eq '#{@feature}' -or $_.DisplayName -eq '#{@feature}'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json"
|
|
|
|
cmd = inspec.command(features_cmd)
|
2015-09-20 15:42:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
@cache = {
|
|
|
|
name: @feature,
|
|
|
|
type: 'windows-feature',
|
|
|
|
}
|
2015-09-20 15:42:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# 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
|
|
|
|
params = JSON.parse(cmd.stdout)
|
|
|
|
rescue JSON::ParserError => _e
|
|
|
|
return @cache
|
|
|
|
end
|
2015-09-20 15:42:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
@cache = {
|
|
|
|
name: params['Name'],
|
|
|
|
description: params['Description'],
|
|
|
|
installed: params['Installed'],
|
|
|
|
type: 'windows-feature',
|
|
|
|
}
|
|
|
|
end
|
2015-09-20 15:42:09 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def to_s
|
|
|
|
"Windows Feature '#{@feature}'"
|
|
|
|
end
|
2015-09-20 15:42:09 +00:00
|
|
|
end
|
|
|
|
end
|