From 9f579c2e1ced93185420f8c20c3a4ca43c557fcf Mon Sep 17 00:00:00 2001 From: Devang Date: Sun, 30 May 2021 14:05:25 +0530 Subject: [PATCH] added version property in sarif output --- go.mod | 6 +++--- go.sum | 6 ++++++ pkg/http-server/helpers.go | 2 +- pkg/http-server/remote-repo.go | 2 +- pkg/version/version.go | 9 ++++++++- pkg/writer/sarif.go | 5 ++++- pkg/writer/sarif_test.go | 5 ++++- 7 files changed, 27 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index a56594b17..efbd478bf 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 682b3ce99..03eed2346 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/pkg/http-server/helpers.go b/pkg/http-server/helpers.go index 3f4161a24..79fa0384a 100644 --- a/pkg/http-server/helpers.go +++ b/pkg/http-server/helpers.go @@ -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) diff --git a/pkg/http-server/remote-repo.go b/pkg/http-server/remote-repo.go index 5403b8107..ee819a223 100644 --- a/pkg/http-server/remote-repo.go +++ b/pkg/http-server/remote-repo.go @@ -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"` diff --git a/pkg/version/version.go b/pkg/version/version.go index b118c307b..03027cc06 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -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 } diff --git a/pkg/writer/sarif.go b/pkg/writer/sarif.go index 1d0790751..4bea5b460 100644 --- a/pkg/writer/sarif.go +++ b/pkg/writer/sarif.go @@ -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" @@ -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) @@ -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(). diff --git a/pkg/writer/sarif_test.go b/pkg/writer/sarif_test.go index f8954c6a0..c89e43f25 100644 --- a/pkg/writer/sarif_test.go +++ b/pkg/writer/sarif_test.go @@ -17,6 +17,7 @@ const expectedSarifOutput1 = `{ "tool": { "driver": { "name": "terrascan", + "version": "1.6.0", "informationUri": "https://github.com/accurics/terrascan", "rules": [ { @@ -47,7 +48,7 @@ const expectedSarifOutput1 = `{ "uri": "file://test/modules/m1/main.tf" }, "region": { - "startLine": 19 + "startLine": 20 } }, "logicalLocations": [ @@ -72,6 +73,7 @@ const expectedSarifOutput2 = `{ "tool": { "driver": { "name": "terrascan", + "version": "1.6.0", "informationUri": "https://github.com/accurics/terrascan" } }, @@ -88,6 +90,7 @@ const expectedSarifOutput3 = `{ "tool": { "driver": { "name": "terrascan", + "version": "1.6.0", "informationUri": "https://github.com/accurics/terrascan", "rules": [ {