Skip to content

Commit d37899e

Browse files
Merge pull request #6 from dev-sec/chris-rock/lint
improve code style
2 parents 6b75b2c + 34f0b14 commit d37899e

6 files changed

Lines changed: 183 additions & 38 deletions

File tree

.rubocop.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
AllCops:
3+
Exclude:
4+
- Gemfile
5+
- Rakefile
6+
- 'test/**/*'
7+
- 'vendor/**/*'
8+
Documentation:
9+
Enabled: false
10+
AlignParameters:
11+
Enabled: true
12+
Encoding:
13+
Enabled: false
14+
HashSyntax:
15+
Enabled: true
16+
LineLength:
17+
Enabled: false
18+
EmptyLinesAroundBlockBody:
19+
Enabled: false
20+
MethodLength:
21+
Max: 40
22+
NumericLiterals:
23+
MinDigits: 10
24+
Metrics/CyclomaticComplexity:
25+
Max: 10
26+
Metrics/PerceivedComplexity:
27+
Max: 11
28+
Metrics/AbcSize:
29+
Max: 33
30+
Style/PercentLiteralDelimiters:
31+
PreferredDelimiters:
32+
'%': '{}'
33+
'%i': ()
34+
'%q': '{}'
35+
'%Q': ()
36+
'%r': '{}'
37+
'%s': ()
38+
'%w': '{}'
39+
'%W': ()
40+
'%x': ()
41+
Style/AlignHash:
42+
Enabled: false
43+
Style/PredicateName:
44+
Enabled: false
45+
Style/ZeroLengthPredicate:
46+
Enabled: false
47+
Style/NumericPredicate:
48+
Enabled: false
49+
Style/ClassAndModuleChildren:
50+
Enabled: false
51+
Style/ConditionalAssignment:
52+
Enabled: false
53+
Style/BracesAroundHashParameters:
54+
Enabled: false
55+
Style/AndOr:
56+
Enabled: false
57+
Style/Not:
58+
Enabled: false
59+
Style/FileName:
60+
Enabled: false
61+
Style/TrailingCommaInLiteral:
62+
EnforcedStyleForMultiline: comma
63+
Style/TrailingCommaInArguments:
64+
EnforcedStyleForMultiline: comma
65+
Style/NegatedIf:
66+
Enabled: false
67+
Style/UnlessElse:
68+
Enabled: false
69+
BlockDelimiters:
70+
Enabled: false
71+
Style/SpaceAroundOperators:
72+
Enabled: false
73+
Style/IfUnlessModifier:
74+
Enabled: false

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
language: ruby
3+
cache: bundler
4+
rvm:
5+
- 2.0
6+
- 2.2
7+
- 2.3.1
8+
9+
bundler_args: --without integration
10+
script: bundle exec rake

Gemfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'rake'
4+
gem 'rack', '1.6.4'
5+
gem 'inspec', '~> 1'
6+
gem 'rubocop', '~> 0.44.0'
7+
gem 'highline', '~> 1.6.0'
8+
9+
group :integration do
10+
gem 'berkshelf'
11+
gem 'kitchen-inspec'
12+
gem 'test-kitchen'
13+
gem 'kitchen-vagrant'
14+
end
15+
16+
group :tools do
17+
gem 'github_changelog_generator', '~> 1.12.0'
18+
end

Rakefile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env rake
2+
3+
require 'rake/testtask'
4+
require 'rubocop/rake_task'
5+
6+
# Rubocop
7+
desc 'Run Rubocop lint checks'
8+
task :rubocop do
9+
RuboCop::RakeTask.new
10+
end
11+
12+
# lint the project
13+
desc 'Run robocop linter'
14+
task lint: [:rubocop]
15+
16+
# run tests
17+
task default: [:lint, 'test:check']
18+
19+
namespace :test do
20+
# run inspec check to verify that the profile is properly configured
21+
task :check do
22+
dir = File.join(File.dirname(__FILE__))
23+
sh("bundle exec inspec check #{dir}")
24+
end
25+
end
26+
27+
# Automatically generate a changelog for this project. Only loaded if
28+
# the necessary gem is installed.
29+
# use `rake changelog to=1.2.0`
30+
begin
31+
v = ENV['to']
32+
require 'github_changelog_generator/task'
33+
GitHubChangelogGenerator::RakeTask.new :changelog do |config|
34+
config.future_release = v
35+
config.user = 'dev-sec'
36+
config.project = 'windows-patch-baseline'
37+
end
38+
rescue LoadError
39+
puts '>>>>> GitHub Changelog Generator not loaded, omitting tasks'
40+
end

controls/patches.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111

1212
control 'verify-kb' do
1313
impact 0.3
14-
title "All updates should be installed"
14+
title 'All updates should be installed'
1515
describe win_update.all.length do
16-
it { should eq 0}
16+
it { should eq 0 }
1717
end
1818
end
1919

2020
control 'important-count' do
2121
impact 1.0
22-
title "No important updates should be available"
22+
title 'No important updates should be available'
2323
describe win_update.important.length do
24-
it { should eq 0}
24+
it { should eq 0 }
2525
end
2626
end
2727

2828
control 'important-patches' do
2929
impact 1.0
30-
title "All important updates are installed"
30+
title 'All important updates are installed'
3131
win_update.important.each { |update|
3232
describe update do
3333
it { should be_installed }
@@ -37,15 +37,15 @@
3737

3838
control 'optional-count' do
3939
impact 0.3
40-
title "No optional updates should be available"
40+
title 'No optional updates should be available'
4141
describe win_update.optional.length do
42-
it { should eq 0}
42+
it { should eq 0 }
4343
end
4444
end
4545

4646
control 'optional-patches' do
4747
impact 0.3
48-
title "All optional updates are installed"
48+
title 'All optional updates are installed'
4949
win_update.optional.each { |update|
5050
describe update do
5151
it { should be_installed }

libraries/windows_updates.rb

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def criticality
3535
end
3636

3737
def installed?
38-
return false
38+
false
3939
end
4040

4141
def to_s
@@ -55,28 +55,29 @@ def initialize
5555

5656
# returns all available updates
5757
def all
58-
updates = fetchUpdates
58+
updates = fetch_updates
5959
updates.map { |update| WindowsUpdate.new(update) }
6060
end
6161

6262
# returns all important updates
6363
def important
64-
updates = fetchUpdates
64+
updates = fetch_updates
6565
updates
6666
.select { |update|
67-
@update_mgmt.isImportant(update)
67+
@update_mgmt.important?(update)
68+
}.map { |update| # rubocop:disable Style/MultilineBlockChain
69+
WindowsUpdate.new(update)
6870
}
69-
.map { |update| WindowsUpdate.new(update) }
7071
end
7172

7273
# returns all optional updates
7374
def optional
74-
updates = fetchUpdates
75-
updates
76-
.select { |update|
77-
@update_mgmt.isOptional(update)
78-
}
79-
.map { |update| WindowsUpdate.new(update) }
75+
updates = fetch_updates
76+
updates.select { |update|
77+
@update_mgmt.optional?(update)
78+
}.map { |update| # rubocop:disable Style/MultilineBlockChain
79+
WindowsUpdate.new(update)
80+
}
8081
end
8182

8283
def reboot_required?
@@ -85,29 +86,31 @@ def reboot_required?
8586
end
8687

8788
def to_s
88-
"Windows Update Services"
89+
'Windows Update Services'
8990
end
9091

9192
# private
9293

9394
# detection for nano server
9495
# @see https://msdn.microsoft.com/en-us/library/hh846315(v=vs.85).aspx
95-
def detect_nano
96+
def windows_nano?
9697
return false unless inspec.os[:release].to_i >= 10
9798
'1' == inspec.powershell('Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Server\ServerLevels" | Select -ExpandProperty "NanoServer" ').stdout.chomp
9899
end
99100

101+
private
102+
100103
def select_update_mgmt
101-
if detect_nano
104+
if windows_nano?
102105
WindowsNanoUpdateFetcher.new(inspec)
103106
else
104107
Windows2012UpdateFetcher.new(inspec)
105108
end
106109
end
107110

108-
def fetchUpdates
111+
def fetch_updates
109112
return [] if @update_mgmt.nil?
110-
@update_mgmt.fetchUpdates
113+
@update_mgmt.fetch_updates
111114
end
112115

113116
def hotfixes
@@ -125,7 +128,7 @@ def hotfixes
125128
[]
126129
end
127130

128-
def fetchUpdates
131+
def fetch_updates
129132
[]
130133
end
131134
end
@@ -134,7 +137,7 @@ class Windows2012UpdateFetcher < UpdateFetcher
134137
def hotfixes
135138
return @cache_hotfix_installed if defined?(@cache_hotfix_installed)
136139

137-
hotfix_cmd = "Get-HotFix | Select-Object -Property Status, Description, HotFixId, Caption, InstallDate, InstalledBy | ConvertTo-Json"
140+
hotfix_cmd = 'Get-HotFix | Select-Object -Property Status, Description, HotFixId, Caption, InstallDate, InstalledBy | ConvertTo-Json'
138141
cmd = @inspec.command(hotfix_cmd)
139142
begin
140143
@cache_hotfix_installed = JSON.parse(cmd.stdout)
@@ -143,7 +146,7 @@ def hotfixes
143146
end
144147
end
145148

146-
def fetchUpdates
149+
def fetch_updates
147150
return @cache_available if defined?(@cache_available)
148151
script = <<-EOH
149152
$updateSession = new-object -com "Microsoft.Update.Session"
@@ -175,29 +178,29 @@ def fetchUpdates
175178
end
176179
end
177180

178-
def isImportant(update)
179-
isSecurityCategory(update['CategoryIDs'])
181+
def important?(update)
182+
security_category?(update['CategoryIDs'])
180183
end
181184

182-
def isOptional(update)
183-
!isImportant(update)
185+
def optional?(update)
186+
!important?(update)
184187
end
185188

186189
# @see: https://msdn.microsoft.com/en-us/library/ff357803(v=vs.85).aspx
187190
# e6cf1350-c01b-414d-a61f-263d14d133b4 -> Critical Updates
188191
# 0fa1201d-4330-4fa8-8ae9-b877473b6441 -> Security Updates
189192
# 28bc880e-0592-4cbf-8f95-c79b17911d5f -> Update Rollups
190193
# does not include recommended updates yet
191-
def isSecurityCategory(uuids)
194+
def security_category?(uuids)
192195
return if uuids.nil?
193196
uuids.include?('0fa1201d-4330-4fa8-8ae9-b877473b6441') ||
194-
uuids.include?('28bc880e-0592-4cbf-8f95-c79b17911d5f') ||
195-
uuids.include?('e6cf1350-c01b-414d-a61f-263d14d133b4')
197+
uuids.include?('28bc880e-0592-4cbf-8f95-c79b17911d5f') ||
198+
uuids.include?('e6cf1350-c01b-414d-a61f-263d14d133b4')
196199
end
197200
end
198201

199202
class WindowsNanoUpdateFetcher < UpdateFetcher
200-
def fetchUpdates
203+
def fetch_updates
201204
return @cache_available if defined?(@cache_available)
202205
script = <<-EOH
203206
$sess = New-CimInstance -Namespace root/Microsoft/Windows/WindowsUpdate -ClassName MSFT_WUOperationsSession
@@ -224,11 +227,11 @@ def fetchUpdates
224227
end
225228
end
226229

227-
def isImportant(update)
230+
def important?(update)
228231
%w{Important Critical}.include? update['MsrcSeverity']
229232
end
230233

231-
def isOptional(update)
232-
!isImportant(update)
234+
def optional?(update)
235+
!important?(update)
233236
end
234237
end

0 commit comments

Comments
 (0)