|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.aws.typescript.codegen.auth.http.integration; |
| 7 | + |
| 8 | +import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService; |
| 9 | +import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isSigV4Service; |
| 10 | + |
| 11 | +import java.util.logging.Logger; |
| 12 | +import software.amazon.smithy.aws.traits.ServiceTrait; |
| 13 | +import software.amazon.smithy.aws.traits.auth.SigV4Trait; |
| 14 | +import software.amazon.smithy.aws.typescript.codegen.AddAwsAuthPlugin; |
| 15 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 16 | +import software.amazon.smithy.model.Model; |
| 17 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 18 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 19 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 20 | +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; |
| 21 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 22 | + |
| 23 | +/** |
| 24 | + * Configure clients with Default AWS Signing Name. |
| 25 | + * |
| 26 | + * This is the experimental behavior for `experimentalIdentityAndAuth`. |
| 27 | + */ |
| 28 | +@SmithyInternalApi |
| 29 | +public final class AddAwsDefaultSigningName implements TypeScriptIntegration { |
| 30 | + private static final Logger LOGGER = Logger.getLogger(AddAwsAuthPlugin.class.getName()); |
| 31 | + |
| 32 | + /** |
| 33 | + * Integration should only be used if `experimentalIdentityAndAuth` flag is true. |
| 34 | + */ |
| 35 | + @Override |
| 36 | + public boolean matchesSettings(TypeScriptSettings settings) { |
| 37 | + return settings.getExperimentalIdentityAndAuth(); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void addConfigInterfaceFields( |
| 42 | + TypeScriptSettings settings, |
| 43 | + Model model, |
| 44 | + SymbolProvider symbolProvider, |
| 45 | + TypeScriptWriter writer |
| 46 | + ) { |
| 47 | + ServiceShape service = settings.getService(model); |
| 48 | + // Do nothing if service doesn't use sigv4 and is not an AWS service |
| 49 | + if (!isSigV4Service(service) && !isAwsService(service)) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + // Write config field if service uses sigv4 and is not an AWS service |
| 54 | + if (isSigV4Service(service) && !isAwsService(service)) { |
| 55 | + writer |
| 56 | + .writeDocs("The service name to use as the signing service for AWS Auth\n@internal") |
| 57 | + .write("signingName?: string;\n"); |
| 58 | + } |
| 59 | + |
| 60 | + // Set default name setting for service based on: SigV4 first, then the AWS Service trait |
| 61 | + try { |
| 62 | + settings.setDefaultSigningName( |
| 63 | + service.getTrait(SigV4Trait.class) |
| 64 | + .map(SigV4Trait::getName) |
| 65 | + .orElseGet(() -> service.expectTrait(ServiceTrait.class).getArnNamespace()) |
| 66 | + ); |
| 67 | + } catch (Exception e) { |
| 68 | + LOGGER.warning("Unable to set service default signing name. A SigV4 or Service trait is needed."); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments