Skip to content

Commit 0e77b6a

Browse files
committed
misc: add lazily initialized creds provider and use it in default credentials provider chain
1 parent a78d614 commit 0e77b6a

File tree

4 files changed

+54
-32
lines changed

4 files changed

+54
-32
lines changed

.changes/announcement.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ S3Client{
5959
credentialsProvider = CredentialsProviderChain(
6060
SystemPropertyCredentialsProvider(),
6161
EnvironmentCredentialsProvider(),
62-
StsWebIdentityProvider(),
62+
LazilyInitializedCredentialsProvider("EnvironmentStsWebIdentityCredentialsProvider") {
63+
StsWebIdentityCredentialsProvider.fromEnvironment()
64+
},
6365
ProfileCredentialsProvider(),
6466
EcsCredentialsProvider(),
6567
ImdsCredentialsProvider(),

aws-runtime/aws-config/api/aws-config.api

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/DefaultChainBearerTok
3939
public final fun getHttpClient ()Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;
4040
public final fun getPlatformProvider ()Laws/smithy/kotlin/runtime/util/PlatformProvider;
4141
public final fun getProfileName ()Ljava/lang/String;
42+
public fun getProviderName ()Ljava/lang/String;
4243
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
4344
}
4445

@@ -49,6 +50,7 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/DefaultChainCredentia
4950
public fun close ()V
5051
public final fun getPlatformProvider ()Laws/smithy/kotlin/runtime/util/PlatformProvider;
5152
public final fun getProfileName ()Ljava/lang/String;
53+
public fun getProviderName ()Ljava/lang/String;
5254
public final fun getRegion ()Ljava/lang/String;
5355
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
5456
}
@@ -61,6 +63,7 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/EcsCredentialsProvide
6163
public synthetic fun <init> (Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;Laws/smithy/kotlin/runtime/net/HostResolver;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
6264
public fun close ()V
6365
public final fun getPlatformProvider ()Laws/smithy/kotlin/runtime/util/PlatformProvider;
66+
public fun getProviderName ()Ljava/lang/String;
6467
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
6568
}
6669

@@ -69,6 +72,7 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/EnvironmentCredential
6972
public fun <init> (Lkotlin/jvm/functions/Function1;)V
7073
public synthetic fun <init> (Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
7174
public final fun getGetEnv ()Lkotlin/jvm/functions/Function1;
75+
public fun getProviderName ()Ljava/lang/String;
7276
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
7377
}
7478

@@ -80,6 +84,7 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/ImdsCredentialsProvid
8084
public final fun getClient ()Lkotlin/Lazy;
8185
public final fun getPlatformProvider ()Laws/smithy/kotlin/runtime/util/PlatformEnvironProvider;
8286
public final fun getProfileOverride ()Ljava/lang/String;
87+
public fun getProviderName ()Ljava/lang/String;
8388
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
8489
}
8590

@@ -93,9 +98,17 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/InvalidSsoTokenExcept
9398
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
9499
}
95100

101+
public final class aws/sdk/kotlin/runtime/auth/credentials/LazilyInitializedCredentialsProvider : aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider {
102+
public fun <init> (Ljava/lang/String;Lkotlin/jvm/functions/Function0;)V
103+
public synthetic fun <init> (Ljava/lang/String;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
104+
public fun getProviderName ()Ljava/lang/String;
105+
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
106+
}
107+
96108
public final class aws/sdk/kotlin/runtime/auth/credentials/ProcessCredentialsProvider : aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider {
97109
public fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;JJ)V
98110
public synthetic fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;JJILkotlin/jvm/internal/DefaultConstructorMarker;)V
111+
public fun getProviderName ()Ljava/lang/String;
99112
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
100113
}
101114

@@ -110,6 +123,7 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/ProfileCredentialsPro
110123
public final fun getHttpClient ()Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;
111124
public final fun getPlatformProvider ()Laws/smithy/kotlin/runtime/util/PlatformProvider;
112125
public final fun getProfileName ()Ljava/lang/String;
126+
public fun getProviderName ()Ljava/lang/String;
113127
public final fun getRegion ()Ljava/lang/String;
114128
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
115129
}
@@ -125,6 +139,7 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/SsoCredentialsProvide
125139
public final fun getAccountId ()Ljava/lang/String;
126140
public final fun getHttpClient ()Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;
127141
public final fun getPlatformProvider ()Laws/smithy/kotlin/runtime/util/PlatformProvider;
142+
public fun getProviderName ()Ljava/lang/String;
128143
public final fun getRoleName ()Ljava/lang/String;
129144
public final fun getSsoRegion ()Ljava/lang/String;
130145
public final fun getSsoSessionName ()Ljava/lang/String;
@@ -137,6 +152,7 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/SsoTokenProvider : aw
137152
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLaws/smithy/kotlin/runtime/http/engine/HttpClientEngine;Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/time/Clock;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
138153
public final fun getHttpClient ()Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;
139154
public final fun getPlatformProvider ()Laws/smithy/kotlin/runtime/util/PlatformProvider;
155+
public fun getProviderName ()Ljava/lang/String;
140156
public final fun getRefreshBufferWindow-UwyO8pc ()J
141157
public final fun getSsoRegion ()Ljava/lang/String;
142158
public final fun getSsoSessionName ()Ljava/lang/String;
@@ -149,6 +165,7 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/StaticCredentialsProv
149165
public synthetic fun <init> (Laws/sdk/kotlin/runtime/auth/credentials/StaticCredentialsProvider$Builder;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
150166
public fun <init> (Laws/smithy/kotlin/runtime/auth/awscredentials/Credentials;)V
151167
public final fun getCredentials ()Laws/smithy/kotlin/runtime/auth/awscredentials/Credentials;
168+
public fun getProviderName ()Ljava/lang/String;
152169
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
153170
}
154171

@@ -177,6 +194,7 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/StsAssumeRoleCredenti
177194
public final fun getAssumeRoleParameters ()Laws/sdk/kotlin/runtime/auth/credentials/AssumeRoleParameters;
178195
public final fun getBootstrapCredentialsProvider ()Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;
179196
public final fun getHttpClient ()Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;
197+
public fun getProviderName ()Ljava/lang/String;
180198
public final fun getRegion ()Ljava/lang/String;
181199
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
182200
}
@@ -189,6 +207,7 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/StsWebIdentityCredent
189207
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLaws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
190208
public final fun getHttpClient ()Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;
191209
public final fun getPlatformProvider ()Laws/smithy/kotlin/runtime/util/PlatformProvider;
210+
public fun getProviderName ()Ljava/lang/String;
192211
public final fun getRegion ()Ljava/lang/String;
193212
public final fun getWebIdentityParameters ()Laws/sdk/kotlin/runtime/auth/credentials/AssumeRoleWithWebIdentityParameters;
194213
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
@@ -199,19 +218,12 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/StsWebIdentityCredent
199218
public static synthetic fun fromEnvironment-TUY-ock$default (Laws/sdk/kotlin/runtime/auth/credentials/StsWebIdentityCredentialsProvider$Companion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLaws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;ILjava/lang/Object;)Laws/sdk/kotlin/runtime/auth/credentials/StsWebIdentityCredentialsProvider;
200219
}
201220

202-
public final class aws/sdk/kotlin/runtime/auth/credentials/StsWebIdentityProvider : aws/smithy/kotlin/runtime/auth/awscredentials/CloseableCredentialsProvider {
203-
public fun <init> ()V
204-
public fun <init> (Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;Ljava/lang/String;)V
205-
public synthetic fun <init> (Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
206-
public fun close ()V
207-
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
208-
}
209-
210221
public final class aws/sdk/kotlin/runtime/auth/credentials/SystemPropertyCredentialsProvider : aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider {
211222
public fun <init> ()V
212223
public fun <init> (Lkotlin/jvm/functions/Function1;)V
213224
public synthetic fun <init> (Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
214225
public final fun getGetProperty ()Lkotlin/jvm/functions/Function1;
226+
public fun getProviderName ()Ljava/lang/String;
215227
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
216228
}
217229

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

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ package aws.sdk.kotlin.runtime.auth.credentials
77

88
import aws.sdk.kotlin.runtime.config.AwsSdkSetting
99
import aws.sdk.kotlin.runtime.config.imds.ImdsClient
10-
import aws.smithy.kotlin.runtime.auth.awscredentials.CachedCredentialsProvider
11-
import aws.smithy.kotlin.runtime.auth.awscredentials.CloseableCredentialsProvider
12-
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
13-
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProviderChain
10+
import aws.smithy.kotlin.runtime.auth.awscredentials.*
1411
import aws.smithy.kotlin.runtime.collections.Attributes
1512
import aws.smithy.kotlin.runtime.http.engine.DefaultHttpEngine
1613
import aws.smithy.kotlin.runtime.http.engine.HttpClientEngine
@@ -55,8 +52,14 @@ public class DefaultChainCredentialsProvider constructor(
5552
private val chain = CredentialsProviderChain(
5653
SystemPropertyCredentialsProvider(platformProvider::getProperty),
5754
EnvironmentCredentialsProvider(platformProvider::getenv),
58-
// STS web identity provider can be constructed from either the profile OR 100% from the environment
59-
StsWebIdentityProvider(platformProvider = platformProvider, httpClient = engine, region = region),
55+
LazilyInitializedCredentialsProvider("EnvironmentStsWebIdentityCredentialsProvider") {
56+
// STS web identity provider can be constructed from either the profile OR 100% from the environment
57+
StsWebIdentityCredentialsProvider.fromEnvironment(
58+
platformProvider = platformProvider,
59+
httpClient = httpClient,
60+
region = region,
61+
)
62+
},
6063
ProfileCredentialsProvider(profileName = profileName, platformProvider = platformProvider, httpClient = engine, region = region),
6164
EcsCredentialsProvider(platformProvider, engine),
6265
ImdsCredentialsProvider(
@@ -81,20 +84,3 @@ public class DefaultChainCredentialsProvider constructor(
8184
}
8285
}
8386
}
84-
85-
/**
86-
* Wrapper around [StsWebIdentityCredentialsProvider] that delays any exceptions until [resolve] is invoked.
87-
* This allows it to be part of the default chain and any failures result in the chain to move onto the next provider.
88-
*/
89-
public class StsWebIdentityProvider(
90-
private val platformProvider: PlatformProvider = PlatformProvider.System,
91-
private val httpClient: HttpClientEngine? = null,
92-
private val region: String? = null,
93-
) : CloseableCredentialsProvider {
94-
override suspend fun resolve(attributes: Attributes): Credentials {
95-
val wrapped = StsWebIdentityCredentialsProvider.fromEnvironment(platformProvider = platformProvider, httpClient = httpClient, region = region)
96-
return wrapped.resolve(attributes)
97-
}
98-
99-
override fun close() { }
100-
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package aws.sdk.kotlin.runtime.auth.credentials
2+
3+
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
4+
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider
5+
import aws.smithy.kotlin.runtime.collections.Attributes
6+
7+
/**
8+
* A [CredentialsProvider] implementation that delays the initialization of the underlying provider until
9+
* the first call to [resolve]. This is useful when the initialization of the credentials provider is expensive
10+
* or should be deferred until credentials are actually needed.
11+
*
12+
* @param providerName The name of the credentials provider that is being wrapped.
13+
* @param initializer A lambda function that provides the actual [CredentialsProvider] to be initialized lazily.
14+
*/
15+
public class LazilyInitializedCredentialsProvider(
16+
override val providerName: String? = null,
17+
initializer: () -> CredentialsProvider,
18+
) : CredentialsProvider {
19+
private val provider = lazy(initializer)
20+
21+
override suspend fun resolve(attributes: Attributes): Credentials = provider.value.resolve(attributes)
22+
}

0 commit comments

Comments
 (0)