Skip to content

Commit 77ed784

Browse files
authored
feature/cloudfront/sign: Add json Unmarshal for AWSEpochTime (#1298)
Adds UnmarshalJSON for AWSEpochTime to correctly unmarshal AWSEpochTime
1 parent d00f3da commit 77ed784

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "a6739ebd-15a7-45c3-b728-4fe3b91746e9",
3+
"type": "feature",
4+
"description": "Add UnmarshalJSON for AWSEpochTime to correctly unmarshal AWSEpochTime, ([#1298](https://github.com/aws/aws-sdk-go-v2/pull/1298))",
5+
"modules": [
6+
"feature/cloudfront/sign"
7+
]
8+
}

feature/cloudfront/sign/policy.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ func (t AWSEpochTime) MarshalJSON() ([]byte, error) {
3232
return []byte(fmt.Sprintf(`{"AWS:EpochTime":%d}`, t.UTC().Unix())), nil
3333
}
3434

35+
// UnmarshalJSON unserializes AWS Profile epoch time.
36+
func (t *AWSEpochTime) UnmarshalJSON(data []byte) error {
37+
var epochTime struct {
38+
Sec int64 `json:"AWS:EpochTime"`
39+
}
40+
err := json.Unmarshal(data, &epochTime)
41+
if err != nil {
42+
return err
43+
}
44+
t.Time = time.Unix(epochTime.Sec, 0).UTC()
45+
return nil
46+
}
47+
3548
// An IPAddress wraps an IPAddress source IP providing JSON serialization information
3649
type IPAddress struct {
3750
SourceIP string `json:"AWS:SourceIp"`

feature/cloudfront/sign/policy_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/rsa"
77
"crypto/sha1"
88
"encoding/base64"
9+
"encoding/json"
910
"fmt"
1011
"math/rand"
1112
"strings"
@@ -27,6 +28,22 @@ func TestEpochTimeMarshal(t *testing.T) {
2728
}
2829
}
2930

31+
func TestEpochTimeUnmarshal(t *testing.T) {
32+
now := time.Now().Round(time.Second)
33+
data := fmt.Sprintf(`{"AWS:EpochTime":%d}`, now.Unix())
34+
var v AWSEpochTime
35+
err := json.Unmarshal([]byte(data), &v)
36+
if err != nil {
37+
t.Fatalf("Unexpected error, %#v", err)
38+
}
39+
40+
expected := now.UTC()
41+
if v.Time != expected {
42+
t.Errorf("Expected unmarshaled time to match, expect: %s, actual: %s",
43+
expected, v.Time)
44+
}
45+
}
46+
3047
var testCreateResource = []struct {
3148
scheme, u string
3249
expect string

0 commit comments

Comments
 (0)