Skip to content

Commit 7c924ed

Browse files
authored
feat: make credentials provider chain accept list of credentials providers (#910)
1 parent 9f3ea3c commit 7c924ed

File tree

7 files changed

+90
-0
lines changed

7 files changed

+90
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "493ccb85-b5ab-4a0b-89eb-c4ef45bfcf42",
3+
"type": "feature",
4+
"description": "Make CredentialsProviderChain accept list of CredentialProviders",
5+
"issues": [
6+
"awslabs/aws-sdk-kotlin#1001"
7+
]
8+
}

runtime/auth/aws-credentials/api/aws-credentials.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public abstract interface class aws/smithy/kotlin/runtime/auth/awscredentials/Cr
3939
}
4040

4141
public final class aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProviderChain : aws/smithy/kotlin/runtime/identity/IdentityProviderChain, aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider {
42+
public fun <init> (Ljava/util/List;)V
4243
public fun <init> ([Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;)V
4344
public fun resolve (Laws/smithy/kotlin/runtime/util/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
4445
}

runtime/auth/aws-credentials/common/src/aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProviderChain.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ import aws.smithy.kotlin.runtime.util.Attributes
1616
*/
1717
public class CredentialsProviderChain(vararg providers: CredentialsProvider) :
1818
IdentityProviderChain<CredentialsProvider, Credentials>(*providers), CredentialsProvider {
19+
20+
public constructor(providers: List<CredentialsProvider>) : this(*providers.toTypedArray())
21+
1922
override suspend fun resolve(attributes: Attributes): Credentials = super.resolve(attributes)
2023
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package aws.smithy.kotlin.runtime.auth.awscredentials
7+
8+
import aws.smithy.kotlin.runtime.util.Attributes
9+
import kotlinx.coroutines.ExperimentalCoroutinesApi
10+
import kotlinx.coroutines.test.runTest
11+
import org.junit.jupiter.api.Test
12+
import kotlin.test.assertEquals
13+
14+
@OptIn(ExperimentalCoroutinesApi::class)
15+
class CredentialsProviderChainTest {
16+
private class TestCredentialsProvider(val accessKeyId: String? = null, val secretAccessKey: String? = null) : CredentialsProvider {
17+
override suspend fun resolve(attributes: Attributes): Credentials =
18+
if (accessKeyId != null && secretAccessKey != null) Credentials(accessKeyId, secretAccessKey) else error("no credentials available")
19+
}
20+
21+
@Test
22+
fun testVararg() = runTest {
23+
val chain = CredentialsProviderChain(TestCredentialsProvider(), TestCredentialsProvider("AKeyID", "SecretKey"))
24+
assertEquals(Credentials("AKeyID", "SecretKey"), chain.resolve())
25+
}
26+
27+
@Test
28+
fun testList() = runTest {
29+
val credentialsProviders = listOf<CredentialsProvider>(TestCredentialsProvider(), TestCredentialsProvider("AKeyID", "SecretKey"))
30+
val chain = CredentialsProviderChain(credentialsProviders)
31+
assertEquals(Credentials("AKeyID", "SecretKey"), chain.resolve())
32+
}
33+
}

runtime/auth/http-auth/api/http-auth.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public abstract interface class aws/smithy/kotlin/runtime/http/auth/BearerTokenP
3737
}
3838

3939
public final class aws/smithy/kotlin/runtime/http/auth/BearerTokenProviderChain : aws/smithy/kotlin/runtime/identity/IdentityProviderChain, aws/smithy/kotlin/runtime/http/auth/BearerTokenProvider {
40+
public fun <init> (Ljava/util/List;)V
4041
public fun <init> ([Laws/smithy/kotlin/runtime/http/auth/BearerTokenProvider;)V
4142
public fun resolve (Laws/smithy/kotlin/runtime/util/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
4243
}

runtime/auth/http-auth/common/src/aws/smithy/kotlin/runtime/http/auth/BearerTokenProviderChain.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ import aws.smithy.kotlin.runtime.util.Attributes
1818
public class BearerTokenProviderChain(vararg providers: BearerTokenProvider) :
1919
IdentityProviderChain<BearerTokenProvider, BearerToken>(*providers),
2020
BearerTokenProvider {
21+
22+
public constructor(providers: List<BearerTokenProvider>) : this(*providers.toTypedArray())
23+
2124
override suspend fun resolve(attributes: Attributes): BearerToken = super.resolve(attributes)
2225
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package aws.smithy.kotlin.runtime.http.auth
7+
8+
import aws.smithy.kotlin.runtime.time.Instant
9+
import aws.smithy.kotlin.runtime.util.Attributes
10+
import aws.smithy.kotlin.runtime.util.emptyAttributes
11+
import kotlinx.coroutines.ExperimentalCoroutinesApi
12+
import kotlinx.coroutines.test.runTest
13+
import org.junit.jupiter.api.Assertions.assertEquals
14+
import org.junit.jupiter.api.Test
15+
16+
@OptIn(ExperimentalCoroutinesApi::class)
17+
class BearerTokenProviderChainTest {
18+
private class TestTokenProvider(val token: String? = null) : BearerTokenProvider {
19+
override suspend fun resolve(attributes: Attributes): BearerToken =
20+
if (token != null) Token(token) else error("no credentials available")
21+
}
22+
23+
@Test
24+
fun testVararg() = runTest {
25+
val chain = BearerTokenProviderChain(TestTokenProvider(), TestTokenProvider("token"))
26+
assertEquals(Token("token"), chain.resolve())
27+
}
28+
29+
@Test
30+
fun testList() = runTest {
31+
val tokenProviders = listOf<BearerTokenProvider>(TestTokenProvider(), TestTokenProvider("token"))
32+
val chain = BearerTokenProviderChain(tokenProviders)
33+
assertEquals(Token("token"), chain.resolve())
34+
}
35+
}
36+
37+
private data class Token(
38+
override val token: String,
39+
override val attributes: Attributes = emptyAttributes(),
40+
override val expiration: Instant? = null,
41+
) : BearerToken

0 commit comments

Comments
 (0)