-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: example showing how to use native-tls (#946)
* add: native-tls example and test update: aws-config to correctly separate the native-tls and rustls features for its dependencies fix: native-tls feature gate typo update: prefix unused id field with an underscore * update: CHANGELOG.next.toml * remove: unnecessary aws-sdk-sts feature deps from aws-config * remove: obsolete dep features
- Loading branch information
Zelda Hessler
authored
Dec 9, 2021
1 parent
a491afe
commit 979c703
Showing
6 changed files
with
88 additions
and
4 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
18 changes: 18 additions & 0 deletions
18
aws/sdk/examples/using_native_tls_instead_of_rustls/Cargo.toml
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,18 @@ | ||
[package] | ||
name = "using_native_tls_instead_of_rustls" | ||
version = "0.1.0" | ||
authors = ["Zelda Hessler zhessler@amazon.com>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
# aws-config pulls in rustls and several other things by default. We have to disable defaults in order to use native-tls | ||
# and then manually bring the other defaults back | ||
aws-config = { path = "../../build/aws-sdk/sdk/aws-config", default-features = false, features = ["default-provider", "native-tls", "rt-tokio", "dns", "tcp-connector"] } | ||
# aws-sdk-s3 brings in rustls by default so we disable that in order to use native-tls only | ||
aws-sdk-s3 = { package = "aws-sdk-s3", path = "../../build/aws-sdk/sdk/s3", default-features = false, features = ["native-tls"] } | ||
# aws-sdk-sts is the same as aws-sdk-s3 | ||
aws-sdk-sts = { package = "aws-sdk-sts", path = "../../build/aws-sdk/sdk/sts", default-features = false, features = ["native-tls"] } | ||
tokio = { version = "1", features = ["full"] } | ||
tracing-subscriber = "0.2.18" |
56 changes: 56 additions & 0 deletions
56
aws/sdk/examples/using_native_tls_instead_of_rustls/src/main.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,56 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
/// The SDK defaults to using RusTLS by default but you can also use [`native_tls`](https://github.com/sfackler/rust-native-tls) | ||
/// which will choose a TLS implementation appropriate for your platform. This example looks much like | ||
/// any other. Activating and deactivating `features` in your app's `Cargo.toml` is all that's needed. | ||
#[tokio::main] | ||
async fn main() -> Result<(), aws_sdk_s3::Error> { | ||
tracing_subscriber::fmt::init(); | ||
|
||
let shared_config = aws_config::load_from_env().await; | ||
|
||
let s3_config = aws_sdk_s3::Config::from(&shared_config); | ||
let client = aws_sdk_s3::Client::from_conf(s3_config); | ||
|
||
let resp = client.list_buckets().send().await?; | ||
|
||
for bucket in resp.buckets().unwrap_or_default() { | ||
println!("bucket: {:?}", bucket.name().unwrap_or_default()) | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
/// You can run this test to ensure that this example is only using `native-tls` | ||
/// and that nothing is pulling in `rustls` as a dependency | ||
#[test] | ||
#[should_panic = "error: package ID specification `rustls` did not match any packages"] | ||
fn test_rustls_is_not_in_dependency_tree() { | ||
let cargo_location = std::env::var("CARGO").unwrap(); | ||
let cargo_command = std::process::Command::new(&cargo_location) | ||
.arg("tree") | ||
.arg("--invert") | ||
.arg("rustls") | ||
.output() | ||
.expect("failed to run 'cargo tree'"); | ||
|
||
let stderr = String::from_utf8_lossy(&cargo_command.stderr); | ||
|
||
// We expect the call to `cargo tree` to error out. If it did, we panic with the resulting | ||
// message here. In the case that no error message is set, that's bad. | ||
if !stderr.is_empty() { | ||
panic!("{}", stderr); | ||
} | ||
|
||
// Uh oh. We expected an error message but got none, likely because `cargo tree` found | ||
// `rustls` in our dependencies. We'll print out the message we got to see what went wrong. | ||
let stdout = String::from_utf8_lossy(&cargo_command.stdout); | ||
|
||
println!("{}", stdout) | ||
} | ||
} |
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