|
4 | 4 | */ |
5 | 5 | package software.amazon.smithy.python.aws.codegen; |
6 | 6 |
|
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | +import software.amazon.smithy.aws.traits.auth.SigV4Trait; |
| 10 | +import software.amazon.smithy.codegen.core.Symbol; |
| 11 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 12 | +import software.amazon.smithy.model.traits.HttpApiKeyAuthTrait; |
| 13 | +import software.amazon.smithy.python.codegen.ApplicationProtocol; |
| 14 | +import software.amazon.smithy.python.codegen.CodegenUtils; |
| 15 | +import software.amazon.smithy.python.codegen.ConfigProperty; |
| 16 | +import software.amazon.smithy.python.codegen.DerivedProperty; |
| 17 | +import software.amazon.smithy.python.codegen.GenerationContext; |
| 18 | +import software.amazon.smithy.python.codegen.SmithyPythonDependency; |
| 19 | +import software.amazon.smithy.python.codegen.integrations.AuthScheme; |
7 | 20 | import software.amazon.smithy.python.codegen.integrations.PythonIntegration; |
| 21 | +import software.amazon.smithy.python.codegen.integrations.RuntimeClientPlugin; |
8 | 22 | import software.amazon.smithy.utils.SmithyInternalApi; |
9 | 23 |
|
10 | 24 | /** |
11 | 25 | * Adds support for AWS auth traits. |
12 | 26 | */ |
13 | 27 | @SmithyInternalApi |
14 | | -public class AwsAuthIntegration implements PythonIntegration {} |
| 28 | +public class AwsAuthIntegration implements PythonIntegration { |
| 29 | + private static final String SIGV4_OPTION_GENERATOR_NAME = "_generate_sigv4_option"; |
| 30 | + |
| 31 | + @Override |
| 32 | + public List<RuntimeClientPlugin> getClientPlugins(GenerationContext context) { |
| 33 | + var regionConfig = ConfigProperty.builder() |
| 34 | + .name("region") |
| 35 | + .type(Symbol.builder().name("str").build()) |
| 36 | + .documentation(" The AWS region to connect to. The configured region is used to " |
| 37 | + + "determine the service endpoint.") |
| 38 | + .build(); |
| 39 | + |
| 40 | + return List.of( |
| 41 | + RuntimeClientPlugin.builder() |
| 42 | + .servicePredicate((model, service) -> service.hasTrait(SigV4Trait.class)) |
| 43 | + .addConfigProperty(ConfigProperty.builder() |
| 44 | + // TODO: Naming of this config RE: backwards compatability/migation considerations |
| 45 | + .name("aws_credentials_identity_resolver") |
| 46 | + .documentation("Resolves AWS Credentials. Required for operations that use Sigv4 Auth.") |
| 47 | + .type(Symbol.builder() |
| 48 | + .name("IdentityResolver[AWSCredentialIdentity, IdentityProperties]") |
| 49 | + .addReference(Symbol.builder() |
| 50 | + .addDependency(SmithyPythonDependency.SMITHY_CORE) |
| 51 | + .name("IdentityResolver") |
| 52 | + .namespace("smithy_core.aio.interfaces.identity", ".") |
| 53 | + .build()) |
| 54 | + .addReference(Symbol.builder() |
| 55 | + .addDependency(AwsPythonDependency.SMITHY_AWS_CORE) |
| 56 | + .name("AWSCredentialIdentity") |
| 57 | + .namespace("smithy_aws_core.identity", ".") |
| 58 | + .build()) |
| 59 | + .addReference(Symbol.builder() |
| 60 | + .addDependency(SmithyPythonDependency.SMITHY_CORE) |
| 61 | + .name("IdentityProperties") |
| 62 | + .namespace("smithy_core.interfaces.identity", ".") |
| 63 | + .build()) |
| 64 | + .build()) |
| 65 | + // TODO: Initialize with the provider chain? |
| 66 | + .nullable(true) |
| 67 | + .build()) |
| 68 | + .addConfigProperty(regionConfig) |
| 69 | + .authScheme(new Sigv4AuthScheme()) |
| 70 | + .build() |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void customize(GenerationContext context) { |
| 76 | + if (!hasSigV4Auth(context)) { |
| 77 | + return; |
| 78 | + } |
| 79 | + var trait = context.settings().service(context.model()).expectTrait(SigV4Trait.class); |
| 80 | + var params = CodegenUtils.getHttpAuthParamsSymbol(context.settings()); |
| 81 | + var resolver = CodegenUtils.getHttpAuthSchemeResolverSymbol(context.settings()); |
| 82 | + |
| 83 | + // Add a function that generates the http auth option for api key auth. |
| 84 | + // This needs to be generated because there's modeled parameters that |
| 85 | + // must be accounted for. |
| 86 | + context.writerDelegator().useFileWriter(resolver.getDefinitionFile(), resolver.getNamespace(), writer -> { |
| 87 | + writer.addDependency(SmithyPythonDependency.SMITHY_HTTP); |
| 88 | + writer.addImport("smithy_http.aio.interfaces.auth", "HTTPAuthOption"); |
| 89 | + writer.pushState(); |
| 90 | + |
| 91 | + writer.write(""" |
| 92 | + def $1L(auth_params: $2T) -> HTTPAuthOption | None: |
| 93 | + return HTTPAuthOption( |
| 94 | + scheme_id=$3S, |
| 95 | + identity_properties={}, |
| 96 | + signer_properties={ |
| 97 | + "service": $4S, |
| 98 | + "region": auth_params.region |
| 99 | + } |
| 100 | + ) |
| 101 | + """, |
| 102 | + SIGV4_OPTION_GENERATOR_NAME, |
| 103 | + params, |
| 104 | + SigV4Trait.ID.toString(), |
| 105 | + trait.getName()); |
| 106 | + writer.popState(); |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + private boolean hasSigV4Auth(GenerationContext context) { |
| 111 | + var service = context.settings().service(context.model()); |
| 112 | + return service.hasTrait(SigV4Trait.class); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * The AuthScheme representing api key auth. |
| 117 | + */ |
| 118 | + private static final class Sigv4AuthScheme implements AuthScheme { |
| 119 | + |
| 120 | + @Override |
| 121 | + public ShapeId getAuthTrait() { |
| 122 | + return SigV4Trait.ID; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public ApplicationProtocol getApplicationProtocol() { |
| 127 | + return ApplicationProtocol.createDefaultHttpApplicationProtocol(); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public List<DerivedProperty> getAuthProperties() { |
| 132 | + return List.of( |
| 133 | + DerivedProperty.builder() |
| 134 | + .name("region") |
| 135 | + .source(DerivedProperty.Source.CONFIG) |
| 136 | + .type(Symbol.builder().name("str").build()) |
| 137 | + .sourcePropertyName("region") |
| 138 | + .build() |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + @Override |
| 144 | + public Symbol getAuthOptionGenerator(GenerationContext context) { |
| 145 | + var resolver = CodegenUtils.getHttpAuthSchemeResolverSymbol(context.settings()); |
| 146 | + return Symbol.builder() |
| 147 | + .name(SIGV4_OPTION_GENERATOR_NAME) |
| 148 | + .namespace(resolver.getNamespace(), ".") |
| 149 | + .definitionFile(resolver.getDefinitionFile()) |
| 150 | + .build(); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public Symbol getAuthSchemeSymbol(GenerationContext context) { |
| 155 | + return Symbol.builder() |
| 156 | + .name("SigV4AuthScheme") |
| 157 | + .namespace("smithy_aws_core.auth.sigv4", ".") |
| 158 | + .addDependency(AwsPythonDependency.SMITHY_AWS_CORE) |
| 159 | + .build(); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments