-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
NoCredentialsCache
that offers no caching ability (#2720)
## Motivation and Context Related to awslabs/aws-sdk-rust#809 ## Description It has been discovered that when `AssumeRoleProvider` is used, the Rust SDK emits `credentials cache miss occurred` twice per request. The reason why that log is shown twice is illustrated in the following diagram: ![Screenshot 2023-05-19 at 4 10 20 PM](https://github.com/awslabs/smithy-rs/assets/15333866/c6cce018-c821-4b46-8d47-b414af7b4d1e) One of the cache miss messages is due to the fact `AssumeRoleProvider` internally uses an STS client, which, in turn, is wrapped by a `LazyCredentialsCache` by default. However, that use of `LazyCredentialsCache` is pointless because caching is already in effect with the outermost `LazyCredentialsCache`. This PR adds a new kind of `CredentialsCache`, `NoCredentialsCache`. As its name suggests, it simplify delegates `provide_cached_credentials` to the underlying provider's `provide_credentials` with no caching functionality. We then update `SsoCredentialsProvider`, `AssumeRoleProvider`, and `WebIdentityTokenCredentialsProvider` to use `NoCredentialsCache` for their STS clients so the logs won't show `credentials cache miss occurred` twice per request. ## Testing - Added unit tests for `NoCredentialsCache` - Updated unit test for `AssumeRoleProvider` to verify `NoCredentialsCache` is used by default ## Checklist - [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: Yuki Saito <[email protected]>
- Loading branch information
1 parent
bbe9d52
commit 7ccac06
Showing
6 changed files
with
157 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
aws/rust-runtime/aws-credential-types/src/cache/no_caching.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
//! Credentials cache that offers no caching ability | ||
use crate::cache::ProvideCachedCredentials; | ||
use crate::provider::SharedCredentialsProvider; | ||
use crate::provider::{future, ProvideCredentials}; | ||
use tracing::debug; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct NoCredentialsCache { | ||
provider: SharedCredentialsProvider, | ||
} | ||
|
||
impl NoCredentialsCache { | ||
pub(crate) fn new(provider: SharedCredentialsProvider) -> Self { | ||
Self { provider } | ||
} | ||
} | ||
|
||
impl ProvideCachedCredentials for NoCredentialsCache { | ||
fn provide_cached_credentials<'a>(&'a self) -> future::ProvideCredentials<'_> | ||
where | ||
Self: 'a, | ||
{ | ||
debug!("Delegating `provide_cached_credentials` to `provide_credentials` on the provider"); | ||
self.provider.provide_credentials() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::credential_fn::provide_credentials_fn; | ||
use crate::Credentials; | ||
use std::sync::{Arc, Mutex}; | ||
use std::time::{Duration, SystemTime}; | ||
|
||
fn test_provider(load_list: Vec<crate::provider::Result>) -> NoCredentialsCache { | ||
let load_list = Arc::new(Mutex::new(load_list)); | ||
NoCredentialsCache::new(SharedCredentialsProvider::new(provide_credentials_fn( | ||
move || { | ||
let list = load_list.clone(); | ||
async move { | ||
let next = list.lock().unwrap().remove(0); | ||
next | ||
} | ||
}, | ||
))) | ||
} | ||
|
||
fn epoch_secs(secs: u64) -> SystemTime { | ||
SystemTime::UNIX_EPOCH + Duration::from_secs(secs) | ||
} | ||
|
||
fn credentials(expired_secs: u64) -> Credentials { | ||
Credentials::new("test", "test", None, Some(epoch_secs(expired_secs)), "test") | ||
} | ||
|
||
async fn expect_creds(expired_secs: u64, provider: &NoCredentialsCache) { | ||
let creds = provider | ||
.provide_cached_credentials() | ||
.await | ||
.expect("expected credentials"); | ||
assert_eq!(Some(epoch_secs(expired_secs)), creds.expiry()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn no_caching() { | ||
let credentials_cache = test_provider(vec![ | ||
Ok(credentials(1000)), | ||
Ok(credentials(2000)), | ||
Ok(credentials(3000)), | ||
]); | ||
|
||
expect_creds(1000, &credentials_cache).await; | ||
expect_creds(2000, &credentials_cache).await; | ||
expect_creds(3000, &credentials_cache).await; | ||
} | ||
} |