Skip to content

Commit 63d89e4

Browse files
authored
RUST-766 Gate MONGODB-AWS authentication behind the aws-auth feature flag (#436)
1 parent eb7389a commit 63d89e4

File tree

9 files changed

+64
-19
lines changed

9 files changed

+64
-19
lines changed

.evergreen/aws-ecs-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ tokio = "1.0.2"
99

1010
[dependencies.mongodb]
1111
path = "../.."
12+
features = ["aws-auth"]

.evergreen/config.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,17 @@ functions:
309309
${PREPARE_SHELL}
310310
311311
.evergreen/run-plain-tests.sh
312+
313+
"run connection string tests":
314+
- command: shell.exec
315+
type: test
316+
params:
317+
shell: bash
318+
working_dir: "src"
319+
script: |
320+
${PREPARE_SHELL}
321+
322+
.evergreen/run-connection-string-tests.sh
312323
313324
"prepare resources":
314325
- command: shell.exec
@@ -823,6 +834,10 @@ tasks:
823834
- func: "run aws auth test with aws EC2 credentials"
824835
- func: "run aws ECS auth test"
825836

837+
- name: "test-connection-string"
838+
commands:
839+
- func: "run connection string tests"
840+
826841
- name: "test-atlas-connectivity"
827842
tags: ["atlas-connect"]
828843
commands:
@@ -1390,6 +1405,7 @@ buildvariants:
13901405
display_name: "Atlas Connectivity ${os} with ${async-runtime}"
13911406
tasks:
13921407
- ".atlas-connect"
1408+
13931409
- matrix_name: "aws-auth"
13941410
matrix_spec:
13951411
os:
@@ -1398,6 +1414,7 @@ buildvariants:
13981414
display_name: "${os} AWS Auth with ${async-runtime}"
13991415
tasks:
14001416
- ".aws-auth"
1417+
- "test-connection-string"
14011418
# TODO: RUST-361 enable these tests once OCSP support is implemented
14021419
# - matrix_name: "ocsp"
14031420
# matrix_spec:

.evergreen/run-aws-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ set -o errexit
4444

4545
. ~/.cargo/env
4646

47-
RUST_BACKTRACE=1 cargo test auth_aws::auth_aws
47+
RUST_BACKTRACE=1 cargo test --features aws-auth auth_aws::auth_aws
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
set -o errexit
4+
set -o xtrace
5+
6+
. ~/.cargo/env
7+
8+
RUST_BACKTRACE=1 cargo test --features aws-auth spec::auth
9+
RUST_BACKTRACE=1 cargo test --features aws-auth uri_options
10+
RUST_BACKTRACE=1 cargo test --features aws-auth connection_string

Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,26 @@ exclude = [
2121

2222
[features]
2323
default = ["tokio-runtime"]
24-
tokio-runtime = ["tokio/macros", "tokio/net", "tokio/rt", "tokio/time", "reqwest", "serde_bytes"]
24+
tokio-runtime = ["tokio/macros", "tokio/net", "tokio/rt", "tokio/time", "serde_bytes"]
2525
async-std-runtime = ["async-std", "async-std/attributes", "async-std-resolver", "tokio-util/compat"]
2626
sync = ["async-std-runtime"]
27+
2728
# The bson/u2i feature enables automatic conversion from unsigned to signed types during
2829
# serialization. This feature is intended for use when serializing data types in third-party crates
2930
# whose implementation cannot be changed; otherwise, it is preferred to use the helper functions
3031
# provided in the bson::serde_helpers module.
3132
bson-u2i = ["bson/u2i"]
33+
3234
# Enable support for v0.4 of the chrono crate in the public API of the BSON library.
3335
bson-chrono-0_4 = ["bson/chrono-0_4"]
36+
3437
# Enable support for v0.8 of the uuid crate in the public API of the BSON library.
3538
bson-uuid-0_8 = ["bson/uuid-0_8"]
3639

40+
# Enable support for MONGODB-AWS authentication.
41+
# This can only be used with the tokio-runtime feature flag.
42+
aws-auth = ["reqwest"]
43+
3744
[dependencies]
3845
async-trait = "0.1.42"
3946
base64 = "0.13.0"

src/client/auth/mod.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Contains the types needed to specify the auth configuration for a
22
//! [`Client`](struct.Client.html).
33
4-
#[cfg(feature = "tokio-runtime")]
4+
#[cfg(feature = "aws-auth")]
55
mod aws;
66
mod plain;
77
mod sasl;
@@ -82,7 +82,7 @@ pub enum AuthMechanism {
8282
///
8383
/// Note: Only server versions 4.4+ support AWS authentication. Additionally, the driver only
8484
/// supports AWS authentication with the tokio runtime.
85-
#[cfg(feature = "tokio-runtime")]
85+
#[cfg(feature = "aws-auth")]
8686
MongoDbAws,
8787
}
8888

@@ -164,7 +164,7 @@ impl AuthMechanism {
164164

165165
Ok(())
166166
}
167-
#[cfg(feature = "tokio-runtime")]
167+
#[cfg(feature = "aws-auth")]
168168
AuthMechanism::MongoDbAws => {
169169
if credential.username.is_some() && credential.password.is_none() {
170170
return Err(ErrorKind::InvalidArgument {
@@ -190,7 +190,7 @@ impl AuthMechanism {
190190
AuthMechanism::MongoDbX509 => MONGODB_X509_STR,
191191
AuthMechanism::Gssapi => GSSAPI_STR,
192192
AuthMechanism::Plain => PLAIN_STR,
193-
#[cfg(feature = "tokio-runtime")]
193+
#[cfg(feature = "aws-auth")]
194194
AuthMechanism::MongoDbAws => MONGODB_AWS_STR,
195195
}
196196
}
@@ -205,7 +205,7 @@ impl AuthMechanism {
205205
}
206206
AuthMechanism::MongoDbX509 => "$external",
207207
AuthMechanism::Plain => "$external",
208-
#[cfg(feature = "tokio-runtime")]
208+
#[cfg(feature = "aws-auth")]
209209
AuthMechanism::MongoDbAws => "$external",
210210
_ => "",
211211
}
@@ -233,7 +233,7 @@ impl AuthMechanism {
233233
x509::build_speculative_client_first(credential),
234234
)))),
235235
Self::Plain => Ok(None),
236-
#[cfg(feature = "tokio-runtime")]
236+
#[cfg(feature = "aws-auth")]
237237
AuthMechanism::MongoDbAws => Ok(None),
238238
AuthMechanism::MongoDbCr => Err(ErrorKind::Authentication {
239239
message: "MONGODB-CR is deprecated and not supported by this driver. Use SCRAM \
@@ -253,7 +253,7 @@ impl AuthMechanism {
253253
stream: &mut Connection,
254254
credential: &Credential,
255255
server_api: Option<&ServerApi>,
256-
#[cfg_attr(not(feature = "tokio-runtime"), allow(unused))] http_client: &HttpClient,
256+
#[cfg_attr(not(feature = "aws-auth"), allow(unused))] http_client: &HttpClient,
257257
) -> Result<()> {
258258
self.validate_credential(credential)?;
259259

@@ -274,7 +274,7 @@ impl AuthMechanism {
274274
AuthMechanism::Plain => {
275275
plain::authenticate_stream(stream, credential, server_api).await
276276
}
277-
#[cfg(feature = "tokio-runtime")]
277+
#[cfg(feature = "aws-auth")]
278278
AuthMechanism::MongoDbAws => {
279279
aws::authenticate_stream(stream, credential, server_api, http_client).await
280280
}
@@ -304,11 +304,13 @@ impl FromStr for AuthMechanism {
304304
GSSAPI_STR => Ok(AuthMechanism::Gssapi),
305305
PLAIN_STR => Ok(AuthMechanism::Plain),
306306

307-
#[cfg(feature = "tokio-runtime")]
307+
#[cfg(feature = "aws-auth")]
308308
MONGODB_AWS_STR => Ok(AuthMechanism::MongoDbAws),
309-
#[cfg(not(feature = "tokio-runtime"))]
309+
#[cfg(not(feature = "aws-auth"))]
310310
MONGODB_AWS_STR => Err(ErrorKind::InvalidArgument {
311-
message: "MONGODB-AWS auth is only supported with the tokio runtime".into(),
311+
message: "MONGODB-AWS auth is only supported with the aws-auth feature flag and \
312+
the tokio runtime"
313+
.into(),
312314
}
313315
.into()),
314316

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@
8484
#![cfg_attr(test, type_length_limit = "80000000")]
8585
#![doc(html_root_url = "https://docs.rs/mongodb/2.0.0-beta.3")]
8686

87+
#[cfg(all(
88+
feature = "aws-auth",
89+
feature = "async-std-runtime"
90+
))]
91+
compile_error!(
92+
"The `aws-auth` feature flag is only supported on the tokio runtime."
93+
);
94+
8795
macro_rules! define_if_single_runtime_enabled {
8896
( $( $def:item )+ ) => {
8997
$(

src/runtime/http.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
#[cfg(feature = "tokio-runtime")]
1+
#[cfg(feature = "aws-auth")]
22
use reqwest::{Method, Response};
3-
#[cfg(feature = "tokio-runtime")]
3+
#[cfg(feature = "aws-auth")]
44
use serde::Deserialize;
55

66
#[derive(Clone, Debug, Default)]
77
pub(crate) struct HttpClient {
8-
#[cfg(feature = "tokio-runtime")]
8+
#[cfg(feature = "aws-auth")]
99
inner: reqwest::Client,
1010
}
1111

12-
#[cfg(feature = "tokio-runtime")]
12+
#[cfg(feature = "aws-auth")]
1313
impl HttpClient {
1414
/// Executes an HTTP GET request and deserializes the JSON response.
1515
pub(crate) async fn get_and_deserialize_json<'a, T>(

src/test/spec/auth.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ async fn run_auth_test(test_file: TestFile) {
5454
"GSSAPI",
5555
"PLAIN",
5656
"MONGODB-CR",
57-
#[cfg(not(feature = "tokio-runtime"))]
57+
#[cfg(not(feature = "aws-auth"))]
5858
"MONGODB-AWS",
5959
];
6060

6161
// TODO: GSSAPI (RUST-196)
62-
// TODO: PLAIN (RUST-197)
62+
// TODO: PLAIN (RUST-992)
6363
if skipped_mechanisms
6464
.iter()
6565
.any(|mech| test_case.description.contains(mech))

0 commit comments

Comments
 (0)