Skip to content

Commit

Permalink
Merge branch 'main' into msrv-1.63.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Velfi authored Jan 19, 2023
2 parents f5b24ee + 10e915b commit b8510db
Show file tree
Hide file tree
Showing 30 changed files with 142 additions and 107 deletions.
19 changes: 8 additions & 11 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ After:
```rust
use aws_credential_types::cache::CredentialsCache;
use aws_types::provider::ProvideCredentials;
use std::sync::Arc;
fn make_provider() -> impl ProvideCredentials {
// --snip--
Expand All @@ -170,7 +169,7 @@ fn make_provider() -> impl ProvideCredentials {
// Wrapping a result of `make_provider` in `LazyCredentialsCache` is done automatically.
let sdk_config = aws_config::from_env()
.credentials_cache(CredentialsCache::lazy()) // This line can be omitted because it is on by default.
.credentials_provider(Arc::new(make_provider()))
.credentials_provider(make_provider())
.load()
.await;
Expand Down Expand Up @@ -207,7 +206,6 @@ After:
```rust
use aws_credential_types::cache::CredentialsCache;
use aws_types::provider::ProvideCredentials;
use std::sync::Arc;
use std::time::Duration;
fn make_provider() -> impl ProvideCredentials {
Expand All @@ -220,7 +218,7 @@ let sdk_config = aws_config::from_env()
.load_timeout(Duration::from_secs(60)) // Configures timeout.
.into_credentials_cache(),
)
.credentials_provider(Arc::new(make_provider()))
.credentials_provider(make_provider())
.load()
.await;
Expand Down Expand Up @@ -259,7 +257,6 @@ After:
```rust
use aws_config::default_provider::credentials::default_provider;
use aws_credential_types::cache::CredentialsCache;
use std::sync::Arc;
use std::time::Duration;
// Previously used methods no longer exist on the builder for `DefaultCredentialsChain`.
Expand All @@ -272,7 +269,7 @@ let sdk_config = aws_config::from_env()
.default_credential_expiration(Duration::from_secs(20 * 60))
.into_credentials_cache(),
)
.credentials_provider(Arc::new(credentials_provider))
.credentials_provider(credentials_provider)
.load()
.await;
Expand All @@ -281,13 +278,13 @@ let client = aws_sdk_s3::Client::new(&sdk_config);
</details>
"""
references = ["smithy-rs#2122"]
references = ["smithy-rs#2122", "smithy-rs#2227"]
meta = { "breaking" = true, "tada" = false, "bug" = false }
author = "ysaito1001"

[[aws-sdk-rust]]
message = """
The introduction of `CredentialsCache` comes with an accompanying type `SharedCredentialsCache`. This replaces `SharedCredentialsProvider` and as a result, `aws_http::auth:set_provider` has been updated to `aws_http::auth::set_credentials_cache`.
The introduction of `CredentialsCache` comes with an accompanying type `SharedCredentialsCache`, which we will store in the property bag instead of a `SharedCredentialsProvider`. As a result, `aws_http::auth:set_provider` has been updated to `aws_http::auth::set_credentials_cache`.
Before:
```rust
Expand All @@ -309,23 +306,23 @@ After:
```rust
use aws_credential_types::Credentials;
use aws_credential_types::cache::{CredentialsCache, SharedCredentialsCache};
use aws_credential_types::provider::SharedCredentialsProvider;
use aws_http::auth::set_credentials_cache;
use aws_smithy_http::body::SdkBody;
use aws_smithy_http::operation;
use std::sync::Arc;
let mut req = operation::Request::new(http::Request::new(SdkBody::from("some body")));
let credentials = Credentials::new("example", "example", None, None, "my_provider_name");
let credentials_cache = CredentialsCache::lazy_builder()
.into_credentials_cache()
.create_cache(Arc::new(credentials));
.create_cache(SharedCredentialsProvider::new(credentials));
set_credentials_cache(
&mut req.properties_mut(),
SharedCredentialsCache::new(credentials_cache),
);
```
"""
references = ["smithy-rs#2122"]
references = ["smithy-rs#2122", "smithy-rs#2227"]
meta = { "breaking" = true, "tada" = false, "bug" = false }
author = "ysaito1001"

Expand Down
1 change: 1 addition & 0 deletions aws/rust-runtime/aws-config/external-types.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ allowed_external_types = [
"aws_credential_types::cache::CredentialsCache",
"aws_credential_types::provider::ProvideCredentials",
"aws_credential_types::provider::Result",
"aws_credential_types::provider::SharedCredentialsProvider",
"aws_sdk_sts::model::PolicyDescriptorType",
"aws_smithy_async::rt::sleep::AsyncSleep",
"aws_smithy_client::bounds::SmithyConnector",
Expand Down
13 changes: 6 additions & 7 deletions aws/rust-runtime/aws-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ mod loader {
use std::sync::Arc;

use aws_credential_types::cache::CredentialsCache;
use aws_credential_types::provider::ProvideCredentials;
use aws_credential_types::provider::{ProvideCredentials, SharedCredentialsProvider};
use aws_smithy_async::rt::sleep::{default_async_sleep, AsyncSleep};
use aws_smithy_client::http_connector::{ConnectorSettings, HttpConnector};
use aws_smithy_types::retry::RetryConfig;
Expand Down Expand Up @@ -179,7 +179,7 @@ mod loader {
pub struct ConfigLoader {
app_name: Option<AppName>,
credentials_cache: Option<CredentialsCache>,
credentials_provider: Option<Arc<dyn ProvideCredentials>>,
credentials_provider: Option<SharedCredentialsProvider>,
endpoint_resolver: Option<Arc<dyn ResolveAwsEndpoint>>,
endpoint_url: Option<String>,
region: Option<Box<dyn ProvideRegion>>,
Expand Down Expand Up @@ -326,22 +326,21 @@ mod loader {
/// Override the credentials provider but load the default value for region:
/// ```no_run
/// # use aws_credential_types::Credentials;
/// # use std::sync::Arc;
/// # fn create_my_credential_provider() -> Credentials {
/// # Credentials::new("example", "example", None, None, "example")
/// # }
/// # async fn create_config() {
/// let config = aws_config::from_env()
/// .credentials_provider(Arc::new(create_my_credential_provider()))
/// .credentials_provider(create_my_credential_provider())
/// .load()
/// .await;
/// # }
/// ```
pub fn credentials_provider(
mut self,
credentials_provider: Arc<dyn ProvideCredentials>,
credentials_provider: impl ProvideCredentials + 'static,
) -> Self {
self.credentials_provider = Some(credentials_provider);
self.credentials_provider = Some(SharedCredentialsProvider::new(credentials_provider));
self
}

Expand Down Expand Up @@ -600,7 +599,7 @@ mod loader {
} else {
let mut builder = credentials::DefaultCredentialsChain::builder().configure(conf);
builder.set_region(region.clone());
Arc::new(builder.build().await)
SharedCredentialsProvider::new(builder.build().await)
};

let endpoint_resolver = self.endpoint_resolver;
Expand Down
14 changes: 7 additions & 7 deletions aws/rust-runtime/aws-credential-types/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod lazy_caching;
pub use expiring_cache::ExpiringCache;
pub use lazy_caching::Builder as LazyBuilder;

use crate::provider::{future, ProvideCredentials};
use crate::provider::{future, SharedCredentialsProvider};
use std::sync::Arc;

/// Asynchronous Cached Credentials Provider
Expand All @@ -24,13 +24,13 @@ pub trait ProvideCachedCredentials: Send + Sync + std::fmt::Debug {

/// Credentials cache wrapper that may be shared
///
/// Newtype wrapper around ProvideCachedCredentials that implements Clone using an internal
/// Arc.
/// Newtype wrapper around `ProvideCachedCredentials` that implements `Clone` using an internal
/// `Arc`.
#[derive(Clone, Debug)]
pub struct SharedCredentialsCache(Arc<dyn ProvideCachedCredentials>);

impl SharedCredentialsCache {
/// Create a new `SharedCredentialsCache` from `ProvideCredentialsCache`
/// Create a new `SharedCredentialsCache` from `ProvideCachedCredentials`
///
/// The given `cache` will be wrapped in an internal `Arc`. If your
/// cache is already in an `Arc`, use `SharedCredentialsCache::from(cache)` instead.
Expand Down Expand Up @@ -73,11 +73,11 @@ pub(crate) enum Inner {
/// use aws_credential_types::Credentials;
/// use aws_credential_types::cache::CredentialsCache;
/// use aws_credential_types::credential_fn::provide_credentials_fn;
/// use std::sync::Arc;
/// use aws_credential_types::provider::SharedCredentialsProvider;
///
/// let credentials_cache = CredentialsCache::lazy_builder()
/// .into_credentials_cache()
/// .create_cache(Arc::new(provide_credentials_fn(|| async {
/// .create_cache(SharedCredentialsProvider::new(provide_credentials_fn(|| async {
/// // An async process to retrieve credentials would go here:
/// Ok(Credentials::new(
/// "example",
Expand Down Expand Up @@ -105,7 +105,7 @@ impl CredentialsCache {
}

/// Creates a [`SharedCredentialsCache`] wrapping a concrete caching implementation.
pub fn create_cache(self, provider: Arc<dyn ProvideCredentials>) -> SharedCredentialsCache {
pub fn create_cache(self, provider: SharedCredentialsProvider) -> SharedCredentialsCache {
match self.inner {
Inner::Lazy(builder) => SharedCredentialsCache::new(builder.build(provider)),
}
Expand Down
28 changes: 15 additions & 13 deletions aws/rust-runtime/aws-credential-types/src/cache/lazy_caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use aws_smithy_async::rt::sleep::AsyncSleep;
use tracing::{debug, info, info_span, Instrument};

use crate::cache::{ExpiringCache, ProvideCachedCredentials};
use crate::provider::SharedCredentialsProvider;
use crate::provider::{error::CredentialsError, future, ProvideCredentials};
use crate::time_source::TimeSource;

Expand All @@ -25,7 +26,7 @@ pub(crate) struct LazyCredentialsCache {
time: TimeSource,
sleeper: Arc<dyn AsyncSleep>,
cache: ExpiringCache<Credentials, CredentialsError>,
provider: Arc<dyn ProvideCredentials>,
provider: SharedCredentialsProvider,
load_timeout: Duration,
default_credential_expiration: Duration,
}
Expand All @@ -34,7 +35,7 @@ impl LazyCredentialsCache {
fn new(
time: TimeSource,
sleeper: Arc<dyn AsyncSleep>,
provider: Arc<dyn ProvideCredentials>,
provider: SharedCredentialsProvider,
load_timeout: Duration,
default_credential_expiration: Duration,
buffer_time: Duration,
Expand Down Expand Up @@ -110,7 +111,7 @@ mod builder {
use std::time::Duration;

use crate::cache::{CredentialsCache, Inner};
use crate::provider::ProvideCredentials;
use crate::provider::SharedCredentialsProvider;
use aws_smithy_async::rt::sleep::{default_async_sleep, AsyncSleep};

use super::TimeSource;
Expand All @@ -122,9 +123,9 @@ mod builder {
/// Builder for constructing a `LazyCredentialsCache`.
///
/// `LazyCredentialsCache` implements [`ProvideCachedCredentials`](crate::cache::ProvideCachedCredentials) by caching
/// credentials that it loads by calling a user-provided [`ProvideCredentials`] implementation.
/// credentials that it loads by calling a user-provided [`ProvideCredentials`](crate::provider::ProvideCredentials) implementation.
///
/// For example, you can provide a [`ProvideCredentials`] implementation that calls
/// For example, you can provide a [`ProvideCredentials`](crate::provider::ProvideCredentials) implementation that calls
/// AWS STS's AssumeRole operation to get temporary credentials, and `LazyCredentialsCache`
/// will cache those credentials until they expire.
///
Expand Down Expand Up @@ -178,15 +179,15 @@ mod builder {
self
}

/// Timeout for the given [`ProvideCredentials`] implementation.
/// Timeout for the given [`ProvideCredentials`](crate::provider::ProvideCredentials) implementation.
///
/// Defaults to 5 seconds.
pub fn load_timeout(mut self, timeout: Duration) -> Self {
self.set_load_timeout(Some(timeout));
self
}

/// Timeout for the given [`ProvideCredentials`] implementation.
/// Timeout for the given [`ProvideCredentials`](crate::provider::ProvideCredentials) implementation.
///
/// Defaults to 5 seconds.
pub fn set_load_timeout(&mut self, timeout: Option<Duration>) -> &mut Self {
Expand Down Expand Up @@ -220,7 +221,7 @@ mod builder {

/// Default expiration time to set on credentials if they don't have an expiration time.
///
/// This is only used if the given [`ProvideCredentials`] returns
/// This is only used if the given [`ProvideCredentials`](crate::provider::ProvideCredentials) returns
/// [`Credentials`](crate::Credentials) that don't have their `expiry` set.
/// This must be at least 15 minutes.
///
Expand All @@ -232,7 +233,7 @@ mod builder {

/// Default expiration time to set on credentials if they don't have an expiration time.
///
/// This is only used if the given [`ProvideCredentials`] returns
/// This is only used if the given [`ProvideCredentials`](crate::provider::ProvideCredentials) returns
/// [`Credentials`](crate::Credentials) that don't have their `expiry` set.
/// This must be at least 15 minutes.
///
Expand All @@ -258,7 +259,7 @@ mod builder {
/// This will panic if no `sleep` implementation is given and if no default crate features
/// are used. By default, the [`TokioSleep`](aws_smithy_async::rt::sleep::TokioSleep)
/// implementation will be set automatically.
pub(crate) fn build(self, provider: Arc<dyn ProvideCredentials>) -> LazyCredentialsCache {
pub(crate) fn build(self, provider: SharedCredentialsProvider) -> LazyCredentialsCache {
let default_credential_expiration = self
.default_credential_expiration
.unwrap_or(DEFAULT_CREDENTIAL_EXPIRATION);
Expand Down Expand Up @@ -289,6 +290,7 @@ mod tests {
use tracing::info;
use tracing_test::traced_test;

use crate::provider::SharedCredentialsProvider;
use crate::{
cache::ProvideCachedCredentials, credential_fn::provide_credentials_fn,
provider::error::CredentialsError, time_source::TestingTimeSource, Credentials,
Expand All @@ -307,7 +309,7 @@ mod tests {
LazyCredentialsCache::new(
time,
Arc::new(TokioSleep::new()),
Arc::new(provide_credentials_fn(move || {
SharedCredentialsProvider::new(provide_credentials_fn(move || {
let list = load_list.clone();
async move {
let next = list.lock().unwrap().remove(0);
Expand Down Expand Up @@ -341,7 +343,7 @@ mod tests {
#[tokio::test]
async fn initial_populate_credentials() {
let time = TestingTimeSource::new(UNIX_EPOCH);
let provider = Arc::new(provide_credentials_fn(|| async {
let provider = SharedCredentialsProvider::new(provide_credentials_fn(|| async {
info!("refreshing the credentials");
Ok(credentials(1000))
}));
Expand Down Expand Up @@ -464,7 +466,7 @@ mod tests {
let credentials_cache = LazyCredentialsCache::new(
TimeSource::testing(&time),
Arc::new(TokioSleep::new()),
Arc::new(provide_credentials_fn(|| async {
SharedCredentialsProvider::new(provide_credentials_fn(|| async {
aws_smithy_async::future::never::Never::new().await;
Ok(credentials(1000))
})),
Expand Down
38 changes: 38 additions & 0 deletions aws/rust-runtime/aws-credential-types/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,41 @@ impl ProvideCredentials for Arc<dyn ProvideCredentials> {
self.as_ref().provide_credentials()
}
}

/// Credentials Provider wrapper that may be shared
///
/// Newtype wrapper around ProvideCredentials that implements Clone using an internal
/// Arc.
#[derive(Clone, Debug)]
pub struct SharedCredentialsProvider(Arc<dyn ProvideCredentials>);

impl SharedCredentialsProvider {
/// Create a new SharedCredentials provider from `ProvideCredentials`
///
/// The given provider will be wrapped in an internal `Arc`. If your
/// provider is already in an `Arc`, use `SharedCredentialsProvider::from(provider)` instead.
pub fn new(provider: impl ProvideCredentials + 'static) -> Self {
Self(Arc::new(provider))
}
}

impl AsRef<dyn ProvideCredentials> for SharedCredentialsProvider {
fn as_ref(&self) -> &(dyn ProvideCredentials + 'static) {
self.0.as_ref()
}
}

impl From<Arc<dyn ProvideCredentials>> for SharedCredentialsProvider {
fn from(provider: Arc<dyn ProvideCredentials>) -> Self {
SharedCredentialsProvider(provider)
}
}

impl ProvideCredentials for SharedCredentialsProvider {
fn provide_credentials<'a>(&'a self) -> future::ProvideCredentials<'a>
where
Self: 'a,
{
self.0.provide_credentials()
}
}
8 changes: 4 additions & 4 deletions aws/rust-runtime/aws-http/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ mod tests {
CredentialsCache, ProvideCachedCredentials, SharedCredentialsCache,
};
use aws_credential_types::credential_fn::provide_credentials_fn;
use aws_credential_types::provider::SharedCredentialsProvider;
use aws_credential_types::provider::{error::CredentialsError, future};
use aws_credential_types::Credentials;
use aws_smithy_http::body::SdkBody;
use aws_smithy_http::middleware::AsyncMapRequest;
use aws_smithy_http::operation;
use std::sync::Arc;

#[derive(Debug)]
struct Unhandled;
Expand Down Expand Up @@ -185,9 +185,9 @@ mod tests {
let mut req = operation::Request::new(http::Request::new(SdkBody::from("some body")));
let credentials_cache = CredentialsCache::lazy_builder()
.into_credentials_cache()
.create_cache(Arc::new(provide_credentials_fn(|| async {
Ok(Credentials::for_tests())
})));
.create_cache(SharedCredentialsProvider::new(provide_credentials_fn(
|| async { Ok(Credentials::for_tests()) },
)));
set_credentials_cache(
&mut req.properties_mut(),
SharedCredentialsCache::from(credentials_cache),
Expand Down
Loading

0 comments on commit b8510db

Please sign in to comment.