Skip to content

Commit eff99c7

Browse files
committed
appeased rubocop
1 parent 3d494ce commit eff99c7

File tree

5 files changed

+37
-43
lines changed

5 files changed

+37
-43
lines changed

.rubocop_todo.yml

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2021-05-18 15:11:52 UTC using RuboCop version 1.14.0.
3+
# on 2021-08-05 04:31:39 UTC using RuboCop version 1.14.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -19,12 +19,11 @@ Lint/DuplicateBranch:
1919
Exclude:
2020
- 'lib/heimdall_tools/dbprotect_mapper.rb'
2121

22-
# Offense count: 2
22+
# Offense count: 1
2323
# Configuration parameters: MaximumRangeSize.
2424
Lint/MissingCopEnableDirective:
2525
Exclude:
2626
- 'lib/heimdall_tools/nessus_mapper.rb'
27-
- 'lib/heimdall_tools/zap_mapper.rb'
2827

2928
# Offense count: 1
3029
Lint/RequireParentheses:
@@ -38,10 +37,10 @@ Lint/UnusedMethodArgument:
3837
Exclude:
3938
- 'lib/heimdall_tools/hdf.rb'
4039

41-
# Offense count: 32
40+
# Offense count: 43
4241
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
4342
Metrics/AbcSize:
44-
Max: 73
43+
Max: 165
4544

4645
# Offense count: 4
4746
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
@@ -54,17 +53,17 @@ Metrics/BlockLength:
5453
Metrics/BlockNesting:
5554
Max: 5
5655

57-
# Offense count: 8
56+
# Offense count: 9
5857
# Configuration parameters: CountComments, CountAsOne.
5958
Metrics/ClassLength:
6059
Max: 171
6160

62-
# Offense count: 10
61+
# Offense count: 13
6362
# Configuration parameters: IgnoredMethods.
6463
Metrics/CyclomaticComplexity:
65-
Max: 17
64+
Max: 30
6665

67-
# Offense count: 38
66+
# Offense count: 42
6867
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
6968
Metrics/MethodLength:
7069
Max: 52
@@ -74,10 +73,10 @@ Metrics/MethodLength:
7473
Metrics/ParameterLists:
7574
Max: 18
7675

77-
# Offense count: 8
76+
# Offense count: 11
7877
# Configuration parameters: IgnoredMethods.
7978
Metrics/PerceivedComplexity:
80-
Max: 17
79+
Max: 30
8180

8281
# Offense count: 3
8382
Naming/AccessorMethodName:
@@ -99,10 +98,11 @@ Naming/VariableName:
9998
Exclude:
10099
- 'lib/heimdall_tools/burpsuite_mapper.rb'
101100

102-
# Offense count: 12
101+
# Offense count: 15
103102
# Configuration parameters: AllowedVariables.
104103
Style/GlobalVars:
105104
Exclude:
105+
- 'lib/heimdall_tools/asff_mapper.rb'
106106
- 'lib/heimdall_tools/jfrog_xray_mapper.rb'
107107
- 'lib/heimdall_tools/nessus_mapper.rb'
108108
- 'lib/heimdall_tools/nikto_mapper.rb'

heimdall_tools.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ Gem::Specification.new do |spec|
2828
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
2929
spec.require_paths = ['lib']
3030

31-
spec.add_runtime_dependency 'aws-sdk-securityhub', '~> 1'
3231
spec.add_runtime_dependency 'aws-sdk-configservice', '~> 1'
32+
spec.add_runtime_dependency 'aws-sdk-securityhub', '~> 1'
3333
spec.add_runtime_dependency 'csv', '~> 3.1'
3434
spec.add_runtime_dependency 'git-lite-version-bump', '>= 0.17.2'
3535
spec.add_runtime_dependency 'htmlentities', '~> 4.3.4'

lib/heimdall_tools/asff_compatible_products/securityhub.rb

+8-9
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ def self.supporting_docs(standards:)
2525
{ controls: controls, aws_config_mapping: aws_config_mapping }
2626
end
2727

28-
def self.finding_id(finding, *, controls: nil, encode:, **)
28+
def self.finding_id(finding, *, encode:, controls: nil, **)
2929
ret = if !controls.nil? && !(control = corresponding_control(controls, finding)).nil?
3030
control['ControlId']
31+
elsif finding['ProductFields'].member?('ControlId') # check if aws
32+
finding['ProductFields']['ControlId']
33+
elsif finding['ProductFields'].member?('RuleId') # check if cis
34+
finding['ProductFields']['RuleId']
3135
else
32-
if finding['ProductFields'].member?('ControlId') # check if aws
33-
finding['ProductFields']['ControlId']
34-
elsif finding['ProductFields'].member?('RuleId') # check if cis
35-
finding['ProductFields']['RuleId']
36-
else
37-
finding['GeneratorId'].split('/')[-1]
38-
end
36+
finding['GeneratorId'].split('/')[-1]
3937
end
4038
encode.call(ret)
4139
end
@@ -54,10 +52,11 @@ def self.finding_impact(finding, *, controls: nil, **)
5452

5553
def self.finding_nist_tag(finding, *, aws_config_mapping:, **)
5654
return {} unless finding['ProductFields']['RelatedAWSResources:0/type'] == 'AWS::Config::ConfigRule'
55+
5756
aws_config_mapping.select { |rule| finding['ProductFields']['RelatedAWSResources:0/name'].include? rule[:awsconfigrulename] }
5857
end
5958

60-
def self.finding_title(finding, *, controls: nil, encode:, **)
59+
def self.finding_title(finding, *, encode:, controls: nil, **)
6160
ret = if !controls.nil? && !(control = corresponding_control(controls, finding)).nil?
6261
control['Title']
6362
else

lib/heimdall_tools/asff_mapper.rb

+16-19
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
require 'heimdall_tools/asff_compatible_products/prowler'
77
require 'heimdall_tools/asff_compatible_products/securityhub'
88

9-
109
module HeimdallTools
1110
DEFAULT_NIST_TAG = %w{SA-11 RA-5}.freeze
1211

@@ -40,19 +39,19 @@ class ASFFMapper
4039
}.freeze
4140

4241
PRODUCT_ARN_MAPPING = {
43-
/arn:.+:securityhub:.+:.*:product\/aws\/firewall-manager/ => FirewallManager,
44-
/arn:.+:securityhub:.+:.*:product\/aws\/securityhub/ => SecurityHub,
45-
/arn:.+:securityhub:.+:.*:product\/prowler\/prowler/ => Prowler
42+
%r{arn:.+:securityhub:.+:.*:product/aws/firewall-manager} => FirewallManager,
43+
%r{arn:.+:securityhub:.+:.*:product/aws/securityhub} => SecurityHub,
44+
%r{arn:.+:securityhub:.+:.*:product/prowler/prowler} => Prowler
4645
}.freeze
4746

4847
def initialize(asff_json, securityhub_standards_json_array: nil, meta: nil)
4948
@meta = meta
5049

5150
@supporting_docs = {}
52-
@supporting_docs[SecurityHub] = SecurityHub.supporting_docs({standards: securityhub_standards_json_array})
51+
@supporting_docs[SecurityHub] = SecurityHub.supporting_docs({ standards: securityhub_standards_json_array })
5352

5453
begin
55-
asff_required_keys = %w(AwsAccountId CreatedAt Description GeneratorId Id ProductArn Resources SchemaVersion Severity Title Types UpdatedAt)
54+
asff_required_keys = %w{AwsAccountId CreatedAt Description GeneratorId Id ProductArn Resources SchemaVersion Severity Title Types UpdatedAt}
5655
@report = JSON.parse(asff_json)
5756
if @report.length == 1 && @report.member?('Findings') && @report['Findings'].each { |finding| asff_required_keys.difference(finding.keys).none? }.all?
5857
# ideal case that is spec compliant
@@ -61,7 +60,7 @@ def initialize(asff_json, securityhub_standards_json_array: nil, meta: nil)
6160
# individual finding so have to add wrapping array
6261
@report = { 'Findings' => [@report] }
6362
else
64-
raise "Not a findings file nor an individual finding"
63+
raise 'Not a findings file nor an individual finding'
6564
end
6665
rescue StandardError => e
6766
raise "Invalid ASFF file provided:\nException: #{e}"
@@ -79,12 +78,10 @@ def external_product_handler(product, data, func, default)
7978
keywords = { encode: method(:encode) }
8079
keywords = keywords.merge(@supporting_docs[PRODUCT_ARN_MAPPING[arn || product]]) if @supporting_docs.member?(PRODUCT_ARN_MAPPING[arn || product])
8180
PRODUCT_ARN_MAPPING[arn || product].send(func, data, **keywords)
81+
elsif default.is_a? Proc
82+
default.call
8283
else
83-
if default.is_a? Proc
84-
default.call
85-
else
86-
default
87-
end
84+
default
8885
end
8986
end
9087

@@ -100,7 +97,7 @@ def impact(finding)
10097
imp = :INFORMATIONAL
10198
else
10299
# severity is required, but can be either 'label' or 'normalized' internally with 'label' being preferred. other values can be in here too such as the original severity rating.
103-
default = Proc.new { finding['Severity'].key?('Label') ? finding['Severity']['Label'].to_sym : finding['Severity']['Normalized']/100.0 }
100+
default = proc { finding['Severity'].key?('Label') ? finding['Severity']['Label'].to_sym : finding['Severity']['Normalized']/100.0 }
104101
imp = external_product_handler(finding['ProductArn'], finding, :finding_impact, default)
105102
end
106103
imp.is_a?(Symbol) ? IMPACT_MAPPING[imp] : imp
@@ -140,7 +137,7 @@ def subfindings(finding)
140137

141138
subfinding['code_desc'] = external_product_handler(finding['ProductArn'], finding, :subfindings_code_desc, '')
142139
subfinding['code_desc'] += '; ' unless subfinding['code_desc'].empty?
143-
subfinding['code_desc'] += "Resources: [#{finding['Resources'].map { |r| "Type: #{encode(r['Type'])}, Id: #{encode(r['Id'])}#{', Partition: ' + encode(r['Partition']) if r.key?('Partition')}#{', Region: ' + encode(r['Region']) if r.key?('Region')}" }.join(', ') }]"
140+
subfinding['code_desc'] += "Resources: [#{finding['Resources'].map { |r| "Type: #{encode(r['Type'])}, Id: #{encode(r['Id'])}#{", Partition: #{encode(r['Partition'])}" if r.key?('Partition')}#{", Region: #{encode(r['Region'])}" if r.key?('Region')}" }.join(', ')}]"
144141

145142
subfinding['start_time'] = finding.key?('LastObservedAt') ? finding['LastObservedAt'] : finding['UpdatedAt']
146143

@@ -167,7 +164,7 @@ def to_hdf
167164
item['desc'] = encode(finding['Description'])
168165

169166
item['descriptions'] = []
170-
item['descriptions'] << desc_tags(finding['Remediation']['Recommendation'].map { |k,v| encode(v) }.join("\n"), 'fix') if finding.key?('Remediation') && finding['Remediation'].key?('Recommendation')
167+
item['descriptions'] << desc_tags(finding['Remediation']['Recommendation'].map { |_k, v| encode(v) }.join("\n"), 'fix') if finding.key?('Remediation') && finding['Remediation'].key?('Recommendation')
171168

172169
item['refs'] = []
173170
item['refs'] << { url: finding['SourceUrl'] } if finding.key?('SourceUrl')
@@ -203,7 +200,7 @@ def to_hdf
203200
# add product name to id if any ids are the same across products
204201
item['id'] = product_groups.filter { |pg| pg != product }.values.any? { |ig| ig.keys.include?(id) } ? "[#{product_name}] #{id}" : id
205202

206-
item['title'] = "#{product_name}: #{group.map { |d| d['title'] }.uniq.join(";")}"
203+
item['title'] = "#{product_name}: #{group.map { |d| d['title'] }.uniq.join(';')}"
207204

208205
item['tags'] = { nist: group.map { |d| d['tags'][:nist] }.flatten.uniq }
209206

@@ -216,16 +213,16 @@ def to_hdf
216213
item['refs'] = group.map { |d| d['refs'] }.flatten.compact.reject(&:empty?).uniq
217214

218215
item['source_location'] = NA_HASH
219-
item['code'] = JSON.pretty_generate({ "Findings": findings })
216+
item['code'] = JSON.pretty_generate({ Findings: findings })
220217

221218
item['results'] = group.map { |d| d['results'] }.flatten.uniq
222219

223220
controls << item
224221
end
225222
end
226223

227-
results = HeimdallDataFormat.new(profile_name: @meta && @meta.key?('name') ? @meta['name'] : 'AWS Security Finding Format',
228-
title: @meta && @meta.key?('title') ? @meta['title'] : "ASFF findings",
224+
results = HeimdallDataFormat.new(profile_name: @meta&.key?('name') ? @meta['name'] : 'AWS Security Finding Format',
225+
title: @meta&.key?('title') ? @meta['title'] : 'ASFF findings',
229226
controls: controls)
230227
results.to_hdf
231228
end

lib/heimdall_tools/nessus_mapper.rb

-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
NA_PLUGIN_OUTPUT = 'This Nessus Plugin does not provide output message.'.freeze
2727

28-
# rubocop:disable Metrics/AbcSize
29-
3028
# Loading spinner sign
3129
$spinner = Enumerator.new do |e|
3230
loop do

0 commit comments

Comments
 (0)