inspec 0.14.8 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -2
  3. data/bin/inspec +3 -4
  4. data/examples/inheritance/README.md +19 -0
  5. data/examples/inheritance/controls/example.rb +11 -0
  6. data/examples/inheritance/inspec.yml +10 -0
  7. data/lib/bundles/inspec-compliance/cli.rb +1 -4
  8. data/lib/bundles/inspec-supermarket/cli.rb +1 -4
  9. data/lib/inspec/dsl.rb +48 -55
  10. data/lib/inspec/profile.rb +6 -2
  11. data/lib/inspec/profile_context.rb +21 -8
  12. data/lib/inspec/runner.rb +17 -12
  13. data/lib/inspec/runner_rspec.rb +1 -0
  14. data/lib/inspec/version.rb +1 -1
  15. data/lib/resources/apache.rb +20 -18
  16. data/lib/resources/apache_conf.rb +92 -90
  17. data/lib/resources/apt.rb +92 -90
  18. data/lib/resources/audit_policy.rb +35 -33
  19. data/lib/resources/auditd_conf.rb +41 -39
  20. data/lib/resources/auditd_rules.rb +155 -153
  21. data/lib/resources/bond.rb +1 -1
  22. data/lib/resources/bridge.rb +97 -95
  23. data/lib/resources/command.rb +47 -45
  24. data/lib/resources/csv.rb +23 -21
  25. data/lib/resources/directory.rb +1 -1
  26. data/lib/resources/etc_group.rb +116 -114
  27. data/lib/resources/file.rb +1 -1
  28. data/lib/resources/gem.rb +39 -37
  29. data/lib/resources/group.rb +100 -98
  30. data/lib/resources/host.rb +103 -101
  31. data/lib/resources/inetd_conf.rb +42 -40
  32. data/lib/resources/ini.rb +15 -13
  33. data/lib/resources/interface.rb +106 -104
  34. data/lib/resources/iptables.rb +36 -34
  35. data/lib/resources/json.rb +64 -62
  36. data/lib/resources/kernel_module.rb +30 -28
  37. data/lib/resources/kernel_parameter.rb +44 -42
  38. data/lib/resources/limits_conf.rb +41 -39
  39. data/lib/resources/login_def.rb +38 -36
  40. data/lib/resources/mount.rb +43 -41
  41. data/lib/resources/mysql.rb +67 -65
  42. data/lib/resources/mysql_conf.rb +89 -87
  43. data/lib/resources/mysql_session.rb +46 -44
  44. data/lib/resources/npm.rb +35 -33
  45. data/lib/resources/ntp_conf.rb +44 -42
  46. data/lib/resources/oneget.rb +46 -44
  47. data/lib/resources/os.rb +22 -20
  48. data/lib/resources/os_env.rb +47 -45
  49. data/lib/resources/package.rb +213 -211
  50. data/lib/resources/parse_config.rb +59 -57
  51. data/lib/resources/passwd.rb +89 -87
  52. data/lib/resources/pip.rb +60 -58
  53. data/lib/resources/port.rb +352 -350
  54. data/lib/resources/postgres.rb +26 -24
  55. data/lib/resources/postgres_conf.rb +66 -64
  56. data/lib/resources/postgres_session.rb +47 -45
  57. data/lib/resources/processes.rb +56 -54
  58. data/lib/resources/registry_key.rb +150 -148
  59. data/lib/resources/script.rb +30 -28
  60. data/lib/resources/security_policy.rb +56 -54
  61. data/lib/resources/service.rb +638 -636
  62. data/lib/resources/shadow.rb +98 -96
  63. data/lib/resources/ssh_conf.rb +58 -56
  64. data/lib/resources/user.rb +363 -361
  65. data/lib/resources/windows_feature.rb +46 -44
  66. data/lib/resources/xinetd.rb +111 -109
  67. data/lib/resources/yaml.rb +16 -14
  68. data/lib/resources/yum.rb +107 -105
  69. data/lib/utils/base_cli.rb +18 -0
  70. data/test/helper.rb +2 -2
  71. data/test/unit/profile_context_test.rb +1 -1
  72. data/test/unit/resources/file_test.rb +1 -1
  73. data/test/unit/resources/mount_test.rb +1 -1
  74. metadata +5 -2
data/lib/resources/npm.rb CHANGED
@@ -2,45 +2,47 @@
2
2
  # author: Christoph Hartmann
3
3
  # author: Dominik Richter
4
4
 
5
- class NpmPackage < Inspec.resource(1)
6
- name 'npm'
7
- desc 'Use the npm InSpec audit resource to test if a global npm package is installed. npm is the the package manager for Nodejs packages, such as bower and StatsD.'
8
- example "
9
- describe npm('bower') do
10
- it { should be_installed }
5
+ module Inspec::Resources
6
+ class NpmPackage < Inspec.resource(1)
7
+ name 'npm'
8
+ desc 'Use the npm InSpec audit resource to test if a global npm package is installed. npm is the the package manager for Nodejs packages, such as bower and StatsD.'
9
+ example "
10
+ describe npm('bower') do
11
+ it { should be_installed }
12
+ end
13
+ "
14
+
15
+ def initialize(package_name)
16
+ @package_name = package_name
17
+ @cache = nil
11
18
  end
12
- "
13
19
 
14
- def initialize(package_name)
15
- @package_name = package_name
16
- @cache = nil
17
- end
18
-
19
- def info
20
- return @info if defined?(@info)
20
+ def info
21
+ return @info if defined?(@info)
21
22
 
22
- cmd = inspec.command("npm ls -g --json #{@package_name}")
23
- @info = {
24
- name: @package_name,
25
- type: 'npm',
26
- installed: cmd.exit_status == 0,
27
- }
28
- return @info unless @info[:installed]
23
+ cmd = inspec.command("npm ls -g --json #{@package_name}")
24
+ @info = {
25
+ name: @package_name,
26
+ type: 'npm',
27
+ installed: cmd.exit_status == 0,
28
+ }
29
+ return @info unless @info[:installed]
29
30
 
30
- pkgs = JSON.parse(cmd.stdout)
31
- @info[:version] = pkgs['dependencies'][@package_name]['version']
32
- @info
33
- end
31
+ pkgs = JSON.parse(cmd.stdout)
32
+ @info[:version] = pkgs['dependencies'][@package_name]['version']
33
+ @info
34
+ end
34
35
 
35
- def installed?
36
- info[:installed] == true
37
- end
36
+ def installed?
37
+ info[:installed] == true
38
+ end
38
39
 
39
- def version
40
- info[:version]
41
- end
40
+ def version
41
+ info[:version]
42
+ end
42
43
 
43
- def to_s
44
- "Npm Package #{@package_name}"
44
+ def to_s
45
+ "Npm Package #{@package_name}"
46
+ end
45
47
  end
46
48
  end
@@ -6,53 +6,55 @@
6
6
 
7
7
  require 'utils/simpleconfig'
8
8
 
9
- class NtpConf < Inspec.resource(1)
10
- name 'ntp_conf'
11
- desc 'Use the ntp_conf InSpec audit resource to test the synchronization settings defined in the ntp.conf file. This file is typically located at /etc/ntp.conf.'
12
- example "
13
- describe ntp_conf do
14
- its('server') { should_not eq nil }
15
- its('restrict') { should include '-4 default kod notrap nomodify nopeer noquery'}
9
+ module Inspec::Resources
10
+ class NtpConf < Inspec.resource(1)
11
+ name 'ntp_conf'
12
+ desc 'Use the ntp_conf InSpec audit resource to test the synchronization settings defined in the ntp.conf file. This file is typically located at /etc/ntp.conf.'
13
+ example "
14
+ describe ntp_conf do
15
+ its('server') { should_not eq nil }
16
+ its('restrict') { should include '-4 default kod notrap nomodify nopeer noquery'}
17
+ end
18
+ "
19
+
20
+ def initialize(path = nil)
21
+ @conf_path = path || '/etc/ntp.conf'
16
22
  end
17
- "
18
23
 
19
- def initialize(path = nil)
20
- @conf_path = path || '/etc/ntp.conf'
21
- end
22
-
23
- def method_missing(name)
24
- param = read_params[name.to_s]
25
- # extract first value if we have only one value in array
26
- return param[0] if param.is_a?(Array) and param.length == 1
27
- param
28
- end
29
-
30
- def to_s
31
- 'ntp.conf'
32
- end
33
-
34
- private
35
-
36
- def read_params
37
- return @params if defined?(@params)
38
-
39
- if !inspec.file(@conf_path).file?
40
- skip_resource "Can't find file \"#{@conf_path}\""
41
- return @params = {}
24
+ def method_missing(name)
25
+ param = read_params[name.to_s]
26
+ # extract first value if we have only one value in array
27
+ return param[0] if param.is_a?(Array) and param.length == 1
28
+ param
42
29
  end
43
30
 
44
- content = inspec.file(@conf_path).content
45
- if content.empty? && inspec.file(@conf_path).size > 0
46
- skip_resource "Can't read file \"#{@conf_path}\""
47
- return @params = {}
31
+ def to_s
32
+ 'ntp.conf'
48
33
  end
49
34
 
50
- # parse the file
51
- conf = SimpleConfig.new(
52
- content,
53
- assignment_re: /^\s*(\S+)\s+(.*)\s*$/,
54
- multiple_values: true,
55
- )
56
- @params = conf.params
35
+ private
36
+
37
+ def read_params
38
+ return @params if defined?(@params)
39
+
40
+ if !inspec.file(@conf_path).file?
41
+ skip_resource "Can't find file \"#{@conf_path}\""
42
+ return @params = {}
43
+ end
44
+
45
+ content = inspec.file(@conf_path).content
46
+ if content.empty? && inspec.file(@conf_path).size > 0
47
+ skip_resource "Can't read file \"#{@conf_path}\""
48
+ return @params = {}
49
+ end
50
+
51
+ # parse the file
52
+ conf = SimpleConfig.new(
53
+ content,
54
+ assignment_re: /^\s*(\S+)\s+(.*)\s*$/,
55
+ multiple_values: true,
56
+ )
57
+ @params = conf.params
58
+ end
57
59
  end
58
60
  end
@@ -9,61 +9,63 @@
9
9
  # describe oneget('zoomit') do
10
10
  # it { should be_installed }
11
11
  # end
12
- class OneGetPackage < Inspec.resource(1)
13
- name 'oneget'
14
- desc 'Use the oneget InSpec audit resource to test if the named package and/or package version is installed on the system. This resource uses OneGet, which is part of the Windows Management Framework 5.0 and Windows 10. This resource uses the Get-Package cmdlet to return all of the package names in the OneGet repository.'
15
- example "
16
- describe oneget('zoomit') do
17
- it { should be_installed }
18
- end
19
- "
12
+ module Inspec::Resources
13
+ class OneGetPackage < Inspec.resource(1)
14
+ name 'oneget'
15
+ desc 'Use the oneget InSpec audit resource to test if the named package and/or package version is installed on the system. This resource uses OneGet, which is part of the Windows Management Framework 5.0 and Windows 10. This resource uses the Get-Package cmdlet to return all of the package names in the OneGet repository.'
16
+ example "
17
+ describe oneget('zoomit') do
18
+ it { should be_installed }
19
+ end
20
+ "
20
21
 
21
- def initialize(package_name)
22
- @package_name = package_name
22
+ def initialize(package_name)
23
+ @package_name = package_name
23
24
 
24
- # verify that this resource is only supported on Windows
25
- return skip_resource 'The `oneget` resource is not supported on your OS.' if inspec.os[:family] != 'windows'
26
- end
25
+ # verify that this resource is only supported on Windows
26
+ return skip_resource 'The `oneget` resource is not supported on your OS.' if inspec.os[:family] != 'windows'
27
+ end
27
28
 
28
- def info
29
- return @info if defined?(@info)
29
+ def info
30
+ return @info if defined?(@info)
30
31
 
31
- @info = {}
32
- @info[:type] = 'oneget'
33
- @info[:installed] = false
32
+ @info = {}
33
+ @info[:type] = 'oneget'
34
+ @info[:installed] = false
34
35
 
35
- cmd = inspec.command("Get-Package -Name '#{@package_name}' | ConvertTo-Json")
36
- # cannot rely on exit code for now, successful command returns exit code 1
37
- # return nil if cmd.exit_status != 0
38
- # try to parse json
36
+ cmd = inspec.command("Get-Package -Name '#{@package_name}' | ConvertTo-Json")
37
+ # cannot rely on exit code for now, successful command returns exit code 1
38
+ # return nil if cmd.exit_status != 0
39
+ # try to parse json
39
40
 
40
- begin
41
- pkgs = JSON.parse(cmd.stdout)
42
- @info[:installed] = true
41
+ begin
42
+ pkgs = JSON.parse(cmd.stdout)
43
+ @info[:installed] = true
43
44
 
44
- # sometimes we get multiple values
45
- if pkgs.is_a?(Array)
46
- # select the first entry
47
- pkgs = pkgs.first
45
+ # sometimes we get multiple values
46
+ if pkgs.is_a?(Array)
47
+ # select the first entry
48
+ pkgs = pkgs.first
49
+ end
50
+ rescue JSON::ParserError => _e
51
+ return @info
48
52
  end
49
- rescue JSON::ParserError => _e
50
- return @info
51
- end
52
53
 
53
- @info[:name] = pkgs['Name'] if pkgs.key?('Name')
54
- @info[:version] = pkgs['Version'] if pkgs.key?('Version')
55
- @info
56
- end
54
+ @info[:name] = pkgs['Name'] if pkgs.key?('Name')
55
+ @info[:version] = pkgs['Version'] if pkgs.key?('Version')
56
+ @info
57
+ end
57
58
 
58
- def installed?
59
- info[:installed] == true
60
- end
59
+ def installed?
60
+ info[:installed] == true
61
+ end
61
62
 
62
- def version
63
- info[:version]
64
- end
63
+ def version
64
+ info[:version]
65
+ end
65
66
 
66
- def to_s
67
- "OneGet Package #{@package_name}"
67
+ def to_s
68
+ "OneGet Package #{@package_name}"
69
+ end
68
70
  end
69
71
  end
data/lib/resources/os.rb CHANGED
@@ -2,29 +2,31 @@
2
2
  # author: Dominik Richter
3
3
  # author: Christoph Hartmann
4
4
 
5
- class OS < Inspec.resource(1)
6
- name 'os'
7
- desc 'Use the os InSpec audit resource to test the platform on which the system is running.'
8
- example "
9
- describe os[:family] do
10
- it { should eq 'redhat' }
11
- end
12
- "
5
+ module Inspec::Resources
6
+ class OSResource < Inspec.resource(1)
7
+ name 'os'
8
+ desc 'Use the os InSpec audit resource to test the platform on which the system is running.'
9
+ example "
10
+ describe os[:family] do
11
+ it { should eq 'redhat' }
12
+ end
13
+ "
13
14
 
14
- # reuse helper methods from backend
15
- %w{aix? redhat? debian? suse? bsd? solaris? linux? unix? windows?}.each do |os_family|
16
- define_method(os_family.to_sym) do
17
- inspec.backend.os.send(os_family)
15
+ # reuse helper methods from backend
16
+ %w{aix? redhat? debian? suse? bsd? solaris? linux? unix? windows?}.each do |os_family|
17
+ define_method(os_family.to_sym) do
18
+ inspec.backend.os.send(os_family)
19
+ end
18
20
  end
19
- end
20
21
 
21
- def [](name)
22
- # convert string to symbol
23
- name = name.to_sym if name.is_a? String
24
- inspec.backend.os[name]
25
- end
22
+ def [](name)
23
+ # convert string to symbol
24
+ name = name.to_sym if name.is_a? String
25
+ inspec.backend.os[name]
26
+ end
26
27
 
27
- def to_s
28
- 'Operating System Detection'
28
+ def to_s
29
+ 'Operating System Detection'
30
+ end
29
31
  end
30
32
  end
@@ -13,60 +13,62 @@
13
13
 
14
14
  require 'utils/simpleconfig'
15
15
 
16
- class OsEnv < Inspec.resource(1)
17
- name 'os_env'
18
- desc 'Use the os_env InSpec audit resource to test the environment variables for the platform on which the system is running.'
19
- example "
20
- describe os_env('VARIABLE') do
21
- its('matcher') { should eq 1 }
22
- end
23
- "
16
+ module Inspec::Resources
17
+ class OsEnv < Inspec.resource(1)
18
+ name 'os_env'
19
+ desc 'Use the os_env InSpec audit resource to test the environment variables for the platform on which the system is running.'
20
+ example "
21
+ describe os_env('VARIABLE') do
22
+ its('matcher') { should eq 1 }
23
+ end
24
+ "
24
25
 
25
- attr_reader :content
26
- def initialize(env = nil)
27
- @osenv = env
28
- @content = nil
29
- @content = value_for(env) unless env.nil?
30
- end
26
+ attr_reader :content
27
+ def initialize(env = nil)
28
+ @osenv = env
29
+ @content = nil
30
+ @content = value_for(env) unless env.nil?
31
+ end
31
32
 
32
- def split
33
- # we can't take advantage of `File::PATH_SEPARATOR` as code is
34
- # evaluated on the host machine
35
- path_separator = inspec.os.windows? ? ';' : ':'
36
- # -1 is required to catch cases like dir1::dir2:
37
- # where we have a trailing :
38
- @content.nil? ? [] : @content.split(path_separator, -1)
39
- end
33
+ def split
34
+ # we can't take advantage of `File::PATH_SEPARATOR` as code is
35
+ # evaluated on the host machine
36
+ path_separator = inspec.os.windows? ? ';' : ':'
37
+ # -1 is required to catch cases like dir1::dir2:
38
+ # where we have a trailing :
39
+ @content.nil? ? [] : @content.split(path_separator, -1)
40
+ end
40
41
 
41
- def to_s
42
- if @osenv.nil?
43
- 'Environment variables'
44
- else
45
- "Environment variable #{@osenv}"
42
+ def to_s
43
+ if @osenv.nil?
44
+ 'Environment variables'
45
+ else
46
+ "Environment variable #{@osenv}"
47
+ end
46
48
  end
47
- end
48
49
 
49
- private
50
+ private
50
51
 
51
- def value_for(env)
52
- command = if inspec.os.windows?
53
- "$Env:#{env}"
54
- else
55
- 'env'
56
- end
52
+ def value_for(env)
53
+ command = if inspec.os.windows?
54
+ "$Env:#{env}"
55
+ else
56
+ 'env'
57
+ end
57
58
 
58
- out = inspec.command(command)
59
+ out = inspec.command(command)
59
60
 
60
- unless out.exit_status == 0
61
- skip_resource "Can't read environment variables on #{os[:family]}. "\
62
- "Tried `#{command}` which returned #{out.exit_status}"
63
- end
61
+ unless out.exit_status == 0
62
+ skip_resource "Can't read environment variables on #{os[:family]}. "\
63
+ "Tried `#{command}` which returned #{out.exit_status}"
64
+ end
64
65
 
65
- if inspec.os.windows?
66
- out.stdout.strip
67
- else
68
- params = SimpleConfig.new(out.stdout).params
69
- params[env]
66
+ if inspec.os.windows?
67
+ out.stdout.strip
68
+ else
69
+ params = SimpleConfig.new(out.stdout).params
70
+ params[env]
71
+ end
70
72
  end
71
73
  end
72
74
  end
@@ -8,253 +8,255 @@
8
8
  # describe package('nginx') do
9
9
  # it { should be_installed }
10
10
  # end
11
- class Package < Inspec.resource(1)
12
- name 'package'
13
- desc 'Use the package InSpec audit resource to test if the named package and/or package version is installed on the system.'
14
- example "
15
- describe package('nginx') do
16
- it { should be_installed }
17
- its('version') { should eq 1.9.5 }
18
- end
19
- "
11
+ module Inspec::Resources
12
+ class Package < Inspec.resource(1)
13
+ name 'package'
14
+ desc 'Use the package InSpec audit resource to test if the named package and/or package version is installed on the system.'
15
+ example "
16
+ describe package('nginx') do
17
+ it { should be_installed }
18
+ its('version') { should eq 1.9.5 }
19
+ end
20
+ "
20
21
 
21
- def initialize(package_name = nil) # rubocop:disable Metrics/AbcSize
22
- @package_name = package_name
23
- @name = @package_name
24
- @cache = nil
25
- # select package manager
26
- @pkgman = nil
22
+ def initialize(package_name = nil) # rubocop:disable Metrics/AbcSize
23
+ @package_name = package_name
24
+ @name = @package_name
25
+ @cache = nil
26
+ # select package manager
27
+ @pkgman = nil
27
28
 
28
- os = inspec.os
29
- if os.debian?
30
- @pkgman = Deb.new(inspec)
31
- elsif os.redhat? || os.suse?
32
- @pkgman = Rpm.new(inspec)
33
- elsif ['arch'].include?(os[:family])
34
- @pkgman = Pacman.new(inspec)
35
- elsif ['darwin'].include?(os[:family])
36
- @pkgman = Brew.new(inspec)
37
- elsif inspec.os.windows?
38
- @pkgman = WindowsPkg.new(inspec)
39
- elsif ['aix'].include?(os[:family])
40
- @pkgman = BffPkg.new(inspec)
41
- elsif os.solaris?
42
- @pkgman = SolarisPkg.new(inspec)
43
- else
44
- return skip_resource 'The `package` resource is not supported on your OS yet.'
29
+ os = inspec.os
30
+ if os.debian?
31
+ @pkgman = Deb.new(inspec)
32
+ elsif os.redhat? || os.suse?
33
+ @pkgman = Rpm.new(inspec)
34
+ elsif ['arch'].include?(os[:family])
35
+ @pkgman = Pacman.new(inspec)
36
+ elsif ['darwin'].include?(os[:family])
37
+ @pkgman = Brew.new(inspec)
38
+ elsif inspec.os.windows?
39
+ @pkgman = WindowsPkg.new(inspec)
40
+ elsif ['aix'].include?(os[:family])
41
+ @pkgman = BffPkg.new(inspec)
42
+ elsif os.solaris?
43
+ @pkgman = SolarisPkg.new(inspec)
44
+ else
45
+ return skip_resource 'The `package` resource is not supported on your OS yet.'
46
+ end
45
47
  end
46
- end
47
48
 
48
- # returns true if the package is installed
49
- def installed?(_provider = nil, _version = nil)
50
- return false if info.nil?
51
- info[:installed] == true
52
- end
49
+ # returns true if the package is installed
50
+ def installed?(_provider = nil, _version = nil)
51
+ return false if info.nil?
52
+ info[:installed] == true
53
+ end
53
54
 
54
- # returns the package description
55
- def info
56
- return @cache if !@cache.nil?
57
- return nil if @pkgman.nil?
58
- @pkgman.info(@package_name)
59
- end
55
+ # returns the package description
56
+ def info
57
+ return @cache if !@cache.nil?
58
+ return nil if @pkgman.nil?
59
+ @pkgman.info(@package_name)
60
+ end
60
61
 
61
- # return the package version
62
- def version
63
- info = @pkgman.info(@package_name)
64
- return nil if info.nil?
65
- info[:version]
66
- end
62
+ # return the package version
63
+ def version
64
+ info = @pkgman.info(@package_name)
65
+ return nil if info.nil?
66
+ info[:version]
67
+ end
67
68
 
68
- def to_s
69
- "System Package #{@package_name}"
69
+ def to_s
70
+ "System Package #{@package_name}"
71
+ end
70
72
  end
71
- end
72
73
 
73
- class PkgManagement
74
- attr_reader :inspec
75
- def initialize(inspec)
76
- @inspec = inspec
74
+ class PkgManagement
75
+ attr_reader :inspec
76
+ def initialize(inspec)
77
+ @inspec = inspec
78
+ end
77
79
  end
78
- end
79
80
 
80
- # Debian / Ubuntu
81
- class Deb < PkgManagement
82
- def info(package_name)
83
- cmd = inspec.command("dpkg -s #{package_name}")
84
- return nil if cmd.exit_status.to_i != 0
81
+ # Debian / Ubuntu
82
+ class Deb < PkgManagement
83
+ def info(package_name)
84
+ cmd = inspec.command("dpkg -s #{package_name}")
85
+ return nil if cmd.exit_status.to_i != 0
85
86
 
86
- params = SimpleConfig.new(
87
- cmd.stdout.chomp,
88
- assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
89
- multiple_values: false,
90
- ).params
91
- {
92
- name: params['Package'],
93
- installed: true,
94
- version: params['Version'],
95
- type: 'deb',
96
- }
87
+ params = SimpleConfig.new(
88
+ cmd.stdout.chomp,
89
+ assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
90
+ multiple_values: false,
91
+ ).params
92
+ {
93
+ name: params['Package'],
94
+ installed: true,
95
+ version: params['Version'],
96
+ type: 'deb',
97
+ }
98
+ end
97
99
  end
98
- end
99
100
 
100
- # RHEL family
101
- class Rpm < PkgManagement
102
- def info(package_name)
103
- cmd = inspec.command("rpm -qia #{package_name}")
104
- # CentOS does not return an error code if the package is not installed,
105
- # therefore we need to check for emptyness
106
- return nil if cmd.exit_status.to_i != 0 || cmd.stdout.chomp.empty?
107
- params = SimpleConfig.new(
108
- cmd.stdout.chomp,
109
- assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
110
- multiple_values: false,
111
- ).params
112
- # On some (all?) systems, the linebreak before the vendor line is missing
113
- if params['Version'] =~ /\s*Vendor:/
114
- v = params['Version'].split(' ')[0]
115
- else
116
- v = params['Version']
117
- end
118
- # On some (all?) systems, the linebreak before the build line is missing
119
- if params['Release'] =~ /\s*Build Date:/
120
- r = params['Release'].split(' ')[0]
121
- else
122
- r = params['Release']
101
+ # RHEL family
102
+ class Rpm < PkgManagement
103
+ def info(package_name)
104
+ cmd = inspec.command("rpm -qia #{package_name}")
105
+ # CentOS does not return an error code if the package is not installed,
106
+ # therefore we need to check for emptyness
107
+ return nil if cmd.exit_status.to_i != 0 || cmd.stdout.chomp.empty?
108
+ params = SimpleConfig.new(
109
+ cmd.stdout.chomp,
110
+ assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
111
+ multiple_values: false,
112
+ ).params
113
+ # On some (all?) systems, the linebreak before the vendor line is missing
114
+ if params['Version'] =~ /\s*Vendor:/
115
+ v = params['Version'].split(' ')[0]
116
+ else
117
+ v = params['Version']
118
+ end
119
+ # On some (all?) systems, the linebreak before the build line is missing
120
+ if params['Release'] =~ /\s*Build Date:/
121
+ r = params['Release'].split(' ')[0]
122
+ else
123
+ r = params['Release']
124
+ end
125
+ {
126
+ name: params['Name'],
127
+ installed: true,
128
+ version: "#{v}-#{r}",
129
+ type: 'rpm',
130
+ }
123
131
  end
124
- {
125
- name: params['Name'],
126
- installed: true,
127
- version: "#{v}-#{r}",
128
- type: 'rpm',
129
- }
130
132
  end
131
- end
132
133
 
133
- # MacOS / Darwin implementation
134
- class Brew < PkgManagement
135
- def info(package_name)
136
- cmd = inspec.command("brew info --json=v1 #{package_name}")
137
- return nil if cmd.exit_status.to_i != 0
138
- # parse data
139
- pkg = JSON.parse(cmd.stdout)[0]
140
- {
141
- name: pkg.name.to_s,
142
- installed: true,
143
- version: pkg.installed.version.to_s,
144
- type: 'brew',
145
- }
134
+ # MacOS / Darwin implementation
135
+ class Brew < PkgManagement
136
+ def info(package_name)
137
+ cmd = inspec.command("brew info --json=v1 #{package_name}")
138
+ return nil if cmd.exit_status.to_i != 0
139
+ # parse data
140
+ pkg = JSON.parse(cmd.stdout)[0]
141
+ {
142
+ name: pkg.name.to_s,
143
+ installed: true,
144
+ version: pkg.installed.version.to_s,
145
+ type: 'brew',
146
+ }
147
+ end
146
148
  end
147
- end
148
149
 
149
- # Arch Linux
150
- class Pacman < PkgManagement
151
- def info(package_name)
152
- cmd = inspec.command("pacman -Qi #{package_name}")
153
- return nil if cmd.exit_status.to_i != 0
150
+ # Arch Linux
151
+ class Pacman < PkgManagement
152
+ def info(package_name)
153
+ cmd = inspec.command("pacman -Qi #{package_name}")
154
+ return nil if cmd.exit_status.to_i != 0
154
155
 
155
- params = SimpleConfig.new(
156
- cmd.stdout.chomp,
157
- assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
158
- multiple_values: false,
159
- ).params
156
+ params = SimpleConfig.new(
157
+ cmd.stdout.chomp,
158
+ assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
159
+ multiple_values: false,
160
+ ).params
160
161
 
161
- {
162
- name: params['Name'],
163
- installed: true,
164
- version: params['Version'],
165
- type: 'pacman',
166
- }
162
+ {
163
+ name: params['Name'],
164
+ installed: true,
165
+ version: params['Version'],
166
+ type: 'pacman',
167
+ }
168
+ end
167
169
  end
168
- end
169
170
 
170
- # Determines the installed packages on Windows
171
- # Currently we use 'Get-WmiObject -Class Win32_Product' as a detection method
172
- # TODO: evaluate if alternative methods as proposed by Microsoft are still valid:
173
- # @see: http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/15/use-powershell-to-find-installed-software.aspx
174
- class WindowsPkg < PkgManagement
175
- def info(package_name)
176
- # Find the package
177
- 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")
171
+ # Determines the installed packages on Windows
172
+ # Currently we use 'Get-WmiObject -Class Win32_Product' as a detection method
173
+ # TODO: evaluate if alternative methods as proposed by Microsoft are still valid:
174
+ # @see: http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/15/use-powershell-to-find-installed-software.aspx
175
+ class WindowsPkg < PkgManagement
176
+ def info(package_name)
177
+ # Find the package
178
+ 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")
178
179
 
179
- begin
180
- package = JSON.parse(cmd.stdout)
181
- rescue JSON::ParserError => _e
182
- return nil
183
- end
180
+ begin
181
+ package = JSON.parse(cmd.stdout)
182
+ rescue JSON::ParserError => _e
183
+ return nil
184
+ end
184
185
 
185
- {
186
- name: package['Name'],
187
- installed: true,
188
- version: package['Version'],
189
- type: 'windows',
190
- }
186
+ {
187
+ name: package['Name'],
188
+ installed: true,
189
+ version: package['Version'],
190
+ type: 'windows',
191
+ }
192
+ end
191
193
  end
192
- end
193
194
 
194
- # AIX
195
- class BffPkg < PkgManagement
196
- def info(package_name)
197
- cmd = inspec.command("lslpp -cL #{package_name}")
198
- return nil if cmd.exit_status.to_i != 0
195
+ # AIX
196
+ class BffPkg < PkgManagement
197
+ def info(package_name)
198
+ cmd = inspec.command("lslpp -cL #{package_name}")
199
+ return nil if cmd.exit_status.to_i != 0
199
200
 
200
- bff_pkg = cmd.stdout.split("\n").last.split(':')
201
- {
202
- name: bff_pkg[1],
203
- installed: true,
204
- version: bff_pkg[2],
205
- type: 'bff',
206
- }
201
+ bff_pkg = cmd.stdout.split("\n").last.split(':')
202
+ {
203
+ name: bff_pkg[1],
204
+ installed: true,
205
+ version: bff_pkg[2],
206
+ type: 'bff',
207
+ }
208
+ end
207
209
  end
208
- end
209
210
 
210
- # Solaris
211
- class SolarisPkg < PkgManagement
212
- def info(package_name)
213
- if inspec.os[:release].to_i <= 10
214
- solaris10_info(package_name)
215
- else
216
- solaris11_info(package_name)
211
+ # Solaris
212
+ class SolarisPkg < PkgManagement
213
+ def info(package_name)
214
+ if inspec.os[:release].to_i <= 10
215
+ solaris10_info(package_name)
216
+ else
217
+ solaris11_info(package_name)
218
+ end
217
219
  end
218
- end
219
220
 
220
- # solaris 10
221
- def solaris10_info(package_name)
222
- cmd = inspec.command("pkginfo -l #{package_name}")
223
- return nil if cmd.exit_status.to_i != 0
221
+ # solaris 10
222
+ def solaris10_info(package_name)
223
+ cmd = inspec.command("pkginfo -l #{package_name}")
224
+ return nil if cmd.exit_status.to_i != 0
224
225
 
225
- params = SimpleConfig.new(
226
- cmd.stdout.chomp,
227
- assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
228
- multiple_values: false,
229
- ).params
226
+ params = SimpleConfig.new(
227
+ cmd.stdout.chomp,
228
+ assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
229
+ multiple_values: false,
230
+ ).params
230
231
 
231
- # parse 11.10.0,REV=2006.05.18.01.46
232
- v = params['VERSION'].split(',')
233
- {
234
- name: params['PKGINST'],
235
- installed: true,
236
- version: v[0] + '-' + v[1].split('=')[1],
237
- type: 'pkg',
238
- }
239
- end
232
+ # parse 11.10.0,REV=2006.05.18.01.46
233
+ v = params['VERSION'].split(',')
234
+ {
235
+ name: params['PKGINST'],
236
+ installed: true,
237
+ version: v[0] + '-' + v[1].split('=')[1],
238
+ type: 'pkg',
239
+ }
240
+ end
240
241
 
241
- # solaris 11
242
- def solaris11_info(package_name)
243
- cmd = inspec.command("pkg info #{package_name}")
244
- return nil if cmd.exit_status.to_i != 0
242
+ # solaris 11
243
+ def solaris11_info(package_name)
244
+ cmd = inspec.command("pkg info #{package_name}")
245
+ return nil if cmd.exit_status.to_i != 0
245
246
 
246
- params = SimpleConfig.new(
247
- cmd.stdout.chomp,
248
- assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
249
- multiple_values: false,
250
- ).params
247
+ params = SimpleConfig.new(
248
+ cmd.stdout.chomp,
249
+ assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
250
+ multiple_values: false,
251
+ ).params
251
252
 
252
- {
253
- name: params['Name'],
254
- installed: true,
255
- # 0.5.11-0.175.3.1.0.5.0
256
- version: "#{params['Version']}-#{params['Branch']}",
257
- type: 'pkg',
258
- }
253
+ {
254
+ name: params['Name'],
255
+ installed: true,
256
+ # 0.5.11-0.175.3.1.0.5.0
257
+ version: "#{params['Version']}-#{params['Branch']}",
258
+ type: 'pkg',
259
+ }
260
+ end
259
261
  end
260
262
  end