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