From 1811eb6666321659ada2a3fde66f4b70e9d102bb Mon Sep 17 00:00:00 2001 From: Victoria Jeffrey Date: Mon, 9 May 2016 15:19:56 -0400 Subject: [PATCH] Expose label for processes only on linux --- lib/resources/processes.rb | 67 ++++++++++++++++++++------- test/helper.rb | 1 + test/unit/mock/cmd/ps-auxZ | 3 ++ test/unit/resources/processes_test.rb | 31 ++++++++++++- 4 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 test/unit/mock/cmd/ps-auxZ diff --git a/lib/resources/processes.rb b/lib/resources/processes.rb index bf084f1d5..46e796f7f 100644 --- a/lib/resources/processes.rb +++ b/lib/resources/processes.rb @@ -45,30 +45,61 @@ module Inspec::Resources private def ps_aux - # get all running processes - cmd = inspec.command('ps aux') + os = inspec.os + + if os.linux? + command = 'ps auxZ' + regex = /^([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+(.*)$/ + else + command = 'ps aux' + regex = /^([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+(.*)$/ + end + build_process_list(command, regex, os) + end + + def build_process_list(command, regex, os) # rubocop:disable MethodLength, Metrics/AbcSize + cmd = inspec.command(command) all = cmd.stdout.split("\n")[1..-1] return [] if all.nil? lines = all.map do |line| - # user 32296 0.0 0.0 42592 7972 pts/15 Ss+ Apr06 0:00 zsh - line.match(/^([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+(.*)$/) + line.match(regex) end.compact - lines.map do |m| - { - user: m[1], - pid: m[2].to_i, - cpu: m[3], - mem: m[4], - vsz: m[5].to_i, - rss: m[6].to_i, - tty: m[7], - stat: m[8], - start: m[9], - time: m[10], - command: m[11], - } + if os.linux? + lines.map do |m| + { + label: m[1], + user: m[2], + pid: m[3].to_i, + cpu: m[4], + mem: m[5], + vsz: m[6].to_i, + rss: m[7].to_i, + tty: m[8], + stat: m[9], + start: m[10], + time: m[11], + command: m[12], + } + end + else + lines.map do |m| + { + label: nil, + user: m[1], + pid: m[2].to_i, + cpu: m[3], + mem: m[4], + vsz: m[5].to_i, + rss: m[6].to_i, + tty: m[7], + stat: m[8], + start: m[9], + time: m[10], + command: m[11], + } + end end end end diff --git a/test/helper.rb b/test/helper.rb index 07aeb0408..0027e3ecc 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -137,6 +137,7 @@ class MockLoader mock.commands = { 'ps aux' => cmd.call('ps-aux'), + 'ps auxZ' => cmd.call('ps-auxZ'), 'Get-Content win_secpol.cfg' => cmd.call('secedit-export'), 'secedit /export /cfg win_secpol.cfg' => cmd.call('success'), 'Remove-Item win_secpol.cfg' => cmd.call('success'), diff --git a/test/unit/mock/cmd/ps-auxZ b/test/unit/mock/cmd/ps-auxZ new file mode 100644 index 000000000..a70f437da --- /dev/null +++ b/test/unit/mock/cmd/ps-auxZ @@ -0,0 +1,3 @@ +LABEL USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND +system_u:system_r:kernel_t:s0 root 1 0.0 0.0 19232 1492 ? Ss May04 0:01 /sbin/init +system_u:system_r:kernel_t:s0 root 39 0.0 0.0 0 0 ? S May04 0:00 crypto/0 diff --git a/test/unit/resources/processes_test.rb b/test/unit/resources/processes_test.rb index bfc65c099..1e1476866 100644 --- a/test/unit/resources/processes_test.rb +++ b/test/unit/resources/processes_test.rb @@ -12,8 +12,9 @@ describe 'Inspec::Resources::Processes' do end it 'verify processes resource' do - resource = load_resource('processes', '/bin/bash') + resource = MockLoader.new(:freebsd10).load_resource('processes', '/bin/bash') _(resource.list).must_equal [{ + label: nil, user: 'root', pid: 1, cpu: '0.0', @@ -30,9 +31,35 @@ describe 'Inspec::Resources::Processes' do _(resource.list.length).must_equal 1 end + it 'verify processes resource on linux os' do + resource = MockLoader.new(:centos6).load_resource('processes', '/sbin/init') + _(resource.list).must_equal [{ + label: 'system_u:system_r:kernel_t:s0', + user: 'root', + pid: 1, + cpu: '0.0', + mem: '0.0', + vsz: 19232, + rss: 1492, + tty: '?', + stat: 'Ss', + start: 'May04', + time: '0:01', + command: '/sbin/init', + }] + + _(resource.list.length).must_equal 1 + end + it 'retrieves the users and states as arrays' do - resource = load_resource('processes', 'svc') + resource = MockLoader.new(:freebsd10).load_resource('processes', 'svc') _(resource.users.sort).must_equal ['noot'] _(resource.states.sort).must_equal ['S', 'Ss'] end + + it 'retrieves the users and states as arrays on linux os' do + resource = MockLoader.new(:centos6).load_resource('processes', 'crypto/0') + _(resource.users.sort).must_equal ['root'] + _(resource.states.sort).must_equal ['S'] + end end