Skip to content

Commit

Permalink
chore(codegen): add sigv4a trait detection (#5850)
Browse files Browse the repository at this point in the history
* chore(codegen): add sigv4a trait detection

* chore: no need to check operations for sigv4a

* chore: import ordering

* chore: add id&auth settings matcher to sigv4a plugin
  • Loading branch information
kuhe committed Mar 7, 2024
1 parent 304ca3e commit 73399ff
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

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

import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;
import software.amazon.smithy.aws.traits.auth.SigV4ATrait;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.ServiceIndex;
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.MapUtils;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Detects when to add sigv4a signer.
*/
@SmithyInternalApi
public final class AddSigv4aPlugin implements TypeScriptIntegration {

@Override
public boolean matchesSettings(TypeScriptSettings settings) {
return !settings.getExperimentalIdentityAndAuth();
}

public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
LanguageTarget target
) {
final boolean addSigv4aSignerToConfig = ServiceIndex.of(model)
.getEffectiveAuthSchemes(settings.getService(), ServiceIndex.AuthSchemeMode.NO_AUTH_AWARE)
.containsKey(SigV4ATrait.ID);

// the sigv4a trait also appears on operations, but we will not check
// them individually because it must appear on the service as well in that case.

if (!addSigv4aSignerToConfig) {
return Collections.emptyMap();
}

// TODO: add to shared config when sigv4a implementation is available in browser.
switch (target) {
case NODE:
return MapUtils.of("signerConstructor", writer -> {
writer
.addDependency(AwsDependency.SIGNATURE_V4_MULTIREGION)
.addImport("SignatureV4MultiRegion", null, 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 73399ff

Please sign in to comment.