Skip to content

Commit

Permalink
Add CreatedAt to the JSON report. (#5542)
Browse files Browse the repository at this point in the history
  • Loading branch information
u5surf committed Nov 9, 2023
1 parent cb241a8 commit 942b1c7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/docs/supply-chain/attestation/vuln.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ $ trivy image --format cosign-vuln --output vuln.json alpine:3.10
},
"result": {
"SchemaVersion": 2,
"CreatedAt": 1629894030,
"ArtifactName": "alpine:3.10",
"ArtifactType": "container_image",
"Metadata": {
Expand Down
5 changes: 5 additions & 0 deletions pkg/cloud/aws/commands/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (

defsecTypes "github.com/aquasecurity/defsec/pkg/types"
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy/pkg/clock"
"github.com/aquasecurity/trivy/pkg/compliance/spec"
"github.com/aquasecurity/trivy/pkg/flag"
)

const expectedS3ScanResult = `{
"CreatedAt": 1629894030,
"ArtifactName": "12345678",
"ArtifactType": "aws_account",
"Metadata": {
Expand Down Expand Up @@ -264,6 +266,7 @@ const expectedS3ScanResult = `{
`

const expectedCustomScanResult = `{
"CreatedAt": 1629894030,
"ArtifactName": "12345678",
"ArtifactType": "aws_account",
"Metadata": {
Expand Down Expand Up @@ -544,6 +547,7 @@ const expectedCustomScanResult = `{
`

const expectedS3AndCloudTrailResult = `{
"CreatedAt": 1629894030,
"ArtifactName": "123456789",
"ArtifactType": "aws_account",
"Metadata": {
Expand Down Expand Up @@ -1125,6 +1129,7 @@ Summary Report for compliance: my-custom-spec
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
clock.SetFakeTime(t, time.Date(2021, 8, 25, 12, 20, 30, 5, time.UTC))
if test.allServices != nil {
oldAllSupportedServicesFunc := allSupportedServicesFunc
allSupportedServicesFunc = func() []string {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloud/report/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/aquasecurity/defsec/pkg/scan"
defsecTypes "github.com/aquasecurity/defsec/pkg/types"
"github.com/aquasecurity/trivy/pkg/clock"
"github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy/pkg/flag"
)
Expand Down Expand Up @@ -148,6 +150,7 @@ Scan Overview for AWS Account
fromCache: false,
expected: `{
"ArtifactType": "aws_account",
"CreatedAt": 1629894030,
"Metadata": {
"ImageConfig": {
"architecture": "",
Expand Down Expand Up @@ -309,6 +312,7 @@ Scan Overview for AWS Account
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
clock.SetFakeTime(t, time.Date(2021, 8, 25, 12, 20, 30, 5, time.UTC))
report := New(
"AWS",
tt.options.AWSOptions.Account,
Expand Down
3 changes: 3 additions & 0 deletions pkg/report/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

cr "github.com/aquasecurity/trivy/pkg/compliance/report"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/clock"
"github.com/aquasecurity/trivy/pkg/flag"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/report/cyclonedx"
Expand Down Expand Up @@ -91,6 +92,8 @@ func Write(report types.Report, option flag.Options) error {
return xerrors.Errorf("unknown format: %v", option.Format)
}

report.CreatedAt = clock.Now().Unix()

if err := writer.Write(report); err != nil {
return xerrors.Errorf("failed to write results: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/types/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
// Report represents a scan result
type Report struct {
SchemaVersion int `json:",omitempty"`
CreatedAt int64 `json:",omitempty"`
ArtifactName string `json:",omitempty"`
ArtifactType ftypes.ArtifactType `json:",omitempty"`
Metadata Metadata `json:",omitempty"`
Expand Down

0 comments on commit 942b1c7

Please sign in to comment.