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
|
2016-03-08 18:06:55 +00:00
|
|
|
module Inspec::Resources
|
|
|
|
class Package < Inspec.resource(1)
|
|
|
|
name 'package'
|
|
|
|
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 }
|
2017-08-18 15:29:23 +00:00
|
|
|
it { should_not be_held } # for dpkg platforms that support holding a version from being upgraded
|
2016-03-08 18:06:55 +00:00
|
|
|
its('version') { should eq 1.9.5 }
|
|
|
|
end
|
|
|
|
"
|
2015-09-08 22:05:05 +00:00
|
|
|
|
2017-12-15 18:52:41 +00:00
|
|
|
def initialize(package_name, opts = {}) # rubocop:disable Metrics/AbcSize
|
2016-03-08 18:06:55 +00:00
|
|
|
@package_name = package_name
|
|
|
|
@name = @package_name
|
|
|
|
@cache = nil
|
|
|
|
# select package manager
|
|
|
|
@pkgman = nil
|
2016-01-28 13:24:51 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
os = inspec.os
|
|
|
|
if os.debian?
|
|
|
|
@pkgman = Deb.new(inspec)
|
2017-02-14 13:24:48 +00:00
|
|
|
elsif os.redhat? || %w{suse amazon fedora}.include?(os[:family])
|
2017-06-28 10:21:15 +00:00
|
|
|
@pkgman = Rpm.new(inspec, opts)
|
2018-01-05 20:16:54 +00:00
|
|
|
elsif ['arch'].include?(os[:name])
|
2016-03-08 18:06:55 +00:00
|
|
|
@pkgman = Pacman.new(inspec)
|
|
|
|
elsif ['darwin'].include?(os[:family])
|
|
|
|
@pkgman = Brew.new(inspec)
|
|
|
|
elsif inspec.os.windows?
|
|
|
|
@pkgman = WindowsPkg.new(inspec)
|
|
|
|
elsif ['aix'].include?(os[:family])
|
|
|
|
@pkgman = BffPkg.new(inspec)
|
|
|
|
elsif os.solaris?
|
|
|
|
@pkgman = SolarisPkg.new(inspec)
|
2016-04-21 08:31:56 +00:00
|
|
|
elsif ['hpux'].include?(os[:family])
|
|
|
|
@pkgman = HpuxPkg.new(inspec)
|
2016-03-08 18:06:55 +00:00
|
|
|
else
|
2017-12-15 18:52:41 +00:00
|
|
|
raise Inspec::Exceptions::ResourceSkipped, 'The `package` resource is not supported on your OS yet.'
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2017-06-28 10:21:15 +00:00
|
|
|
|
|
|
|
evaluate_missing_requirements
|
2015-09-08 22:05:05 +00:00
|
|
|
end
|
|
|
|
|
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-08 22:05:05 +00:00
|
|
|
|
2017-08-18 15:29:23 +00:00
|
|
|
# returns true it the package is held (if the OS supports it)
|
|
|
|
def held?(_provider = nil, _version = nil)
|
|
|
|
info[:held] == true
|
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# returns the package description
|
|
|
|
def info
|
|
|
|
return @cache if !@cache.nil?
|
2018-01-16 22:27:31 +00:00
|
|
|
# All `@pkgman.info` methods return `{}`. This matches that
|
|
|
|
# behavior if `@pkgman` can't be determined, thus avoiding the
|
|
|
|
# `undefined method 'info' for nil:NilClass` error
|
|
|
|
return {} if @pkgman.nil?
|
2016-03-08 18:06:55 +00:00
|
|
|
@pkgman.info(@package_name)
|
|
|
|
end
|
2015-09-08 22:05:05 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# return the package version
|
|
|
|
def version
|
|
|
|
info = @pkgman.info(@package_name)
|
|
|
|
info[:version]
|
|
|
|
end
|
2015-09-08 22:05:05 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def to_s
|
|
|
|
"System Package #{@package_name}"
|
|
|
|
end
|
2017-06-28 10:21:15 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def evaluate_missing_requirements
|
|
|
|
missing_requirements_string = @pkgman.missing_requirements.uniq.join(', ')
|
|
|
|
return if missing_requirements_string.empty?
|
2017-12-15 18:52:41 +00:00
|
|
|
raise Inspec::Exceptions::ResourceSkipped, "The following requirements are not met for this resource: #{missing_requirements_string}"
|
2017-06-28 10:21:15 +00:00
|
|
|
end
|
2015-09-08 22:05:05 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
class PkgManagement
|
|
|
|
attr_reader :inspec
|
|
|
|
def initialize(inspec)
|
|
|
|
@inspec = inspec
|
|
|
|
end
|
2017-06-28 10:21:15 +00:00
|
|
|
|
|
|
|
def missing_requirements
|
|
|
|
# Each provider can provide an Array of missing requirements that will be
|
2017-12-15 18:52:41 +00:00
|
|
|
# combined into a `ResourceSkipped` exception message.
|
2017-06-28 10:21:15 +00:00
|
|
|
[]
|
|
|
|
end
|
2015-09-08 22:05:05 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# Debian / Ubuntu
|
|
|
|
class Deb < PkgManagement
|
|
|
|
def info(package_name)
|
|
|
|
cmd = inspec.command("dpkg -s #{package_name}")
|
2017-12-15 18:52:41 +00:00
|
|
|
return {} if cmd.exit_status.to_i != 0
|
2015-09-08 22:05:05 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
params = SimpleConfig.new(
|
|
|
|
cmd.stdout.chomp,
|
2017-04-26 21:18:14 +00:00
|
|
|
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
|
2016-03-08 18:06:55 +00:00
|
|
|
multiple_values: false,
|
|
|
|
).params
|
2017-08-18 15:29:23 +00:00
|
|
|
# If the package is installed, Status is "install ok installed"
|
|
|
|
# If the package is installed and marked hold, Status is "hold ok installed"
|
2016-09-21 07:41:23 +00:00
|
|
|
# If the package is removed and not purged, Status is "deinstall ok config-files" with exit_status 0
|
|
|
|
# If the package is purged cmd fails with non-zero exit status
|
2016-03-08 18:06:55 +00:00
|
|
|
{
|
|
|
|
name: params['Package'],
|
2017-08-18 15:29:23 +00:00
|
|
|
installed: params['Status'].split(' ')[2] == 'installed',
|
|
|
|
held: params['Status'].split(' ')[0] == 'hold',
|
2016-03-08 18:06:55 +00:00
|
|
|
version: params['Version'],
|
|
|
|
type: 'deb',
|
|
|
|
}
|
|
|
|
end
|
2015-09-08 22:05:05 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# RHEL family
|
|
|
|
class Rpm < PkgManagement
|
2017-06-28 10:21:15 +00:00
|
|
|
def initialize(inspec, opts)
|
|
|
|
super(inspec)
|
|
|
|
|
|
|
|
@dbpath = opts.fetch(:rpm_dbpath, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def missing_requirements
|
|
|
|
missing_requirements = []
|
|
|
|
|
|
|
|
unless @dbpath.nil? || inspec.directory(@dbpath).directory?
|
|
|
|
missing_requirements << "RPMDB #{@dbpath} does not exist"
|
|
|
|
end
|
|
|
|
|
|
|
|
missing_requirements
|
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def info(package_name)
|
2017-06-28 10:21:15 +00:00
|
|
|
rpm_cmd = rpm_command(package_name)
|
|
|
|
cmd = inspec.command(rpm_cmd)
|
2016-03-08 18:06:55 +00:00
|
|
|
# CentOS does not return an error code if the package is not installed,
|
|
|
|
# therefore we need to check for emptyness
|
2017-12-15 18:52:41 +00:00
|
|
|
return {} if cmd.exit_status.to_i != 0 || cmd.stdout.chomp.empty?
|
2016-03-08 18:06:55 +00:00
|
|
|
params = SimpleConfig.new(
|
|
|
|
cmd.stdout.chomp,
|
2017-04-26 21:18:14 +00:00
|
|
|
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
|
2016-03-08 18:06:55 +00:00
|
|
|
multiple_values: false,
|
|
|
|
).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
|
|
|
|
{
|
|
|
|
name: params['Name'],
|
|
|
|
installed: true,
|
|
|
|
version: "#{v}-#{r}",
|
|
|
|
type: 'rpm',
|
|
|
|
}
|
2015-11-25 01:06:08 +00:00
|
|
|
end
|
2017-06-28 10:21:15 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def rpm_command(package_name)
|
|
|
|
cmd = ''
|
|
|
|
cmd += 'rpm -qia'
|
|
|
|
cmd += " --dbpath #{@dbpath}" if @dbpath
|
|
|
|
cmd += ' ' + package_name
|
|
|
|
|
|
|
|
cmd
|
|
|
|
end
|
2015-09-08 22:05:05 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# MacOS / Darwin implementation
|
|
|
|
class Brew < PkgManagement
|
|
|
|
def info(package_name)
|
2017-09-13 12:41:09 +00:00
|
|
|
brew_path = inspec.command('brew').exist? ? 'brew' : '/usr/local/bin/brew'
|
|
|
|
cmd = inspec.command("#{brew_path} info --json=v1 #{package_name}")
|
2017-12-15 18:52:41 +00:00
|
|
|
return {} if cmd.exit_status.to_i != 0
|
2016-03-08 18:06:55 +00:00
|
|
|
# parse data
|
|
|
|
pkg = JSON.parse(cmd.stdout)[0]
|
|
|
|
{
|
2017-01-03 10:53:08 +00:00
|
|
|
name: pkg['name'],
|
2016-03-08 18:06:55 +00:00
|
|
|
installed: true,
|
2017-01-03 10:53:08 +00:00
|
|
|
version: pkg['installed'][0]['version'],
|
2016-03-08 18:06:55 +00:00
|
|
|
type: 'brew',
|
|
|
|
}
|
2017-12-15 18:52:41 +00:00
|
|
|
rescue JSON::ParserError => e
|
|
|
|
raise Inspec::Exceptions::ResourceFailed,
|
|
|
|
'Failed to parse JSON from `brew` command. ' \
|
|
|
|
"Error: #{e}"
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2015-09-08 22:05:05 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# Arch Linux
|
|
|
|
class Pacman < PkgManagement
|
|
|
|
def info(package_name)
|
|
|
|
cmd = inspec.command("pacman -Qi #{package_name}")
|
2017-12-15 18:52:41 +00:00
|
|
|
return {} if cmd.exit_status.to_i != 0
|
2015-09-08 22:05:05 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
params = SimpleConfig.new(
|
|
|
|
cmd.stdout.chomp,
|
2017-04-26 21:18:14 +00:00
|
|
|
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
|
2016-03-08 18:06:55 +00:00
|
|
|
multiple_values: false,
|
|
|
|
).params
|
2015-09-08 22:05:05 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
{
|
|
|
|
name: params['Name'],
|
|
|
|
installed: true,
|
|
|
|
version: params['Version'],
|
|
|
|
type: 'pacman',
|
|
|
|
}
|
|
|
|
end
|
2015-09-08 22:05:05 +00:00
|
|
|
end
|
2015-09-18 08:49:46 +00:00
|
|
|
|
2016-04-21 08:31:56 +00:00
|
|
|
class HpuxPkg < PkgManagement
|
|
|
|
def info(package_name)
|
|
|
|
cmd = inspec.command("swlist -l product | grep #{package_name}")
|
2017-12-15 18:52:41 +00:00
|
|
|
return {} if cmd.exit_status.to_i != 0
|
2016-04-21 08:31:56 +00:00
|
|
|
pkg = cmd.stdout.strip.split(' ')
|
|
|
|
{
|
|
|
|
name: pkg[0],
|
|
|
|
installed: true,
|
|
|
|
version: pkg[1],
|
|
|
|
type: 'pkg',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-23 23:37:52 +00:00
|
|
|
# Determines the installed packages on Windows using the Windows package registry entries.
|
2016-03-08 18:06:55 +00:00
|
|
|
# @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)
|
2016-08-23 23:37:52 +00:00
|
|
|
search_paths = [
|
|
|
|
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*',
|
2016-08-24 12:40:03 +00:00
|
|
|
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*',
|
2016-08-23 23:37:52 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# add 64 bit search paths
|
2016-08-24 12:40:03 +00:00
|
|
|
if inspec.os.arch == 'x86_64'
|
2016-08-23 23:37:52 +00:00
|
|
|
search_paths << 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
|
|
|
|
search_paths << 'HKCU:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
|
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# Find the package
|
2016-08-24 12:40:03 +00:00
|
|
|
cmd = inspec.command <<-EOF.gsub(/^\s*/, '')
|
2016-08-23 23:37:52 +00:00
|
|
|
Get-ItemProperty (@("#{search_paths.join('", "')}") | Where-Object { Test-Path $_ }) |
|
2016-10-05 16:15:02 +00:00
|
|
|
Where-Object { $_.DisplayName -like "#{package_name}" -or $_.PSChildName -like "#{package_name}" } |
|
2016-08-23 23:37:52 +00:00
|
|
|
Select-Object -Property DisplayName,DisplayVersion | ConvertTo-Json
|
|
|
|
EOF
|
2015-09-18 08:49:46 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
begin
|
|
|
|
package = JSON.parse(cmd.stdout)
|
2017-12-15 18:52:41 +00:00
|
|
|
rescue JSON::ParserError => e
|
|
|
|
raise Inspec::Exceptions::ResourceFailed,
|
|
|
|
'Failed to parse JSON from PowerShell. ' \
|
|
|
|
"Error: #{e}"
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2015-09-18 08:49:46 +00:00
|
|
|
|
2016-08-23 23:37:52 +00:00
|
|
|
# What if we match multiple packages? just pick the first one for now.
|
2016-08-24 12:40:03 +00:00
|
|
|
package = package[0] if package.is_a?(Array)
|
2016-08-23 23:37:52 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
{
|
2016-08-23 23:37:52 +00:00
|
|
|
name: package['DisplayName'],
|
2016-03-08 18:06:55 +00:00
|
|
|
installed: true,
|
2016-08-23 23:37:52 +00:00
|
|
|
version: package['DisplayVersion'],
|
2016-03-08 18:06:55 +00:00
|
|
|
type: 'windows',
|
|
|
|
}
|
|
|
|
end
|
2015-09-18 08:49:46 +00:00
|
|
|
end
|
2015-12-21 17:58:56 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# AIX
|
|
|
|
class BffPkg < PkgManagement
|
|
|
|
def info(package_name)
|
|
|
|
cmd = inspec.command("lslpp -cL #{package_name}")
|
2017-12-15 18:52:41 +00:00
|
|
|
return {} if cmd.exit_status.to_i != 0
|
2015-12-21 17:58:56 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
bff_pkg = cmd.stdout.split("\n").last.split(':')
|
|
|
|
{
|
|
|
|
name: bff_pkg[1],
|
|
|
|
installed: true,
|
|
|
|
version: bff_pkg[2],
|
|
|
|
type: 'bff',
|
|
|
|
}
|
|
|
|
end
|
2015-12-21 17:58:56 +00:00
|
|
|
end
|
2016-01-28 13:25:20 +00:00
|
|
|
|
2016-03-08 18:06:55 +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
|
2016-01-28 13:25:20 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# solaris 10
|
|
|
|
def solaris10_info(package_name)
|
|
|
|
cmd = inspec.command("pkginfo -l #{package_name}")
|
2017-12-15 18:52:41 +00:00
|
|
|
return {} if cmd.exit_status.to_i != 0
|
2016-01-28 13:25:20 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
params = SimpleConfig.new(
|
|
|
|
cmd.stdout.chomp,
|
2017-04-26 21:18:14 +00:00
|
|
|
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
|
2016-03-08 18:06:55 +00:00
|
|
|
multiple_values: false,
|
|
|
|
).params
|
2016-01-28 13:25:20 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# 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
|
2016-01-28 13:25:20 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# solaris 11
|
|
|
|
def solaris11_info(package_name)
|
|
|
|
cmd = inspec.command("pkg info #{package_name}")
|
2017-12-15 18:52:41 +00:00
|
|
|
return {} if cmd.exit_status.to_i != 0
|
2016-01-28 13:25:20 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
params = SimpleConfig.new(
|
|
|
|
cmd.stdout.chomp,
|
2017-04-26 21:18:14 +00:00
|
|
|
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
|
2016-03-08 18:06:55 +00:00
|
|
|
multiple_values: false,
|
|
|
|
).params
|
2016-01-28 13:25:20 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
{
|
|
|
|
name: params['Name'],
|
|
|
|
installed: true,
|
|
|
|
# 0.5.11-0.175.3.1.0.5.0
|
|
|
|
version: "#{params['Version']}-#{params['Branch']}",
|
|
|
|
type: 'pkg',
|
|
|
|
}
|
|
|
|
end
|
2016-01-28 13:25:20 +00:00
|
|
|
end
|
|
|
|
end
|