diff --git a/.changelog/a6739ebd15a745c3b7284fe3b91746e9.json b/.changelog/a6739ebd15a745c3b7284fe3b91746e9.json new file mode 100644 index 00000000000..2bb9fcdfe92 --- /dev/null +++ b/.changelog/a6739ebd15a745c3b7284fe3b91746e9.json @@ -0,0 +1,8 @@ +{ + "id": "a6739ebd-15a7-45c3-b728-4fe3b91746e9", + "type": "feature", + "description": "Add UnmarshalJSON for AWSEpochTime to correctly unmarshal AWSEpochTime, ([#1298](https://github.com/aws/aws-sdk-go-v2/pull/1298))", + "modules": [ + "feature/cloudfront/sign" + ] +} \ No newline at end of file diff --git a/feature/cloudfront/sign/policy.go b/feature/cloudfront/sign/policy.go index 75263b1b6f5..f780ed5110c 100644 --- a/feature/cloudfront/sign/policy.go +++ b/feature/cloudfront/sign/policy.go @@ -32,6 +32,19 @@ func (t AWSEpochTime) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`{"AWS:EpochTime":%d}`, t.UTC().Unix())), nil } +// UnmarshalJSON unserializes AWS Profile epoch time. +func (t *AWSEpochTime) UnmarshalJSON(data []byte) error { + var epochTime struct { + Sec int64 `json:"AWS:EpochTime"` + } + err := json.Unmarshal(data, &epochTime) + if err != nil { + return err + } + t.Time = time.Unix(epochTime.Sec, 0).UTC() + return nil +} + // An IPAddress wraps an IPAddress source IP providing JSON serialization information type IPAddress struct { SourceIP string `json:"AWS:SourceIp"` diff --git a/feature/cloudfront/sign/policy_test.go b/feature/cloudfront/sign/policy_test.go index 7f335aafcb7..4273c242e18 100644 --- a/feature/cloudfront/sign/policy_test.go +++ b/feature/cloudfront/sign/policy_test.go @@ -6,6 +6,7 @@ import ( "crypto/rsa" "crypto/sha1" "encoding/base64" + "encoding/json" "fmt" "math/rand" "strings" @@ -27,6 +28,22 @@ func TestEpochTimeMarshal(t *testing.T) { } } +func TestEpochTimeUnmarshal(t *testing.T) { + now := time.Now().Round(time.Second) + data := fmt.Sprintf(`{"AWS:EpochTime":%d}`, now.Unix()) + var v AWSEpochTime + err := json.Unmarshal([]byte(data), &v) + if err != nil { + t.Fatalf("Unexpected error, %#v", err) + } + + expected := now.UTC() + if v.Time != expected { + t.Errorf("Expected unmarshaled time to match, expect: %s, actual: %s", + expected, v.Time) + } +} + var testCreateResource = []struct { scheme, u string expect string