Skip to content

Commit

Permalink
added gojay for report v2 for validation and such
Browse files Browse the repository at this point in the history
  • Loading branch information
LiorAlafiArmo committed Jan 3, 2022
1 parent 32211ec commit ea9029d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion reporthandling/v2/datastructures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
19 changes: 19 additions & 0 deletions reporthandling/v2/datastructures_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v2

import (
"bytes"
"encoding/json"
"testing"
"time"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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())

}
49 changes: 49 additions & 0 deletions reporthandling/v2/gojayunmarshaller.go
Original file line number Diff line number Diff line change
@@ -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
}

//------------------------

0 comments on commit ea9029d

Please sign in to comment.