This repository has been archived by the owner on May 18, 2022. It is now read-only.
forked from rzandonai/gitleaks
/
report.go
99 lines (90 loc) 路 2.3 KB
/
report.go
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
package scan
import (
"encoding/csv"
"encoding/json"
"os"
"time"
"github.com/ed-wp/gitleaks/v7/config"
"github.com/ed-wp/gitleaks/v7/options"
"github.com/ed-wp/gitleaks/v7/version"
"github.com/sirupsen/logrus"
)
// Report is a container for leaks and number of commits scanned
type Report struct {
Leaks []Leak
Commits int
}
// WriteReport accepts a report and options and will write a report if --report has been set
func WriteReport(report Report, opts options.Options, cfg config.Config) error {
if !(opts.NoGit || opts.CheckUncommitted()) {
logrus.Info("commits scanned: ", report.Commits)
}
if len(report.Leaks) != 0 {
logrus.Warn("leaks found: ", len(report.Leaks))
} else {
logrus.Info("No leaks found")
}
if opts.Report == "" {
return nil
}
if opts.Redact {
var redactedLeaks []Leak
for _, leak := range report.Leaks {
redactedLeaks = append(redactedLeaks, RedactLeak(leak))
}
report.Leaks = redactedLeaks
}
file, err := os.Create(opts.Report)
if err != nil {
return err
}
defer rable(file.Close)
if opts.Report != "" {
switch opts.ReportFormat {
case "json":
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
err = encoder.Encode(report.Leaks)
if err != nil {
return err
}
case "csv":
w := csv.NewWriter(file)
err = w.Write([]string{"repo", "line", "commit", "offender", "leakURL", "rule", "tags", "commitMsg", "author", "email", "file", "date"})
if err != nil {
return err
}
for _, leak := range report.Leaks {
err := w.Write([]string{leak.Repo, leak.Line, leak.Commit, leak.Offender, leak.LeakURL, leak.Rule, leak.Tags, leak.Message, leak.Author, leak.Email, leak.File, leak.Date.Format(time.RFC3339)})
if err != nil {
return err
}
}
w.Flush()
case "sarif":
s := Sarif{
Schema: "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json",
Version: "2.1.0",
Runs: []Runs{
{
Tool: Tool{
Driver: Driver{
Name: "Gitleaks",
SemanticVersion: version.Version,
Rules: configToRules(cfg),
},
},
Results: leaksToResults(report.Leaks),
},
},
}
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
err = encoder.Encode(s)
if err != nil {
return err
}
}
}
return nil
}