gosec/output/formatter.go

219 lines
5.3 KiB
Go
Raw Normal View History

2016-07-20 11:02:01 +01:00
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package output
import (
"encoding/csv"
2016-07-26 00:39:55 +01:00
"encoding/json"
2018-01-26 03:16:49 +00:00
"encoding/xml"
2016-10-18 06:36:35 +01:00
htmlTemplate "html/template"
2016-07-20 11:02:01 +01:00
"io"
2019-03-11 20:13:48 +00:00
"strconv"
"strings"
2016-10-18 06:36:35 +01:00
plainTemplate "text/template"
2016-07-20 11:02:01 +01:00
"github.com/securego/gosec"
"gopkg.in/yaml.v2"
2016-07-20 11:02:01 +01:00
)
// ReportFormat enumerates the output format for reported issues
2016-07-20 11:02:01 +01:00
type ReportFormat int
const (
// ReportText is the default format that writes to stdout
2016-07-20 11:02:01 +01:00
ReportText ReportFormat = iota // Plain text format
// ReportJSON set the output format to json
ReportJSON // Json format
// ReportCSV set the output format to csv
ReportCSV // CSV format
2018-01-26 03:16:49 +00:00
2018-01-27 04:14:35 +00:00
// ReportJUnitXML set the output format to junit xml
ReportJUnitXML // JUnit XML format
2019-03-12 13:22:58 +00:00
2019-03-13 00:25:11 +00:00
//SonarqubeEffortMinutes effort to fix in minutes
2019-03-12 13:22:58 +00:00
SonarqubeEffortMinutes = 5
2016-07-20 11:02:01 +01:00
)
var text = `Results:
{{range $filePath,$fileErrors := .Errors}}
Golang errors in file: [{{ $filePath }}]:
{{range $index, $error := $fileErrors}}
> [line {{$error.Line}} : column {{$error.Column}}] - {{$error.Err}}
{{end}}
{{end}}
2016-07-20 11:02:01 +01:00
{{ range $index, $issue := .Issues }}
2018-04-16 06:44:54 +01:00
[{{ $issue.File }}:{{ $issue.Line }}] - {{ $issue.RuleID }}: {{ $issue.What }} (Confidence: {{ $issue.Confidence}}, Severity: {{ $issue.Severity }})
2016-07-20 11:02:01 +01:00
> {{ $issue.Code }}
{{ end }}
Summary:
Files: {{.Stats.NumFiles}}
Lines: {{.Stats.NumLines}}
Nosec: {{.Stats.NumNosec}}
Issues: {{.Stats.NumFound}}
`
2017-04-26 01:57:12 +01:00
type reportInfo struct {
Errors map[string][]gosec.Error `json:"Golang errors"`
Issues []*gosec.Issue
Stats *gosec.Metrics
2017-04-26 01:57:12 +01:00
}
2017-12-13 07:39:00 +00:00
// CreateReport generates a report based for the supplied issues and metrics given
// the specified format. The formats currently accepted are: json, csv, html and text.
2019-03-11 20:13:48 +00:00
func CreateReport(w io.Writer, format, rootPath string, issues []*gosec.Issue, metrics *gosec.Metrics, errors map[string][]gosec.Error) error {
2017-04-26 01:57:12 +01:00
data := &reportInfo{
Errors: errors,
2017-04-26 01:57:12 +01:00
Issues: issues,
Stats: metrics,
}
2016-07-26 00:39:55 +01:00
var err error
2016-07-20 11:02:01 +01:00
switch format {
case "json":
2016-07-26 00:39:55 +01:00
err = reportJSON(w, data)
case "yaml":
err = reportYAML(w, data)
2016-07-26 00:39:55 +01:00
case "csv":
err = reportCSV(w, data)
2018-01-27 04:19:38 +00:00
case "junit-xml":
err = reportJUnitXML(w, data)
2016-10-18 06:36:35 +01:00
case "html":
err = reportFromHTMLTemplate(w, html, data)
2016-07-20 11:02:01 +01:00
case "text":
2016-10-18 06:36:35 +01:00
err = reportFromPlaintextTemplate(w, text, data)
2019-03-11 20:13:48 +00:00
case "sonarqube":
err = reportSonarqube(rootPath, w, data)
2016-07-20 11:02:01 +01:00
default:
2016-10-18 06:36:35 +01:00
err = reportFromPlaintextTemplate(w, text, data)
2016-07-26 00:39:55 +01:00
}
return err
}
2019-03-11 20:13:48 +00:00
func reportSonarqube(rootPath string, w io.Writer, data *reportInfo) error {
2019-03-11 21:32:32 +00:00
var si sonarIssues
2019-03-11 20:13:48 +00:00
for _, issue := range data.Issues {
lines := strings.Split(issue.Line, "-")
startLine, err := strconv.Atoi(lines[0])
if err != nil {
return err
}
2019-03-11 20:13:48 +00:00
endLine := startLine
if len(lines) > 1 {
endLine, err = strconv.Atoi(lines[1])
if err != nil {
return err
}
2019-03-11 20:13:48 +00:00
}
s := sonarIssue{
2019-03-11 20:16:30 +00:00
EngineID: "gosec",
RuleID: issue.RuleID,
2019-03-11 20:13:48 +00:00
PrimaryLocation: location{
Message: issue.What,
FilePath: strings.Replace(issue.File, rootPath+"/", "", 1),
TextRange: textRange{StartLine: startLine, EndLine: endLine},
},
Type: "VULNERABILITY",
Severity: getSonarSeverity(issue.Severity.String()),
2019-03-12 13:22:58 +00:00
EffortMinutes: SonarqubeEffortMinutes,
2019-03-11 20:13:48 +00:00
}
2019-03-11 21:32:32 +00:00
si.SonarIssues = append(si.SonarIssues, s)
2019-03-11 20:13:48 +00:00
}
2019-03-11 21:32:32 +00:00
raw, err := json.MarshalIndent(si, "", "\t")
2019-03-11 20:13:48 +00:00
if err != nil {
2019-03-13 00:23:45 +00:00
return err
2019-03-11 20:13:48 +00:00
}
_, err = w.Write(raw)
return err
}
2017-04-26 01:57:12 +01:00
func reportJSON(w io.Writer, data *reportInfo) error {
2016-07-26 00:39:55 +01:00
raw, err := json.MarshalIndent(data, "", "\t")
if err != nil {
2019-03-13 00:23:45 +00:00
return err
2016-07-20 11:02:01 +01:00
}
2016-07-26 00:39:55 +01:00
_, err = w.Write(raw)
return err
}
func reportYAML(w io.Writer, data *reportInfo) error {
raw, err := yaml.Marshal(data)
if err != nil {
return err
}
_, err = w.Write(raw)
return err
}
2017-04-26 01:57:12 +01:00
func reportCSV(w io.Writer, data *reportInfo) error {
out := csv.NewWriter(w)
defer out.Flush()
for _, issue := range data.Issues {
err := out.Write([]string{
issue.File,
2017-10-01 01:31:39 +01:00
issue.Line,
issue.What,
issue.Severity.String(),
issue.Confidence.String(),
issue.Code,
})
if err != nil {
return err
}
}
return nil
}
2018-01-27 04:19:38 +00:00
func reportJUnitXML(w io.Writer, data *reportInfo) error {
2018-01-27 04:14:35 +00:00
groupedData := groupDataByRules(data)
junitXMLStruct := createJUnitXMLStruct(groupedData)
2018-01-26 03:16:49 +00:00
raw, err := xml.MarshalIndent(junitXMLStruct, "", "\t")
2018-01-26 03:16:49 +00:00
if err != nil {
2018-01-30 01:54:30 +00:00
return err
2018-01-26 03:16:49 +00:00
}
2018-01-27 04:14:35 +00:00
xmlHeader := []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
raw = append(xmlHeader, raw...)
2018-01-26 03:16:49 +00:00
_, err = w.Write(raw)
if err != nil {
2018-01-30 01:54:30 +00:00
return err
2018-01-26 03:16:49 +00:00
}
2018-01-30 01:54:30 +00:00
return nil
2018-01-26 03:16:49 +00:00
}
2017-04-26 01:57:12 +01:00
func reportFromPlaintextTemplate(w io.Writer, reportTemplate string, data *reportInfo) error {
t, e := plainTemplate.New("gosec").Parse(reportTemplate)
2016-10-18 06:36:35 +01:00
if e != nil {
return e
}
return t.Execute(w, data)
}
2017-04-26 01:57:12 +01:00
func reportFromHTMLTemplate(w io.Writer, reportTemplate string, data *reportInfo) error {
t, e := htmlTemplate.New("gosec").Parse(reportTemplate)
2016-07-20 11:02:01 +01:00
if e != nil {
return e
}
return t.Execute(w, data)
}