Skip to content

Commit a87bdfb

Browse files
committed
refactor
1 parent 98a811b commit a87bdfb

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/SdkIdTransform.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import software.amazon.smithy.kotlin.codegen.utils.toPascalCase
88

99
private val whitespaceRegex = Regex("\\s")
1010

11-
private val dashRegex = Regex("-")
11+
private val spaceOrDashRegex = Regex("\\s|-")
1212

1313
/**
1414
* Base interface for string transformers.
@@ -72,7 +72,7 @@ object SigV4NameTransform {
7272
* Replace all dashes from the SigV4 service signing name with underscores and capitalize all letters.
7373
*/
7474
object UpperSnakeCase : SigV4NameTransformer {
75-
override fun transform(id: String): String = id.replaceDash("_").uppercase()
75+
override fun transform(id: String): String = id.lowercase().replaceDash("_").uppercase()
7676
}
7777

7878
/**
@@ -90,4 +90,4 @@ fun <T : StringTransformer> String.withTransform(transformer: T): String = trans
9090

9191
private fun String.replaceWhitespace(replacement: String) = replace(whitespaceRegex, replacement)
9292

93-
private fun String.replaceDash(replacement: String) = replace(dashRegex, replacement)
93+
private fun String.replaceDash(replacement: String) = replace(spaceOrDashRegex, replacement)

codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/EnvironmentBearerTokenCustomization.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ import software.amazon.smithy.model.shapes.ServiceShape
2222
import 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
*/
3232
class 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

Comments
 (0)