-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SARIF output support #573
Merged
Merged
SARIF output support #573
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dfe507e
SARIF output support
arothian 6c9d596
Resolve todo
arothian d8cb64f
Update location details to use relative paths to srcroot
arothian d6193cb
Provide a default line number
arothian e1cb0fc
Migrate to a version.rb file for tracking version internally
arothian 34462fe
Remove ENV version override
arothian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
require_relative 'result_view/simple_stdout_results' | ||
require_relative 'result_view/colored_stdout_results' | ||
require_relative 'result_view/json_results' | ||
require_relative 'result_view/sarif_results' | ||
require 'cfn-model' | ||
|
||
# Top-level CfnNag class for running profiles | ||
|
@@ -96,10 +97,10 @@ def audit(cloudformation_string:, parameter_values_string: nil, condition_values | |
violations = filter_violations_by_deny_list_and_profile(violations) | ||
violations = mark_line_numbers(violations, cfn_model) | ||
rescue RuleRepoException, Psych::SyntaxError, ParserError => fatal_error | ||
violations << fatal_violation(fatal_error.to_s) | ||
violations << Violation.fatal_violation(fatal_error.to_s) | ||
rescue JSON::ParserError => json_parameters_error | ||
error = "JSON Parameter values parse error: #{json_parameters_error}" | ||
violations << fatal_violation(error) | ||
violations << Violation.fatal_violation(error) | ||
end | ||
|
||
violations = prune_fatal_violations(violations) if @config.ignore_fatal | ||
|
@@ -112,7 +113,7 @@ def prune_fatal_violations(violations) | |
|
||
def render_results(aggregate_results:, | ||
output_format:) | ||
results_renderer(output_format).new.render(aggregate_results) | ||
results_renderer(output_format).new.render(aggregate_results, @config.custom_rule_loader.rule_definitions) | ||
end | ||
|
||
private | ||
|
@@ -141,7 +142,7 @@ def filter_violations_by_deny_list_and_profile(violations) | |
violations: violations | ||
) | ||
rescue StandardError => deny_list_or_profile_parse_error | ||
violations << fatal_violation(deny_list_or_profile_parse_error.to_s) | ||
violations << Violation.fatal_violation(deny_list_or_profile_parse_error.to_s) | ||
violations | ||
end | ||
|
||
|
@@ -152,17 +153,12 @@ def audit_result(violations) | |
} | ||
end | ||
|
||
def fatal_violation(message) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was moved to a class method in |
||
Violation.new(id: 'FATAL', | ||
type: Violation::FAILING_VIOLATION, | ||
message: message) | ||
end | ||
|
||
def results_renderer(output_format) | ||
registry = { | ||
'colortxt' => ColoredStdoutResults, | ||
'txt' => SimpleStdoutResults, | ||
'json' => JsonResults | ||
'json' => JsonResults, | ||
'sarif' => SarifResults | ||
} | ||
registry[output_format] | ||
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allow rule implementation classes to generate violation objects without needing to run
audit
. This is useful for unit testing and other locations