Skip to content

Commit

Permalink
chore(codegen): add sigv4a trait detection
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Mar 4, 2024
1 parent af5215a commit 520aa3d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2022 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 java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.ServiceIndex;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.Trait;
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.SmithyInternalApi;

/**
* Detects when to add sigv4a signer.
*/
@SmithyInternalApi
public final class AddSigv4aPlugin implements TypeScriptIntegration {
public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
LanguageTarget target
) {
boolean useSigv4aSigner = false;

ServiceIndex serviceIndex = ServiceIndex.of(model);
Set<ServiceShape> services = model.getServiceShapes();
for (ServiceShape service of services) {
Map<ShapeId, Trait> authSchemes = serviceIndex.getAuthSchemes(service);
useSigv4aSigner = useSigv4aSigner || authSchemes.values().stream()
.anyMatch(trait -> trait.toString().equals("aws.auth#sigv4a"));

TopDownIndex topDownIndex = TopDownIndex.of(service.getModel());
for (OperationShape operationShape : topDownIndex.getContainedOperations(service)) {
useSigv4aSigner = useSigv4aSigner || operationShape.getAllTraits()
.values().stream()
.anyMatch(trait -> trait.toString().equals("aws.auth#sigv4a"));
}
}
if (!useSigv4aSigner) {
return Collections.emptyMap();
}
switch (target) {
case SHARED:
return MapUtils.of("signerConstructor", writer -> {
writer.addDependency(AwsDependency.SIGNATURE_V4_MULTIREGION)
.addImport("SignatureV4MultiRegion", "SignatureV4MultiRegion",
AwsDependency.SIGNATURE_V4_MULTIREGION)
.write("SignatureV4MultiRegion");
});
default:
return Collections.emptyMap();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ software.amazon.smithy.aws.typescript.codegen.AddDocumentClientPlugin
software.amazon.smithy.aws.typescript.codegen.AddEndpointDiscoveryPlugin
software.amazon.smithy.aws.typescript.codegen.AddHttpChecksumDependency
software.amazon.smithy.aws.typescript.codegen.AddEventBridgePlugin
software.amazon.smithy.aws.typescript.codegen.AddSigv4aPlugin
software.amazon.smithy.aws.typescript.codegen.AddCloudFrontKeyValueStorePlugin
software.amazon.smithy.aws.typescript.codegen.auth.http.integration.AwsSdkCustomizeHttpBearerTokenAuth
software.amazon.smithy.aws.typescript.codegen.auth.http.integration.SupportSigV4Auth
Expand Down

0 comments on commit 520aa3d

Please sign in to comment.