Skip to content

Commit

Permalink
feat: Migrate and apply ApplyMd5BodyChecksumMiddleware (#493)
Browse files Browse the repository at this point in the history
* feat: add md5 body checksum middleware

* feat: add ApplyMd5BodyChecksum plugin
  • Loading branch information
Chase Coalwell authored and trivikr committed Jan 3, 2020
1 parent ad7f978 commit 87b5549
Show file tree
Hide file tree
Showing 22 changed files with 421 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public class AddBuiltinPlugins implements TypeScriptIntegration {

private static final Set<String> SSEC_OPERATIONS = SetUtils.of("SSECustomerKey", "CopySourceSSECustomerKey");

private static final Set<String> S3_MD5_OPERATIONS = SetUtils.of(
"DeleteObjects",
"PutBucketCors",
"PutBucketLifecycle",
"PutBucketLifecycleConfiguration",
"PutBucketPolicy",
"PutBucketTagging",
"PutBucketReplication"
);

@Override
public List<RuntimeClientPlugin> getClientPlugins() {
// Note that order is significant because configurations might
Expand Down Expand Up @@ -93,6 +103,12 @@ public List<RuntimeClientPlugin> getClientPlugins() {
HAS_MIDDLEWARE)
.servicePredicate((m, s) -> testServiceId(s, "S3"))
.operationPredicate((m, s, o) -> o.getId().getName().equals("CreateBucket"))
.build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.BODY_CHECKSUM.dependency, "ApplyMd5BodyChecksum",
HAS_MIDDLEWARE)
.servicePredicate((m, s) -> testServiceId(s, "S3"))
.operationPredicate((m, s, o) -> S3_MD5_OPERATIONS.contains(o.getId().getName()))
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.aws.typescript.codegen;

import software.amazon.smithy.aws.traits.ServiceTrait;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.typescript.codegen.LanguageTarget;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
import software.amazon.smithy.utils.SetUtils;

import java.util.Set;

/**
* Adds Md5Hash if needed.
*/
public class AddMd5HashDependency implements TypeScriptIntegration {
private static final Set<String> SERVICE_IDS = SetUtils.of("S3", "SQS");

@Override
public void addConfigInterfaceFields(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer
) {
if (!needsMd5Dep(settings.getService(model))) {
return;
}

writer.addImport("HashConstructor", "__HashConstructor", "@aws-sdk/types");
writer.writeDocs("A constructor for a class implementing the @aws-sdk/types.Hash interface \n"
+ "that computes MD5 hashes");
writer.write("md5?: __HashConstructor;\n");
}

@Override
public void addRuntimeConfigValues(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer,
LanguageTarget target
) {
if (!needsMd5Dep(settings.getService(model))) {
return;
}

switch (target) {
case NODE:
writer.addImport("HashConstructor", "__HashConstructor", "@aws-sdk/types");
writer.write("md5: Hash.bind(null, \"md5\"),");
break;
case BROWSER:
writer.addDependency(AwsDependency.MD5_BROWSER);
writer.addImport("Md5", "Md5", AwsDependency.MD5_BROWSER.packageName);
writer.write("md5: Md5,");
break;
default:
// do nothing
}
}

private static boolean needsMd5Dep(ServiceShape service) {
String serviceId = service.getTrait(ServiceTrait.class).map(ServiceTrait::getSdkId).orElse("");
return SERVICE_IDS.contains(serviceId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.aws.typescript.codegen;

import software.amazon.smithy.aws.traits.ServiceTrait;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.typescript.codegen.LanguageTarget;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;

/**
* Adds StreamHasher if needed.
*/
public class AddStreamHasherDependency implements TypeScriptIntegration {

@Override
public void addConfigInterfaceFields(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer
) {
if (!needsStreamHasher(settings.getService(model))) {
return;
}

writer.addImport("Readable", "Readable", "stream");
writer.addImport("StreamHasher", "__StreamHasher", "@aws-sdk/types");
writer.writeDocs("A function that, given a hash constructor and a stream, calculates the \n"
+ "hash of the streamed value");
writer.write("streamHasher?: __StreamHasher<Readable|Blob>;\n");
}

@Override
public void addRuntimeConfigValues(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer,
LanguageTarget target
) {
if (!needsStreamHasher(settings.getService(model))) {
return;
}

switch (target) {
case NODE:
writer.addDependency(AwsDependency.STREAM_HASHER_NODE);
writer.addImport("calculateSha256", "streamHasher", AwsDependency.STREAM_HASHER_NODE.packageName);
writer.write("streamHasher,");
break;
case BROWSER:
writer.addDependency(AwsDependency.STREAM_HASHER_BROWSER);
writer.addImport("calculateSha256", "streamHasher", AwsDependency.STREAM_HASHER_BROWSER.packageName);
writer.write("streamHasher,");
break;
default:
// do nothing
}
}

private static boolean needsStreamHasher(ServiceShape service) {
String serviceId = service.getTrait(ServiceTrait.class).map(ServiceTrait::getSdkId).orElse("");
if (serviceId.equals("S3")) {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public enum AwsDependency implements SymbolDependencyContainer {
ADD_EXPECT_CONTINUE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-expect-continue", "^0.1.0-preview.5"),
ADD_GLACIER_API_VERSION(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-glacier", "^0.1.0-preview.7"),
SSEC_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-ssec", "^0.1.0-preview.5"),
LOCATION_CONSTRAINT(NORMAL_DEPENDENCY, "@aws-sdk/middleware-location-constraint", "^0.1.0-preview.5");
LOCATION_CONSTRAINT(NORMAL_DEPENDENCY, "@aws-sdk/middleware-location-constraint", "^0.1.0-preview.5"),
MD5_BROWSER(NORMAL_DEPENDENCY, "@aws-sdk/md5-js", "^0.1.0-preview.8"),
STREAM_HASHER_NODE(NORMAL_DEPENDENCY, "@aws-sdk/hash-stream-node", "^0.1.0-preview.4"),
STREAM_HASHER_BROWSER(NORMAL_DEPENDENCY, "@aws-sdk/hash-blob-browser", "^0.1.0-preview.4"),
BODY_CHECKSUM(NORMAL_DEPENDENCY, "@aws-sdk/middleware-apply-body-checksum", "^0.1.0-preview.5");

public final String packageName;
public final String version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ software.amazon.smithy.aws.typescript.codegen.AddAwsRuntimeConfig
software.amazon.smithy.aws.typescript.codegen.AddBuiltinPlugins
software.amazon.smithy.aws.typescript.codegen.AddProtocols
software.amazon.smithy.aws.typescript.codegen.AwsServiceIdIntegration
software.amazon.smithy.aws.typescript.codegen.AddMd5HashDependency
software.amazon.smithy.aws.typescript.codegen.AddStreamHasherDependency
7 changes: 0 additions & 7 deletions packages/apply-body-checksum-middleware/README.md

This file was deleted.

Loading

0 comments on commit 87b5549

Please sign in to comment.