Skip to content

Commit

Permalink
Fix to not hash again if Checksum is set
Browse files Browse the repository at this point in the history
  • Loading branch information
xibz committed Apr 13, 2016
1 parent 1411945 commit 9b0eaa1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
14 changes: 5 additions & 9 deletions service/glacier/customizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,16 @@ func addAccountID(r *request.Request) {
}

func addChecksum(r *request.Request) {
if r.Body == nil {
if r.Body == nil || r.HTTPRequest.Header.Get("X-Amz-Sha256-Tree-Hash") != "" {
return
}

h := ComputeHashes(r.Body)
hstr := hex.EncodeToString(h.TreeHash)
r.HTTPRequest.Header.Set("X-Amz-Sha256-Tree-Hash", hstr)

if r.HTTPRequest.Header.Get("X-Amz-Content-Sha256") == "" {
hstr := hex.EncodeToString(h.LinearHash)
r.HTTPRequest.Header.Set("X-Amz-Content-Sha256", hstr)
}
if r.HTTPRequest.Header.Get("X-Amz-Sha256-Tree-Hash") == "" {
hstr := hex.EncodeToString(h.TreeHash)
r.HTTPRequest.Header.Set("X-Amz-Sha256-Tree-Hash", hstr)
}
hLstr := hex.EncodeToString(h.LinearHash)
r.HTTPRequest.Header.Set("X-Amz-Content-Sha256", hLstr)
}

func addAPIVersion(r *request.Request) {
Expand Down
13 changes: 13 additions & 0 deletions service/glacier/customizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,16 @@ func TestFillAccountIDWithNilStruct(t *testing.T) {
assert.Equal(t, empty, req.HTTPRequest.Header.Get("x-amz-content-sha256"))
assert.Equal(t, "", req.HTTPRequest.Header.Get("x-amz-sha256-tree-hash"))
}

func TestHashOnce(t *testing.T) {
req, _ := svc.UploadArchiveRequest(&glacier.UploadArchiveInput{
VaultName: aws.String("vault"),
Body: payloadBuf,
})
req.HTTPRequest.Header.Set("X-Amz-Sha256-Tree-Hash", "0")

err := req.Build()
assert.NoError(t, err)

assert.Equal(t, "0", req.HTTPRequest.Header.Get("x-amz-sha256-tree-hash"))
}

0 comments on commit 9b0eaa1

Please sign in to comment.