Skip to content

Commit aac9bec

Browse files
authored
Avoid logging signable body by default whose data can be very large (#3917)
## Motivation and Context While investigating a connect timeout issue for uploading object(s) in [`aws-s3-transfer-manager-rs`](https://github.com/awslabs/aws-s3-transfer-manager-rs), we saw that the size of trace log was about 70 GB and that the last 1 GB only had 30 lines, with each line having couple MB's body to be logged (due to [this location](https://github.com/awslabs/aws-sdk-rust/blob/953cd6c7af04f02938a0dcf36f793ebe7a06cc57/sdk/aws-sigv4/src/http_request/sign.rs#L224)). ## Description This PR disables logging the actual body data in `SignableBody` by default. Customers can set the `LOG_SIGNABLE_BODY` environment variable to log the body data if they want to, as described in the comment within the `Debug` implementation. ## Testing - Added a small unit test - Tests in CI ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
1 parent df77d5f commit aac9bec

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

aws/rust-runtime/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aws/rust-runtime/aws-sigv4/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "aws-sigv4"
3-
version = "1.2.5"
3+
version = "1.2.6"
44
authors = ["AWS Rust SDK Team <[email protected]>", "David Barsky <[email protected]>"]
55
description = "SigV4 signer for HTTP requests and Event Stream messages."
66
edition = "2021"

aws/rust-runtime/aws-sigv4/src/http_request/sign.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use std::borrow::Cow;
1919
use std::fmt::{Debug, Formatter};
2020
use std::str;
2121

22+
const LOG_SIGNABLE_BODY: &str = "LOG_SIGNABLE_BODY";
23+
2224
/// Represents all of the information necessary to sign an HTTP request.
2325
#[derive(Debug)]
2426
#[non_exhaustive]
@@ -72,7 +74,7 @@ impl<'a> SignableRequest<'a> {
7274
}
7375

7476
/// A signable HTTP request body
75-
#[derive(Debug, Clone, Eq, PartialEq)]
77+
#[derive(Clone, Eq, PartialEq)]
7678
#[non_exhaustive]
7779
pub enum SignableBody<'a> {
7880
/// A body composed of a slice of bytes
@@ -93,6 +95,30 @@ pub enum SignableBody<'a> {
9395
StreamingUnsignedPayloadTrailer,
9496
}
9597

98+
/// Formats the value using the given formatter. To print the body data, set the environment variable `LOG_SIGNABLE_BODY=true`.
99+
impl<'a> Debug for SignableBody<'a> {
100+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
101+
let should_log_signable_body = std::env::var(LOG_SIGNABLE_BODY)
102+
.map(|v| v.eq_ignore_ascii_case("true"))
103+
.unwrap_or_default();
104+
match self {
105+
Self::Bytes(arg0) => {
106+
if should_log_signable_body {
107+
f.debug_tuple("Bytes").field(arg0).finish()
108+
} else {
109+
let redacted = format!("** REDACTED **. To print {body_size} bytes of raw data, set environment variable `LOG_SIGNABLE_BODY=true`", body_size = arg0.len());
110+
f.debug_tuple("Bytes").field(&redacted).finish()
111+
}
112+
}
113+
Self::UnsignedPayload => write!(f, "UnsignedPayload"),
114+
Self::Precomputed(arg0) => f.debug_tuple("Precomputed").field(arg0).finish(),
115+
Self::StreamingUnsignedPayloadTrailer => {
116+
write!(f, "StreamingUnsignedPayloadTrailer")
117+
}
118+
}
119+
}
120+
}
121+
96122
impl SignableBody<'_> {
97123
/// Create a new empty signable body
98124
pub fn empty() -> SignableBody<'static> {
@@ -1121,4 +1147,22 @@ mod tests {
11211147
request.uri().path_and_query().unwrap().to_string()
11221148
);
11231149
}
1150+
1151+
#[test]
1152+
fn test_debug_signable_body() {
1153+
let sut = SignableBody::Bytes(b"hello signable body");
1154+
assert_eq!(
1155+
"Bytes(\"** REDACTED **. To print 19 bytes of raw data, set environment variable `LOG_SIGNABLE_BODY=true`\")",
1156+
format!("{sut:?}")
1157+
);
1158+
1159+
let sut = SignableBody::UnsignedPayload;
1160+
assert_eq!("UnsignedPayload", format!("{sut:?}"));
1161+
1162+
let sut = SignableBody::Precomputed("precomputed".to_owned());
1163+
assert_eq!("Precomputed(\"precomputed\")", format!("{sut:?}"));
1164+
1165+
let sut = SignableBody::StreamingUnsignedPayloadTrailer;
1166+
assert_eq!("StreamingUnsignedPayloadTrailer", format!("{sut:?}"));
1167+
}
11241168
}

0 commit comments

Comments
 (0)