Skip to content

Commit fd2ea3f

Browse files
committed
Add tests for resolving from environment
1 parent 5c08921 commit fd2ea3f

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/auth/ResolveAuthSchemePreference.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public suspend fun resolveAuthSchemePreference(platform: PlatformProvider = Plat
1717
?.split(",")
1818
?.map { it.trim() }
1919
?.filter { it.isNotEmpty() }
20-
?.mapNotNull { AUTH_SCHEME_NAME_MAP[it] }
20+
?.mapNotNull { AUTH_SCHEME_NAME_MAP[it.lowercase()] }
2121
?.toList()
2222
?: emptyList()
2323
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.sdk.kotlin.runtime.config.auth
6+
7+
import aws.sdk.kotlin.runtime.config.profile.AwsConfigurationSource
8+
import aws.sdk.kotlin.runtime.config.profile.FileType
9+
import aws.sdk.kotlin.runtime.config.profile.parse
10+
import aws.sdk.kotlin.runtime.config.profile.toSharedConfig
11+
import aws.smithy.kotlin.runtime.auth.AuthSchemeId
12+
import aws.smithy.kotlin.runtime.telemetry.logging.Logger
13+
import aws.smithy.kotlin.runtime.util.TestPlatformProvider
14+
import aws.smithy.kotlin.runtime.util.asyncLazy
15+
import kotlinx.coroutines.test.runTest
16+
import kotlin.test.Test
17+
import kotlin.test.assertEquals
18+
19+
class ResolveAuthSchemePreferenceTest {
20+
@Test
21+
fun testProfile() = runTest {
22+
assertEquals(
23+
AuthSchemeId.AwsSigV4,
24+
testResolveAuthSchemePreference(
25+
profileContent = """
26+
[default]
27+
auth_scheme_preference = sigv4
28+
""".trimIndent()
29+
).single()
30+
)
31+
}
32+
33+
@Test
34+
fun testEnvironment() = runTest {
35+
// Environment takes precedence over profile
36+
assertEquals(
37+
AuthSchemeId.AwsSigV4Asymmetric,
38+
testResolveAuthSchemePreference(
39+
env = mapOf("AWS_AUTH_SCHEME_PREFERENCE" to "sigv4a"),
40+
profileContent = """
41+
[default]
42+
auth_scheme_preference = sigv4
43+
""".trimIndent()
44+
).single()
45+
)
46+
}
47+
48+
@Test
49+
fun testSystemProperties() = runTest {
50+
// System properties take precedence over environment and profile
51+
assertEquals(
52+
AuthSchemeId.HttpBearer,
53+
testResolveAuthSchemePreference(
54+
env = mapOf("AWS_AUTH_SCHEME_PREFERENCE" to "sigv4a"),
55+
sysProps = mapOf("aws.authSchemePreference" to "httpBearerAuth"),
56+
profileContent = """
57+
[default]
58+
auth_scheme_preference = sigv4
59+
""".trimIndent()
60+
).single()
61+
)
62+
}
63+
64+
@Test
65+
fun testResolveMultipleSchemes() = runTest {
66+
assertEquals(
67+
listOf(AuthSchemeId.HttpBearer, AuthSchemeId.AwsSigV4Asymmetric, AuthSchemeId.AwsSigV4),
68+
testResolveAuthSchemePreference(
69+
env = mapOf("AWS_AUTH_SCHEME_PREFERENCE" to "httpBearerAuth, sigv4a, sigv4"),
70+
)
71+
)
72+
}
73+
74+
@Test
75+
fun testIgnoreWhitespace() = runTest {
76+
assertEquals(
77+
listOf(AuthSchemeId.HttpBearer, AuthSchemeId.AwsSigV4Asymmetric),
78+
testResolveAuthSchemePreference(
79+
env = mapOf("AWS_AUTH_SCHEME_PREFERENCE" to "httpBearerAuth , sigv4a "),
80+
)
81+
)
82+
}
83+
84+
@Test
85+
fun testDontFailOnInvalidSchemes() = runTest {
86+
assertEquals(
87+
listOf(AuthSchemeId.HttpBearer, AuthSchemeId.AwsSigV4),
88+
testResolveAuthSchemePreference(
89+
env = mapOf("AWS_AUTH_SCHEME_PREFERENCE" to "httpBearerAuth, whatIsThisScheme, sigv4"),
90+
)
91+
)
92+
}
93+
94+
private suspend fun testResolveAuthSchemePreference(
95+
env: Map<String, String> = mapOf(),
96+
sysProps: Map<String, String> = mapOf(),
97+
profileContent: String = "",
98+
): List<AuthSchemeId> {
99+
val platform = TestPlatformProvider(env = env, props = sysProps)
100+
val source = AwsConfigurationSource("default", "", "")
101+
val profile = asyncLazy {
102+
parse(Logger.None, FileType.CONFIGURATION, profileContent).toSharedConfig(source).activeProfile
103+
}
104+
105+
return resolveAuthSchemePreference(platform, profile)
106+
}
107+
}

0 commit comments

Comments
 (0)