Skip to content

Commit 031a25e

Browse files
authored
feat: add more parameters for fetching STS credentials (#1035)
1 parent 2d9aedd commit 031a25e

File tree

8 files changed

+382
-86
lines changed

8 files changed

+382
-86
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "72d2bb9a-8154-47b8-8b2d-b4af66b90ba7",
3+
"type": "feature",
4+
"description": "Add more parameters for fetching STS credentials",
5+
"issues": [
6+
"awslabs/aws-sdk-kotlin#1000"
7+
]
8+
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
public final class aws/sdk/kotlin/runtime/auth/credentials/AssumeRoleParameters {
2+
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/util/List;Ljava/lang/String;Ljava/util/Map;Ljava/util/List;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
3+
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/util/List;Ljava/lang/String;Ljava/util/Map;Ljava/util/List;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
4+
public final fun getDuration-UwyO8pc ()J
5+
public final fun getExternalId ()Ljava/lang/String;
6+
public final fun getPolicy ()Ljava/lang/String;
7+
public final fun getPolicyArns ()Ljava/util/List;
8+
public final fun getRoleArn ()Ljava/lang/String;
9+
public final fun getRoleSessionName ()Ljava/lang/String;
10+
public final fun getSerialNumber ()Ljava/lang/String;
11+
public final fun getSourceIdentity ()Ljava/lang/String;
12+
public final fun getTags ()Ljava/util/Map;
13+
public final fun getTokenCode ()Ljava/lang/String;
14+
public final fun getTransitiveTagKeys ()Ljava/util/List;
15+
}
16+
17+
public final class aws/sdk/kotlin/runtime/auth/credentials/AssumeRoleWithWebIdentityParameters {
18+
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/util/List;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
19+
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/util/List;Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
20+
public final fun getDuration-UwyO8pc ()J
21+
public final fun getPolicy ()Ljava/lang/String;
22+
public final fun getPolicyArns ()Ljava/util/List;
23+
public final fun getProviderId ()Ljava/lang/String;
24+
public final fun getRoleArn ()Ljava/lang/String;
25+
public final fun getRoleSessionName ()Ljava/lang/String;
26+
public final fun getWebIdentityTokenFilePath ()Ljava/lang/String;
27+
}
28+
129
public final class aws/sdk/kotlin/runtime/auth/credentials/CredentialsNotLoadedException : aws/sdk/kotlin/runtime/ClientException {
230
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
331
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
@@ -112,13 +140,17 @@ public final class aws/sdk/kotlin/runtime/auth/credentials/StaticCredentialsProv
112140
}
113141

114142
public final class aws/sdk/kotlin/runtime/auth/credentials/StsAssumeRoleCredentialsProvider : aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider {
143+
public fun <init> (Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;Laws/sdk/kotlin/runtime/auth/credentials/AssumeRoleParameters;Ljava/lang/String;Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;)V
144+
public synthetic fun <init> (Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;Laws/sdk/kotlin/runtime/auth/credentials/AssumeRoleParameters;Ljava/lang/String;Laws/smithy/kotlin/runtime/http/engine/HttpClientEngine;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
115145
public synthetic fun <init> (Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLaws/smithy/kotlin/runtime/http/engine/HttpClientEngine;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
116146
public synthetic fun <init> (Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLaws/smithy/kotlin/runtime/http/engine/HttpClientEngine;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
117147
public fun resolve (Laws/smithy/kotlin/runtime/util/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
118148
}
119149

120150
public final class aws/sdk/kotlin/runtime/auth/credentials/StsWebIdentityCredentialsProvider : aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider {
121151
public static final field Companion Laws/sdk/kotlin/runtime/auth/credentials/StsWebIdentityCredentialsProvider$Companion;
152+
public 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;)V
153+
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
122154
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
123155
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
124156
public fun resolve (Laws/smithy/kotlin/runtime/util/Attributes;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;

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

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

88
import aws.sdk.kotlin.runtime.auth.credentials.internal.sts.StsClient
99
import aws.sdk.kotlin.runtime.auth.credentials.internal.sts.assumeRole
10+
import aws.sdk.kotlin.runtime.auth.credentials.internal.sts.model.PolicyDescriptorType
1011
import aws.sdk.kotlin.runtime.auth.credentials.internal.sts.model.RegionDisabledException
12+
import aws.sdk.kotlin.runtime.auth.credentials.internal.sts.model.Tag
1113
import aws.sdk.kotlin.runtime.config.AwsSdkSetting
1214
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
1315
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider
@@ -37,29 +39,58 @@ private const val PROVIDER_NAME = "AssumeRoleProvider"
3739
* to get AWS credentials for STS. Then, it will call STS to get assumed credentials for the desired role.
3840
*
3941
* @param credentialsProvider The underlying provider to use for source credentials
40-
* @param roleArn The ARN of the target role to assume, e.g. `arn:aws:iam:123456789:role/example`
42+
* @param assumeRoleParameters The parameters to pass to the `AssumeRole` call
4143
* @param region The AWS region to assume the role in. If not set then the global STS endpoint will be used.
42-
* @param roleSessionName The name to associate with the session. Use the role session name to uniquely identify a session
43-
* when the same role is assumed by different principals or for different reasons. In cross-account scenarios, the
44-
* role session name is visible to, and can be logged by the account that owns the role. The role session name is also
45-
* in the ARN of the assumed role principal.
46-
* @param externalId A unique identifier that might be required when you assume a role in another account. If the
47-
* administrator of the account to which the role belongs provided you with an external ID, then provide that value
48-
* in this parameter.
49-
* @param duration The expiry duration of the STS credentials. Defaults to 15 minutes if not set.
5044
* @param httpClient the [HttpClientEngine] instance to use to make requests. NOTE: This engine's resources and lifetime
5145
* are NOT managed by the provider. Caller is responsible for closing.
5246
*/
5347
public class StsAssumeRoleCredentialsProvider(
5448
private val credentialsProvider: CredentialsProvider,
55-
private val roleArn: String,
49+
private val assumeRoleParameters: AssumeRoleParameters,
5650
private val region: String? = null,
57-
private val roleSessionName: String? = null,
58-
private val externalId: String? = null,
59-
private val duration: Duration = DEFAULT_CREDENTIALS_REFRESH_SECONDS.seconds,
6051
private val httpClient: HttpClientEngine? = null,
6152
) : CredentialsProvider {
6253

54+
/**
55+
* A [CredentialsProvider] that uses another provider to assume a role from the AWS Security Token Service (STS).
56+
*
57+
* When asked to provide credentials, this provider will first invoke the inner credentials provider
58+
* to get AWS credentials for STS. Then, it will call STS to get assumed credentials for the desired role.
59+
*
60+
* @param credentialsProvider The underlying provider to use for source credentials
61+
* @param roleArn The ARN of the target role to assume, e.g. `arn:aws:iam:123456789:role/example`
62+
* @param region The AWS region to assume the role in. If not set then the global STS endpoint will be used.
63+
* @param roleSessionName The name to associate with the session. Use the role session name to uniquely identify a
64+
* session when the same role is assumed by different principals or for different reasons. In cross-account
65+
* scenarios, the role session name is visible to, and can be logged by the account that owns the role. The role
66+
* session name is also in the ARN of the assumed role principal.
67+
* @param externalId A unique identifier that might be required when you assume a role in another account. If the
68+
* administrator of the account to which the role belongs provided you with an external ID, then provide that value
69+
* in this parameter.
70+
* @param duration The expiry duration of the STS credentials. Defaults to 15 minutes if not set.
71+
* @param httpClient the [HttpClientEngine] instance to use to make requests. NOTE: This engine's resources and
72+
* lifetime are NOT managed by the provider. Caller is responsible for closing.
73+
*/
74+
public constructor(
75+
credentialsProvider: CredentialsProvider,
76+
roleArn: String,
77+
region: String? = null,
78+
roleSessionName: String? = null,
79+
externalId: String? = null,
80+
duration: Duration = DEFAULT_CREDENTIALS_REFRESH_SECONDS.seconds,
81+
httpClient: HttpClientEngine? = null,
82+
) : this(
83+
credentialsProvider,
84+
AssumeRoleParameters(
85+
roleArn = roleArn,
86+
roleSessionName = roleSessionName,
87+
externalId = externalId,
88+
duration = duration,
89+
),
90+
region,
91+
httpClient,
92+
)
93+
6394
override suspend fun resolve(attributes: Attributes): Credentials {
6495
val logger = coroutineContext.logger<StsAssumeRoleCredentialsProvider>()
6596
logger.debug { "retrieving assumed credentials" }
@@ -76,10 +107,19 @@ public class StsAssumeRoleCredentialsProvider(
76107

77108
val resp = try {
78109
client.assumeRole {
79-
roleArn = provider.roleArn
80-
externalId = provider.externalId
81-
roleSessionName = provider.roleSessionName ?: defaultSessionName()
82-
durationSeconds = provider.duration.inWholeSeconds.toInt()
110+
val params = provider.assumeRoleParameters
111+
112+
roleArn = params.roleArn
113+
externalId = params.externalId
114+
roleSessionName = params.roleSessionName ?: defaultSessionName()
115+
durationSeconds = params.duration.inWholeSeconds.toInt()
116+
policyArns = params.convertedPolicyArns
117+
policy = params.policy
118+
tags = params.convertedTags
119+
transitiveTagKeys = params.transitiveTagKeys
120+
serialNumber = params.serialNumber
121+
tokenCode = params.tokenCode
122+
sourceIdentity = params.sourceIdentity
83123
}
84124
} catch (ex: Exception) {
85125
logger.debug { "sts refused to grant assumed role credentials" }
@@ -107,6 +147,44 @@ public class StsAssumeRoleCredentialsProvider(
107147
}
108148
}
109149

150+
/**
151+
* Parameters passed to an `AssumeRole` call
152+
* @param roleArn The ARN of the target role to assume, e.g. `arn:aws:iam:123456789:role/example`
153+
* @param roleSessionName The name to associate with the session. Use the role session name to uniquely identify a
154+
* session when the same role is assumed by different principals or for different reasons. In cross-account scenarios,
155+
* the role session name is visible to, and can be logged by the account that owns the role. The role session name is
156+
* also in the ARN of the assumed role principal.
157+
* @param externalId A unique identifier that might be required when you assume a role in another account. If the
158+
* administrator of the account to which the role belongs provided you with an external ID, then provide that value in
159+
* this parameter.
160+
* @param duration The expiry duration of the STS credentials. Defaults to 15 minutes if not set.
161+
* @param policyArns The Amazon Resource Names (ARNs) of the IAM managed policies that you want to use as managed
162+
* session policies
163+
* @param policy An IAM policy in JSON format that you want to use as an inline session policy
164+
* @param tags A list of session tags that you want to pass
165+
* @param transitiveTagKeys A list of keys for session tags that you want to set as transitive
166+
* @param serialNumber The identification number of the MFA device that is associated with the user who is making the
167+
* `AssumeRole` call
168+
* @param tokenCode The value provided by the MFA device, if the trust policy of the role being assumed requires MFA
169+
* @param sourceIdentity The source identity specified by the principal that is calling the `AssumeRole` operation
170+
*/
171+
public class AssumeRoleParameters(
172+
public val roleArn: String,
173+
public val roleSessionName: String? = null,
174+
public val externalId: String? = null,
175+
public val duration: Duration = DEFAULT_CREDENTIALS_REFRESH_SECONDS.seconds,
176+
public val policyArns: List<String>? = null,
177+
public val policy: String? = null,
178+
public val tags: Map<String, String>? = null,
179+
public val transitiveTagKeys: List<String>? = null,
180+
public val serialNumber: String? = null,
181+
public val tokenCode: String? = null,
182+
public val sourceIdentity: String? = null,
183+
) {
184+
internal val convertedPolicyArns = policyArns?.map { PolicyDescriptorType { arn = it } }
185+
internal val convertedTags = tags?.map { Tag { key = it.key; value = it.value } }
186+
}
187+
110188
// role session name must be provided to assume a role, when the user doesn't provide one we choose a name for them
111189
internal fun defaultSessionName(platformEnvironProvider: PlatformEnvironProvider = PlatformProvider.System): String =
112190
AwsSdkSetting.AwsRoleSessionName.resolve(platformEnvironProvider) ?: "aws-sdk-kotlin-${Instant.now().epochMilliseconds}"

0 commit comments

Comments
 (0)