|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.rustsdk |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test |
| 9 | +import software.amazon.smithy.rust.codegen.core.rustlang.Attribute |
| 10 | +import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate |
| 11 | +import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType |
| 12 | +import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel |
| 13 | +import software.amazon.smithy.rust.codegen.core.testutil.testModule |
| 14 | +import software.amazon.smithy.rust.codegen.core.testutil.tokioTest |
| 15 | +import software.amazon.smithy.rust.codegen.core.testutil.unitTest |
| 16 | + |
| 17 | +internal class CredentialCacheConfigTest { |
| 18 | + private val model = """ |
| 19 | + namespace com.example |
| 20 | + use aws.protocols#awsJson1_0 |
| 21 | + use aws.api#service |
| 22 | + use smithy.rules#endpointRuleSet |
| 23 | +
|
| 24 | + @service(sdkId: "Some Value") |
| 25 | + @awsJson1_0 |
| 26 | + @endpointRuleSet({ |
| 27 | + "version": "1.0", |
| 28 | + "rules": [{ |
| 29 | + "type": "endpoint", |
| 30 | + "conditions": [{"fn": "isSet", "argv": [{"ref": "Region"}]}], |
| 31 | + "endpoint": { "url": "https://example.com" } |
| 32 | + }], |
| 33 | + "parameters": { |
| 34 | + "Region": { "required": false, "type": "String", "builtIn": "AWS::Region" }, |
| 35 | + } |
| 36 | + }) |
| 37 | + service HelloService { |
| 38 | + operations: [SayHello], |
| 39 | + version: "1" |
| 40 | + } |
| 41 | +
|
| 42 | + @optionalAuth |
| 43 | + operation SayHello { input: TestInput } |
| 44 | + structure TestInput { |
| 45 | + foo: String, |
| 46 | + } |
| 47 | + """.asSmithyModel() |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `config override for credentials`() { |
| 51 | + awsSdkIntegrationTest(model, defaultToOrchestrator = true) { clientCodegenContext, rustCrate -> |
| 52 | + val runtimeConfig = clientCodegenContext.runtimeConfig |
| 53 | + val codegenScope = arrayOf( |
| 54 | + *RuntimeType.preludeScope, |
| 55 | + "Credentials" to AwsRuntimeType.awsCredentialTypesTestUtil(runtimeConfig) |
| 56 | + .resolve("Credentials"), |
| 57 | + "CredentialsCache" to AwsRuntimeType.awsCredentialTypes(runtimeConfig) |
| 58 | + .resolve("cache::CredentialsCache"), |
| 59 | + "ProvideCachedCredentials" to AwsRuntimeType.awsCredentialTypes(runtimeConfig) |
| 60 | + .resolve("cache::ProvideCachedCredentials"), |
| 61 | + "RuntimePlugin" to RuntimeType.smithyRuntimeApi(runtimeConfig) |
| 62 | + .resolve("client::runtime_plugin::RuntimePlugin"), |
| 63 | + "SharedCredentialsCache" to AwsRuntimeType.awsCredentialTypes(runtimeConfig) |
| 64 | + .resolve("cache::SharedCredentialsCache"), |
| 65 | + "SharedCredentialsProvider" to AwsRuntimeType.awsCredentialTypes(runtimeConfig) |
| 66 | + .resolve("provider::SharedCredentialsProvider"), |
| 67 | + ) |
| 68 | + rustCrate.testModule { |
| 69 | + unitTest( |
| 70 | + "test_overriding_only_credentials_provider_should_panic", |
| 71 | + additionalAttributes = listOf(Attribute.shouldPanic("also specify `.credentials_cache` when overriding credentials provider for the operation")), |
| 72 | + ) { |
| 73 | + rustTemplate( |
| 74 | + """ |
| 75 | + use #{RuntimePlugin}; |
| 76 | +
|
| 77 | + let client_config = crate::config::Config::builder().build(); |
| 78 | + let config_override = |
| 79 | + crate::config::Config::builder().credentials_provider(#{Credentials}::for_tests()); |
| 80 | + let sut = crate::config::ConfigOverrideRuntimePlugin { |
| 81 | + client_config: client_config.config().unwrap(), |
| 82 | + config_override, |
| 83 | + }; |
| 84 | +
|
| 85 | + // this should cause `panic!` |
| 86 | + let _ = sut.config().unwrap(); |
| 87 | + """, |
| 88 | + *codegenScope, |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + unitTest( |
| 93 | + "test_overriding_only_credentials_cache_should_panic", |
| 94 | + additionalAttributes = listOf(Attribute.shouldPanic("also specify `.credentials_provider` when overriding credentials cache for the operation")), |
| 95 | + ) { |
| 96 | + rustTemplate( |
| 97 | + """ |
| 98 | + use #{RuntimePlugin}; |
| 99 | +
|
| 100 | + let client_config = crate::config::Config::builder().build(); |
| 101 | + let config_override = crate::config::Config::builder() |
| 102 | + .credentials_cache(#{CredentialsCache}::no_caching()); |
| 103 | + let sut = crate::config::ConfigOverrideRuntimePlugin { |
| 104 | + client_config: client_config.config().unwrap(), |
| 105 | + config_override, |
| 106 | + }; |
| 107 | +
|
| 108 | + // this should cause `panic!` |
| 109 | + let _ = sut.config().unwrap(); |
| 110 | + """, |
| 111 | + *codegenScope, |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + tokioTest("test_overriding_cache_and_provider_leads_to_shared_credentials_cache_in_layer") { |
| 116 | + rustTemplate( |
| 117 | + """ |
| 118 | + use #{ProvideCachedCredentials}; |
| 119 | + use #{RuntimePlugin}; |
| 120 | +
|
| 121 | + let client_config = crate::config::Config::builder() |
| 122 | + .credentials_provider(#{Credentials}::for_tests()) |
| 123 | + .build(); |
| 124 | + let client_config_layer = client_config.config().unwrap(); |
| 125 | +
|
| 126 | + // make sure test credentials are set in the client config level |
| 127 | + assert_eq!(#{Credentials}::for_tests(), |
| 128 | + client_config_layer |
| 129 | + .load::<#{SharedCredentialsCache}>() |
| 130 | + .unwrap() |
| 131 | + .provide_cached_credentials() |
| 132 | + .await |
| 133 | + .unwrap() |
| 134 | + ); |
| 135 | +
|
| 136 | + let credentials = #{Credentials}::new( |
| 137 | + "test", |
| 138 | + "test", |
| 139 | + #{None}, |
| 140 | + #{None}, |
| 141 | + "test", |
| 142 | + ); |
| 143 | + let config_override = crate::config::Config::builder() |
| 144 | + .credentials_cache(#{CredentialsCache}::lazy()) |
| 145 | + .credentials_provider(credentials.clone()); |
| 146 | + let sut = crate::config::ConfigOverrideRuntimePlugin { |
| 147 | + client_config: client_config_layer, |
| 148 | + config_override, |
| 149 | + }; |
| 150 | + let sut_layer = sut.config().unwrap(); |
| 151 | +
|
| 152 | + // make sure `.provide_cached_credentials` returns credentials set through `config_override` |
| 153 | + assert_eq!(credentials, |
| 154 | + sut_layer |
| 155 | + .load::<#{SharedCredentialsCache}>() |
| 156 | + .unwrap() |
| 157 | + .provide_cached_credentials() |
| 158 | + .await |
| 159 | + .unwrap() |
| 160 | + ); |
| 161 | + """, |
| 162 | + *codegenScope, |
| 163 | + ) |
| 164 | + } |
| 165 | + |
| 166 | + unitTest("test_not_overriding_cache_and_provider_leads_to_no_shared_credentials_cache_in_layer") { |
| 167 | + rustTemplate( |
| 168 | + """ |
| 169 | + use #{RuntimePlugin}; |
| 170 | +
|
| 171 | + let client_config = crate::config::Config::builder().build(); |
| 172 | + let config_override = crate::config::Config::builder(); |
| 173 | + let sut = crate::config::ConfigOverrideRuntimePlugin { |
| 174 | + client_config: client_config.config().unwrap(), |
| 175 | + config_override, |
| 176 | + }; |
| 177 | + let sut_layer = sut.config().unwrap(); |
| 178 | + assert!(sut_layer |
| 179 | + .load::<#{SharedCredentialsCache}>() |
| 180 | + .is_none()); |
| 181 | + """, |
| 182 | + *codegenScope, |
| 183 | + ) |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments