-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* SARIF output support - Pass rule definitions to result formatters - Add names to rule definitions/violations * Resolve todo * Update location details to use relative paths to srcroot * Provide a default line number * Migrate to a version.rb file for tracking version internally * Remove ENV version override Update version spec to reflect the version.rb values
- Loading branch information
Kevin Formsma
authored
Oct 25, 2021
1 parent
351682e
commit 67ae380
Showing
48 changed files
with
455 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'json' | ||
require 'pathname' | ||
|
||
class SarifResults | ||
def render(results, rule_registry) | ||
sarif_results = [] | ||
results.each do |file| | ||
# For each file in the results, review the violations | ||
file[:file_results][:violations].each do |violation| | ||
# For each violation, generate a sarif result for each logical resource id in the violation | ||
violation.logical_resource_ids.each_with_index do |_logical_resource_id, index| | ||
sarif_results << sarif_result(file_name: file[:filename], violation: violation, index: index) | ||
end | ||
end | ||
end | ||
|
||
sarif_report = { | ||
version: '2.1.0', | ||
'$schema': 'https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json', | ||
runs: [ | ||
tool: { | ||
driver: driver(rule_registry.rules) | ||
}, | ||
results: sarif_results | ||
] | ||
} | ||
|
||
puts JSON.pretty_generate(sarif_report) | ||
end | ||
|
||
# Generates a SARIF driver object, which describes the tool and the rules used | ||
def driver(rules) | ||
{ | ||
name: 'cfn_nag', | ||
informationUri: 'https://github.com/stelligent/cfn_nag', | ||
semanticVersion: CfnNagVersion::VERSION, | ||
rules: rules.map do |rule_definition| | ||
{ | ||
id: "CFN_NAG_#{rule_definition.id}", | ||
name: rule_definition.name, | ||
fullDescription: { | ||
text: rule_definition.message | ||
} | ||
} | ||
end | ||
} | ||
end | ||
|
||
# Given a cfn_nag Violation object, and index, generates a SARIF result object for the finding | ||
def sarif_result(file_name:, violation:, index:) | ||
{ | ||
ruleId: "CFN_NAG_#{violation.id}", | ||
level: sarif_level(violation.type), | ||
message: { | ||
text: violation.message | ||
}, | ||
locations: [ | ||
{ | ||
physicalLocation: { | ||
artifactLocation: { | ||
uri: relative_path(file_name), | ||
uriBaseId: '%SRCROOT%' | ||
}, | ||
region: { | ||
startLine: sarif_line_number(violation.line_numbers[index]) | ||
} | ||
}, | ||
logicalLocations: [ | ||
{ | ||
name: violation.logical_resource_ids[index] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
end | ||
|
||
# Line number defaults to 1 unless provided with valid number | ||
def sarif_line_number(line_number) | ||
line_number.nil? || line_number.to_i < 1 ? 1 : line_number.to_i | ||
end | ||
|
||
def sarif_level(violation_type) | ||
case violation_type | ||
when RuleDefinition::WARNING | ||
'warning' | ||
else | ||
'error' | ||
end | ||
end | ||
|
||
def relative_path(file_name) | ||
file_pathname = Pathname.new(file_name) | ||
|
||
if file_pathname.relative? | ||
file_pathname.to_s | ||
else | ||
file_pathname.relative_path_from(Pathname.pwd).to_s | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module CfnNagVersion | ||
# This is managed at release time via scripts/publish.sh | ||
VERSION = '0.0.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.