From ea9029dcfc087183d7d66087262c77ac214a9bb5 Mon Sep 17 00:00:00 2001 From: Lior Alafi Date: Mon, 3 Jan 2022 11:05:56 +0200 Subject: [PATCH] added gojay for report v2 for validation and such --- reporthandling/v2/datastructures.go | 2 +- reporthandling/v2/datastructures_test.go | 19 +++++++++ reporthandling/v2/gojayunmarshaller.go | 49 ++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 reporthandling/v2/gojayunmarshaller.go diff --git a/reporthandling/v2/datastructures.go b/reporthandling/v2/datastructures.go index 6bd106d7..4bd87cb0 100644 --- a/reporthandling/v2/datastructures.go +++ b/reporthandling/v2/datastructures.go @@ -15,7 +15,7 @@ type PostureReport struct { CustomerGUID string `json:"customerGUID"` ClusterName string `json:"clusterName"` ClusterCloudProvider string `json:"clusterCloudProvider"` - ReportID string `json:"reportID"` + ReportID string `json:"reportGUID"` JobID string `json:"jobID"` ClusterAPIServerInfo *version.Info `json:"clusterAPIServerInfo"` ReportGenerationTime time.Time `json:"generationTime"` diff --git a/reporthandling/v2/datastructures_test.go b/reporthandling/v2/datastructures_test.go index 75be7c62..f46c73df 100644 --- a/reporthandling/v2/datastructures_test.go +++ b/reporthandling/v2/datastructures_test.go @@ -1,6 +1,7 @@ package v2 import ( + "bytes" "encoding/json" "testing" "time" @@ -9,6 +10,7 @@ import ( "github.com/armosec/opa-utils/objectsenvelopes" "github.com/armosec/opa-utils/reporthandling/results/v1/reportsummary" "github.com/armosec/opa-utils/reporthandling/results/v1/resourcesresults" + "github.com/francoispqt/gojay" "github.com/stretchr/testify/assert" ) @@ -93,3 +95,20 @@ func TestPostureReportMock(t *testing.T) { assert.Equal(t, 22, len(p.Resources)) // t.Error(p.ToString()) } + +//this test validates the unmarshaller that used to validate the posture object in e.r and other places +func TestPostureReportGojayUnmarshal(t *testing.T) { + postureReport := &PostureReport{} + original := GetPostureReportMock() + asBytes, err := json.Marshal(original) + assert.NoError(t, err, "failed to marshal postureReport") + + err = gojay.NewDecoder(bytes.NewReader(asBytes)).Decode(postureReport) + assert.NoError(t, err, "failed to unmarshal using gojay postureReport") + + assert.Equal(t, original.ReportID, postureReport.ReportID) + assert.Equal(t, original.CustomerGUID, postureReport.CustomerGUID) + assert.Equal(t, original.ClusterName, postureReport.ClusterName) + assert.Equal(t, original.ReportGenerationTime.UTC(), postureReport.ReportGenerationTime.UTC()) + +} diff --git a/reporthandling/v2/gojayunmarshaller.go b/reporthandling/v2/gojayunmarshaller.go new file mode 100644 index 00000000..f20ead70 --- /dev/null +++ b/reporthandling/v2/gojayunmarshaller.go @@ -0,0 +1,49 @@ +package v2 + +import ( + "time" + + "github.com/francoispqt/gojay" +) + +/* + responsible on fast unmarshaling of various COMMON posture report v2 structure for basic validation + +*/ +// UnmarshalJSONObject - File inside a pkg +func (r *PostureReport) UnmarshalJSONObject(dec *gojay.Decoder, key string) (err error) { + + switch key { + case "customerGUID": + err = dec.String(&(r.CustomerGUID)) + + case "clusterName": + err = dec.String(&(r.ClusterName)) + + case "reportGUID": + err = dec.String(&(r.ReportID)) + case "jobID": + err = dec.String(&(r.JobID)) + case "generationTime": + err = dec.Time(&(r.ReportGenerationTime), time.RFC3339) + r.ReportGenerationTime = r.ReportGenerationTime.Local() + } + return err + +} + +// func (files *PkgFiles) UnmarshalJSONArray(dec *gojay.Decoder) error { +// lae := PackageFile{} +// if err := dec.Object(&lae); err != nil { +// return err +// } + +// *files = append(*files, lae) +// return nil +// } + +func (file *PostureReport) NKeys() int { + return 0 +} + +//------------------------