@@ -22,15 +22,15 @@ import software.amazon.smithy.model.shapes.ServiceShape
2222import software.amazon.smithy.model.traits.HttpBearerAuthTrait
2323
2424/* *
25- * Customization that enables sourcing Bearer tokens from JVM system properties and environment variables
25+ * Customization that enables sourcing Bearer tokens from JVM system properties and system environment variables
2626 *
27- * When a service-specific JVM system property (e.g., aws.bearerTokenBedrock) or environment variable
27+ * When a service-specific JVM system property (e.g., aws.bearerTokenBedrock) or system environment variable
2828 * for bearer tokens is present (e.g., AWS_BEARER_TOKEN_BEDROCK), this customization configures the
2929 * auth scheme resolver to prefer the smithy.api#httpBearerAuth scheme over other authentication methods.
3030 * Additionally, it configures a token provider that extracts the bearer token from these sources.
3131 */
3232class EnvironmentBearerTokenCustomization : KotlinIntegration {
33- // Currently only services with sigv4 service name 'bedrock' need this customization
33+ // Currently only services with sigV4 service name 'bedrock' need this customization
3434 private val supportedSigningServiceNames = setOf (" bedrock" )
3535
3636 override fun enabledForService (model : Model , settings : KotlinSettings ): Boolean {
@@ -68,11 +68,11 @@ class EnvironmentBearerTokenCustomization : KotlinIntegration {
6868 ) {
6969 val serviceSymbol = ctx.symbolProvider.toSymbol(serviceShape)
7070 val signingServiceName = AwsSignatureVersion4 .signingServiceName(serviceShape)
71- // Transform signing service name to service name suffix
71+ // Transform signing service name to environment variable key suffix
7272 val envVarSuffix = signingServiceName.withTransform(SigV4NameTransform .UpperSnakeCase )
73- val jvmSysPropSuffix = signingServiceName.withTransform(SigV4NameTransform .PascalCase )
74- val envVarName = " AWS_BEARER_TOKEN_$envVarSuffix "
75- val sysPropName = " aws.bearerToken$jvmSysPropSuffix "
73+ val sysPropSuffix = signingServiceName.withTransform(SigV4NameTransform .PascalCase )
74+ val envVarKey = " AWS_BEARER_TOKEN_$envVarSuffix "
75+ val sysPropKey = " aws.bearerToken$sysPropSuffix "
7676 val authSchemeId = RuntimeTypes .Auth .Identity .AuthSchemeId
7777
7878 writer.withBlock(
@@ -81,9 +81,13 @@ class EnvironmentBearerTokenCustomization : KotlinIntegration {
8181 serviceSymbol,
8282 RuntimeTypes .Core .Utils .PlatformProvider ,
8383 ) {
84- write(" val bearerToken = provider.getProperty(#S) ?: provider.getenv(#S)" , sysPropName, envVarName)
84+ withBlock(" val sourceKey = when {" , " }" ) {
85+ write(" provider.getProperty(#1S) != null -> #1S" , sysPropKey)
86+ write(" provider.getenv(#1S) != null -> #1S" , envVarKey)
87+ write(" else -> null" )
88+ }
8589 // The customization does nothing if environment variable and JVM system property are not set
86- write(" if (bearerToken == null) return" )
90+ write(" if (sourceKey == null) return" )
8791 // Configure auth scheme preference if customer hasn't specify one
8892 write(" builder.config.authSchemePreference = builder.config.authSchemePreference ?: listOf(#T.HttpBearer)" , authSchemeId)
8993
@@ -95,7 +99,7 @@ class EnvironmentBearerTokenCustomization : KotlinIntegration {
9599 write(" builder.config.authSchemePreference = listOf(#1T.HttpBearer) + filteredSchemes" , authSchemeId)
96100
97101 write(
98- " builder.config.bearerTokenProvider = builder.config.bearerTokenProvider ?: #T(bearerToken )" ,
102+ " builder.config.bearerTokenProvider = builder.config.bearerTokenProvider ?: #T(sourceKey, provider )" ,
99103 RuntimeTypes .Auth .HttpAuth .EnvironmentBearerTokenProvider ,
100104 )
101105 }
0 commit comments