inspec/lib/resources/package.rb

261 lines
6.4 KiB
Ruby
Raw Normal View History

2015-09-08 22:05:05 +00:00
# encoding: utf-8
2015-10-06 16:55:44 +00:00
# author: Christoph Hartmann
# author: Dominik Richter
2015-09-08 22:05:05 +00:00
# Resource to determine package information
#
# Usage:
# describe package('nginx') do
# it { should be_installed }
# end
2015-10-26 03:04:18 +00:00
class Package < Inspec.resource(1)
2015-09-08 22:05:05 +00:00
name 'package'
2015-11-27 13:02:38 +00:00
desc 'Use the package InSpec audit resource to test if the named package and/or package version is installed on the system.'
example "
describe package('nginx') do
it { should be_installed }
its('version') { should eq 1.9.5 }
end
"
2015-09-08 22:05:05 +00:00
2016-01-28 13:25:20 +00:00
def initialize(package_name = nil) # rubocop:disable Metrics/AbcSize
2015-09-08 22:05:05 +00:00
@package_name = package_name
@name = @package_name
@cache = nil
# select package manager
@pkgman = nil
os = inspec.os
if os.debian?
2015-10-26 03:04:18 +00:00
@pkgman = Deb.new(inspec)
elsif os.redhat? || os.suse?
2015-10-26 03:04:18 +00:00
@pkgman = Rpm.new(inspec)
elsif ['arch'].include?(os[:family])
2015-10-26 03:04:18 +00:00
@pkgman = Pacman.new(inspec)
elsif ['darwin'].include?(os[:family])
2015-10-26 03:04:18 +00:00
@pkgman = Brew.new(inspec)
elsif inspec.os.windows?
2015-10-26 03:04:18 +00:00
@pkgman = WindowsPkg.new(inspec)
elsif ['aix'].include?(os[:family])
2015-12-21 17:58:56 +00:00
@pkgman = BffPkg.new(inspec)
2016-01-28 13:25:20 +00:00
elsif os.solaris?
@pkgman = SolarisPkg.new(inspec)
2015-09-08 22:05:05 +00:00
else
return skip_resource 'The `package` resource is not supported on your OS yet.'
2015-09-08 22:05:05 +00:00
end
end
# returns true if the package is installed
2015-09-23 13:57:41 +00:00
def installed?(_provider = nil, _version = nil)
return false if info.nil?
info[:installed] == true
2015-09-08 22:05:05 +00:00
end
# returns the package description
def info
return @cache if !@cache.nil?
return nil if @pkgman.nil?
2015-09-08 22:05:05 +00:00
@pkgman.info(@package_name)
end
# return the package version
def version
info = @pkgman.info(@package_name)
return nil if info.nil?
info[:version]
end
def to_s
"System Package #{@package_name}"
2015-09-08 22:05:05 +00:00
end
end
class PkgManagement
2015-10-26 03:04:18 +00:00
attr_reader :inspec
def initialize(inspec)
@inspec = inspec
2015-09-08 22:05:05 +00:00
end
end
# Debian / Ubuntu
class Deb < PkgManagement
def info(package_name)
2015-10-26 03:04:18 +00:00
cmd = inspec.command("dpkg -s #{package_name}")
2015-09-08 22:05:05 +00:00
return nil if cmd.exit_status.to_i != 0
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
2015-09-08 22:05:05 +00:00
).params
{
name: params['Package'],
installed: true,
version: params['Version'],
type: 'deb',
2015-09-08 22:05:05 +00:00
}
end
end
# RHEL family
class Rpm < PkgManagement
def info(package_name)
2015-10-26 03:04:18 +00:00
cmd = inspec.command("rpm -qia #{package_name}")
# CentOS does not return an error code if the package is not installed,
# therefore we need to check for emptyness
return nil if cmd.exit_status.to_i != 0 || cmd.stdout.chomp.empty?
2015-09-08 22:05:05 +00:00
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
2015-09-08 22:05:05 +00:00
).params
# On some (all?) systems, the linebreak before the vendor line is missing
if params['Version'] =~ /\s*Vendor:/
v = params['Version'].split(' ')[0]
else
v = params['Version']
end
# On some (all?) systems, the linebreak before the build line is missing
if params['Release'] =~ /\s*Build Date:/
r = params['Release'].split(' ')[0]
else
r = params['Release']
end
2015-09-08 22:05:05 +00:00
{
name: params['Name'],
installed: true,
version: "#{v}-#{r}",
type: 'rpm',
2015-09-08 22:05:05 +00:00
}
end
end
# MacOS / Darwin implementation
class Brew < PkgManagement
def info(package_name)
2015-10-26 03:04:18 +00:00
cmd = inspec.command("brew info --json=v1 #{package_name}")
2015-09-08 22:05:05 +00:00
return nil if cmd.exit_status.to_i != 0
# parse data
pkg = JSON.parse(cmd.stdout)[0]
{
2016-01-15 02:59:00 +00:00
name: pkg.name.to_s,
2015-09-08 22:05:05 +00:00
installed: true,
2016-01-15 02:59:00 +00:00
version: pkg.installed.version.to_s,
type: 'brew',
2015-09-08 22:05:05 +00:00
}
end
end
# Arch Linux
class Pacman < PkgManagement
def info(package_name)
2015-10-26 03:04:18 +00:00
cmd = inspec.command("pacman -Qi #{package_name}")
2015-09-08 22:05:05 +00:00
return nil if cmd.exit_status.to_i != 0
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
2015-09-08 22:05:05 +00:00
).params
{
name: params['Name'],
installed: true,
version: params['Version'],
type: 'pacman',
2015-09-08 22:05:05 +00:00
}
end
end
2015-09-18 08:49:46 +00:00
# Determines the installed packages on Windows
# Currently we use 'Get-WmiObject -Class Win32_Product' as a detection method
# TODO: evaluate if alternative methods as proposed by Microsoft are still valid:
# @see: http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/15/use-powershell-to-find-installed-software.aspx
class WindowsPkg < PkgManagement
def info(package_name)
# Find the package
2015-10-26 03:04:18 +00:00
cmd = inspec.command("Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq '#{package_name}'} | Select-Object -Property Name,Version,Vendor,PackageCode,Caption,Description | ConvertTo-Json")
2015-09-18 08:49:46 +00:00
begin
package = JSON.parse(cmd.stdout)
rescue JSON::ParserError => _e
return nil
end
{
name: package['Name'],
installed: true,
version: package['Version'],
type: 'windows',
}
end
end
2015-12-21 17:58:56 +00:00
# AIX
class BffPkg < PkgManagement
def info(package_name)
cmd = inspec.command("lslpp -cL #{package_name}")
return nil if cmd.exit_status.to_i != 0
2015-12-23 19:32:32 +00:00
bff_pkg = cmd.stdout.split("\n").last.split(':')
2015-12-21 17:58:56 +00:00
{
name: bff_pkg[1],
installed: true,
version: bff_pkg[2],
type: 'bff',
}
end
end
2016-01-28 13:25:20 +00:00
# Solaris
class SolarisPkg < PkgManagement
def info(package_name)
if inspec.os[:release].to_i <= 10
solaris10_info(package_name)
else
solaris11_info(package_name)
end
end
# solaris 10
def solaris10_info(package_name)
cmd = inspec.command("pkginfo -l #{package_name}")
return nil if cmd.exit_status.to_i != 0
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
).params
# parse 11.10.0,REV=2006.05.18.01.46
v = params['VERSION'].split(',')
{
name: params['PKGINST'],
installed: true,
version: v[0] + '-' + v[1].split('=')[1],
type: 'pkg',
}
end
# solaris 11
def solaris11_info(package_name)
cmd = inspec.command("pkg info #{package_name}")
return nil if cmd.exit_status.to_i != 0
params = SimpleConfig.new(
cmd.stdout.chomp,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
).params
{
name: params['Name'],
installed: true,
# 0.5.11-0.175.3.1.0.5.0
version: "#{params['Version']}-#{params['Branch']}",
type: 'pkg',
}
end
end