From 606578d3ae949dcf1d51a5e28eca8c9f3efd508e Mon Sep 17 00:00:00 2001 From: Jason Del Ponte <961963+jasdel@users.noreply.github.com> Date: Mon, 7 Mar 2022 16:51:29 -0800 Subject: [PATCH] service/internal/checksum: Fix handling of require checksum Fixes SDK's checksum handling of require checksum with unseekable streams. Adds additional unit tests to validate the expected behavior of empty streams that are unseekable. Related to https://github.com/aws/smithy-go/pull/357 --- modman.toml | 2 +- .../middleware_compute_input_checksum.go | 6 +++ .../middleware_compute_input_checksum_test.go | 48 ++++++++++++++++++- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/modman.toml b/modman.toml index a18a53a3a3e..ba41f0cf246 100644 --- a/modman.toml +++ b/modman.toml @@ -1,6 +1,6 @@ [dependencies] - "github.com/aws/smithy-go" = "v1.11.0" + "github.com/aws/smithy-go" = "v1.11.1-0.20220308004241-26b41c98827" "github.com/google/go-cmp" = "v0.5.7" "github.com/jmespath/go-jmespath" = "v0.4.0" "golang.org/x/net" = "v0.0.0-20220127200216-cd36cc0744dd" diff --git a/service/internal/checksum/middleware_compute_input_checksum.go b/service/internal/checksum/middleware_compute_input_checksum.go index 675d0928ced..7c4d419d5cf 100644 --- a/service/internal/checksum/middleware_compute_input_checksum.go +++ b/service/internal/checksum/middleware_compute_input_checksum.go @@ -461,6 +461,12 @@ func setMD5Checksum(ctx context.Context, req *smithyhttp.Request) (string, error if stream == nil { return "", nil } + + if !req.IsStreamSeekable() { + return "", fmt.Errorf( + "unseekable stream is not supported for computing md5 checksum") + } + v, err := computeMD5Checksum(stream) if err != nil { return "", err diff --git a/service/internal/checksum/middleware_compute_input_checksum_test.go b/service/internal/checksum/middleware_compute_input_checksum_test.go index f8c0e6e4912..a3e1b3fe12d 100644 --- a/service/internal/checksum/middleware_compute_input_checksum_test.go +++ b/service/internal/checksum/middleware_compute_input_checksum_test.go @@ -167,6 +167,52 @@ func TestComputeInputPayloadChecksum(t *testing.T) { "CRC32": "AAAAAA==", }, }, + "https empty stream unseekable": { + initContext: func(ctx context.Context) context.Context { + return setContextInputAlgorithm(ctx, string(AlgorithmCRC32)) + }, + buildInput: middleware.BuildInput{ + Request: func() *smithyhttp.Request { + r := smithyhttp.NewStackRequest().(*smithyhttp.Request) + r.URL, _ = url.Parse("https://example.aws") + r.ContentLength = 0 + r = requestMust(r.SetStream(&bytes.Buffer{})) + return r + }(), + }, + expectHeader: http.Header{ + "X-Amz-Checksum-Crc32": []string{"AAAAAA=="}, + }, + expectContentLength: 0, + expectPayload: nil, + expectPayloadHash: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + expectChecksumMetadata: map[string]string{ + "CRC32": "AAAAAA==", + }, + }, + "http empty stream unseekable": { + initContext: func(ctx context.Context) context.Context { + return setContextInputAlgorithm(ctx, string(AlgorithmCRC32)) + }, + buildInput: middleware.BuildInput{ + Request: func() *smithyhttp.Request { + r := smithyhttp.NewStackRequest().(*smithyhttp.Request) + r.URL, _ = url.Parse("http://example.aws") + r.ContentLength = 0 + r = requestMust(r.SetStream(&bytes.Buffer{})) + return r + }(), + }, + expectHeader: http.Header{ + "X-Amz-Checksum-Crc32": []string{"AAAAAA=="}, + }, + expectContentLength: 0, + expectPayload: nil, + expectPayloadHash: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + expectChecksumMetadata: map[string]string{ + "CRC32": "AAAAAA==", + }, + }, "https nil stream": { initContext: func(ctx context.Context) context.Context { return setContextInputAlgorithm(ctx, string(AlgorithmCRC32)) @@ -430,7 +476,7 @@ func TestComputeInputPayloadChecksum(t *testing.T) { return r }(), }, - expectErr: "failed to rewind stream", + expectErr: "unseekable stream is not supported", expectBuildErr: true, }, "http unseekable stream": {