Debian Codegraph @

example/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "encoding/json"
5 "os"
6 "strings"
7
8 "github.com/owenrumney/go-sarif/v2/sarif"
9 )
10
11 // TfsecResults is a simple structure for the output of tfsec
12 type TfsecResults struct {
13 Results []struct {
14 RuleID string `json:"rule_id"`
15 RuleDescription string `json:"rule_description"`
16 RuleProvider string `json:"rule_provider"`
17 Link string `json:"link"`
18 Location struct {
19 Filename string `json:"filename"`
20 StartLine int `json:"start_line"`
21 EndLine int `json:"end_line"`
22 } `json:"location"`
23 Description string `json:"description"`
24 Impact string `json:"impact"`
25 Resolution string `json:"resolution"`
26 Severity string `json:"severity"`
27 Passed bool `json:"passed"`
28 } `json:"results"`
29 }
30
31 func main() {
32
33 // Get the results from the results file
34 tfsecResults, err := loadTfsecResults()
35 if err != nil {
36 panic(err)
37 }
38
39 // create a new report object
40 report, err := sarif.New(sarif.Version210)
41 if err != nil {
42 panic(err)
43 }
44
45 // create a run for tfsec
46 run := sarif.NewRunWithInformationURI("tfsec", "https://tfsec.dev")
47
48 // for each result, add the
49 for _, r := range tfsecResults.Results {
50
51 // create a property bag for the non standard stuff
52 pb := sarif.NewPropertyBag()
53 pb.Add("impact", r.Impact)
54 pb.Add("resolution", r.Resolution)
55
56 // create a new rule for each rule id
57 run.AddRule(r.RuleID).
58 WithDescription(r.Description).
59 WithHelpURI(r.Link).
60 WithProperties(pb.Properties).
61 WithMarkdownHelp("# markdown")
62
63 // add the location as a unique artifact
64 run.AddDistinctArtifact(r.Location.Filename)
65
66 // add each of the results with the details of where the issue occurred
67 run.CreateResultForRule(r.RuleID).
68 WithLevel(strings.ToLower(r.Severity)).
69 WithMessage(sarif.NewTextMessage(r.Description)).
70 AddLocation(
71 sarif.NewLocationWithPhysicalLocation(
72 sarif.NewPhysicalLocation().
73 WithArtifactLocation(
74 sarif.NewSimpleArtifactLocation(r.Location.Filename),
75 ).WithRegion(
76 sarif.NewSimpleRegion(r.Location.StartLine, r.Location.EndLine),
77 ),
78 ),
79 )
80 }
81
82 // add the run to the report
83 report.AddRun(run)
84
85 // print the report to stdout
86 _ = report.PrettyWrite(os.Stdout)
87
88 // save the report
89 if err := report.WriteFile("example-report.sarif"); err != nil {
90 panic(err)
91 }
92
93 }
94
95 // load the example results file
96 func loadTfsecResults() (TfsecResults, error) {
97
98 jsonResult, err := os.ReadFile("../example/results.json")
99 if err != nil {
100 panic(err)
101 }
102
103 var results TfsecResults
104
105 err = json.Unmarshal(jsonResult, &results)
106 return results, err
107 }