mirror of
https://github.com/inspec/inspec
synced 2024-11-23 13:13:22 +00:00
93 lines
2.1 KiB
Ruby
93 lines
2.1 KiB
Ruby
|
# encoding: utf-8
|
||
|
|
||
|
module Inspec::Resources
|
||
|
class PlatformResource < Inspec.resource(1)
|
||
|
name 'platform'
|
||
|
desc 'Use the platform InSpec resource to test the platform on which the system is running.'
|
||
|
example "
|
||
|
describe platform do
|
||
|
its('name') { should eq 'redhat' }
|
||
|
end
|
||
|
|
||
|
describe platform do
|
||
|
it { should be_in_family('unix') }
|
||
|
end
|
||
|
"
|
||
|
|
||
|
def initialize
|
||
|
@platform = inspec.backend.os
|
||
|
end
|
||
|
|
||
|
# add helper methods for easy access of properties
|
||
|
%w{name family release arch}.each do |property|
|
||
|
define_method(property.to_sym) do
|
||
|
@platform.send(property)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def [](name)
|
||
|
# convert string to symbol
|
||
|
name = name.to_sym if name.is_a? String
|
||
|
@platform[name]
|
||
|
end
|
||
|
|
||
|
def platform?(name)
|
||
|
@platform.name == name ||
|
||
|
@platform.family_hierarchy.include?(name)
|
||
|
end
|
||
|
|
||
|
def in_family?(family)
|
||
|
@platform.family_hierarchy.include?(family)
|
||
|
end
|
||
|
|
||
|
def families
|
||
|
@platform.family_hierarchy
|
||
|
end
|
||
|
|
||
|
def supported?(supports)
|
||
|
return true if supports.nil? || supports.empty?
|
||
|
|
||
|
status = true
|
||
|
supports.each do |s|
|
||
|
s.each do |k, v|
|
||
|
# ignore the inspec check for supports
|
||
|
# TODO: remove in inspec 2.0
|
||
|
if k == :inspec
|
||
|
next
|
||
|
elsif %i(os_family os-family platform_family platform-family).include?(k)
|
||
|
status = in_family?(v)
|
||
|
elsif %i(os platform).include?(k)
|
||
|
status = platform?(v)
|
||
|
elsif %i(os_name os-name platform_name platform-name).include?(k)
|
||
|
status = name == v
|
||
|
elsif k == :release
|
||
|
status = check_release(v)
|
||
|
else
|
||
|
status = false
|
||
|
end
|
||
|
break if status == false
|
||
|
end
|
||
|
return true if status == true
|
||
|
end
|
||
|
|
||
|
status
|
||
|
end
|
||
|
|
||
|
def to_s
|
||
|
'Platform Detection'
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def check_release(value)
|
||
|
# allow wild card matching
|
||
|
if value.include?('*')
|
||
|
cleaned = Regexp.escape(value).gsub('\*', '.*?')
|
||
|
!(release =~ /#{cleaned}/).nil?
|
||
|
else
|
||
|
release == value
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|