manuzhang commented on code in PR #2396:
URL: https://github.com/apache/iceberg-rust/pull/2396#discussion_r3600348815


##########
crates/integrations/aws/src/config.rs:
##########
@@ -0,0 +1,449 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! AWS SDK configuration utilities.
+
+use std::collections::HashMap;
+use std::time::{Duration, SystemTime, UNIX_EPOCH};
+
+use aws_config::sts::AssumeRoleProvider;
+use aws_config::{BehaviorVersion, Region, SdkConfig};
+use aws_credential_types::Credentials as AwsSdkCredentials;
+use aws_credential_types::provider::{
+    ProvideCredentials, SharedCredentialsProvider, future as 
credentials_future,
+};
+use aws_sdk_sts::config::Credentials;
+use iceberg::io::{
+    S3_ACCESS_KEY_ID, S3_ASSUME_ROLE_ARN, S3_ASSUME_ROLE_EXTERNAL_ID, 
S3_ASSUME_ROLE_SESSION_NAME,
+    S3_ENDPOINT, S3_REGION, S3_SECRET_ACCESS_KEY, S3_SESSION_TOKEN,
+};
+use reqsign_aws_v4::Credential as AwsCredential;
+use reqsign_core::time::Timestamp;
+use reqsign_core::{Context, Error as ReqsignError, ProvideCredential};
+
+use crate::{
+    AWS_ACCESS_KEY_ID, AWS_ASSUME_ROLE_ARN, AWS_ASSUME_ROLE_EXTERNAL_ID,
+    AWS_ASSUME_ROLE_SESSION_NAME, AWS_PROFILE_NAME, AWS_REGION_NAME, 
AWS_SECRET_ACCESS_KEY,
+    AWS_SESSION_TOKEN,
+};
+
+/// Creates an AWS SDK configuration from catalog properties.
+///
+/// When `client.assume-role.arn` is set, the configured base credentials,
+/// profile, region, and runtime settings are used for the STS request. The
+/// returned configuration uses the refreshable assumed-role provider.
+pub async fn create_sdk_config(
+    properties: &HashMap<String, String>,
+    endpoint_uri: Option<&str>,
+    default_session_name: &str,
+) -> SdkConfig {
+    let mut loader = aws_config::defaults(BehaviorVersion::latest());
+
+    if let Some(endpoint) = endpoint_uri {
+        loader = loader.endpoint_url(endpoint);
+    }
+
+    if let (Some(access_key), Some(secret_key)) = (
+        properties.get(AWS_ACCESS_KEY_ID),
+        properties.get(AWS_SECRET_ACCESS_KEY),
+    ) {
+        let session_token = properties.get(AWS_SESSION_TOKEN).cloned();
+        let credentials_provider =
+            Credentials::new(access_key, secret_key, session_token, None, 
"properties");
+        loader = loader.credentials_provider(credentials_provider);
+    }
+
+    if let Some(profile_name) = properties.get(AWS_PROFILE_NAME) {
+        loader = loader.profile_name(profile_name);
+    }
+
+    if let Some(region_name) = properties.get(AWS_REGION_NAME) {
+        loader = loader.region(Region::new(region_name.clone()));
+    }
+
+    let base_config = loader.load().await;
+    let Some(role_arn) = properties.get(AWS_ASSUME_ROLE_ARN) else {
+        return base_config;
+    };
+
+    let session_name = properties
+        .get(AWS_ASSUME_ROLE_SESSION_NAME)
+        .map(String::as_str)
+        .unwrap_or(default_session_name);
+    let mut assume_role_builder = AssumeRoleProvider::builder(role_arn)
+        .session_name(session_name)
+        .configure(&base_config);
+
+    if let Some(external_id) = properties.get(AWS_ASSUME_ROLE_EXTERNAL_ID) {
+        assume_role_builder = assume_role_builder.external_id(external_id);
+    }
+
+    let assume_role_provider = 
SharedCredentialsProvider::new(RefreshingCredentialsProvider {
+        inner: assume_role_builder.build().await,
+        cached: tokio::sync::Mutex::new(None),
+    });
+    base_config
+        .into_builder()
+        .credentials_provider(assume_role_provider)
+        .build()
+}
+
+#[derive(Debug)]
+struct RefreshingCredentialsProvider {
+    inner: AssumeRoleProvider,
+    cached: tokio::sync::Mutex<Option<AwsSdkCredentials>>,
+}
+
+impl RefreshingCredentialsProvider {
+    async fn credentials(&self) -> aws_credential_types::provider::Result {
+        let mut cached = self.cached.lock().await;

Review Comment:
   This rejects cached credentials during their final five minutes and 
immediately returns any STS refresh error. During a transient STS outage, 
FileIO stops working even though AWS would still accept the cached credentials. 
If refresh fails, return the cached credential while its actual expiry remains 
in the future.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to