Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid extending IMDS credentials expiry unconditionally #2694

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ message = "Implement `Ord` and `PartialOrd` for `DateTime`."
author = "henriiik"
references = ["smithy-rs#2653", "smithy-rs#2656"]
meta = { "breaking" = false, "tada" = false, "bug" = false }

[[aws-sdk-rust]]
message = "Avoid extending IMDS credentials' expiry unconditionally, which may incorrectly extend it beyond what is originally defined; If returned credentials are not stale, use them as they are."
references = ["smithy-rs#2687", "smithy-rs#2694"]
meta = { "breaking" = false, "tada" = false, "bug" = true }
author = "ysaito1001"
68 changes: 57 additions & 11 deletions aws/rust-runtime/aws-config/src/imds/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::fmt;
use std::sync::{Arc, RwLock};
use std::time::{Duration, SystemTime};

const CREDENTIAL_EXPIRATION_INTERVAL: Duration = Duration::from_secs(15 * 60);
const CREDENTIAL_EXPIRATION_INTERVAL: Duration = Duration::from_secs(10 * 60);

#[derive(Debug)]
struct ImdsCommunicationError {
Expand Down Expand Up @@ -192,21 +192,20 @@ impl ImdsCredentialsProvider {
//
// This allows continued use of the credentials even when IMDS returns expired ones.
fn maybe_extend_expiration(&self, expiration: SystemTime) -> SystemTime {
let now = self.time_source.now();
// If credentials from IMDS are not stale, use them as they are.
if now < expiration {
return expiration;
}

let rng = fastrand::Rng::with_seed(
self.time_source
.now()
.duration_since(SystemTime::UNIX_EPOCH)
now.duration_since(SystemTime::UNIX_EPOCH)
.expect("now should be after UNIX EPOCH")
.as_secs(),
);
// calculate credentials' refresh offset with jitter
let refresh_offset =
CREDENTIAL_EXPIRATION_INTERVAL + Duration::from_secs(rng.u64(120..=600));
let new_expiry = self.time_source.now() + refresh_offset;

if new_expiry < expiration {
return expiration;
}
let refresh_offset = CREDENTIAL_EXPIRATION_INTERVAL + Duration::from_secs(rng.u64(0..=300));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for changing the expiration interval and rng range?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per our spec, 15-minute is the minimum amount of time credentials are valid for. Setting refresh_offset to something longer than that may have the risk of the credentials expiring before the next refresh (especially when the credentials expiry is set to the very minimum of 15 minutes from now).

Now that we've added the early return, however, it may not matter much because valid credentials will be used as they are without the extension. But just wanted to make this inline with the spec.

Added comment in c265e86.

let new_expiry = now + refresh_offset;

tracing::warn!(
"Attempting credential expiration extension due to a credential service availability issue. \
Expand Down Expand Up @@ -342,6 +341,53 @@ mod test {
connection.assert_requests_match(&[]);
}

#[tokio::test]
#[traced_test]
async fn credentials_not_stale_should_be_used_as_they_are() {
let connection = TestConnection::new(vec![
(
token_request("http://169.254.169.254", 21600),
token_response(21600, TOKEN_A),
),
(
imds_request("http://169.254.169.254/latest/meta-data/iam/security-credentials/", TOKEN_A),
imds_response(r#"profile-name"#),
),
(
imds_request("http://169.254.169.254/latest/meta-data/iam/security-credentials/profile-name", TOKEN_A),
imds_response("{\n \"Code\" : \"Success\",\n \"LastUpdated\" : \"2021-09-20T21:42:26Z\",\n \"Type\" : \"AWS-HMAC\",\n \"AccessKeyId\" : \"ASIARTEST\",\n \"SecretAccessKey\" : \"testsecret\",\n \"Token\" : \"testtoken\",\n \"Expiration\" : \"2021-09-21T04:16:53Z\"\n}"),
),
]);

// set to 2021-09-21T04:16:50Z that makes returned credentials' expiry (2021-09-21T04:16:53Z)
// not stale
let time_of_request_to_fetch_credentials = UNIX_EPOCH + Duration::from_secs(1632197810);
let time_source = TimeSource::testing(&TestingTimeSource::new(
time_of_request_to_fetch_credentials,
));

tokio::time::pause();

let provider_config = ProviderConfig::no_configuration()
.with_http_connector(DynConnector::new(connection.clone()))
.with_time_source(time_source)
.with_sleep(TokioSleep::new());
let client = crate::imds::Client::builder()
.configure(&provider_config)
.build()
.await
.expect("valid client");
let provider = ImdsCredentialsProvider::builder()
.configure(&provider_config)
.imds_client(client)
.build();
let creds = provider.provide_credentials().await.expect("valid creds");
assert!(creds.expiry().unwrap() > time_of_request_to_fetch_credentials);
ysaito1001 marked this conversation as resolved.
Show resolved Hide resolved
connection.assert_requests_match(&[]);

// There should not be logs indicating credentials are extended for stability.
assert!(!logs_contain("Attempting credential expiration extension"));
ysaito1001 marked this conversation as resolved.
Show resolved Hide resolved
}
#[tokio::test]
#[traced_test]
async fn expired_credentials_should_be_extended() {
Expand Down