Skip to content

Commit cbfbfa1

Browse files
authored
fix: load IRSA web identity config for S3 (#1649)
Addressed with a smaller invariant-based implementation. Static S3 credentials and IRSA web identity values are now validated as pairs fixes #1646
1 parent 829abd5 commit cbfbfa1

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

src/storage/s3.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ use super::{
6969
// in bytes
7070
// const MULTIPART_UPLOAD_SIZE: usize = 1024 * 1024 * 100;
7171
const AWS_CONTAINER_CREDENTIALS_RELATIVE_URI: &str = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI";
72+
const AWS_WEB_IDENTITY_TOKEN_FILE: &str = "AWS_WEB_IDENTITY_TOKEN_FILE";
73+
const AWS_ROLE_ARN: &str = "AWS_ROLE_ARN";
74+
const AWS_ROLE_SESSION_NAME: &str = "AWS_ROLE_SESSION_NAME";
7275

7376
#[derive(Debug, Clone, clap::Args)]
7477
#[command(
@@ -261,12 +264,35 @@ impl S3Config {
261264
builder = builder.with_checksum_algorithm(Checksum::SHA256)
262265
}
263266

267+
assert!(
268+
self.access_key_id.is_some() == self.secret_key.is_some(),
269+
"P_S3_ACCESS_KEY and P_S3_SECRET_KEY must be set together"
270+
);
271+
264272
if let Some((access_key, secret_key)) =
265273
self.access_key_id.as_ref().zip(self.secret_key.as_ref())
266274
{
267275
builder = builder
268276
.with_access_key_id(access_key)
269277
.with_secret_access_key(secret_key);
278+
} else {
279+
let token_file = std::env::var(AWS_WEB_IDENTITY_TOKEN_FILE).ok();
280+
let role_arn = std::env::var(AWS_ROLE_ARN).ok();
281+
282+
assert!(
283+
token_file.is_some() == role_arn.is_some(),
284+
"{AWS_WEB_IDENTITY_TOKEN_FILE} and {AWS_ROLE_ARN} must be set together"
285+
);
286+
287+
if let Some((token_file, role_arn)) = token_file.zip(role_arn) {
288+
builder = builder
289+
.with_config(AmazonS3ConfigKey::WebIdentityTokenFile, token_file)
290+
.with_config(AmazonS3ConfigKey::RoleArn, role_arn);
291+
292+
if let Ok(session_name) = std::env::var(AWS_ROLE_SESSION_NAME) {
293+
builder = builder.with_config(AmazonS3ConfigKey::RoleSessionName, session_name);
294+
}
295+
}
270296
}
271297

272298
if let Some(ssec_encryption_key) = &self.ssec_encryption_key {

0 commit comments

Comments
 (0)