2018-01-02 19:04:13 +00:00
|
|
|
# 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
|
2018-02-06 18:40:21 +00:00
|
|
|
@platform = inspec.backend.platform
|
2018-01-02 19:04:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# add helper methods for easy access of properties
|
2018-01-04 19:15:02 +00:00
|
|
|
%w{family release arch}.each do |property|
|
2018-01-02 19:04:13 +00:00
|
|
|
define_method(property.to_sym) do
|
2018-02-06 18:40:21 +00:00
|
|
|
@platform[property]
|
2018-01-02 19:04:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-04 19:15:02 +00:00
|
|
|
# This is a string override for platform.name.
|
|
|
|
# TODO: removed in inspec 2.0
|
|
|
|
class NameCleaned < String
|
|
|
|
def ==(other)
|
|
|
|
if other =~ /[A-Z ]/
|
|
|
|
cleaned = other.downcase.tr(' ', '_')
|
|
|
|
Inspec::Log.warn "[DEPRECATED] Platform names will become lowercase in InSpec 2.0. Please match on '#{cleaned}' instead of '#{other}'"
|
|
|
|
super(cleaned)
|
|
|
|
else
|
|
|
|
super(other)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
NameCleaned.new(@platform.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](key)
|
2018-01-02 19:04:13 +00:00
|
|
|
# convert string to symbol
|
2018-01-04 19:15:02 +00:00
|
|
|
key = key.to_sym if key.is_a? String
|
|
|
|
return name if key == :name
|
|
|
|
|
|
|
|
@platform[key]
|
2018-01-02 19:04:13 +00:00
|
|
|
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
|