-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for sarif formatted violation reports
- Loading branch information
1 parent
bea2473
commit 80aaf89
Showing
4 changed files
with
65 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package writer | ||
|
||
import ( | ||
"github.com/accurics/terrascan/pkg/policy" | ||
"io" | ||
"github.com/owenrumney/go-sarif/sarif" | ||
) | ||
|
||
const ( | ||
sarifFormat supportedFormat = "sarif" | ||
) | ||
|
||
func init() { | ||
RegisterWriter(sarifFormat, SarifWriter) | ||
} | ||
|
||
// SarifWriter writes sarif formatted violation results report | ||
func SarifWriter(data interface{}, writer io.Writer) error { | ||
outputData := data.(policy.EngineOutput) | ||
//summary := outputData.Summary | ||
report, err := sarif.New(sarif.Version210) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
run := sarif.NewRun("terrascan", "https://github.com/accurics/terrascan") | ||
// add a run to the report | ||
report.AddRun(run) | ||
|
||
|
||
// for each result add the rule, location and result to the report | ||
for _, violation := range outputData.Violations { | ||
m := make(map[string]string) | ||
m["category"] = violation.Category | ||
m["severity"] = violation.Severity | ||
|
||
rule := run.AddRule(string(violation.RuleID)). | ||
WithDescription(violation.Description).WithName(violation.RuleName).WithProperties(m) | ||
|
||
run.AddResult(rule.ID). | ||
WithMessage(sarif.NewTextMessage(violation.Description)). | ||
WithMessage(sarif.NewMarkdownMessage(violation.Description)). | ||
WithLevel(string(violation.Severity)). | ||
WithLocation(sarif.NewLocation(). | ||
WithPhysicalLocation(sarif.NewPhysicalLocation(). | ||
WithArtifactLocation(sarif.NewSimpleArtifactLocation(violation.File)). | ||
WithContextRegion(sarif.NewRegion().WithStartLine(violation.LineNumber)))) | ||
} | ||
|
||
// print the report to anything that implements `io.Writer` | ||
return report.PrettyWrite(writer) | ||
} |