Skip to content

Commit 3b3ae92

Browse files
committed
feat(codegen): support SigV4 for non AWS services
Write `signingName` from the SigV4 trait for non AWS service. `region` is used for the signingRegion. A separate SigV4Auth is used for SigV4 logic for non AWS services.
1 parent c6dc029 commit 3b3ae92

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

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

Lines changed: 52 additions & 7 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.isAwsService;
1819
import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isSigV4Service;
1920
import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_CONFIG;
2021
import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE;
@@ -26,6 +27,7 @@
2627
import java.util.function.BiConsumer;
2728
import java.util.function.Consumer;
2829
import software.amazon.smithy.aws.traits.ServiceTrait;
30+
import software.amazon.smithy.aws.traits.auth.SigV4Trait;
2931
import software.amazon.smithy.codegen.core.SymbolProvider;
3032
import software.amazon.smithy.model.Model;
3133
import software.amazon.smithy.model.knowledge.TopDownIndex;
@@ -48,7 +50,6 @@
4850
/**
4951
* Configure clients with AWS auth configurations and plugin.
5052
*/
51-
// TODO: Think about AWS Auth supported for only some operations and not all, when not AWS service, with say @auth([])
5253
@SmithyInternalApi
5354
public final class AddAwsAuthPlugin implements TypeScriptIntegration {
5455
static final String STS_CLIENT_PREFIX = "sts-client-";
@@ -67,21 +68,37 @@ public void addConfigInterfaceFields(
6768
if (!isSigV4Service(service)) {
6869
return;
6970
}
71+
72+
if (!isAwsService(service)) {
73+
writer.writeDocs("The service name to use as the signing service for AWS Auth\n@internal")
74+
.write("signingName?: string;\n");
75+
}
76+
7077
if (!areAllOptionalAuthOperations(model, service)) {
7178
writer.addImport("Credentials", "__Credentials", TypeScriptDependency.AWS_SDK_TYPES.packageName);
7279
writer.writeDocs("Default credentials provider; Not available in browser runtime.")
7380
.write("credentialDefaultProvider?: (input: any) => __Provider<__Credentials>;\n");
7481
}
7582
}
7683

84+
// Only one of AwsAuth or SigV4Auth should be used
85+
// AwsAuth - for AWS services
86+
// SigV4Auth - for non AWS services
7787
@Override
7888
public List<RuntimeClientPlugin> getClientPlugins() {
7989
return ListUtils.of(
8090
RuntimeClientPlugin.builder()
8191
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth", HAS_CONFIG)
8292
.servicePredicate((m, s) -> isSigV4Service(s)
83-
&& !areAllOptionalAuthOperations(m, s)
84-
&& !testServiceId(s, "STS"))
93+
&& isAwsService(s)
94+
&& !testServiceId(s, "STS")
95+
&& !areAllOptionalAuthOperations(m, s))
96+
.build(),
97+
RuntimeClientPlugin.builder()
98+
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "SigV4Auth", HAS_CONFIG)
99+
.servicePredicate((m, s) -> isSigV4Service(s)
100+
&& !isAwsService(s)
101+
&& !areAllOptionalAuthOperations(m, s))
85102
.build(),
86103
RuntimeClientPlugin.builder()
87104
.withConventions(AwsDependency.STS_MIDDLEWARE.dependency,
@@ -92,13 +109,31 @@ public List<RuntimeClientPlugin> getClientPlugins() {
92109
RuntimeClientPlugin.builder()
93110
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth", HAS_MIDDLEWARE)
94111
// See operationUsesAwsAuth() below for AwsAuth Middleware customizations.
95-
.servicePredicate(
96-
(m, s) -> !testServiceId(s, "STS") && isSigV4Service(s) && !hasOptionalAuthOperation(m, s)
112+
.servicePredicate((m, s) -> isSigV4Service(s)
113+
&& isAwsService(s)
114+
&& !testServiceId(s, "STS")
115+
&& !hasOptionalAuthOperation(m, s)
116+
).build(),
117+
RuntimeClientPlugin.builder()
118+
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "SigV4Auth", HAS_MIDDLEWARE)
119+
// See operationUsesAwsAuth() below for AwsAuth Middleware customizations.
120+
.servicePredicate((m, s) -> isSigV4Service(s)
121+
&& !isAwsService(s)
122+
&& !hasOptionalAuthOperation(m, s)
97123
).build(),
98124
RuntimeClientPlugin.builder()
99125
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth", HAS_MIDDLEWARE)
100-
.operationPredicate(AddAwsAuthPlugin::operationUsesAwsAuth)
126+
.operationPredicate((m, s, o) -> isSigV4Service(s)
127+
&& isAwsService(s)
128+
&& operationUsesAwsAuth(m, s, o))
129+
.build(),
130+
RuntimeClientPlugin.builder()
131+
.withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "SigV4Auth", HAS_MIDDLEWARE)
132+
.operationPredicate((m, s, o) -> isSigV4Service(s)
133+
&& !isAwsService(s)
134+
&& operationUsesAwsAuth(m, s, o))
101135
.build()
136+
102137
);
103138
}
104139

@@ -114,6 +149,16 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
114149
return Collections.emptyMap();
115150
}
116151
switch (target) {
152+
case SHARED:
153+
if (isAwsService(service)) {
154+
return Collections.emptyMap();
155+
}
156+
String signingService = service.getTrait(SigV4Trait.class).get().getName();
157+
return MapUtils.of(
158+
"signingName", writer -> {
159+
writer.write("signingName: $S,", signingService);
160+
}
161+
);
117162
case BROWSER:
118163
return MapUtils.of(
119164
"credentialDefaultProvider", writer -> {
@@ -208,7 +253,7 @@ private static boolean operationUsesAwsAuth(Model model, ServiceShape service, O
208253
}
209254

210255
// optionalAuth trait doesn't require authentication.
211-
if (isSigV4Service(service) && hasOptionalAuthOperation(model, service)) {
256+
if (hasOptionalAuthOperation(model, service)) {
212257
return !operation.hasTrait(OptionalAuthTrait.class);
213258
}
214259
return false;

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,13 @@ public void addConfigInterfaceFields(
9595
.write("serviceId?: string;\n");
9696
}
9797
if (isSigV4Service(settings, model)) {
98-
writer.writeDocs("The AWS region to which this client will send requests or use as signingRegion")
99-
.write("region?: string | __Provider<string>;\n");
98+
if (isAwsService(settings, model)) {
99+
writer.writeDocs("The AWS region to which this client will send requests")
100+
.write("region?: string | __Provider<string>;\n");
101+
} else {
102+
writer.writeDocs("The AWS region to use as signing region for AWS Auth")
103+
.write("region?: string | __Provider<string>;\n");
104+
}
100105
}
101106
writer.writeDocs("Value for how many times a request will be made at most in case of retry.")
102107
.write("maxAttempts?: number | __Provider<number>;\n");
@@ -163,7 +168,6 @@ private Map<String, Consumer<TypeScriptWriter>> getDefaultConfig(
163168
return defaultConfigs;
164169
case NODE:
165170
if (isSigV4Service) {
166-
// TODO: For non-AWS service, figure out how the region should be configured.
167171
defaultConfigs.put("region", writer -> {
168172
writer.addDependency(AwsDependency.NODE_CONFIG_PROVIDER);
169173
writer.addImport("loadConfig", "loadNodeConfig",

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

Lines changed: 2 additions & 1 deletion
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
import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_CONFIG;
2021
import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE;
2122

@@ -47,7 +48,7 @@ public List<RuntimeClientPlugin> getClientPlugins() {
4748
return ListUtils.of(
4849
RuntimeClientPlugin.builder()
4950
.withConventions(TypeScriptDependency.CONFIG_RESOLVER.dependency, "Region", HAS_CONFIG)
50-
.servicePredicate((m, s) -> isAwsService(s))
51+
.servicePredicate((m, s) -> isSigV4Service(s))
5152
.build(),
5253
// Only one of Endpoints or CustomEndpoints should be used
5354
RuntimeClientPlugin.builder()

0 commit comments

Comments
 (0)