Skip to content

Add ellipsis when truncating payload #1753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public abstract class BaseEventStreamAsyncAws4Signer extends BaseAsyncAws4Signer
private static final String HTTP_CONTENT_SHA_256 = "STREAMING-AWS4-HMAC-SHA256-EVENTS";
private static final String EVENT_STREAM_PAYLOAD = "AWS4-HMAC-SHA256-PAYLOAD";

private static final int PAYLOAD_TRUNCATE_LENGTH = 32;


protected BaseEventStreamAsyncAws4Signer() {
}
Expand Down Expand Up @@ -287,15 +289,26 @@ static String toDebugString(Message m, boolean truncatePayload) {
sb.append("}, payload=");

byte[] payload = m.getPayload();

byte[] payloadToLog;

// We don't actually need to truncate if the payload length is already within the truncate limit
truncatePayload = truncatePayload && payload.length > PAYLOAD_TRUNCATE_LENGTH;

if (truncatePayload) {
// Would be nice if BinaryUtils.toHex() could take an array index range instead so we don't need to copy
payloadToLog = Arrays.copyOf(payload, Math.min(32, payload.length));
payloadToLog = Arrays.copyOf(payload, PAYLOAD_TRUNCATE_LENGTH);
} else {
payloadToLog = payload;
}

return sb.append(BinaryUtils.toHex(payloadToLog)).append("}").toString();
sb.append(BinaryUtils.toHex(payloadToLog));

if (truncatePayload) {
sb.append("...");
}

sb.append("}");

return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void toDebugString_largePayload_truncate_generatesCorrectString() {
byte[] first32 = Arrays.copyOf(payload, 32);
String expectedPayloadString = BinaryUtils.toHex(first32);
assertThat(BaseEventStreamAsyncAws4Signer.toDebugString(m, true))
.isEqualTo("Message = {headers={header1={42}, header2={false}, header3={\"Hello world\"}}, payload=" + expectedPayloadString + "}");
.isEqualTo("Message = {headers={header1={42}, header2={false}, header3={\"Hello world\"}}, payload=" + expectedPayloadString + "...}");
}

@Test
Expand All @@ -75,4 +75,15 @@ public void toDebugString_largePayload_noTruncate_generatesCorrectString() {
assertThat(BaseEventStreamAsyncAws4Signer.toDebugString(m, false))
.isEqualTo("Message = {headers={header1={42}, header2={false}, header3={\"Hello world\"}}, payload=" + expectedPayloadString + "}");
}

@Test
public void toDebugString_smallPayload_truncate_doesNotAddEllipsis() {
byte[] payload = new byte[8];
new Random().nextBytes(payload);
Message m = new Message(headers, payload);

String expectedPayloadString = BinaryUtils.toHex(payload);
assertThat(BaseEventStreamAsyncAws4Signer.toDebugString(m, true))
.isEqualTo("Message = {headers={header1={42}, header2={false}, header3={\"Hello world\"}}, payload=" + expectedPayloadString + "}");
}
}