Skip to content

Commit 915b087

Browse files
committed
Remove aws-sdk-auth ff
1 parent 18c3bc8 commit 915b087

File tree

2 files changed

+75
-49
lines changed

2 files changed

+75
-49
lines changed

Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ dns-resolver = ["dep:hickory-resolver", "dep:hickory-proto"]
4141
cert-key-password = ["dep:pem", "dep:pkcs8"]
4242

4343
# Enable support for MONGODB-AWS authentication.
44-
aws-auth = ["dep:reqwest"]
45-
46-
# Enable support AWS SDK for authentication.
47-
# This can only be used with the tokio-runtime and aws-auth feature flags.
48-
aws-sdk-auth = ["dep:reqwest", "aws-config"]
44+
aws-auth = ["dep:reqwest", "aws-config"]
4945

5046
# Enable support for on-demand Azure KMS credentials.
5147
azure-kms = ["dep:reqwest"]

src/client/auth/aws.rs

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
#[cfg(feature = "aws-sdk-auth")]
1+
#[cfg(feature = "aws-auth")]
22
use aws_config::BehaviorVersion;
3-
#[cfg(feature = "aws-sdk-auth")]
3+
4+
#[cfg(feature = "aws-auth")]
45
use aws_credential_types::provider::ProvideCredentials;
5-
#[cfg(feature = "aws-sdk-auth")]
6+
7+
#[allow(unused_imports)]
8+
#[cfg(feature = "aws-auth")]
69
use aws_types::sdk_config::SharedCredentialsProvider;
710

11+
// Note from RUST-1529: commented Duration import since original implementation is commented out
12+
// use std::time::Duration;
13+
814
use chrono::{offset::Utc, DateTime};
915
use hmac::Hmac;
1016
use once_cell::sync::Lazy;
1117
use rand::distributions::{Alphanumeric, DistString};
1218
use serde::Deserialize;
1319
use sha2::{Digest, Sha256};
14-
use std::{fs::File, io::Read, time::Duration};
20+
use std::{fs::File, io::Read};
1521
use tokio::sync::Mutex;
1622

1723
use crate::{
@@ -61,7 +67,9 @@ async fn authenticate_stream_inner(
6167
conn: &mut Connection,
6268
credential: &Credential,
6369
server_api: Option<&ServerApi>,
64-
http_client: &HttpClient,
70+
// Note from RUST-1529: http_client is used in the original non-AWS SDK implementation for
71+
// credentials
72+
_http_client: &HttpClient,
6573
) -> Result<()> {
6674
let source = match credential.source.as_deref() {
6775
Some("$external") | None => "$external",
@@ -96,7 +104,7 @@ async fn authenticate_stream_inner(
96104
let server_first = ServerFirst::parse(server_first_response.auth_response_body(MECH_NAME)?)?;
97105
server_first.validate(&nonce)?;
98106

99-
#[cfg(feature = "aws-sdk-auth")]
107+
// Find credentials using MongoDB URI or AWS SDK
100108
let aws_credential = if let (Some(access_key), Some(secret_key)) =
101109
(&credential.username, &credential.password)
102110
{
@@ -113,41 +121,63 @@ async fn authenticate_stream_inner(
113121
)
114122
} else {
115123
// If credentials are not provided in the URI, use the AWS SDK to load
116-
let creds = aws_config::load_defaults(BehaviorVersion::latest())
124+
// let creds = aws_config::load_defaults(BehaviorVersion::latest())
125+
// .await
126+
// .credentials_provider()
127+
// .expect("no credential provider configured")
128+
// .provide_credentials()
129+
// .await
130+
// .map_err(|e| {
131+
// Error::authentication_error(MECH_NAME, &format!("failed to get creds: {e}"))
132+
// })?;
133+
134+
// AwsCredential::from_sdk_creds(
135+
// creds.access_key_id().to_string(),
136+
// creds.secret_access_key().to_string(),
137+
// creds.session_token().map(|s| s.to_string()),
138+
// None,
139+
// )
140+
let provider = aws_config::load_defaults(BehaviorVersion::latest())
117141
.await
118142
.credentials_provider()
119-
.expect("no credential provider configured")
120-
.provide_credentials()
121-
.await
122-
.map_err(|e| {
123-
Error::authentication_error(MECH_NAME, &format!("failed to get creds: {e}"))
124-
})?;
125-
AwsCredential::from_sdk_creds(
126-
creds.access_key_id().to_string(),
127-
creds.secret_access_key().to_string(),
128-
creds.session_token().map(|s| s.to_string()),
129-
None,
130-
)
131-
};
132-
#[cfg(not(feature = "aws-sdk-auth"))]
133-
let aws_credential = {
134-
// Limit scope of this variable to avoid holding onto the lock for the duration of
135-
// authenticate_stream.
136-
let cached_credential = CACHED_CREDENTIAL.lock().await;
137-
match *cached_credential {
138-
Some(ref aws_credential) if !aws_credential.is_expired() => aws_credential.clone(),
139-
_ => {
140-
// From the spec: the driver MUST not place a lock on making a request.
141-
drop(cached_credential);
142-
let aws_credential = AwsCredential::get(credential, http_client).await?;
143-
if aws_credential.expiration.is_some() {
144-
*CACHED_CREDENTIAL.lock().await = Some(aws_credential.clone());
145-
}
146-
aws_credential
143+
.expect("no credential provider configured");
144+
145+
match provider.provide_credentials().await {
146+
Ok(creds) => AwsCredential::from_sdk_creds(
147+
creds.access_key_id().to_string(),
148+
creds.secret_access_key().to_string(),
149+
creds.session_token().map(|s| s.to_string()),
150+
None,
151+
),
152+
Err(e) => {
153+
eprintln!("AWS credential error: {:#?}", e);
154+
return Err(Error::authentication_error(
155+
MECH_NAME,
156+
&format!("failed to get creds: {e}"),
157+
));
147158
}
148159
}
149160
};
150161

162+
// Find credentials using original implementation without AWS SDK
163+
// let aws_credential = {
164+
// // Limit scope of this variable to avoid holding onto the lock for the duration of
165+
// // authenticate_stream.
166+
// let cached_credential = CACHED_CREDENTIAL.lock().await;
167+
// match *cached_credential {
168+
// Some(ref aws_credential) if !aws_credential.is_expired() => aws_credential.clone(),
169+
// _ => {
170+
// // From the spec: the driver MUST not place a lock on making a request.
171+
// drop(cached_credential);
172+
// let aws_credential = AwsCredential::get(credential, http_client).await?;
173+
// if aws_credential.expiration.is_some() {
174+
// *CACHED_CREDENTIAL.lock().await = Some(aws_credential.clone());
175+
// }
176+
// aws_credential
177+
// }
178+
// }
179+
// };
180+
151181
let date = Utc::now();
152182

153183
let authorization_header = aws_credential.compute_authorization_header(
@@ -526,15 +556,15 @@ impl AwsCredential {
526556
self.session_token.as_deref()
527557
}
528558

529-
fn is_expired(&self) -> bool {
530-
match self.expiration {
531-
Some(expiration) => {
532-
expiration.saturating_duration_since(crate::bson::DateTime::now())
533-
< Duration::from_secs(5 * 60)
534-
}
535-
None => true,
536-
}
537-
}
559+
// fn is_expired(&self) -> bool {
560+
// match self.expiration {
561+
// Some(expiration) => {
562+
// expiration.saturating_duration_since(crate::bson::DateTime::now())
563+
// < Duration::from_secs(5 * 60)
564+
// }
565+
// None => true,
566+
// }
567+
// }
538568
}
539569

540570
/// The response from the server to the `saslStart` command in a MONGODB-AWS authentication attempt.

0 commit comments

Comments
 (0)