From 56d57afb57cd03b09ed1d52f25944a9071e56a24 Mon Sep 17 00:00:00 2001 From: sundowndev Date: Fri, 4 Mar 2022 15:16:28 +0400 Subject: [PATCH 1/2] feat: add scan_duration json field --- pkg/analyser/analysis.go | 5 ++++- pkg/analyser/analyzer_test.go | 5 ++++- pkg/analyser/testdata/output.json | 3 ++- pkg/cmd/scan/output/output_test.go | 2 ++ pkg/cmd/scan/output/testdata/output.json | 3 ++- .../scan/output/testdata/output_access_denied_alert_aws.json | 3 ++- .../output/testdata/output_access_denied_alert_github.json | 3 ++- pkg/cmd/scan/output/testdata/output_computed_fields.json | 3 ++- pkg/cmd/scan/output/testdata/output_multiples_times.json | 3 ++- 9 files changed, 22 insertions(+), 8 deletions(-) diff --git a/pkg/analyser/analysis.go b/pkg/analyser/analysis.go index 49a0b349..c04bbc5a 100644 --- a/pkg/analyser/analysis.go +++ b/pkg/analyser/analysis.go @@ -64,6 +64,7 @@ type serializableAnalysis struct { Alerts map[string][]alerter.SerializableAlert `json:"alerts"` ProviderName string `json:"provider_name"` ProviderVersion string `json:"provider_version"` + Duration int `json:"scan_duration"` } type GenDriftIgnoreOptions struct { @@ -107,6 +108,7 @@ func (a Analysis) MarshalJSON() ([]byte, error) { bla.Coverage = a.Coverage() bla.ProviderName = a.ProviderName bla.ProviderVersion = a.ProviderVersion + bla.Duration = int(a.Duration.Seconds()) return json.Marshal(bla) } @@ -167,7 +169,8 @@ func (a *Analysis) UnmarshalJSON(bytes []byte) error { } a.ProviderName = bla.ProviderName a.ProviderVersion = bla.ProviderVersion - a.summary.TotalIaCSourceCount = bla.Summary.TotalIaCSourceCount + a.SetIaCSourceCount(bla.Summary.TotalIaCSourceCount) + a.Duration = time.Duration(bla.Duration) * time.Second return nil } diff --git a/pkg/analyser/analyzer_test.go b/pkg/analyser/analyzer_test.go index 2f2f2e59..0c79fc6c 100644 --- a/pkg/analyser/analyzer_test.go +++ b/pkg/analyser/analyzer_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "io/ioutil" "testing" + "time" "github.com/snyk/driftctl/pkg/filter" "github.com/stretchr/testify/mock" @@ -1269,7 +1270,9 @@ func addSchemaToRes(res *resource.Resource, repo resource.SchemaRepositoryInterf func TestAnalysis_MarshalJSON(t *testing.T) { goldenFile := "./testdata/output.json" - analysis := Analysis{} + analysis := Analysis{ + Duration: 241 * time.Second, + } analysis.SetIaCSourceCount(1) analysis.AddManaged( &resource.Resource{ diff --git a/pkg/analyser/testdata/output.json b/pkg/analyser/testdata/output.json index 69dc35f0..9acc6122 100644 --- a/pkg/analyser/testdata/output.json +++ b/pkg/analyser/testdata/output.json @@ -70,5 +70,6 @@ ] }, "provider_name": "AWS", - "provider_version": "2.18.5" + "provider_version": "2.18.5", + "scan_duration": 241 } \ No newline at end of file diff --git a/pkg/cmd/scan/output/output_test.go b/pkg/cmd/scan/output/output_test.go index 9714b648..42d47cef 100644 --- a/pkg/cmd/scan/output/output_test.go +++ b/pkg/cmd/scan/output/output_test.go @@ -4,6 +4,7 @@ import ( "fmt" "reflect" "testing" + "time" "github.com/pkg/errors" "github.com/r3labs/diff/v2" @@ -23,6 +24,7 @@ func fakeAnalysis(opts analyser.AnalyzerOptions) *analyser.Analysis { } a := analyser.NewAnalysis(opts) a.SetIaCSourceCount(3) + a.Duration = 12 * time.Second a.AddUnmanaged( &resource.Resource{ Id: "unmanaged-id-1", diff --git a/pkg/cmd/scan/output/testdata/output.json b/pkg/cmd/scan/output/testdata/output.json index bf1ad3da..90be7d78 100644 --- a/pkg/cmd/scan/output/testdata/output.json +++ b/pkg/cmd/scan/output/testdata/output.json @@ -107,5 +107,6 @@ "coverage": 33, "alerts": null, "provider_name": "AWS", - "provider_version": "3.19.0" + "provider_version": "3.19.0", + "scan_duration": 12 } \ No newline at end of file diff --git a/pkg/cmd/scan/output/testdata/output_access_denied_alert_aws.json b/pkg/cmd/scan/output/testdata/output_access_denied_alert_aws.json index 1a807b52..c5f788e1 100644 --- a/pkg/cmd/scan/output/testdata/output_access_denied_alert_aws.json +++ b/pkg/cmd/scan/output/testdata/output_access_denied_alert_aws.json @@ -26,5 +26,6 @@ ] }, "provider_name": "AWS", - "provider_version": "3.19.0" + "provider_version": "3.19.0", + "scan_duration": 0 } \ No newline at end of file diff --git a/pkg/cmd/scan/output/testdata/output_access_denied_alert_github.json b/pkg/cmd/scan/output/testdata/output_access_denied_alert_github.json index 102240db..0fb916d4 100644 --- a/pkg/cmd/scan/output/testdata/output_access_denied_alert_github.json +++ b/pkg/cmd/scan/output/testdata/output_access_denied_alert_github.json @@ -23,5 +23,6 @@ ] }, "provider_name": "AWS", - "provider_version": "3.19.0" + "provider_version": "3.19.0", + "scan_duration": 0 } \ No newline at end of file diff --git a/pkg/cmd/scan/output/testdata/output_computed_fields.json b/pkg/cmd/scan/output/testdata/output_computed_fields.json index 191a543a..7c83d659 100644 --- a/pkg/cmd/scan/output/testdata/output_computed_fields.json +++ b/pkg/cmd/scan/output/testdata/output_computed_fields.json @@ -91,5 +91,6 @@ ] }, "provider_name": "AWS", - "provider_version": "3.19.0" + "provider_version": "3.19.0", + "scan_duration": 0 } \ No newline at end of file diff --git a/pkg/cmd/scan/output/testdata/output_multiples_times.json b/pkg/cmd/scan/output/testdata/output_multiples_times.json index d07929d4..c061981a 100644 --- a/pkg/cmd/scan/output/testdata/output_multiples_times.json +++ b/pkg/cmd/scan/output/testdata/output_multiples_times.json @@ -14,5 +14,6 @@ "coverage": 0, "alerts": null, "provider_name": "", - "provider_version": "" + "provider_version": "", + "scan_duration": 0 } \ No newline at end of file From 981a2aa84f9a7077fb728bb196fc6ccd786f1bcc Mon Sep 17 00:00:00 2001 From: sundowndev Date: Fri, 4 Mar 2022 17:56:53 +0400 Subject: [PATCH 2/2] refactor(analyzer): ScanDuration field in analysis Add omitempty struct tag, rename the field to ScanDuration for consistency and make it uint instead of int. --- pkg/analyser/analysis.go | 7 +++---- .../output/testdata/output_access_denied_alert_aws.json | 3 +-- .../output/testdata/output_access_denied_alert_github.json | 3 +-- pkg/cmd/scan/output/testdata/output_computed_fields.json | 3 +-- pkg/cmd/scan/output/testdata/output_multiples_times.json | 3 +-- 5 files changed, 7 insertions(+), 12 deletions(-) diff --git a/pkg/analyser/analysis.go b/pkg/analyser/analysis.go index c04bbc5a..672f9f56 100644 --- a/pkg/analyser/analysis.go +++ b/pkg/analyser/analysis.go @@ -8,7 +8,6 @@ import ( "time" "github.com/r3labs/diff/v2" - "github.com/snyk/driftctl/pkg/alerter" "github.com/snyk/driftctl/pkg/resource" ) @@ -64,7 +63,7 @@ type serializableAnalysis struct { Alerts map[string][]alerter.SerializableAlert `json:"alerts"` ProviderName string `json:"provider_name"` ProviderVersion string `json:"provider_version"` - Duration int `json:"scan_duration"` + ScanDuration uint `json:"scan_duration,omitempty"` } type GenDriftIgnoreOptions struct { @@ -108,7 +107,7 @@ func (a Analysis) MarshalJSON() ([]byte, error) { bla.Coverage = a.Coverage() bla.ProviderName = a.ProviderName bla.ProviderVersion = a.ProviderVersion - bla.Duration = int(a.Duration.Seconds()) + bla.ScanDuration = uint(a.Duration.Seconds()) return json.Marshal(bla) } @@ -170,7 +169,7 @@ func (a *Analysis) UnmarshalJSON(bytes []byte) error { a.ProviderName = bla.ProviderName a.ProviderVersion = bla.ProviderVersion a.SetIaCSourceCount(bla.Summary.TotalIaCSourceCount) - a.Duration = time.Duration(bla.Duration) * time.Second + a.Duration = time.Duration(bla.ScanDuration) * time.Second return nil } diff --git a/pkg/cmd/scan/output/testdata/output_access_denied_alert_aws.json b/pkg/cmd/scan/output/testdata/output_access_denied_alert_aws.json index c5f788e1..1a807b52 100644 --- a/pkg/cmd/scan/output/testdata/output_access_denied_alert_aws.json +++ b/pkg/cmd/scan/output/testdata/output_access_denied_alert_aws.json @@ -26,6 +26,5 @@ ] }, "provider_name": "AWS", - "provider_version": "3.19.0", - "scan_duration": 0 + "provider_version": "3.19.0" } \ No newline at end of file diff --git a/pkg/cmd/scan/output/testdata/output_access_denied_alert_github.json b/pkg/cmd/scan/output/testdata/output_access_denied_alert_github.json index 0fb916d4..102240db 100644 --- a/pkg/cmd/scan/output/testdata/output_access_denied_alert_github.json +++ b/pkg/cmd/scan/output/testdata/output_access_denied_alert_github.json @@ -23,6 +23,5 @@ ] }, "provider_name": "AWS", - "provider_version": "3.19.0", - "scan_duration": 0 + "provider_version": "3.19.0" } \ No newline at end of file diff --git a/pkg/cmd/scan/output/testdata/output_computed_fields.json b/pkg/cmd/scan/output/testdata/output_computed_fields.json index 7c83d659..191a543a 100644 --- a/pkg/cmd/scan/output/testdata/output_computed_fields.json +++ b/pkg/cmd/scan/output/testdata/output_computed_fields.json @@ -91,6 +91,5 @@ ] }, "provider_name": "AWS", - "provider_version": "3.19.0", - "scan_duration": 0 + "provider_version": "3.19.0" } \ No newline at end of file diff --git a/pkg/cmd/scan/output/testdata/output_multiples_times.json b/pkg/cmd/scan/output/testdata/output_multiples_times.json index c061981a..d07929d4 100644 --- a/pkg/cmd/scan/output/testdata/output_multiples_times.json +++ b/pkg/cmd/scan/output/testdata/output_multiples_times.json @@ -14,6 +14,5 @@ "coverage": 0, "alerts": null, "provider_name": "", - "provider_version": "", - "scan_duration": 0 + "provider_version": "" } \ No newline at end of file