2015-09-08 22:13:08 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
# Usage:
|
|
|
|
# describe pip('Jinja2') do
|
|
|
|
# it { should be_installed }
|
|
|
|
# end
|
2015-10-01 20:48:07 +00:00
|
|
|
#
|
2017-08-30 20:04:22 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
module Inspec::Resources
|
|
|
|
class PipPackage < Inspec.resource(1)
|
|
|
|
name 'pip'
|
2018-02-19 14:26:49 +00:00
|
|
|
supports platform: 'unix'
|
|
|
|
supports platform: 'windows'
|
2016-03-08 18:06:55 +00:00
|
|
|
desc 'Use the pip InSpec audit resource to test packages that are installed using the pip installer.'
|
|
|
|
example "
|
|
|
|
describe pip('Jinja2') do
|
|
|
|
it { should be_installed }
|
|
|
|
end
|
2017-08-30 20:04:22 +00:00
|
|
|
|
|
|
|
describe pip('django', '/path/to/virtualenv/bin/pip') do
|
|
|
|
it { should be_installed }
|
|
|
|
its('version') { should eq('1.11.4')}
|
|
|
|
end
|
2016-03-08 18:06:55 +00:00
|
|
|
"
|
2015-09-08 22:13:08 +00:00
|
|
|
|
2017-08-30 20:04:22 +00:00
|
|
|
def initialize(package_name, pip_path = nil)
|
2016-03-08 18:06:55 +00:00
|
|
|
@package_name = package_name
|
2017-08-30 20:04:22 +00:00
|
|
|
@pip_cmd = pip_path || default_pip_path
|
2018-03-29 17:01:59 +00:00
|
|
|
|
|
|
|
return skip_resource 'pip not found' if @pip_cmd.nil?
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2015-09-08 22:13:08 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def info
|
|
|
|
return @info if defined?(@info)
|
2015-09-17 14:54:01 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
@info = {}
|
|
|
|
@info[:type] = 'pip'
|
2018-03-29 17:01:59 +00:00
|
|
|
return @info unless cmd_successful?
|
2015-09-08 22:13:08 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
params = SimpleConfig.new(
|
|
|
|
cmd.stdout,
|
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
|
|
|
|
@info[:name] = params['Name']
|
|
|
|
@info[:version] = params['Version']
|
|
|
|
@info[:installed] = true
|
|
|
|
@info
|
|
|
|
end
|
2015-09-08 22:13:08 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def installed?
|
|
|
|
info[:installed] == true
|
|
|
|
end
|
2015-09-08 22:13:08 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def version
|
|
|
|
info[:version]
|
|
|
|
end
|
2015-09-08 22:13:08 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def to_s
|
2016-09-21 10:02:36 +00:00
|
|
|
"Pip Package #{@package_name}"
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2015-10-01 21:29:31 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
private
|
2015-10-01 21:29:31 +00:00
|
|
|
|
2018-03-29 17:01:59 +00:00
|
|
|
def cmd
|
|
|
|
@__cmd ||= inspec.command("#{@pip_cmd} show #{@package_name}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def cmd_successful?
|
|
|
|
return true if cmd.exit_status == 0
|
|
|
|
|
|
|
|
if cmd.exit_status != 0
|
|
|
|
# If pip on windows is not the latest, it will create a stderr value along with stdout
|
|
|
|
# Example:
|
|
|
|
# stdout: "Name: Jinja2\r\nVersion: 2.10..."
|
|
|
|
# stderr: "You are using pip version 9.0.1, however version 9.0.3 is available..."
|
|
|
|
if inspec.os.windows? && !cmd.stdout.empty?
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Paths of Python and Pip on windows
|
|
|
|
# {"Pip" => nil, "Python" => "/path/to/python"}
|
|
|
|
#
|
|
|
|
# @return [Hash] of windows_paths
|
|
|
|
def windows_paths
|
|
|
|
return @__windows_paths if @__windows_paths
|
|
|
|
cmd = inspec.command(
|
|
|
|
'New-Object -Type PSObject |
|
|
|
|
Add-Member -MemberType NoteProperty -Name Pip -Value (Invoke-Command -ScriptBlock {where.exe pip}) -PassThru |
|
|
|
|
Add-Member -MemberType NoteProperty -Name Python -Value (Invoke-Command -ScriptBlock {where.exe python}) -PassThru |
|
|
|
|
ConvertTo-Json',
|
|
|
|
)
|
|
|
|
|
|
|
|
@__windows_paths = JSON.parse(cmd.stdout)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Default path of python pip installation
|
|
|
|
#
|
|
|
|
# @return [String] of python pip path
|
2017-08-30 20:04:22 +00:00
|
|
|
def default_pip_path
|
|
|
|
return 'pip' unless inspec.os.windows?
|
|
|
|
|
2018-03-29 17:01:59 +00:00
|
|
|
# If python is not found, return with skip_resource
|
|
|
|
return skip_resource 'python not found' if windows_paths['Python'].nil?
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# Pip is not on the default path for Windows, therefore we do some logic
|
|
|
|
# to find the binary on Windows
|
2017-08-30 20:04:22 +00:00
|
|
|
begin
|
|
|
|
# use pip if it on system path
|
2018-03-29 17:01:59 +00:00
|
|
|
pipcmd = windows_paths['Pip']
|
2017-08-30 20:04:22 +00:00
|
|
|
# calculate path on windows
|
2018-03-29 17:01:59 +00:00
|
|
|
if defined?(windows_paths['Python']) && pipcmd.nil?
|
|
|
|
return nil if windows_paths['Pip'].nil?
|
|
|
|
pipdir = windows_paths['Python'].split('\\')
|
2017-08-30 20:04:22 +00:00
|
|
|
# remove python.exe
|
|
|
|
pipdir.pop
|
|
|
|
pipcmd = pipdir.push('Scripts').push('pip.exe').join('/')
|
2015-10-01 21:29:31 +00:00
|
|
|
end
|
2017-08-30 20:04:22 +00:00
|
|
|
rescue JSON::ParserError => _e
|
|
|
|
return nil
|
2015-10-01 21:29:31 +00:00
|
|
|
end
|
2017-08-30 20:04:22 +00:00
|
|
|
|
|
|
|
pipcmd
|
2015-10-01 21:29:31 +00:00
|
|
|
end
|
|
|
|
end
|
2015-09-08 22:13:08 +00:00
|
|
|
end
|