mirror of
https://github.com/securego/gosec.git
synced 2024-12-25 03:55:54 +00:00
Use encoding/json for -fmt json output
This commit is contained in:
parent
50fb7f4217
commit
271cff19f7
3 changed files with 42 additions and 40 deletions
|
@ -40,10 +40,10 @@ type Rule interface {
|
||||||
type RuleSet map[reflect.Type][]Rule
|
type RuleSet map[reflect.Type][]Rule
|
||||||
|
|
||||||
type Metrics struct {
|
type Metrics struct {
|
||||||
NumFiles int
|
NumFiles int `json:"files"`
|
||||||
NumLines int
|
NumLines int `json:"lines"`
|
||||||
NumNosec int
|
NumNosec int `json:"nosec"`
|
||||||
NumFound int
|
NumFound int `json:"found"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Analyzer struct {
|
type Analyzer struct {
|
||||||
|
@ -51,8 +51,8 @@ type Analyzer struct {
|
||||||
ruleset RuleSet
|
ruleset RuleSet
|
||||||
context Context
|
context Context
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
Issues []Issue
|
Issues []Issue `json:"issues"`
|
||||||
Stats Metrics
|
Stats Metrics `json:"metrics"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAnalyzer(annotations bool, logger *log.Logger) Analyzer {
|
func NewAnalyzer(annotations bool, logger *log.Logger) Analyzer {
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"os"
|
"os"
|
||||||
|
@ -28,12 +29,12 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Issue struct {
|
type Issue struct {
|
||||||
Severity Score
|
Severity Score `json:"severity"`
|
||||||
Confidence Score
|
Confidence Score `json:"confidence"`
|
||||||
What string
|
What string `json:"details"`
|
||||||
File string
|
File string `json:"file"`
|
||||||
Code string
|
Code string `json:"code"`
|
||||||
Line int
|
Line int `json:"line"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MetaData struct {
|
type MetaData struct {
|
||||||
|
@ -42,6 +43,10 @@ type MetaData struct {
|
||||||
What string
|
What string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Score) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal(c.String())
|
||||||
|
}
|
||||||
|
|
||||||
func (c Score) String() string {
|
func (c Score) String() string {
|
||||||
switch c {
|
switch c {
|
||||||
case High:
|
case High:
|
||||||
|
|
|
@ -15,8 +15,9 @@
|
||||||
package output
|
package output
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"text/template"
|
|
||||||
|
|
||||||
gas "github.com/HewlettPackard/gas/core"
|
gas "github.com/HewlettPackard/gas/core"
|
||||||
)
|
)
|
||||||
|
@ -44,25 +45,6 @@ Summary:
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
var json = `{
|
|
||||||
"metrics": {
|
|
||||||
"files": {{.Stats.NumFiles}},
|
|
||||||
"lines": {{.Stats.NumLines}},
|
|
||||||
"nosec": {{.Stats.NumNosec}},
|
|
||||||
"issues": {{.Stats.NumFound}}
|
|
||||||
},
|
|
||||||
"issues": [
|
|
||||||
{{ range $index, $issue := .Issues }}{{ if $index }}, {{ end }}{
|
|
||||||
"file": "{{ $issue.File }}",
|
|
||||||
"line": "{{ $issue.Line }}",
|
|
||||||
"details": "{{ $issue.What }}",
|
|
||||||
"confidence": "{{ $issue.Confidence }}",
|
|
||||||
"severity": "{{ $issue.Severity }}",
|
|
||||||
"code": "{{ js $issue.Code }}"
|
|
||||||
}{{ end }}
|
|
||||||
]
|
|
||||||
}`
|
|
||||||
|
|
||||||
var csv = `{{ range $index, $issue := .Issues -}}
|
var csv = `{{ range $index, $issue := .Issues -}}
|
||||||
{{- $issue.File -}},
|
{{- $issue.File -}},
|
||||||
{{- $issue.Line -}},
|
{{- $issue.Line -}},
|
||||||
|
@ -73,20 +55,35 @@ var csv = `{{ range $index, $issue := .Issues -}}
|
||||||
{{ end }}`
|
{{ end }}`
|
||||||
|
|
||||||
func CreateReport(w io.Writer, format string, data *gas.Analyzer) error {
|
func CreateReport(w io.Writer, format string, data *gas.Analyzer) error {
|
||||||
reportType := text
|
var err error
|
||||||
|
|
||||||
switch format {
|
switch format {
|
||||||
case "csv":
|
|
||||||
reportType = csv
|
|
||||||
case "json":
|
case "json":
|
||||||
reportType = json
|
err = reportJSON(w, data)
|
||||||
|
case "csv":
|
||||||
|
err = reportFromTemplate(w, csv, data)
|
||||||
case "text":
|
case "text":
|
||||||
reportType = text
|
err = reportFromTemplate(w, text, data)
|
||||||
default:
|
default:
|
||||||
reportType = text
|
err = reportFromTemplate(w, text, data)
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
t, e := template.New("gas").Parse(reportType)
|
func reportJSON(w io.Writer, data *gas.Analyzer) error {
|
||||||
|
raw, err := json.MarshalIndent(data, "", "\t")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = w.Write(raw)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func reportFromTemplate(w io.Writer, reportTemplate string, data *gas.Analyzer) error {
|
||||||
|
t, e := template.New("gas").Parse(reportTemplate)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue