Skip to content

Commit d7ba2b9

Browse files
committed
Refactor CREDENTIALS_ENV_VARS_STS_WEB_ID_TOKEN business metrics out of StsWebIdentityCredentialsProvider
1 parent 1a51409 commit d7ba2b9

File tree

4 files changed

+26
-63
lines changed

4 files changed

+26
-63
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/InvalidSsoTokenExcept
9898
}
9999

100100
public final class aws/sdk/kotlin/runtime/auth/credentials/LazilyInitializedCredentialsProvider : aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider {
101-
public fun <init> (Ljava/lang/String;Lkotlin/jvm/functions/Function0;)V
102-
public synthetic fun <init> (Ljava/lang/String;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
101+
public fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/operation/ExecutionContext;Lkotlin/jvm/functions/Function0;)V
102+
public synthetic fun <init> (Ljava/lang/String;Laws/smithy/kotlin/runtime/operation/ExecutionContext;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
103103
public fun resolve (Laws/smithy/kotlin/runtime/collections/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
104104
public fun toString ()Ljava/lang/String;
105105
}
@@ -203,7 +203,6 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/StsWebIdentityCredent
203203
public synthetic fun <init> (Laws/sdk/kotlin/runtime/auth/credentials/AssumeRoleWithWebIdentityParameters;Ljava/lang/String;Laws/smithy/kotlin/runtime/util/PlatformProvider;Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
204204
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;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
205205
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
206-
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;ZLkotlin/jvm/internal/DefaultConstructorMarker;)V
207206
public final fun getHttpClient ()Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;
208207
public final fun getPlatformProvider ()Laws/smithy/kotlin/runtime/util/PlatformProvider;
209208
public final fun getRegion ()Ljava/lang/String;

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ 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.sdk.kotlin.runtime.http.interceptors.AwsBusinessMetric
1011
import aws.smithy.kotlin.runtime.auth.awscredentials.*
12+
import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics
1113
import aws.smithy.kotlin.runtime.collections.Attributes
12-
import aws.smithy.kotlin.runtime.collections.get
1314
import aws.smithy.kotlin.runtime.http.engine.DefaultHttpEngine
1415
import aws.smithy.kotlin.runtime.http.engine.HttpClientEngine
1516
import aws.smithy.kotlin.runtime.io.Closeable
1617
import aws.smithy.kotlin.runtime.io.closeIfCloseable
18+
import aws.smithy.kotlin.runtime.operation.ExecutionContext
1719
import aws.smithy.kotlin.runtime.util.PlatformProvider
1820

1921
/**
@@ -53,7 +55,12 @@ public class DefaultChainCredentialsProvider constructor(
5355
private val chain = CredentialsProviderChain(
5456
SystemPropertyCredentialsProvider(platformProvider::getProperty),
5557
EnvironmentCredentialsProvider(platformProvider::getenv),
56-
LazilyInitializedCredentialsProvider("EnvironmentStsWebIdentityCredentialsProvider") {
58+
LazilyInitializedCredentialsProvider(
59+
"EnvironmentStsWebIdentityCredentialsProvider",
60+
ExecutionContext.build {
61+
attributes[BusinessMetrics] = mutableSetOf(AwsBusinessMetric.Credentials.CREDENTIALS_ENV_VARS_STS_WEB_ID_TOKEN.identifier)
62+
},
63+
) {
5764
// STS web identity provider can be constructed from either the profile OR 100% from the environment
5865
StsWebIdentityCredentialsProvider.fromEnvironment(
5966
platformProvider = platformProvider,

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,36 @@ package aws.sdk.kotlin.runtime.auth.credentials
22

33
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
44
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider
5+
import aws.smithy.kotlin.runtime.businessmetrics.BusinessMetrics
6+
import aws.smithy.kotlin.runtime.businessmetrics.mergeBusinessMetrics
57
import aws.smithy.kotlin.runtime.collections.Attributes
8+
import aws.smithy.kotlin.runtime.collections.MutableAttributes
9+
import aws.smithy.kotlin.runtime.collections.mergeExcept
10+
import aws.smithy.kotlin.runtime.operation.ExecutionContext
611

712
/**
813
* A [CredentialsProvider] implementation that delays the initialization of the underlying provider until
914
* the first call to [resolve]. This is useful when the initialization of the credentials provider is expensive
1015
* or should be deferred until credentials are actually needed.
1116
*
1217
* @param providerName The name of the credentials provider that is being wrapped. Will default to "LazilyInitializedCredentialsProvider".
18+
* @param executionContext Additional execution context to use when resolving credentials. Will default to an empty execution context.
1319
* @param initializer A lambda function that provides the actual [CredentialsProvider] to be initialized lazily.
1420
*/
1521
public class LazilyInitializedCredentialsProvider(
1622
private val providerName: String = "LazilyInitializedCredentialsProvider",
23+
private val executionContext: ExecutionContext = ExecutionContext(),
1724
initializer: () -> CredentialsProvider,
1825
) : CredentialsProvider {
1926
private val provider = lazy(initializer)
2027

21-
override suspend fun resolve(attributes: Attributes): Credentials = provider.value.resolve(attributes)
28+
override suspend fun resolve(attributes: Attributes): Credentials {
29+
if (attributes is MutableAttributes) {
30+
attributes.mergeExcept(executionContext, exceptions = setOf(BusinessMetrics))
31+
attributes.mergeBusinessMetrics(executionContext)
32+
}
33+
return provider.value.resolve(attributes)
34+
}
2235

2336
override fun toString(): String = providerName
2437
}

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

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ public class StsWebIdentityCredentialsProvider(
4444
public val platformProvider: PlatformProvider = PlatformProvider.System,
4545
public val httpClient: HttpClientEngine? = null,
4646
) : CredentialsProvider {
47-
/**
48-
* Indicates if the class was created using [fromEnvironment]
49-
*/
50-
private var createdFromEnvironment: Boolean = false
5147

5248
/**
5349
* A [CredentialsProvider] that exchanges a Web Identity Token for credentials from the AWS Security Token Service
@@ -85,46 +81,6 @@ public class StsWebIdentityCredentialsProvider(
8581
httpClient,
8682
)
8783

88-
/**
89-
* A [CredentialsProvider] that exchanges a Web Identity Token for credentials from the AWS Security Token Service
90-
* (STS).
91-
*
92-
* @param roleArn The ARN of the target role to assume, e.g. `arn:aws:iam:123456789:role/example`
93-
* @param webIdentityTokenFilePath The path to the file containing a JWT token
94-
* @param region The AWS region to assume the role in
95-
* @param roleSessionName The name to associate with the session. Use the role session name to uniquely identify a
96-
* session when the same role is assumed by different principals or for different reasons. In cross-account
97-
* scenarios, the role session name is visible to, and can be logged by the account that owns the role. The role
98-
* session name is also in the ARN of the assumed role principal.
99-
* @param duration The expiry duration of the credentials. Defaults to 15 minutes if not set.
100-
* @param platformProvider The platform API provider
101-
* @param httpClient the [HttpClientEngine] instance to use to make requests. NOTE: This engine's resources and
102-
* lifetime are NOT managed by the provider. Caller is responsible for closing.
103-
* @param createdFromEnvironment If the [StsWebIdentityCredentialsProvider] was created using [fromEnvironment].
104-
*/
105-
private constructor(
106-
roleArn: String,
107-
webIdentityTokenFilePath: String,
108-
region: String?,
109-
roleSessionName: String? = null,
110-
duration: Duration = DEFAULT_CREDENTIALS_REFRESH_SECONDS.seconds,
111-
platformProvider: PlatformProvider = PlatformProvider.System,
112-
httpClient: HttpClientEngine? = null,
113-
createdFromEnvironment: Boolean,
114-
) : this(
115-
AssumeRoleWithWebIdentityParameters(
116-
roleArn = roleArn,
117-
webIdentityTokenFilePath = webIdentityTokenFilePath,
118-
roleSessionName = roleSessionName,
119-
duration = duration,
120-
),
121-
region,
122-
platformProvider,
123-
httpClient,
124-
) {
125-
this.createdFromEnvironment = createdFromEnvironment
126-
}
127-
12884
public companion object {
12985
/**
13086
* Create an [StsWebIdentityCredentialsProvider] from the current execution environment. This will attempt
@@ -143,26 +99,14 @@ public class StsWebIdentityCredentialsProvider(
14399
val resolvedRoleArn = platformProvider.resolve(roleArn, AwsSdkSetting.AwsRoleArn, "roleArn")
144100
val resolvedTokenFilePath = platformProvider.resolve(webIdentityTokenFilePath, AwsSdkSetting.AwsWebIdentityTokenFile, "webIdentityTokenFilePath")
145101
val resolvedRegion = region ?: AwsSdkSetting.AwsRegion.resolve(platformProvider)
146-
return StsWebIdentityCredentialsProvider(
147-
resolvedRoleArn,
148-
resolvedTokenFilePath,
149-
resolvedRegion,
150-
roleSessionName,
151-
duration,
152-
platformProvider,
153-
httpClient,
154-
createdFromEnvironment = true,
155-
)
102+
return StsWebIdentityCredentialsProvider(resolvedRoleArn, resolvedTokenFilePath, resolvedRegion, roleSessionName, duration, platformProvider, httpClient)
156103
}
157104
}
158105

159106
override suspend fun resolve(attributes: Attributes): Credentials {
160107
val logger = coroutineContext.logger<StsAssumeRoleCredentialsProvider>()
161108
logger.debug { "retrieving assumed credentials via web identity" }
162109

163-
if (createdFromEnvironment) {
164-
attributes.emitBusinessMetric(AwsBusinessMetric.Credentials.CREDENTIALS_ENV_VARS_STS_WEB_ID_TOKEN)
165-
}
166110
attributes.emitBusinessMetric(AwsBusinessMetric.Credentials.CREDENTIALS_STS_ASSUME_ROLE_WEB_ID)
167111

168112
val provider = this

0 commit comments

Comments
 (0)