Skip to content

Commit

Permalink
add customization to disable auto decode gzip for s3. Moves customiza…
Browse files Browse the repository at this point in the history
…tions from dynamodb to reuse middleware
  • Loading branch information
skotambkar committed Sep 30, 2020
1 parent 4a1d0a5 commit 6b963de
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ public final class AwsCustomGoDependency extends AwsGoDependency {
"service/apigateway/internal/customizations", "agcust");
public static final GoDependency GLACIER_CUSTOMIZATION = aws(
"service/glacier/internal/customizations", "glaciercust");

public static final GoDependency S3_SHARED_CUSTOMIZATION = awsModuleDep(
"service/internal/s3shared", null, "s3shared");
public static final GoDependency ACCEPT_ENCODING_CUSTOMIZATION = awsModuleDep(
"service/internal/accept-encoding", null, "acceptencodingcust");

private AwsCustomGoDependency() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ private void writeMiddlewareHelper(GoWriter writer) {
writer.openBlock("func $L(stack *middleware.Stack, options Options) {", "}", GZIP_ADDER, () -> {
writer.write("$T(stack, $T{Enable: options.$L})",
SymbolUtils.createValueSymbolBuilder(GZIP_INTERNAL_ADDER,
AwsCustomGoDependency.DYNAMODB_CUSTOMIZATION).build(),
AwsCustomGoDependency.ACCEPT_ENCODING_CUSTOMIZATION).build(),
SymbolUtils.createValueSymbolBuilder(GZIP_INTERNAL_ADDER + "Options",
AwsCustomGoDependency.DYNAMODB_CUSTOMIZATION).build(),
AwsCustomGoDependency.ACCEPT_ENCODING_CUSTOMIZATION).build(),
GZIP_CLIENT_OPTION
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package software.amazon.smithy.aws.go.codegen.customization;

import java.util.List;
import java.util.logging.Logger;
import software.amazon.smithy.aws.go.codegen.AddAwsConfigFields;
import software.amazon.smithy.aws.traits.ServiceTrait;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.go.codegen.GoDelegator;
import software.amazon.smithy.go.codegen.GoSettings;
import software.amazon.smithy.go.codegen.GoWriter;
import software.amazon.smithy.go.codegen.SymbolUtils;
import software.amazon.smithy.go.codegen.integration.ConfigField;
import software.amazon.smithy.go.codegen.integration.GoIntegration;
import software.amazon.smithy.go.codegen.integration.MiddlewareRegistrar;
import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.utils.ListUtils;

/**
* S3AcceptEncodingGzip adds a customization for s3 client to disable
* auto decoding of GZip content by Golang HTTP Client.
*
* This customization provides an option on the S3 client options to enable
* AcceptEncoding for GZIP. The flag if set, will enable auto decompression of
* GZIP by the S3 Client.
*
* By default, the client's auto decompression of GZIP content is turned off.
*/
public class S3AcceptEncodingGzip implements GoIntegration {
private static final Logger LOGGER = Logger.getLogger(AddAwsConfigFields.class.getName());

private static final String GZIP_DISABLE = "disableAcceptEncodingGzip";
private static final String GZIP_INTERNAL_ADDER = "AddAcceptEncodingGzip";

/**
* Gets the sort order of the customization from -128 to 127, with lowest
* executed first.
*
* @return Returns the sort order, defaults to -50.
*/
@Override
public byte getOrder() {
return 127;
}

@Override
public void writeAdditionalFiles(
GoSettings settings,
Model model,
SymbolProvider symbolProvider,
GoDelegator goDelegator
) {
if (!isServiceS3(model, settings.getService(model))) {
return;
}

goDelegator.useShapeWriter(settings.getService(model), this::writeMiddlewareHelper);
}

private void writeMiddlewareHelper(GoWriter writer) {
writer.openBlock("func $L(stack *middleware.Stack) {", "}", GZIP_DISABLE, () -> {
writer.write("$T(stack, $T{})",
SymbolUtils.createValueSymbolBuilder(GZIP_INTERNAL_ADDER,
AwsCustomGoDependency.ACCEPT_ENCODING_CUSTOMIZATION).build(),
SymbolUtils.createValueSymbolBuilder(GZIP_INTERNAL_ADDER + "Options",
AwsCustomGoDependency.ACCEPT_ENCODING_CUSTOMIZATION).build()
);
});
writer.insertTrailingNewline();
}

@Override
public List<RuntimeClientPlugin> getClientPlugins() {
return ListUtils.of(
// register disableAcceptEncodingGzip middleware
RuntimeClientPlugin.builder()
.servicePredicate(S3AcceptEncodingGzip::isServiceS3)
.registerMiddleware(MiddlewareRegistrar.builder()
.resolvedFunction(SymbolUtils.createValueSymbolBuilder(GZIP_DISABLE)
.build())
.build()
)
.build()
);
}

/**
* Return true if service is S3.
*
* @param model the model used for generation.
* @param service the service shape for which default HTTP Client is generated.
* @return true if service is S3
*/
private static boolean isServiceS3(Model model, ServiceShape service) {
return service.expectTrait(ServiceTrait.class).getSdkId().equalsIgnoreCase("S3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ software.amazon.smithy.aws.go.codegen.customization.GlacierCustomizations
software.amazon.smithy.aws.go.codegen.customization.S3ResponseErrorWrapper
software.amazon.smithy.aws.go.codegen.customization.S3MetadataRetriever
software.amazon.smithy.aws.go.codegen.customization.S3ContentSHA256Header
software.amazon.smithy.aws.go.codegen.customization.BackfillS3ObjectSizeMemberShapeType
software.amazon.smithy.aws.go.codegen.customization.BackfillS3ObjectSizeMemberShapeType
software.amazon.smithy.aws.go.codegen.customization.S3AcceptEncodingGzip
86 changes: 40 additions & 46 deletions service/dynamodb/internal/customizations/doc.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
// Package customizations provides customizations for the Amazon DynamoDB API client.
//
// The DynamoDB API client uses two customizations, response checksum validation,
// and manual content-encoding: gzip support.
//
// Middleware layering
//
// Checksum validation needs to be performed first in deserialization chain
// on top of gzip decompression. Since the behavior of Deserialization is
// in reverse order to the other stack steps its easier to consider that
// "after" means "before".
//
// HTTP Response -> Checksum -> gzip decompress -> deserialize
//
// Response checksum validation
//
// DynamoDB responses can include a X-Amz-Crc32 header with the CRC32 checksum
// value of the response body. If the response body is content-encoding: gzip, the
// checksum is of the gzipped response content.
//
// If the header is present, the SDK should validate that the response payload
// computed CRC32 checksum matches the value provided in the header. The checksum
// header is based on the original payload provided returned by the service. Which
// means that if the response is gzipped the checksum is of the gzipped response,
// not the decompressed response bytes.
//
// Customization option:
// DisableValidateResponseChecksum (Enabled by Default)
//
// Accept encoding gzip
//
// The Go HTTP client automatically supports accept-encoding and content-encoding
// gzip by default. This default behavior is not desired by the SDK, and prevents
// validating the response body's checksum. To prevent this the SDK must manually
// control usage of content-encoding gzip.
//
// To control content-encoding, the SDK must always set the `Accept-Encoding`
// header to a value. This prevents the HTTP client from using gzip automatically.
// When gzip is enabled on the API client, the SDK's customization will control
// decompressing the gzip data in order to not break the checksum validation. When
// gzip is disabled, the API client will disable gzip, preventing the HTTP
// client's default behavior.
//
// Customization option:
// EnableAcceptEncodingGzip (Disabled by Default)
//
/*
Package customizations provides customizations for the Amazon DynamoDB API client.
The DynamoDB API client uses two customizations, response checksum validation,
and manual content-encoding: gzip support.
Middleware layering
Checksum validation needs to be performed first in deserialization chain
on top of gzip decompression. Since the behavior of Deserialization is
in reverse order to the other stack steps its easier to consider that
"after" means "before".
HTTP Response -> Checksum -> gzip decompress -> deserialize
Response checksum validation
DynamoDB responses can include a X-Amz-Crc32 header with the CRC32 checksum
value of the response body. If the response body is content-encoding: gzip, the
checksum is of the gzipped response content.
If the header is present, the SDK should validate that the response payload
computed CRC32 checksum matches the value provided in the header. The checksum
header is based on the original payload provided returned by the service. Which
means that if the response is gzipped the checksum is of the gzipped response,
not the decompressed response bytes.
Customization option:
DisableValidateResponseChecksum (Enabled by Default)
Accept encoding gzip
For customization around accept encoding, dynamodb client uses the middlewares
defined at service/internal/accept-encoding. Please refer to the documentation for
`accept-encoding` package for more details.
Customization option:
EnableAcceptEncodingGzip (Disabled by Default)
*/
package customizations
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package customizations
package acceptencoding

import (
"compress/gzip"
"context"
"fmt"
"io"

smithy "github.com/awslabs/smithy-go"
"github.com/awslabs/smithy-go"
"github.com/awslabs/smithy-go/middleware"
smithyhttp "github.com/awslabs/smithy-go/transport/http"
)
Expand All @@ -25,26 +25,26 @@ type AddAcceptEncodingGzipOptions struct {
// computed without disabling GZIP support.
func AddAcceptEncodingGzip(stack *middleware.Stack, options AddAcceptEncodingGzipOptions) {
if options.Enable {
stack.Finalize.Add(&AcceptEncodingGzipMiddleware{}, middleware.Before)
stack.Finalize.Add(&EnableGzipMiddleware{}, middleware.Before)
stack.Deserialize.Insert(&DecompressGzipMiddleware{}, "OperationDeserializer", middleware.After)
return
}

stack.Finalize.Add(&DisableAcceptEncodingGzipMiddleware{}, middleware.Before)
stack.Finalize.Add(&DisableGzipMiddleware{}, middleware.Before)
}

// DisableAcceptEncodingGzipMiddleware provides the middleware that will
// DisableGzipMiddleware provides the middleware that will
// disable the underlying http client automatically enabling for gzip
// decompress content-encoding support.
type DisableAcceptEncodingGzipMiddleware struct{}
type DisableGzipMiddleware struct{}

// ID returns the id for the middleware.
func (*DisableAcceptEncodingGzipMiddleware) ID() string {
return "DynamoDB:DisableAcceptEncodingGzipMiddleware"
func (*DisableGzipMiddleware) ID() string {
return "DisableAcceptEncodingGzipMiddleware"
}

// HandleFinalize implements the FinalizeMiddlware interface.
func (*DisableAcceptEncodingGzipMiddleware) HandleFinalize(
// HandleFinalize implements the FinalizeMiddleware interface.
func (*DisableGzipMiddleware) HandleFinalize(
ctx context.Context, input middleware.FinalizeInput, next middleware.FinalizeHandler,
) (
output middleware.FinalizeOutput, metadata middleware.Metadata, err error,
Expand All @@ -63,16 +63,16 @@ func (*DisableAcceptEncodingGzipMiddleware) HandleFinalize(
return next.HandleFinalize(ctx, input)
}

// AcceptEncodingGzipMiddleware provides a middleware to enable support for
// EnableGzipMiddleware provides a middleware to enable support for
// gzip responses, with manual decompression. This prevents the underlying HTTP
// client from performing the gzip decompression automatically.
type AcceptEncodingGzipMiddleware struct{}
type EnableGzipMiddleware struct{}

// ID returns the id for the middleware.
func (*AcceptEncodingGzipMiddleware) ID() string { return "DynamoDB:AcceptEncodingGzipMiddleware" }
func (*EnableGzipMiddleware) ID() string { return "AcceptEncodingGzipMiddleware" }

// HandleFinalize implements the FinalizeMiddlware interface.
func (*AcceptEncodingGzipMiddleware) HandleFinalize(
func (*EnableGzipMiddleware) HandleFinalize(
ctx context.Context, input middleware.FinalizeInput, next middleware.FinalizeHandler,
) (
output middleware.FinalizeOutput, metadata middleware.Metadata, err error,
Expand All @@ -96,7 +96,7 @@ func (*AcceptEncodingGzipMiddleware) HandleFinalize(
type DecompressGzipMiddleware struct{}

// ID returns the id for the middleware.
func (*DecompressGzipMiddleware) ID() string { return "DynamoDB:DecompressGzipMiddleware" }
func (*DecompressGzipMiddleware) ID() string { return "DecompressGzipMiddleware" }

// HandleDeserialize implements the DeserializeMiddlware interface.
func (*DecompressGzipMiddleware) HandleDeserialize(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package customizations
package acceptencoding

import (
"bytes"
Expand Down Expand Up @@ -42,7 +42,7 @@ func TestAddAcceptEncodingGzip(t *testing.T) {
}

if c.Enable {
id = (*AcceptEncodingGzipMiddleware)(nil).ID()
id = (*EnableGzipMiddleware)(nil).ID()
if m, ok := stack.Finalize.Get(id); !ok || m == nil {
t.Fatalf("expect %s to be present.", id)
}
Expand All @@ -53,7 +53,7 @@ func TestAddAcceptEncodingGzip(t *testing.T) {
}
return
}
id = (*AcceptEncodingGzipMiddleware)(nil).ID()
id = (*EnableGzipMiddleware)(nil).ID()
if m, ok := stack.Finalize.Get(id); ok || m != nil {
t.Fatalf("expect %s not to be present.", id)
}
Expand All @@ -67,7 +67,7 @@ func TestAddAcceptEncodingGzip(t *testing.T) {
}

func TestAcceptEncodingGzipMiddleware(t *testing.T) {
m := &AcceptEncodingGzipMiddleware{}
m := &EnableGzipMiddleware{}

_, _, err := m.HandleFinalize(context.Background(),
middleware.FinalizeInput{
Expand Down Expand Up @@ -196,3 +196,20 @@ func (*stubOpDeserializer) HandleDeserialize(
) {
return next.HandleDeserialize(ctx, input)
}

type wasClosedReadCloser struct {
io.Reader
closed bool
}

func (c *wasClosedReadCloser) WasClosed() bool {
return c.closed
}

func (c *wasClosedReadCloser) Close() error {
c.closed = true
if v, ok := c.Reader.(io.Closer); ok {
return v.Close()
}
return nil
}
23 changes: 23 additions & 0 deletions service/internal/accept-encoding/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Package customizations provides customizations associated with Accept Encoding Header.
Accept encoding gzip
The Go HTTP client automatically supports accept-encoding and content-encoding
gzip by default. This default behavior is not desired by the SDK, and prevents
validating the response body's checksum. To prevent this the SDK must manually
control usage of content-encoding gzip.
To control content-encoding, the SDK must always set the `Accept-Encoding`
header to a value. This prevents the HTTP client from using gzip automatically.
When gzip is enabled on the API client, the SDK's customization will control
decompressing the gzip data in order to not break the checksum validation. When
gzip is disabled, the API client will disable gzip, preventing the HTTP
client's default behavior.
An `EnableAcceptEncodingGzip` option may or may not be present depending on the client using
the below middleware. The option if present can be used to enable auto decompressing
gzip by the SDK.
*/
package acceptencoding

0 comments on commit 6b963de

Please sign in to comment.