Skip to content

Commit

Permalink
feature/cloudfront/sign: Add json Unmarshal for AWSEpochTime (#1298)
Browse files Browse the repository at this point in the history
Adds UnmarshalJSON for AWSEpochTime to correctly unmarshal AWSEpochTime
  • Loading branch information
iliacimpoes authored Jun 18, 2021
1 parent d00f3da commit 77ed784
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .changelog/a6739ebd15a745c3b7284fe3b91746e9.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
13 changes: 13 additions & 0 deletions feature/cloudfront/sign/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
17 changes: 17 additions & 0 deletions feature/cloudfront/sign/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/rsa"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"math/rand"
"strings"
Expand All @@ -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
Expand Down

0 comments on commit 77ed784

Please sign in to comment.