Skip to content

Commit cda0205

Browse files
committed
appeased rubocop
1 parent 25b5e83 commit cda0205

File tree

5 files changed

+36
-47
lines changed

5 files changed

+36
-47
lines changed

.rubocop_todo.yml

+11-16
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-06-07 20:33:06 UTC using RuboCop version 1.16.0.
3+
# on 2021-08-05 04:56:46 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,6 @@ Lint/DuplicateBranch:
1919
Exclude:
2020
- 'lib/heimdall_tools/dbprotect_mapper.rb'
2121

22-
# Offense count: 1
23-
# Configuration parameters: MaximumRangeSize.
24-
Lint/MissingCopEnableDirective:
25-
Exclude:
26-
- 'lib/heimdall_tools/nessus_mapper.rb'
27-
2822
# Offense count: 1
2923
Lint/RequireParentheses:
3024
Exclude:
@@ -37,10 +31,10 @@ Lint/UnusedMethodArgument:
3731
Exclude:
3832
- 'lib/heimdall_tools/hdf.rb'
3933

40-
# Offense count: 37
34+
# Offense count: 49
4135
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
4236
Metrics/AbcSize:
43-
Max: 124
37+
Max: 165
4438

4539
# Offense count: 5
4640
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
@@ -53,17 +47,17 @@ Metrics/BlockLength:
5347
Metrics/BlockNesting:
5448
Max: 5
5549

56-
# Offense count: 9
50+
# Offense count: 10
5751
# Configuration parameters: CountComments, CountAsOne.
5852
Metrics/ClassLength:
5953
Max: 175
6054

61-
# Offense count: 10
55+
# Offense count: 13
6256
# Configuration parameters: IgnoredMethods.
6357
Metrics/CyclomaticComplexity:
64-
Max: 17
58+
Max: 30
6559

66-
# Offense count: 40
60+
# Offense count: 44
6761
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
6862
Metrics/MethodLength:
6963
Max: 56
@@ -73,10 +67,10 @@ Metrics/MethodLength:
7367
Metrics/ParameterLists:
7468
Max: 18
7569

76-
# Offense count: 8
70+
# Offense count: 11
7771
# Configuration parameters: IgnoredMethods.
7872
Metrics/PerceivedComplexity:
79-
Max: 17
73+
Max: 30
8074

8175
# Offense count: 3
8276
Naming/AccessorMethodName:
@@ -98,10 +92,11 @@ Naming/VariableName:
9892
Exclude:
9993
- 'lib/heimdall_tools/burpsuite_mapper.rb'
10094

101-
# Offense count: 12
95+
# Offense count: 15
10296
# Configuration parameters: AllowedVariables.
10397
Style/GlobalVars:
10498
Exclude:
99+
- 'lib/heimdall_tools/asff_mapper.rb'
105100
- 'lib/heimdall_tools/jfrog_xray_mapper.rb'
106101
- 'lib/heimdall_tools/nessus_mapper.rb'
107102
- '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/fortify_mapper.rb

-2
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,13 @@ def primaries(classid)
5555
findings.uniq
5656
end
5757

58-
# rubocop:disable Layout/LineEndStringConcatenationIndentation
5958
def snippet(snippetid)
6059
snippet = @snippets.select { |x| x['id'].eql?(snippetid) }.first
6160
"\nPath: #{snippet['File']}\n" \
6261
"StartLine: #{snippet['StartLine']}, " \
6362
"EndLine: #{snippet['EndLine']}\n" \
6463
"Code:\n#{snippet['Text']['#cdata-section'].strip}" \
6564
end
66-
# rubocop:enable Layout/LineEndStringConcatenationIndentation
6765

6866
def nist_tag(rule)
6967
references = rule['References']['Reference']

0 commit comments

Comments
 (0)