From 0dbdde837f49afcc2147af8ef0b7fd1f49024cb7 Mon Sep 17 00:00:00 2001 From: Malte Morgenstern <65773564+maltemorgenstern@users.noreply.github.com> Date: Tue, 13 Jun 2023 20:37:32 +0200 Subject: [PATCH] Add junit report format (#920) * add junit report format * add fingerprint to expected test result to fix tests * fix expected junit test report --- .gitignore | 3 + README.md | 2 +- cmd/root.go | 2 +- report/junit.go | 102 +++++++++++++++++++++ report/junit_test.go | 107 ++++++++++++++++++++++ report/report.go | 2 + report/report_test.go | 16 ++++ testdata/expected/report/junit_empty.xml | 4 + testdata/expected/report/junit_simple.xml | 11 +++ 9 files changed, 247 insertions(+), 2 deletions(-) create mode 100644 report/junit.go create mode 100644 report/junit_test.go create mode 100644 testdata/expected/report/junit_empty.xml create mode 100644 testdata/expected/report/junit_simple.xml diff --git a/.gitignore b/.gitignore index 49abd80e..9fb91f97 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,9 @@ build .gitleaks.toml cmd/generate/config/gitleaks.toml +# test results +testdata/expected/report/*.got.* + # Test binary *.out diff --git a/README.md b/README.md index 0a9bef4c..8dae8969 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ Flags: --no-color turn off color for verbose output --no-banner suppress banner --redact redact secrets from logs and stdout - -f, --report-format string output format (json, csv, sarif) (default "json") + -f, --report-format string output format (json, csv, junit, sarif) (default "json") -r, --report-path string report file -s, --source string path to source (default ".") -v, --verbose show verbose output from scan diff --git a/cmd/root.go b/cmd/root.go index 8c61fbdc..72b90625 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -41,7 +41,7 @@ func init() { rootCmd.PersistentFlags().Int("exit-code", 1, "exit code when leaks have been encountered") rootCmd.PersistentFlags().StringP("source", "s", ".", "path to source") rootCmd.PersistentFlags().StringP("report-path", "r", "", "report file") - rootCmd.PersistentFlags().StringP("report-format", "f", "json", "output format (json, csv, sarif)") + rootCmd.PersistentFlags().StringP("report-format", "f", "json", "output format (json, csv, junit, sarif)") rootCmd.PersistentFlags().StringP("baseline-path", "b", "", "path to baseline with issues that can be ignored") rootCmd.PersistentFlags().StringP("log-level", "l", "info", "log level (trace, debug, info, warn, error, fatal)") rootCmd.PersistentFlags().BoolP("verbose", "v", false, "show verbose output from scan") diff --git a/report/junit.go b/report/junit.go new file mode 100644 index 00000000..6cc82ad0 --- /dev/null +++ b/report/junit.go @@ -0,0 +1,102 @@ +package report + +import ( + "encoding/json" + "encoding/xml" + "fmt" + "io" + "strconv" +) + +func writeJunit(findings []Finding, w io.WriteCloser) error { + testSuites := TestSuites{ + TestSuites: getTestSuites(findings), + } + + io.WriteString(w, xml.Header) + encoder := xml.NewEncoder(w) + encoder.Indent("", "\t") + return encoder.Encode(testSuites) +} + +func getTestSuites(findings []Finding) []TestSuite { + return []TestSuite{ + { + Failures: strconv.Itoa(len(findings)), + Name: "gitleaks", + Tests: strconv.Itoa(len(findings)), + TestCases: getTestCases(findings), + Time: "", + }, + } +} + +func getTestCases(findings []Finding) []TestCase { + testCases := []TestCase{} + for _, f := range findings { + testCase := TestCase{ + Classname: f.Description, + Failure: getFailure(f), + File: f.File, + Name: getMessage(f), + Time: "", + } + testCases = append(testCases, testCase) + } + return testCases +} + +func getFailure(f Finding) Failure { + return Failure{ + Data: getData(f), + Message: getMessage(f), + Type: f.Description, + } +} + +func getData(f Finding) string { + data, err := json.MarshalIndent(f, "", "\t") + if err != nil { + fmt.Println(err) + return "" + } + return string(data) +} + +func getMessage(f Finding) string { + if f.Commit == "" { + return fmt.Sprintf("%s has detected a secret in file %s, line %s.", f.RuleID, f.File, strconv.Itoa(f.StartLine)) + } + + return fmt.Sprintf("%s has detected a secret in file %s, line %s, at commit %s.", f.RuleID, f.File, strconv.Itoa(f.StartLine), f.Commit) +} + +type TestSuites struct { + XMLName xml.Name `xml:"testsuites"` + TestSuites []TestSuite +} + +type TestSuite struct { + XMLName xml.Name `xml:"testsuite"` + Failures string `xml:"failures,attr"` + Name string `xml:"name,attr"` + Tests string `xml:"tests,attr"` + TestCases []TestCase `xml:"testcase"` + Time string `xml:"time,attr"` +} + +type TestCase struct { + XMLName xml.Name `xml:"testcase"` + Classname string `xml:"classname,attr"` + Failure Failure `xml:"failure"` + File string `xml:"file,attr"` + Name string `xml:"name,attr"` + Time string `xml:"time,attr"` +} + +type Failure struct { + XMLName xml.Name `xml:"failure"` + Data string `xml:",chardata"` + Message string `xml:"message,attr"` + Type string `xml:"type,attr"` +} diff --git a/report/junit_test.go b/report/junit_test.go new file mode 100644 index 00000000..de59b66d --- /dev/null +++ b/report/junit_test.go @@ -0,0 +1,107 @@ +package report + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func TestWriteJunit(t *testing.T) { + tests := []struct { + findings []Finding + testReportName string + expected string + wantEmpty bool + }{ + { + testReportName: "simple", + expected: filepath.Join(expectPath, "report", "junit_simple.xml"), + findings: []Finding{ + { + + Description: "Test Rule", + RuleID: "test-rule", + Match: "line containing secret", + Secret: "a secret", + StartLine: 1, + EndLine: 2, + StartColumn: 1, + EndColumn: 2, + Message: "opps", + File: "auth.py", + Commit: "0000000000000000", + Author: "John Doe", + Email: "johndoe@gmail.com", + Date: "10-19-2003", + Tags: []string{}, + }, + { + + Description: "Test Rule", + RuleID: "test-rule", + Match: "line containing secret", + Secret: "a secret", + StartLine: 2, + EndLine: 3, + StartColumn: 1, + EndColumn: 2, + Message: "", + File: "auth.py", + Commit: "", + Author: "", + Email: "", + Date: "", + Tags: []string{}, + }, + }, + }, + { + testReportName: "empty", + expected: filepath.Join(expectPath, "report", "junit_empty.xml"), + findings: []Finding{}, + }, + } + + for _, test := range tests { + // create tmp file using os.TempDir() + tmpfile, err := os.Create(filepath.Join(tmpPath, test.testReportName+".xml")) + if err != nil { + os.Remove(tmpfile.Name()) + t.Error(err) + } + err = writeJunit(test.findings, tmpfile) + if err != nil { + os.Remove(tmpfile.Name()) + t.Error(err) + } + got, err := os.ReadFile(tmpfile.Name()) + if err != nil { + os.Remove(tmpfile.Name()) + t.Error(err) + } + if test.wantEmpty { + if len(got) > 0 { + os.Remove(tmpfile.Name()) + t.Errorf("Expected empty file, got %s", got) + } + os.Remove(tmpfile.Name()) + continue + } + want, err := os.ReadFile(test.expected) + if err != nil { + os.Remove(tmpfile.Name()) + t.Error(err) + } + + if string(got) != string(want) { + err = os.WriteFile(strings.Replace(test.expected, ".xml", ".got.xml", 1), got, 0644) + if err != nil { + t.Error(err) + } + t.Errorf("got %s, want %s", string(got), string(want)) + } + + os.Remove(tmpfile.Name()) + } +} diff --git a/report/report.go b/report/report.go index c085c53b..f2a69464 100644 --- a/report/report.go +++ b/report/report.go @@ -24,6 +24,8 @@ func Write(findings []Finding, cfg config.Config, ext string, reportPath string) err = writeJson(findings, file) case ".csv", "csv": err = writeCsv(findings, file) + case ".xml", "junit": + err = writeJunit(findings, file) case ".sarif", "sarif": err = writeSarif(cfg, findings, file) } diff --git a/report/report_test.go b/report/report_test.go index 901aae69..fbea538f 100644 --- a/report/report_test.go +++ b/report/report_test.go @@ -68,6 +68,22 @@ func TestReport(t *testing.T) { }, }, }, + { + ext: ".xml", + findings: []Finding{ + { + RuleID: "test-rule", + }, + }, + }, + { + ext: "junit", + findings: []Finding{ + { + RuleID: "test-rule", + }, + }, + }, // { // ext: "SARIF", // findings: []Finding{ diff --git a/testdata/expected/report/junit_empty.xml b/testdata/expected/report/junit_empty.xml new file mode 100644 index 00000000..45702c47 --- /dev/null +++ b/testdata/expected/report/junit_empty.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/testdata/expected/report/junit_simple.xml b/testdata/expected/report/junit_simple.xml new file mode 100644 index 00000000..7cabafec --- /dev/null +++ b/testdata/expected/report/junit_simple.xml @@ -0,0 +1,11 @@ + + + + + { "Description": "Test Rule", "StartLine": 1, "EndLine": 2, "StartColumn": 1, "EndColumn": 2, "Match": "line containing secret", "Secret": "a secret", "File": "auth.py", "SymlinkFile": "", "Commit": "0000000000000000", "Entropy": 0, "Author": "John Doe", "Email": "johndoe@gmail.com", "Date": "10-19-2003", "Message": "opps", "Tags": [], "RuleID": "test-rule", "Fingerprint": "" } + + + { "Description": "Test Rule", "StartLine": 2, "EndLine": 3, "StartColumn": 1, "EndColumn": 2, "Match": "line containing secret", "Secret": "a secret", "File": "auth.py", "SymlinkFile": "", "Commit": "", "Entropy": 0, "Author": "", "Email": "", "Date": "", "Message": "", "Tags": [], "RuleID": "test-rule", "Fingerprint": "" } + + + \ No newline at end of file