Skip to content

Commit

Permalink
added version property in sarif output
Browse files Browse the repository at this point in the history
  • Loading branch information
devang-gaur committed May 31, 2021
1 parent 5bec8fb commit 9f579c2
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 8 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ require (
github.com/spf13/cobra v1.1.1
github.com/zclconf/go-cty v1.8.2
go.uber.org/zap v1.16.0
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744
golang.org/x/tools v0.1.1 // indirect
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea
golang.org/x/tools v0.1.2 // indirect
gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
helm.sh/helm/v3 v3.4.0
honnef.co/go/tools v0.1.4 // indirect
honnef.co/go/tools v0.2.0 // indirect
k8s.io/api v0.19.2
k8s.io/apimachinery v0.19.2
k8s.io/client-go v10.0.0+incompatible
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,8 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 h1:yhBbb4IRs2HS9PPlAg6DMC6mUOKexJBNsLf4Z+6En1Q=
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea h1:+WiDlPBBaO+h9vPNZi8uJ3k4BkKQB7Iow3aqwHVA5hI=
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -1233,6 +1235,8 @@ golang.org/x/tools v0.0.0-20201028111035-eafbe7b904eb/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.2 h1:kRBLX7v7Af8W7Gdbbc908OJcdgtK8bOz9Uaj8/F1ACA=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down Expand Up @@ -1388,6 +1392,8 @@ honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.1.4/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las=
honnef.co/go/tools v0.2.0 h1:ws8AfbgTX3oIczLPNPCu5166oBg9ST2vNs0rcht+mDE=
honnef.co/go/tools v0.2.0/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY=
k8s.io/api v0.19.0 h1:XyrFIJqTYZJ2DU7FBE/bSPz7b1HvbVBuBf07oeo6eTc=
k8s.io/api v0.19.0/go.mod h1:I1K45XlvTrDjmj5LoM5LuP/KYrhWbjUKT/SoPG0qTjw=
k8s.io/apiextensions-apiserver v0.19.2 h1:oG84UwiDsVDu7dlsGQs5GySmQHCzMhknfhFExJMz9tA=
Expand Down
2 changes: 1 addition & 1 deletion pkg/http-server/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func apiErrorResponse(w http.ResponseWriter, errMsg string, statusCode int) {
http.Error(w, errMsg, statusCode)
}

func apiSarifResponse(w http.ResponseWriter, output interface{}, statusCode int ) {
func apiSarifResponse(w http.ResponseWriter, output interface{}, statusCode int) {
w.WriteHeader(statusCode)
w.Header().Set("Content-Type", "application/json")
writer.SarifWriter(output, w)
Expand Down
2 changes: 1 addition & 1 deletion pkg/http-server/remote-repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type scanRemoteRepoReq struct {
RemoteType string `json:"remote_type"`
RemoteURL string `json:"remote_url"`
ConfigOnly bool `json:"config_only"`
OutputSarif bool `json:"output_sarif"`
OutputSarif bool `json:"output_sarif"`
ScanRules []string `json:"scan_rules"`
SkipRules []string `json:"skip_rules"`
Categories []string `json:"categories"`
Expand Down
9 changes: 8 additions & 1 deletion pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@

package version

import "fmt"

// Terrascan The Terrascan version
const Terrascan = "v1.6.0"
const Terrascan = "1.6.0"

// Get returns the terrascan version
func Get() string {
return fmt.Sprintf("v%s", Terrascan)
}

// GetNumeric returns the numeric terrascan version
func GetNumeric() string {
return Terrascan
}
5 changes: 4 additions & 1 deletion pkg/writer/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package writer
import (
"fmt"
"github.com/accurics/terrascan/pkg/policy"
"github.com/accurics/terrascan/pkg/version"
"github.com/owenrumney/go-sarif/sarif"
"io"
"path/filepath"
Expand All @@ -42,6 +43,8 @@ func SarifWriter(data interface{}, writer io.Writer) error {
}

run := sarif.NewRun("terrascan", "https://github.com/accurics/terrascan")
run.Tool.Driver.WithVersion(version.GetNumeric())

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

Expand Down Expand Up @@ -71,7 +74,7 @@ func SarifWriter(data interface{}, writer io.Writer) error {
location := sarif.NewLocation().
WithPhysicalLocation(sarif.NewPhysicalLocation().
WithArtifactLocation(sarif.NewSimpleArtifactLocation(fmt.Sprintf("file://%s", absFilePath))).
WithRegion(sarif.NewRegion().WithStartLine(violation.LineNumber - 1)))
WithRegion(sarif.NewRegion().WithStartLine(violation.LineNumber)))

if len(violation.ResourceType) > 0 && len(violation.ResourceName) > 0 {
location.LogicalLocations = append(location.LogicalLocations, sarif.NewLogicalLocation().
Expand Down
5 changes: 4 additions & 1 deletion pkg/writer/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const expectedSarifOutput1 = `{
"tool": {
"driver": {
"name": "terrascan",
"version": "1.6.0",
"informationUri": "https://github.com/accurics/terrascan",
"rules": [
{
Expand Down Expand Up @@ -47,7 +48,7 @@ const expectedSarifOutput1 = `{
"uri": "file://test/modules/m1/main.tf"
},
"region": {
"startLine": 19
"startLine": 20
}
},
"logicalLocations": [
Expand All @@ -72,6 +73,7 @@ const expectedSarifOutput2 = `{
"tool": {
"driver": {
"name": "terrascan",
"version": "1.6.0",
"informationUri": "https://github.com/accurics/terrascan"
}
},
Expand All @@ -88,6 +90,7 @@ const expectedSarifOutput3 = `{
"tool": {
"driver": {
"name": "terrascan",
"version": "1.6.0",
"informationUri": "https://github.com/accurics/terrascan",
"rules": [
{
Expand Down

0 comments on commit 9f579c2

Please sign in to comment.