Skip to content

Commit 8c5f7ca

Browse files
committed
Normalize timezones in JSON output
This commit overrides the marshaling functions of the statement and vex.VEX object to normalize the timezones to Zulu and comply better with the in-toto guidelines (see https://github.com/in-toto/attestation/blob/main/docs/new_predicate_guidelines.md) Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
1 parent 5bf7fa5 commit 8c5f7ca

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

pkg/vex/statement.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ SPDX-License-Identifier: Apache-2.0
66
package vex
77

88
import (
9+
"encoding/json"
910
"fmt"
1011
"sort"
1112
"strings"
@@ -206,3 +207,27 @@ func (stmt *Statement) MatchesProduct(identifier, subidentifier string) bool {
206207
}
207208
return false
208209
}
210+
211+
// MarshalJSON the document object overrides its marshaling function to normalize
212+
// the timezones in all dates to Zulu.
213+
func (stmt *Statement) MarshalJSON() ([]byte, error) {
214+
type alias Statement
215+
var ts, lu string
216+
217+
if stmt.Timestamp != nil {
218+
ts = stmt.Timestamp.UTC().Format(time.RFC3339Nano)
219+
}
220+
if stmt.LastUpdated != nil {
221+
lu = stmt.LastUpdated.UTC().Format(time.RFC3339Nano)
222+
}
223+
224+
return json.Marshal(&struct {
225+
*alias
226+
TimeZonedTimestamp string `json:"timestamp"`
227+
TimeZonedLastUpdated string `json:"last_updated,omitempty"`
228+
}{
229+
alias: (*alias)(stmt),
230+
TimeZonedTimestamp: ts,
231+
TimeZonedLastUpdated: lu,
232+
})
233+
}

pkg/vex/vex.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,30 @@ func (vexDoc *VEX) ToJSON(w io.Writer) error {
132132
return nil
133133
}
134134

135+
// MarshalJSON the document object overrides its marshaling function to normalize
136+
// the timezones in all dates to Zulu.
137+
func (vexDoc *VEX) MarshalJSON() ([]byte, error) {
138+
type alias VEX
139+
var ts, lu string
140+
141+
if vexDoc.Timestamp != nil {
142+
ts = vexDoc.Timestamp.UTC().Format(time.RFC3339)
143+
}
144+
if vexDoc.LastUpdated != nil {
145+
lu = vexDoc.LastUpdated.UTC().Format(time.RFC3339)
146+
}
147+
148+
return json.Marshal(&struct {
149+
*alias
150+
TimeZonedTimestamp string `json:"timestamp"`
151+
TimeZonedLastUpdated string `json:"last_updated,omitempty"`
152+
}{
153+
TimeZonedTimestamp: ts,
154+
TimeZonedLastUpdated: lu,
155+
alias: (*alias)(vexDoc),
156+
})
157+
}
158+
135159
// EffectiveStatement returns the latest VEX statement for a given product and
136160
// vulnerability, that is the statement that contains the latest data about
137161
// impact to a given product.

0 commit comments

Comments
 (0)