File: main.go

package info (click to toggle)
golang-github-owenrumney-go-sarif 2.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: makefile: 13
file content (107 lines) | stat: -rw-r--r-- 2,620 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main

import (
	"encoding/json"
	"os"
	"strings"

	"github.com/owenrumney/go-sarif/v2/sarif"
)

// TfsecResults is a simple structure for the output of tfsec
type TfsecResults struct {
	Results []struct {
		RuleID          string `json:"rule_id"`
		RuleDescription string `json:"rule_description"`
		RuleProvider    string `json:"rule_provider"`
		Link            string `json:"link"`
		Location        struct {
			Filename  string `json:"filename"`
			StartLine int    `json:"start_line"`
			EndLine   int    `json:"end_line"`
		} `json:"location"`
		Description string `json:"description"`
		Impact      string `json:"impact"`
		Resolution  string `json:"resolution"`
		Severity    string `json:"severity"`
		Passed      bool   `json:"passed"`
	} `json:"results"`
}

func main() {

	// Get the results from the results file
	tfsecResults, err := loadTfsecResults()
	if err != nil {
		panic(err)
	}

	// create a new report object
	report, err := sarif.New(sarif.Version210)
	if err != nil {
		panic(err)
	}

	// create a run for tfsec
	run := sarif.NewRunWithInformationURI("tfsec", "https://tfsec.dev")

	// for each result, add the
	for _, r := range tfsecResults.Results {

		// create a property bag for the non standard stuff
		pb := sarif.NewPropertyBag()
		pb.Add("impact", r.Impact)
		pb.Add("resolution", r.Resolution)

		// create a new rule for each rule id
		run.AddRule(r.RuleID).
			WithDescription(r.Description).
			WithHelpURI(r.Link).
			WithProperties(pb.Properties).
			WithMarkdownHelp("# markdown")

		// add the location as a unique artifact
		run.AddDistinctArtifact(r.Location.Filename)

		// add each of the results with the details of where the issue occurred
		run.CreateResultForRule(r.RuleID).
			WithLevel(strings.ToLower(r.Severity)).
			WithMessage(sarif.NewTextMessage(r.Description)).
			AddLocation(
				sarif.NewLocationWithPhysicalLocation(
					sarif.NewPhysicalLocation().
						WithArtifactLocation(
							sarif.NewSimpleArtifactLocation(r.Location.Filename),
						).WithRegion(
						sarif.NewSimpleRegion(r.Location.StartLine, r.Location.EndLine),
					),
				),
			)
	}

	// add the run to the report
	report.AddRun(run)

	// print the report to stdout
	_ = report.PrettyWrite(os.Stdout)

	// save the report
	if err := report.WriteFile("example-report.sarif"); err != nil {
		panic(err)
	}

}

// load the example results file
func loadTfsecResults() (TfsecResults, error) {

	jsonResult, err := os.ReadFile("../example/results.json")
	if err != nil {
		panic(err)
	}

	var results TfsecResults

	err = json.Unmarshal(jsonResult, &results)
	return results, err
}