Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 224 additions & 0 deletions lib/inspec/platform.rb.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
require 'inspec/resource'
require 'rubygems/version'

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 <<~EXAMPLE
describe platform do
its('name') { should eq 'redhat' }
end

describe platform do
it { should be_in_family('unix') }
end
EXAMPLE

class Version < Gem::Version
def initialize(version_string)
super(version_string)
end

def major
segments[0]
end

def minor
segments[1]
end

def patch
segments[2]
end

def build
segments[3..-1].join('.') if segments.size > 3
end

def to_s
@version.to_s
end

def <=>(other)
other_version = convert_to_version(other)
puts "Comparing #{@version} with #{other_version}"
@version <=> other_version
end

def eql?(other)
other_version = convert_to_version(other)
puts "Comparing #{@version} with #{other_version}"
@version.eql?(other_version)
end

def ==(other)
other_version = convert_to_version(other)
puts "Comparing #{@version} with #{other_version}"
@version == other_version
end

def ===(other)
other_version = convert_to_version(other)
puts "Comparing #{@version} with #{other_version}"
@version === other_version
end

def <=(other)
other_version = convert_to_version(other)
puts "Comparing #{@version} with #{other_version}"
@version <= other_version
end

def >=(other)
other_version = convert_to_version(other)
puts "Comparing #{@version} with #{other_version}"
@version >= other_version
end

def <(other)
other_version = convert_to_version(other)
puts "Comparing #{@version} with #{other_version}"
@version < other_version
end

def >(other)
other_version = convert_to_version(other)
puts "Comparing #{@version} with #{other_version}"
@version > other_version
end

private

def convert_to_version(value)
value = format('%.2f', value) if value.is_a?(Numeric)
Gem::Version.new(value.to_s)
end
end

def initialize
@platform = inspec.backend.platform
end

def family
@platform[:family]
end

def release
@platform[:release]
end

def arch
@platform[:arch]
end

def families
@platform[:family_hierarchy]
end

def name
@platform[:name]
end

def [](key)
# convert string to symbol
key = key.to_sym if key.is_a?(String)
return name if key == :name

# For other keys, retrieve them directly from the Train platform object:
case key
when :family
family
when :release
release
when :arch
arch
when :family_hierarchy
families
else
@platform[key]
end
end

def platform?(name)
@platform.name == name ||
@platform.family_hierarchy.include?(name)
end

def in_family?(family)
@platform.family_hierarchy.include?(family)
end

def version
Version.new(release)
end

def params
{
name:,
families:,
release:,
arch:,
version: {
major: version.major,
minor: version.minor,
patch: version.patch,
build: version.build
}
}.tap { |h| h.delete(:arch) if in_family?('api') }
end

def supported?(supports)
raise ArgumentError, '`supports` is nil.' unless supports

supports.any? do |support|
support.all? do |k, v|
case normalize(k)
when :os_family, :platform_family
in_family?(v)
when :os, :platform
platform?(v)
when :os_name, :platform_name
check_name(v)
when :release
check_release(v)
end
end
end || supports.empty?
end

def normalize(key) # TODO: dumb... push this up or remove need
key.to_s.tr('-', '_').to_sym
end

def resource_id
@platform.name || 'platform'
end

def to_s
'Platform Detection'
end

private

def check_name(value)
# allow wild card matching
if value include?('*')
cleaned = Regexp.escape(value).gsub('\*', '.*?')
name =~ /#{cleaned}/
else
name == value
end
end

def check_release(value)
# allow wild card matching
if value include?('*')
cleaned = Regexp.escape(value).gsub('\*', '.*?')
release =~ /#{cleaned}/
else
release == value
end
end
end
end
73 changes: 64 additions & 9 deletions lib/inspec/resources/os.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require "inspec/resources/platform"
require 'inspec/resources/platform'

module Inspec::Resources
class OSResource < PlatformResource
name "os"
supports platform: "unix"
supports platform: "windows"
desc "Use the os InSpec audit resource to test the platform on which the system is running."
name 'os'
supports platform: 'unix'
supports platform: 'windows'
desc 'Use the os InSpec audit resource to test the platform on which the system is running.'
example <<~EXAMPLE
describe os[:family] do
it { should eq 'redhat' }
Expand All @@ -20,19 +20,74 @@ class OSResource < PlatformResource
end
EXAMPLE

def initialize
super
@platform = inspec.backend.platform
end

# reuse helper methods from backend
%w{aix? redhat? debian? suse? bsd? solaris? linux? unix? windows? hpux? darwin?}.each do |os_family|
%w[aix? redhat? debian? suse? bsd? solaris? linux? unix? windows? hpux? darwin?].each do |os_family|
define_method(os_family.to_sym) do
@platform.send(os_family)
platform.send(os_family)
end
end

def resource_id
@platform.name || "OS"
platform_info[:name] || 'OS'
end

def to_s
"Operating System Detection"
'Operating System Detection'
end

def release
platform_specific_info('release') || platform_info[:release]
end

def version
Version.new(release, platform_specific_info('build'))
end

def params
{
name: platform_info[:name],
families: platform_info[:families],
release: platform_info[:release],
arch: platform_info[:arch],
version: {
major: version.major,
minor: version.minor,
patch: version.patch,
build: version.build
}
}.tap { |h| h.delete(:arch) if in_family?('api') }
end

def platform
@platform ||= super
end

private

def platform_info
@platform_info ||= {
name: platform[:name],
families: platform[:family_hierarchy],
release: platform[:release],
arch: platform[:arch]
}
end

def platform_specific_info(type)
case platform_info[:families].first
when 'darwin'
case type
when 'release'
inspec.command('sw_vers -productVersion').stdout.strip
when 'build'
inspec.command('sw_vers -buildVersion').stdout.strip
end
end
end
end
end
Loading