1
- #[ cfg( feature = "aws-sdk- auth" ) ]
1
+ #[ cfg( feature = "aws-auth" ) ]
2
2
use aws_config:: BehaviorVersion ;
3
- #[ cfg( feature = "aws-sdk-auth" ) ]
3
+
4
+ #[ cfg( feature = "aws-auth" ) ]
4
5
use aws_credential_types:: provider:: ProvideCredentials ;
5
- #[ cfg( feature = "aws-sdk-auth" ) ]
6
+
7
+ #[ allow( unused_imports) ]
8
+ #[ cfg( feature = "aws-auth" ) ]
6
9
use aws_types:: sdk_config:: SharedCredentialsProvider ;
7
10
11
+ // Note from RUST-1529: commented Duration import since original implementation is commented out
12
+ // use std::time::Duration;
13
+
8
14
use chrono:: { offset:: Utc , DateTime } ;
9
15
use hmac:: Hmac ;
10
16
use once_cell:: sync:: Lazy ;
11
17
use rand:: distributions:: { Alphanumeric , DistString } ;
12
18
use serde:: Deserialize ;
13
19
use sha2:: { Digest , Sha256 } ;
14
- use std:: { fs:: File , io:: Read , time :: Duration } ;
20
+ use std:: { fs:: File , io:: Read } ;
15
21
use tokio:: sync:: Mutex ;
16
22
17
23
use crate :: {
@@ -61,7 +67,9 @@ async fn authenticate_stream_inner(
61
67
conn : & mut Connection ,
62
68
credential : & Credential ,
63
69
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 ,
65
73
) -> Result < ( ) > {
66
74
let source = match credential. source . as_deref ( ) {
67
75
Some ( "$external" ) | None => "$external" ,
@@ -96,7 +104,7 @@ async fn authenticate_stream_inner(
96
104
let server_first = ServerFirst :: parse ( server_first_response. auth_response_body ( MECH_NAME ) ?) ?;
97
105
server_first. validate ( & nonce) ?;
98
106
99
- # [ cfg ( feature = "aws-sdk-auth" ) ]
107
+ // Find credentials using MongoDB URI or AWS SDK
100
108
let aws_credential = if let ( Some ( access_key) , Some ( secret_key) ) =
101
109
( & credential. username , & credential. password )
102
110
{
@@ -113,41 +121,63 @@ async fn authenticate_stream_inner(
113
121
)
114
122
} else {
115
123
// 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 ( ) )
117
141
. await
118
142
. 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
+ ) ) ;
147
158
}
148
159
}
149
160
} ;
150
161
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
+
151
181
let date = Utc :: now ( ) ;
152
182
153
183
let authorization_header = aws_credential. compute_authorization_header (
@@ -526,15 +556,15 @@ impl AwsCredential {
526
556
self . session_token . as_deref ( )
527
557
}
528
558
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
+ // }
538
568
}
539
569
540
570
/// The response from the server to the `saslStart` command in a MONGODB-AWS authentication attempt.
0 commit comments