|
| 1 | +/* |
| 2 | + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.smithy.aws.typescript.codegen; |
| 17 | + |
| 18 | +import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_CONFIG; |
| 19 | +import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE; |
| 20 | + |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.function.Consumer; |
| 26 | +import software.amazon.smithy.aws.traits.ServiceTrait; |
| 27 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 28 | +import software.amazon.smithy.model.Model; |
| 29 | +import software.amazon.smithy.model.knowledge.TopDownIndex; |
| 30 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 31 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 32 | +import software.amazon.smithy.model.shapes.Shape; |
| 33 | +import software.amazon.smithy.model.traits.OptionalAuthTrait; |
| 34 | +import software.amazon.smithy.typescript.codegen.LanguageTarget; |
| 35 | +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; |
| 36 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 37 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 38 | +import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin; |
| 39 | +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; |
| 40 | +import software.amazon.smithy.utils.ListUtils; |
| 41 | +import software.amazon.smithy.utils.MapUtils; |
| 42 | +import software.amazon.smithy.utils.SetUtils; |
| 43 | + |
| 44 | +/** |
| 45 | + * Configure clients with AWS auth configurations and plugin. |
| 46 | + */ |
| 47 | +public final class AddAwsAuthPlugin implements TypeScriptIntegration { |
| 48 | + |
| 49 | + @Override |
| 50 | + public byte getOrder() { |
| 51 | + // Auth plugin should resolve after region, set it to after AddBuiltinPlugins. |
| 52 | + return 10; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void addConfigInterfaceFields( |
| 57 | + TypeScriptSettings settings, |
| 58 | + Model model, |
| 59 | + SymbolProvider symbolProvider, |
| 60 | + TypeScriptWriter writer |
| 61 | + ) { |
| 62 | + ServiceShape service = settings.getService(model); |
| 63 | + if (!isAllOptionalAuthOperation(model, service)) { |
| 64 | + writer.addImport("Credentials", "__Credentials", TypeScriptDependency.AWS_SDK_TYPES.packageName); |
| 65 | + writer.writeDocs("Default credentials provider; Not available in browser runtime") |
| 66 | + .write("credentialDefaultProvider?: (input: any) => __Provider<__Credentials>;\n"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public List<RuntimeClientPlugin> getClientPlugins() { |
| 72 | + return ListUtils.of( |
| 73 | + RuntimeClientPlugin.builder() |
| 74 | + .withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth", HAS_CONFIG) |
| 75 | + .servicePredicate((m, s) -> !isAllOptionalAuthOperation(m, s)) |
| 76 | + .build(), |
| 77 | + RuntimeClientPlugin.builder() |
| 78 | + .withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth", HAS_MIDDLEWARE) |
| 79 | + // See operationUsesAwsAuth() below for AwsAuth Middleware customizations. |
| 80 | + .servicePredicate( |
| 81 | + (m, s) -> !testServiceId(s, "STS") && !hasOptionalAuthOperation(m, s) |
| 82 | + ).build(), |
| 83 | + RuntimeClientPlugin.builder() |
| 84 | + .withConventions(AwsDependency.MIDDLEWARE_SIGNING.dependency, "AwsAuth", HAS_MIDDLEWARE) |
| 85 | + .operationPredicate(AddAwsAuthPlugin::operationUsesAwsAuth) |
| 86 | + .build() |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters( |
| 92 | + TypeScriptSettings settings, |
| 93 | + Model model, |
| 94 | + SymbolProvider symbolProvider, |
| 95 | + LanguageTarget target |
| 96 | + ) { |
| 97 | + ServiceShape service = settings.getService(model); |
| 98 | + if (isAllOptionalAuthOperation(model, service)) { |
| 99 | + return Collections.emptyMap(); |
| 100 | + } |
| 101 | + switch (target) { |
| 102 | + case BROWSER: |
| 103 | + return MapUtils.of( |
| 104 | + "credentialDefaultProvider", writer -> { |
| 105 | + writer.write( |
| 106 | + "credentialDefaultProvider: (_: unknown) => () => Promise.reject(new Error(" |
| 107 | + + "\"Credential is missing\")),"); |
| 108 | + } |
| 109 | + ); |
| 110 | + case NODE: |
| 111 | + return MapUtils.of( |
| 112 | + "credentialDefaultProvider", writer -> { |
| 113 | + writer.addDependency(AwsDependency.CREDENTIAL_PROVIDER_NODE); |
| 114 | + writer.addImport("defaultProvider", "credentialDefaultProvider", |
| 115 | + AwsDependency.CREDENTIAL_PROVIDER_NODE.packageName); |
| 116 | + writer.write("credentialDefaultProvider,"); |
| 117 | + } |
| 118 | + ); |
| 119 | + default: |
| 120 | + return Collections.emptyMap(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static boolean testServiceId(Shape serviceShape, String expectedId) { |
| 125 | + return serviceShape.getTrait(ServiceTrait.class).map(ServiceTrait::getSdkId).orElse("").equals(expectedId); |
| 126 | + } |
| 127 | + |
| 128 | + private static boolean operationUsesAwsAuth(Model model, ServiceShape service, OperationShape operation) { |
| 129 | + // STS doesn't need auth for AssumeRoleWithWebIdentity, AssumeRoleWithSAML. |
| 130 | + // Remove when optionalAuth model update is published in 0533102932. |
| 131 | + if (testServiceId(service, "STS")) { |
| 132 | + Boolean isUnsignedCommand = SetUtils |
| 133 | + .of("AssumeRoleWithWebIdentity", "AssumeRoleWithSAML") |
| 134 | + .contains(operation.getId().getName()); |
| 135 | + return !isUnsignedCommand; |
| 136 | + } |
| 137 | + |
| 138 | + // optionalAuth trait doesn't require authentication. |
| 139 | + if (hasOptionalAuthOperation(model, service)) { |
| 140 | + return !operation.getTrait(OptionalAuthTrait.class).isPresent(); |
| 141 | + } |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + private static boolean hasOptionalAuthOperation(Model model, ServiceShape service) { |
| 146 | + TopDownIndex topDownIndex = TopDownIndex.of(model); |
| 147 | + Set<OperationShape> operations = topDownIndex.getContainedOperations(service); |
| 148 | + for (OperationShape operation : operations) { |
| 149 | + if (operation.getTrait(OptionalAuthTrait.class).isPresent()) { |
| 150 | + return true; |
| 151 | + } |
| 152 | + } |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + private static boolean isAllOptionalAuthOperation(Model model, ServiceShape service) { |
| 157 | + TopDownIndex topDownIndex = TopDownIndex.of(model); |
| 158 | + Set<OperationShape> operations = topDownIndex.getContainedOperations(service); |
| 159 | + for (OperationShape operation : operations) { |
| 160 | + if (!operation.getTrait(OptionalAuthTrait.class).isPresent()) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + } |
| 164 | + return true; |
| 165 | + } |
| 166 | +} |
0 commit comments