Skip to content

Commit f2d5c09

Browse files
arlimuschris-rock
authored andcommitted
add suse support (#15)
* add suse support * lint Signed-off-by: Dominik Richter <dominik.richter@gmail.com> * add up to date logic for patches * add control for patches Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
1 parent c03e5e2 commit f2d5c09

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This [InSpec](http://inspec.io/) profile verifies that all updates have been ins
99
- RHEL 6/7
1010
- CentOS 6/7
1111
- Ubuntu 12.04+
12+
- OpenSUSE, SuSE 11/12
1213

1314
## License
1415

controls/patches.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@
1717

1818
control 'patches' do
1919
impact 0.3
20-
title 'All operating system updates are installed'
20+
title 'All operating system package updates are installed'
2121
linux_update.updates.each { |update|
2222
describe package(update['name']) do
2323
its('version') { should eq update['version'] }
2424
end
2525
}
2626
only_if { linux_update.updates.length > 0 }
2727
end
28+
29+
control 'os-patches' do
30+
impact 0.3
31+
title 'All operating system patches are installed'
32+
linux_update.patches.each do |patch|
33+
describe patch do
34+
it { should be_nil }
35+
end
36+
end
37+
end

libraries/linux_updates.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
99

1010
require 'json'
11+
require 'rexml/document'
1112

1213
class LinuxUpdateManager < Inspec.resource(1)
1314
name 'linux_update'
@@ -29,6 +30,8 @@ def initialize
2930
@update_mgmt = RHELUpdateFetcher.new(inspec)
3031
when 'debian'
3132
@update_mgmt = UbuntuUpdateFetcher.new(inspec)
33+
when 'suse'
34+
@update_mgmt = SuseUpdateFetcher.new(inspec)
3235
end
3336
return skip_resource 'The `linux_update` resource is not supported on your OS.' if @update_mgmt.nil?
3437
end
@@ -44,6 +47,8 @@ def uptodate?
4447
return nil if @update_mgmt.nil?
4548
u = @update_mgmt.updates
4649
return false if u.nil? || !u['available'].empty?
50+
l = @update_mgmt.patches
51+
return false if l.nil? || !l.empty?
4752
true
4853
end
4954

@@ -54,6 +59,11 @@ def packages
5459
p['installed']
5560
end
5661

62+
def patches
63+
return [] if @update_mgmt.nil?
64+
@update_mgmt.patches || []
65+
end
66+
5767
def to_s
5868
'Linux Update'
5969
end
@@ -72,6 +82,10 @@ def updates
7282
[]
7383
end
7484

85+
def patches
86+
[]
87+
end
88+
7589
def parse_json(script)
7690
cmd = @inspec.bash(script)
7791
begin
@@ -82,6 +96,52 @@ def parse_json(script)
8296
end
8397
end
8498

99+
class SuseUpdateFetcher < UpdateFetcher
100+
def patches
101+
out = zypper_xml('list-updates -t patch')
102+
xml = REXML::Document.new(out)
103+
104+
extract_xml_updates(REXML::XPath.first(xml, '//update-list')) +
105+
extract_xml_updates(REXML::XPath.first(xml, '//blocked-update-list'))
106+
end
107+
108+
def updates
109+
out = zypper_xml('list-updates')
110+
xml = REXML::Document.new(out)
111+
112+
res = extract_xml_updates(REXML::XPath.first(xml, '//update-list')) +
113+
extract_xml_updates(REXML::XPath.first(xml, '//blocked-update-list'))
114+
115+
{ 'available' => res }
116+
end
117+
118+
private
119+
120+
def zypper_xml(cmd)
121+
out = @inspec.command('zypper --xmlout '+cmd)
122+
if out.exit_status != 0
123+
fail_resource('Cannot retrieve package updates from the OS: '+out.stderr)
124+
end
125+
out.stdout.force_encoding('UTF-8')
126+
end
127+
128+
def extract_xml_updates(updates_el)
129+
res = []
130+
return res if updates_el.nil?
131+
132+
REXML::XPath.each(updates_el, 'update') do |el|
133+
a = el.attributes
134+
r = { 'name' => a['name'] }
135+
r['version'] = a['edition'] unless a['arch'].nil?
136+
r['arch'] = a['arch'] unless a['arch'].nil?
137+
r['category'] = a['category'] unless a['category'].nil?
138+
r['severity'] = a['severity'] unless a['severity'].nil?
139+
res.push(r)
140+
end
141+
res
142+
end
143+
end
144+
85145
class UbuntuUpdateFetcher < UpdateFetcher
86146
def packages
87147
ubuntu_packages = ubuntu_base + <<-EOH

0 commit comments

Comments
 (0)