Skip to content

Commit d37ac94

Browse files
Allow user to set policy and policy_arns in WebIdentityTokenCredentialsProvider builder (#3506)
Related PR: #1892 ## Motivation and Context This change allows users to define inline IAM policies and/or set predefined policies (using their ARNs) with `WebIdentityTokenCredentialsProvider` ## Description Adds `policy` and `policy_arns` to `WebIdentityTokenCredentialsProvider` builder. ## Testing ## Checklist <!--- If a checkbox below is not applicable, then please DELETE it rather than leaving it unchecked --> - [x] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or runtime crates - [x] I have updated `CHANGELOG.next.toml` if I made changes to the AWS SDK, generated SDK code, or SDK runtime crates ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._ --------- Co-authored-by: ysaito1001 <awsaito@amazon.com>
1 parent 88405d6 commit d37ac94

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

CHANGELOG.next.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
# meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client | server | all"}
1212
# author = "rcoh"
1313

14+
[[aws-sdk-rust]]
15+
message = "Ability to add an inline policy or a list of policy ARNs to the `WebIdentityTokenCredentialsProvider` builder."
16+
references = ["smithy-rs#3506"]
17+
meta = { "breaking" = false, "tada" = true, "bug" = false }
18+
author = "mokhaled2992"
19+
1420
[[aws-sdk-rust]]
1521
message = "Make `BehaviorVersion` be future-proof by disallowing it to be constructed via the `BehaviorVersion {}` syntax."
1622
references = ["aws-sdk-rust#1111", "smithy-rs#3513"]

aws/rust-runtime/aws-config/src/web_identity_token.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@
6464
use crate::provider_config::ProviderConfig;
6565
use crate::sts;
6666
use aws_credential_types::provider::{self, error::CredentialsError, future, ProvideCredentials};
67-
use aws_sdk_sts::Client as StsClient;
67+
use aws_sdk_sts::{types::PolicyDescriptorType, Client as StsClient};
6868
use aws_smithy_async::time::SharedTimeSource;
6969
use aws_smithy_types::error::display::DisplayErrorContext;
7070
use aws_types::os_shim_internal::{Env, Fs};
71+
7172
use std::borrow::Cow;
7273
use std::path::{Path, PathBuf};
7374

@@ -84,6 +85,8 @@ pub struct WebIdentityTokenCredentialsProvider {
8485
time_source: SharedTimeSource,
8586
fs: Fs,
8687
sts_client: StsClient,
88+
policy: Option<String>,
89+
policy_arns: Option<Vec<PolicyDescriptorType>>,
8790
}
8891

8992
impl WebIdentityTokenCredentialsProvider {
@@ -150,6 +153,8 @@ impl WebIdentityTokenCredentialsProvider {
150153
load_credentials(
151154
&self.fs,
152155
&self.sts_client,
156+
self.policy.clone(),
157+
self.policy_arns.clone(),
153158
&conf.web_identity_token_file,
154159
&conf.role_arn,
155160
&conf.session_name,
@@ -163,6 +168,8 @@ impl WebIdentityTokenCredentialsProvider {
163168
pub struct Builder {
164169
source: Option<Source>,
165170
config: Option<ProviderConfig>,
171+
policy: Option<String>,
172+
policy_arns: Option<Vec<PolicyDescriptorType>>,
166173
}
167174

168175
impl Builder {
@@ -193,6 +200,31 @@ impl Builder {
193200
self
194201
}
195202

203+
/// Set an IAM policy in JSON format that you want to use as an inline session policy.
204+
///
205+
/// This parameter is optional
206+
/// For more information, see
207+
/// [policy](aws_sdk_sts::operation::assume_role::builders::AssumeRoleInputBuilder::policy_arns)
208+
pub fn policy(mut self, policy: impl Into<String>) -> Self {
209+
self.policy = Some(policy.into());
210+
self
211+
}
212+
213+
/// Set the Amazon Resource Names (ARNs) of the IAM managed policies that you want to use as managed session policies.
214+
///
215+
/// This parameter is optional.
216+
/// For more information, see
217+
/// [policy_arns](aws_sdk_sts::operation::assume_role::builders::AssumeRoleInputBuilder::policy_arns)
218+
pub fn policy_arns(mut self, policy_arns: Vec<String>) -> Self {
219+
self.policy_arns = Some(
220+
policy_arns
221+
.into_iter()
222+
.map(|arn| PolicyDescriptorType::builder().arn(arn).build())
223+
.collect::<Vec<_>>(),
224+
);
225+
self
226+
}
227+
196228
/// Build a [`WebIdentityTokenCredentialsProvider`]
197229
///
198230
/// ## Panics
@@ -206,13 +238,17 @@ impl Builder {
206238
fs: conf.fs(),
207239
sts_client: StsClient::new(&conf.client_config()),
208240
time_source: conf.time_source(),
241+
policy: self.policy,
242+
policy_arns: self.policy_arns,
209243
}
210244
}
211245
}
212246

213247
async fn load_credentials(
214248
fs: &Fs,
215249
sts_client: &StsClient,
250+
policy: Option<String>,
251+
policy_arns: Option<Vec<PolicyDescriptorType>>,
216252
token_file: impl AsRef<Path>,
217253
role_arn: &str,
218254
session_name: &str,
@@ -228,6 +264,8 @@ async fn load_credentials(
228264
let resp = sts_client.assume_role_with_web_identity()
229265
.role_arn(role_arn)
230266
.role_session_name(session_name)
267+
.set_policy(policy)
268+
.set_policy_arns(policy_arns)
231269
.web_identity_token(token)
232270
.send()
233271
.await

0 commit comments

Comments
 (0)