Skip to content

Commit f2ba23a

Browse files
committed
chore(codegen): check SigV4 trait for including region
1 parent 9dc7ef5 commit f2ba23a

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddAwsAuthPlugin.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

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

18+
import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isSigV4Service;
1819
import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_CONFIG;
1920
import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE;
2021

@@ -24,7 +25,6 @@
2425
import java.util.Set;
2526
import java.util.function.Consumer;
2627
import software.amazon.smithy.aws.traits.ServiceTrait;
27-
import software.amazon.smithy.aws.traits.auth.SigV4Trait;
2828
import software.amazon.smithy.codegen.core.SymbolProvider;
2929
import software.amazon.smithy.model.Model;
3030
import software.amazon.smithy.model.knowledge.TopDownIndex;
@@ -56,7 +56,7 @@ public void addConfigInterfaceFields(
5656
TypeScriptWriter writer
5757
) {
5858
ServiceShape service = settings.getService(model);
59-
if (!serviceUsesAwsAuth(service)) {
59+
if (!isSigV4Service(service)) {
6060
return;
6161
}
6262
if (!areAllOptionalAuthOperations(model, service)) {
@@ -71,13 +71,13 @@ public List<RuntimeClientPlugin> getClientPlugins() {
7171
return ListUtils.of(
7272
RuntimeClientPlugin.builder()
7373
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth", HAS_CONFIG)
74-
.servicePredicate((m, s) -> serviceUsesAwsAuth(s) && !areAllOptionalAuthOperations(m, s))
74+
.servicePredicate((m, s) -> isSigV4Service(s) && !areAllOptionalAuthOperations(m, s))
7575
.build(),
7676
RuntimeClientPlugin.builder()
7777
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth", HAS_MIDDLEWARE)
7878
// See operationUsesAwsAuth() below for AwsAuth Middleware customizations.
7979
.servicePredicate(
80-
(m, s) -> !testServiceId(s, "STS") && serviceUsesAwsAuth(s) && !hasOptionalAuthOperation(m, s)
80+
(m, s) -> !testServiceId(s, "STS") && isSigV4Service(s) && !hasOptionalAuthOperation(m, s)
8181
).build(),
8282
RuntimeClientPlugin.builder()
8383
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth", HAS_MIDDLEWARE)
@@ -94,7 +94,7 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
9494
LanguageTarget target
9595
) {
9696
ServiceShape service = settings.getService(model);
97-
if (!serviceUsesAwsAuth(service) || areAllOptionalAuthOperations(model, service)) {
97+
if (!isSigV4Service(service) || areAllOptionalAuthOperations(model, service)) {
9898
return Collections.emptyMap();
9999
}
100100
switch (target) {
@@ -124,10 +124,6 @@ private static boolean testServiceId(Shape serviceShape, String expectedId) {
124124
return serviceShape.getTrait(ServiceTrait.class).map(ServiceTrait::getSdkId).orElse("").equals(expectedId);
125125
}
126126

127-
private static boolean serviceUsesAwsAuth(Shape serviceShape) {
128-
return serviceShape.hasTrait(SigV4Trait.class);
129-
}
130-
131127
private static boolean operationUsesAwsAuth(Model model, ServiceShape service, OperationShape operation) {
132128
// STS doesn't need auth for AssumeRoleWithWebIdentity, AssumeRoleWithSAML.
133129
// Remove when optionalAuth model update is published in 0533102932.
@@ -139,7 +135,7 @@ private static boolean operationUsesAwsAuth(Model model, ServiceShape service, O
139135
}
140136

141137
// optionalAuth trait doesn't require authentication.
142-
if (serviceUsesAwsAuth(service) && hasOptionalAuthOperation(model, service)) {
138+
if (isSigV4Service(service) && hasOptionalAuthOperation(model, service)) {
143139
return !operation.hasTrait(OptionalAuthTrait.class);
144140
}
145141
return false;

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddAwsRuntimeConfig.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.amazon.smithy.aws.typescript.codegen;
1717

1818
import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService;
19+
import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isSigV4Service;
1920

2021
import java.util.Collections;
2122
import java.util.HashMap;
@@ -33,6 +34,9 @@
3334
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
3435
import software.amazon.smithy.utils.MapUtils;
3536

37+
// TODO: This javadoc is specific to needs of AWS client. However it has elements that would be needed by non-AWS
38+
// clients too, like logger, region for SigV4. We should refactor these into different Integration or rename this
39+
// class to be generic.
3640
/**
3741
* AWS clients need to know the service name for collecting metrics, the
3842
* region name used to resolve endpoints, the max attempt to retry a request
@@ -87,7 +91,9 @@ public void addConfigInterfaceFields(
8791
if (isAwsService(settings, model)) {
8892
writer.writeDocs("Unique service identifier.\n@internal")
8993
.write("serviceId?: string;\n");
90-
writer.writeDocs("The AWS region to which this client will send requests")
94+
}
95+
if (isSigV4Service(settings, model)) {
96+
writer.writeDocs("The AWS region to which this client will send requests or use as signingRegion")
9197
.write("region?: string | __Provider<string>;\n");
9298
}
9399
writer.writeDocs("Value for how many times a request will be made at most in case of retry.")
@@ -128,7 +134,7 @@ private Map<String, Consumer<TypeScriptWriter>> getDefaultConfig(
128134
Model model
129135
) {
130136
Map<String, Consumer<TypeScriptWriter>> defaultConfigs = new HashMap();
131-
boolean isAwsService = isAwsService(settings, model);
137+
boolean isSigV4Service = isSigV4Service(settings, model);
132138
switch (target) {
133139
case SHARED:
134140
return MapUtils.of(
@@ -138,7 +144,7 @@ private Map<String, Consumer<TypeScriptWriter>> getDefaultConfig(
138144
}
139145
);
140146
case BROWSER:
141-
if (isAwsService) {
147+
if (isSigV4Service) {
142148
defaultConfigs.put("region", writer -> {
143149
writer.addDependency(TypeScriptDependency.INVALID_DEPENDENCY);
144150
writer.addImport("invalidProvider", "invalidProvider",
@@ -154,7 +160,8 @@ private Map<String, Consumer<TypeScriptWriter>> getDefaultConfig(
154160
});
155161
return defaultConfigs;
156162
case NODE:
157-
if (isAwsService) {
163+
if (isSigV4Service) {
164+
// TODO: For non-AWS service, figure out how the region should be configured.
158165
defaultConfigs.put("region", writer -> {
159166
writer.addDependency(AwsDependency.NODE_CONFIG_PROVIDER);
160167
writer.addImport("loadConfig", "loadNodeConfig",

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsTraitsUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.amazon.smithy.aws.typescript.codegen;
1717

1818
import software.amazon.smithy.aws.traits.ServiceTrait;
19+
import software.amazon.smithy.aws.traits.auth.SigV4Trait;
1920
import software.amazon.smithy.model.Model;
2021
import software.amazon.smithy.model.shapes.ServiceShape;
2122
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
@@ -34,4 +35,12 @@ static boolean isAwsService(TypeScriptSettings settings, Model model) {
3435
static boolean isAwsService(ServiceShape serviceShape) {
3536
return serviceShape.hasTrait(ServiceTrait.class);
3637
}
38+
39+
static boolean isSigV4Service(TypeScriptSettings settings, Model model) {
40+
return isSigV4Service(settings.getService(model));
41+
}
42+
43+
static boolean isSigV4Service(ServiceShape serviceShape) {
44+
return serviceShape.hasTrait(SigV4Trait.class);
45+
}
3746
}

0 commit comments

Comments
 (0)