Skip to content

Commit 520aa3d

Browse files
committed
chore(codegen): add sigv4a trait detection
1 parent af5215a commit 520aa3d

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.smithy.aws.typescript.codegen;
17+
18+
import java.util.Map;
19+
import java.util.Set;
20+
import java.util.function.Consumer;
21+
import software.amazon.smithy.codegen.core.SymbolProvider;
22+
import software.amazon.smithy.model.Model;
23+
import software.amazon.smithy.model.knowledge.ServiceIndex;
24+
import software.amazon.smithy.model.shapes.ServiceShape;
25+
import software.amazon.smithy.model.shapes.ShapeId;
26+
import software.amazon.smithy.model.traits.Trait;
27+
import software.amazon.smithy.typescript.codegen.LanguageTarget;
28+
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
29+
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
30+
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
31+
import software.amazon.smithy.utils.SmithyInternalApi;
32+
33+
/**
34+
* Detects when to add sigv4a signer.
35+
*/
36+
@SmithyInternalApi
37+
public final class AddSigv4aPlugin implements TypeScriptIntegration {
38+
public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
39+
TypeScriptSettings settings,
40+
Model model,
41+
SymbolProvider symbolProvider,
42+
LanguageTarget target
43+
) {
44+
boolean useSigv4aSigner = false;
45+
46+
ServiceIndex serviceIndex = ServiceIndex.of(model);
47+
Set<ServiceShape> services = model.getServiceShapes();
48+
for (ServiceShape service of services) {
49+
Map<ShapeId, Trait> authSchemes = serviceIndex.getAuthSchemes(service);
50+
useSigv4aSigner = useSigv4aSigner || authSchemes.values().stream()
51+
.anyMatch(trait -> trait.toString().equals("aws.auth#sigv4a"));
52+
53+
TopDownIndex topDownIndex = TopDownIndex.of(service.getModel());
54+
for (OperationShape operationShape : topDownIndex.getContainedOperations(service)) {
55+
useSigv4aSigner = useSigv4aSigner || operationShape.getAllTraits()
56+
.values().stream()
57+
.anyMatch(trait -> trait.toString().equals("aws.auth#sigv4a"));
58+
}
59+
}
60+
61+
if (!useSigv4aSigner) {
62+
return Collections.emptyMap();
63+
}
64+
65+
switch (target) {
66+
case SHARED:
67+
return MapUtils.of("signerConstructor", writer -> {
68+
writer.addDependency(AwsDependency.SIGNATURE_V4_MULTIREGION)
69+
.addImport("SignatureV4MultiRegion", "SignatureV4MultiRegion",
70+
AwsDependency.SIGNATURE_V4_MULTIREGION)
71+
.write("SignatureV4MultiRegion");
72+
});
73+
default:
74+
return Collections.emptyMap();
75+
}
76+
}
77+
}

codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ software.amazon.smithy.aws.typescript.codegen.AddDocumentClientPlugin
2424
software.amazon.smithy.aws.typescript.codegen.AddEndpointDiscoveryPlugin
2525
software.amazon.smithy.aws.typescript.codegen.AddHttpChecksumDependency
2626
software.amazon.smithy.aws.typescript.codegen.AddEventBridgePlugin
27+
software.amazon.smithy.aws.typescript.codegen.AddSigv4aPlugin
2728
software.amazon.smithy.aws.typescript.codegen.AddCloudFrontKeyValueStorePlugin
2829
software.amazon.smithy.aws.typescript.codegen.auth.http.integration.AwsSdkCustomizeHttpBearerTokenAuth
2930
software.amazon.smithy.aws.typescript.codegen.auth.http.integration.SupportSigV4Auth

0 commit comments

Comments
 (0)