-
Notifications
You must be signed in to change notification settings - Fork 204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connect aws-config token providers to service config via codegen #3443
Changes from 2 commits
98cb375
48d3ea4
e7cf976
374b39f
51c925b
e7bfbce
10105e9
f742a35
3d924d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,14 @@ | |
|
||
package software.amazon.smithy.rustsdk | ||
|
||
import software.amazon.smithy.aws.traits.auth.SigV4Trait | ||
import software.amazon.smithy.model.knowledge.ServiceIndex | ||
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext | ||
import software.amazon.smithy.rust.codegen.client.smithy.ClientRustModule | ||
import software.amazon.smithy.rust.codegen.client.smithy.configReexport | ||
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator | ||
import software.amazon.smithy.rust.codegen.client.smithy.customize.TestUtilFeature | ||
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.supportedAuthSchemes | ||
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.usesSigV4a | ||
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization | ||
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig | ||
import software.amazon.smithy.rust.codegen.core.rustlang.featureGateBlock | ||
|
@@ -22,29 +24,38 @@ import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.Companion.pre | |
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate | ||
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization | ||
import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization | ||
import software.amazon.smithy.rust.codegen.core.util.letIf | ||
|
||
class CredentialsProviderDecorator : ClientCodegenDecorator { | ||
override val name: String = "CredentialsProvider" | ||
override val order: Byte = 0 | ||
|
||
private fun applies(codegenContext: ClientCodegenContext): Boolean = | ||
ServiceIndex.of(codegenContext.model).getEffectiveAuthSchemes(codegenContext.serviceShape) | ||
.containsKey(SigV4Trait.ID) || codegenContext.serviceShape.usesSigV4a() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these changes are to clean up support for bearer-only services? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. For Code Catalyst, it doesn't make sense to have methods to configure credential providers since it doesn't use SigV4. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that a breaking change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose it is in a way, in terms of compiling, except for that the service wouldn't have worked to begin with. Edit: I'll double check that SigV4 auth didn't work with it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed the currently released aws-sdk-codecatalyst doesn't actually work:
|
||
override fun configCustomizations( | ||
codegenContext: ClientCodegenContext, | ||
baseCustomizations: List<ConfigCustomization>, | ||
): List<ConfigCustomization> { | ||
return baseCustomizations + CredentialProviderConfig(codegenContext) | ||
} | ||
): List<ConfigCustomization> = | ||
baseCustomizations.letIf(applies(codegenContext)) { it + CredentialProviderConfig(codegenContext) } | ||
|
||
override fun extraSections(codegenContext: ClientCodegenContext): List<AdHocCustomization> = | ||
listOf( | ||
adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section -> | ||
rust("${section.serviceConfigBuilder}.set_credentials_provider(${section.sdkConfig}.credentials_provider());") | ||
}, | ||
) | ||
emptyList<AdHocCustomization>().letIf(applies(codegenContext)) { | ||
it + | ||
adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section -> | ||
rust("${section.serviceConfigBuilder}.set_credentials_provider(${section.sdkConfig}.credentials_provider());") | ||
} | ||
} | ||
|
||
override fun extras( | ||
codegenContext: ClientCodegenContext, | ||
rustCrate: RustCrate, | ||
) { | ||
if (!applies(codegenContext)) { | ||
return | ||
} | ||
|
||
rustCrate.mergeFeature(TestUtilFeature.copy(deps = listOf("aws-credential-types/test-util"))) | ||
|
||
rustCrate.withModule(ClientRustModule.config) { | ||
|
@@ -125,7 +136,7 @@ class CredentialProviderConfig(private val codegenContext: ClientCodegenContext) | |
""", | ||
*codegenScope, | ||
) { | ||
if (codegenContext.serviceShape.supportedAuthSchemes().contains("sigv4a")) { | ||
if (codegenContext.serviceShape.usesSigV4a()) { | ||
featureGateBlock("sigv4a") { | ||
rustTemplate( | ||
"self.runtime_components.set_identity_resolver(#{SIGV4A_SCHEME_ID}, credentials_provider.clone());", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package software.amazon.smithy.rustsdk | ||
|
||
import software.amazon.smithy.model.knowledge.ServiceIndex | ||
import software.amazon.smithy.model.traits.HttpBearerAuthTrait | ||
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext | ||
import software.amazon.smithy.rust.codegen.client.smithy.configReexport | ||
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator | ||
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization | ||
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig | ||
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency | ||
import software.amazon.smithy.rust.codegen.core.rustlang.rust | ||
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate | ||
import software.amazon.smithy.rust.codegen.core.rustlang.writable | ||
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType | ||
import software.amazon.smithy.rust.codegen.core.smithy.customize.AdHocCustomization | ||
import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomization | ||
|
||
class TokenProvidersDecorator : ClientCodegenDecorator { | ||
override val name: String get() = "TokenProvidersDecorator" | ||
override val order: Byte = 0 | ||
|
||
private fun applies(codegenContext: ClientCodegenContext): Boolean = | ||
ServiceIndex.of(codegenContext.model).getEffectiveAuthSchemes(codegenContext.serviceShape) | ||
.containsKey(HttpBearerAuthTrait.ID) | ||
|
||
override fun configCustomizations( | ||
codegenContext: ClientCodegenContext, | ||
baseCustomizations: List<ConfigCustomization>, | ||
): List<ConfigCustomization> = | ||
if (applies(codegenContext)) { | ||
baseCustomizations + TokenProviderConfig(codegenContext) | ||
} else { | ||
baseCustomizations | ||
} | ||
jdisanti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
override fun extraSections(codegenContext: ClientCodegenContext): List<AdHocCustomization> = | ||
if (applies(codegenContext)) { | ||
jdisanti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
listOf( | ||
adhocCustomization<SdkConfigSection.CopySdkConfigToClientConfig> { section -> | ||
rust("${section.serviceConfigBuilder}.set_token_provider(${section.sdkConfig}.token_provider());") | ||
}, | ||
) | ||
} else { | ||
emptyList() | ||
} | ||
} | ||
|
||
/** | ||
* Add a `.token_provider` field and builder to the `Config` for a given service | ||
*/ | ||
class TokenProviderConfig(private val codegenContext: ClientCodegenContext) : ConfigCustomization() { | ||
private val runtimeConfig = codegenContext.runtimeConfig | ||
private val codegenScope = | ||
arrayOf( | ||
*RuntimeType.preludeScope, | ||
"Token" to configReexport(AwsRuntimeType.awsCredentialTypes(runtimeConfig).resolve("Token")), | ||
"ProvideToken" to | ||
configReexport( | ||
AwsRuntimeType.awsCredentialTypes(runtimeConfig) | ||
.resolve("provider::token::ProvideToken"), | ||
), | ||
"SharedTokenProvider" to | ||
configReexport( | ||
AwsRuntimeType.awsCredentialTypes(runtimeConfig) | ||
.resolve("provider::token::SharedTokenProvider"), | ||
), | ||
"TestToken" to AwsRuntimeType.awsCredentialTypesTestUtil(runtimeConfig).resolve("Token"), | ||
"HTTP_BEARER_AUTH_SCHEME_ID" to | ||
CargoDependency.smithyRuntimeApiClient(runtimeConfig) | ||
.withFeature("http-auth").toType().resolve("client::auth::http").resolve("HTTP_BEARER_AUTH_SCHEME_ID"), | ||
) | ||
|
||
override fun section(section: ServiceConfig) = | ||
writable { | ||
when (section) { | ||
ServiceConfig.BuilderImpl -> { | ||
rustTemplate( | ||
""" | ||
/// Sets the access token provider for this service | ||
pub fn token_provider(mut self, token_provider: impl #{ProvideToken} + 'static) -> Self { | ||
self.set_token_provider(#{Some}(#{SharedTokenProvider}::new(token_provider))); | ||
jdisanti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self | ||
} | ||
|
||
/// Sets the access token provider for this service | ||
pub fn set_token_provider(&mut self, token_provider: #{Option}<#{SharedTokenProvider}>) -> &mut Self { | ||
if let Some(token_provider) = token_provider { | ||
self.runtime_components.set_identity_resolver(#{HTTP_BEARER_AUTH_SCHEME_ID}, token_provider); | ||
} | ||
self | ||
} | ||
""", | ||
*codegenScope, | ||
) | ||
} | ||
|
||
is ServiceConfig.DefaultForTests -> | ||
rustTemplate( | ||
"${section.configBuilderRef}.set_token_provider(Some(#{SharedTokenProvider}::new(#{TestToken}::for_tests())));", | ||
*codegenScope, | ||
) | ||
|
||
else -> emptySection | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we should rename this to
SigV4CredentialsProviderDecorator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm slightly hesitant to make it more specific, but could be convinced. It applies to both SigV4 and SigV4a right now, and it could apply to more things in the future.