Skip to content

Commit cda0d89

Browse files
authored
Fix lint (#45)
* update code to conform to new linting rules Signed-off-by: Martin Schurz <Martin.Schurz@t-systems.com> * update code to conform to new linting rules Signed-off-by: Martin Schurz <Martin.Schurz@t-systems.com> * make rubocop happy Signed-off-by: Martin Schurz <Martin.Schurz@t-systems.com>
1 parent 1eb1085 commit cda0d89

File tree

4 files changed

+55
-46
lines changed

4 files changed

+55
-46
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ Metrics/PerceivedComplexity:
2626
Max: 10
2727
Metrics/AbcSize:
2828
Max: 30
29+
# Lint/AmbiguousBlockAssociation is incompatible with RSpec
30+
# https://github.com/rubocop-hq/rubocop/issues/4222
31+
Lint/AmbiguousBlockAssociation:
32+
Enabled: false

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env rake
22
# encoding: utf-8
3+
# frozen_string_literal: true
34

45
require 'rake/testtask'
56
require 'rubocop/rake_task'

controls/patches.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: utf-8
2+
# frozen_string_literal: true
3+
24
# copyright: 2016, Christoph Hartmann
35
# copyright: 2016, Dominik Richter
46
# license: MPLv2
@@ -18,12 +20,12 @@
1820
control 'patches' do
1921
impact 0.3
2022
title 'All operating system package updates are installed'
21-
linux_update.updates.each { |update|
23+
linux_update.updates.each do |update|
2224
describe package(update['name']) do
2325
its('version') { should eq update['version'] }
2426
end
25-
}
26-
only_if { linux_update.updates.length > 0 }
27+
end
28+
only_if { linux_update.updates.length.positive? }
2729
end
2830

2931
control 'os-patches' do

libraries/linux_updates.rb

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# encoding: utf-8
2+
# frozen_string_literal: true
3+
24
# copyright: 2016, Christoph Hartmann
35
# copyright: 2016, Dominik Richter
46
# license: MPLv2
@@ -25,6 +27,7 @@ class LinuxUpdateManager < Inspec.resource(1)
2527

2628
# Since Amazon Linux is based on RedHat, they may use the same method.
2729
def initialize
30+
super
2831
case inspec.os[:family]
2932
when 'redhat', 'amazon'
3033
@update_mgmt = RHELUpdateFetcher.new(inspec)
@@ -99,17 +102,17 @@ def parse_json(script)
99102
begin
100103
JSON.parse(cmd.stdout)
101104
rescue JSON::ParserError => _e
102-
return []
105+
[]
103106
end
104107
end
105108
end
106109

107110
PatchEntry = Struct.new(:name, :version, :arch, :category, :severity) do
108111
def to_s
109112
r = "System Patch #{name} (v#{version} #{arch}"
110-
r+= ", #{category}" unless category.nil?
111-
r+= ", #{severity}" unless severity.nil?
112-
r + ')'
113+
r += ", #{category}" unless category.nil?
114+
r += ", #{severity}" unless severity.nil?
115+
"#{r})"
113116
end
114117
end
115118

@@ -135,10 +138,8 @@ def updates
135138
private
136139

137140
def zypper_xml(cmd)
138-
out = @inspec.command('zypper --xmlout '+cmd)
139-
if out.exit_status != 0
140-
fail_resource('Cannot retrieve package updates from the OS: '+out.stderr)
141-
end
141+
out = @inspec.command("zypper --xmlout #{cmd}")
142+
fail_resource("Cannot retrieve package updates from the OS: #{out.stderr}") if out.exit_status != 0
142143
out.stdout.force_encoding('UTF-8')
143144
end
144145

@@ -149,7 +150,7 @@ def extract_xml_updates(updates_el)
149150
REXML::XPath.each(updates_el, 'update') do |el|
150151
a = el.attributes
151152
res.push(
152-
PatchEntry.new(a['name'], a['edition'], a['arch'], a['category'], a['severity']),
153+
PatchEntry.new(a['name'], a['edition'], a['arch'], a['category'], a['severity'])
153154
)
154155
end
155156
res
@@ -158,65 +159,66 @@ def extract_xml_updates(updates_el)
158159

159160
class DebianUpdateFetcher < UpdateFetcher
160161
def packages
161-
debian_packages = debian_base + <<-PRINT_JSON
162-
echo -n '{"installed":['
163-
dpkg-query -W -f='${Status}\\t${Package}\\t${Version}\\t${Architecture}\\n' |\\
164-
grep '^install ok installed\\s' |\\
165-
awk '{ printf "{\\"name\\":\\""$4"\\",\\"version\\":\\""$5"\\",\\"arch\\":\\""$6"\\"}," }' | rev | cut -c 2- | rev | tr -d '\\n'
166-
echo -n ']}'
162+
debian_packages = debian_base + <<~PRINT_JSON
163+
echo -n '{"installed":['
164+
dpkg-query -W -f='${Status}\\t${Package}\\t${Version}\\t${Architecture}\\n' |\\
165+
grep '^install ok installed\\s' |\\
166+
awk '{ printf "{\\"name\\":\\""$4"\\",\\"version\\":\\""$5"\\",\\"arch\\":\\""$6"\\"}," }' | rev | cut -c 2- | rev | tr -d '\\n'
167+
echo -n ']}'
167168
PRINT_JSON
168169
parse_json(debian_packages)
169170
end
170171

171172
def updates
172-
debian_updates = debian_base + <<-PRINT_JSON
173-
echo -n '{"available":['
174-
DEBIAN_FRONTEND=noninteractive apt upgrade --dry-run | grep Inst | tr -d '[]()' |\\
175-
awk '{ printf "{\\"name\\":\\""$2"\\",\\"version\\":\\""$4"\\",\\"repo\\":\\""$5"\\",\\"arch\\":\\""$6"\\"}," }' | rev | cut -c 2- | rev | tr -d '\\n'
176-
echo -n ']}'
173+
debian_updates = debian_base + <<~PRINT_JSON
174+
echo -n '{"available":['
175+
DEBIAN_FRONTEND=noninteractive apt upgrade --dry-run | grep Inst | tr -d '[]()' |\\
176+
awk '{ printf "{\\"name\\":\\""$2"\\",\\"version\\":\\""$4"\\",\\"repo\\":\\""$5"\\",\\"arch\\":\\""$6"\\"}," }' | rev | cut -c 2- | rev | tr -d '\\n'
177+
echo -n ']}'
177178
PRINT_JSON
178179
parse_json(debian_updates)
179180
end
180181

181182
private
182183

183184
def debian_base
184-
base = <<-PRINT_JSON
185-
#!/bin/sh
186-
COMMAND="DEBIAN_FRONTEND=noninteractive apt update >>/dev/null 2>&1"
187-
eval $COMMAND
188-
while [ $? -ne 0 ]
189-
do
190-
sleep 30s
191-
eval $COMMAND
192-
done
193-
echo " "
185+
<<~PRINT_JSON
186+
#!/bin/sh
187+
COMMAND="DEBIAN_FRONTEND=noninteractive apt update >>/dev/null 2>&1"
188+
eval $COMMAND
189+
while [ $? -ne 0 ]
190+
do
191+
sleep 30s
192+
eval $COMMAND
193+
done
194+
echo " "
194195
PRINT_JSON
195-
base
196196
end
197197
end
198198

199199
class RHELUpdateFetcher < UpdateFetcher
200200
def packages
201-
rhel_packages = <<-PRINT_JSON
202-
sleep 2 && echo " "
203-
echo -n '{"installed":['
204-
rpm -qa --queryformat '"name":"%{NAME}","version":"%{VERSION}-%{RELEASE}","arch":"%{ARCH}"\\n' |\\
205-
awk '{ printf "{"$1"}," }' | rev | cut -c 2- | rev | tr -d '\\n'
206-
echo -n ']}'
201+
# rubocop:disable Style/FormatStringToken
202+
rhel_packages = <<~PRINT_JSON
203+
sleep 2 && echo " "
204+
echo -n '{"installed":['
205+
rpm -qa --queryformat '"name":"%{NAME}","version":"%{VERSION}-%{RELEASE}","arch":"%{ARCH}"\\n' |\\
206+
awk '{ printf "{"$1"}," }' | rev | cut -c 2- | rev | tr -d '\\n'
207+
echo -n ']}'
207208
PRINT_JSON
208209
parse_json(rhel_packages)
210+
# rubocop:enable Style/FormatStringToken
209211
end
210212

211213
def updates
212-
rhel_updates = <<-PRINT_JSON
213-
#!/bin/sh
214-
python -c 'import sys; sys.path.insert(0, "/usr/share/yum-cli"); import cli; ybc = cli.YumBaseCli(); ybc.setCacheDir("/tmp"); list = ybc.returnPkgLists(["updates"]);res = ["{\\"name\\":\\""+x.name+"\\", \\"version\\":\\""+x.version+"-"+x.release+"\\",\\"arch\\":\\""+x.arch+"\\",\\"repository\\":\\""+x.repo.id+"\\"}" for x in list.updates]; print "{\\"available\\":["+",".join(res)+"]}"'
214+
rhel_updates = <<~PRINT_JSON
215+
#!/bin/sh
216+
python -c 'import sys; sys.path.insert(0, "/usr/share/yum-cli"); import cli; ybc = cli.YumBaseCli(); ybc.setCacheDir("/tmp"); list = ybc.returnPkgLists(["updates"]);res = ["{\\"name\\":\\""+x.name+"\\", \\"version\\":\\""+x.version+"-"+x.release+"\\",\\"arch\\":\\""+x.arch+"\\",\\"repository\\":\\""+x.repo.id+"\\"}" for x in list.updates]; print "{\\"available\\":["+",".join(res)+"]}"'
215217
PRINT_JSON
216218
cmd = @inspec.bash(rhel_updates)
217-
unless cmd.exit_status == 0
219+
unless cmd.exit_status.zero?
218220
# essentially we want https://github.com/chef/inspec/issues/1205
219-
STDERR.puts 'Could not determine patch status.'
221+
warn 'Could not determine patch status.'
220222
return nil
221223
end
222224

@@ -225,7 +227,7 @@ def updates
225227
begin
226228
JSON.parse(res)
227229
rescue JSON::ParserError => _e
228-
return []
230+
[]
229231
end
230232
end
231233
end

0 commit comments

Comments
 (0)