Skip to content

Commit d96469d

Browse files
author
Andrey Ershov
committed
Better logging for TLS message on non-secure transport channel (#45835)
This commit enhances logging for 2 cases: 1. If non-TLS enabled node receives transport message from TLS enabled node on transport port. 2. If non-TLS enabled node receives HTTPs request on transport port. (cherry picked from commit 4f52ebd)
1 parent 1b90019 commit d96469d

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

server/src/main/java/org/elasticsearch/transport/TcpTransport.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,9 @@ public void onException(TcpChannel channel, Exception e) {
607607
BytesArray message = new BytesArray(e.getMessage().getBytes(StandardCharsets.UTF_8));
608608
outboundHandler.sendBytes(channel, message, ActionListener.wrap(() -> CloseableChannel.closeChannel(channel)));
609609
}
610+
} else if (e instanceof StreamCorruptedException) {
611+
logger.warn(() -> new ParameterizedMessage("{}, [{}], closing connection", e.getMessage(), channel));
612+
CloseableChannel.closeChannel(channel);
610613
} else {
611614
logger.warn(() -> new ParameterizedMessage("exception caught on transport layer [{}], closing connection", channel), e);
612615
// close the channel, which will cause a node to be disconnected if relevant
@@ -738,11 +741,17 @@ private static int readHeaderBuffer(BytesReference headerBuffer) throws IOExcept
738741
throw new TcpTransport.HttpOnTransportException("This is not an HTTP port");
739742
}
740743

741-
throw new StreamCorruptedException("invalid internal transport message format, got ("
742-
+ Integer.toHexString(headerBuffer.get(0) & 0xFF) + ","
743-
+ Integer.toHexString(headerBuffer.get(1) & 0xFF) + ","
744-
+ Integer.toHexString(headerBuffer.get(2) & 0xFF) + ","
745-
+ Integer.toHexString(headerBuffer.get(3) & 0xFF) + ")");
744+
String firstBytes = "("
745+
+ Integer.toHexString(headerBuffer.get(0) & 0xFF) + ","
746+
+ Integer.toHexString(headerBuffer.get(1) & 0xFF) + ","
747+
+ Integer.toHexString(headerBuffer.get(2) & 0xFF) + ","
748+
+ Integer.toHexString(headerBuffer.get(3) & 0xFF) + ")";
749+
750+
if (appearsToBeTLS(headerBuffer)) {
751+
throw new StreamCorruptedException("SSL/TLS request received but SSL/TLS is not enabled on this node, got " + firstBytes);
752+
}
753+
754+
throw new StreamCorruptedException("invalid internal transport message format, got " + firstBytes);
746755
}
747756
final int messageLength = headerBuffer.getInt(TcpHeader.MARKER_BYTES_SIZE);
748757

@@ -775,6 +784,10 @@ private static boolean appearsToBeHTTP(BytesReference headerBuffer) {
775784
bufferStartsWith(headerBuffer, "TRACE");
776785
}
777786

787+
private static boolean appearsToBeTLS(BytesReference headerBuffer) {
788+
return headerBuffer.get(0) == 0x16 && headerBuffer.get(1) == 0x03;
789+
}
790+
778791
private static boolean bufferStartsWith(BytesReference buffer, String method) {
779792
char[] chars = method.toCharArray();
780793
for (int i = 0; i < chars.length; i++) {

server/src/test/java/org/elasticsearch/transport/TcpTransportTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,32 @@ public void testInvalidHeader() throws IOException {
288288
}
289289
}
290290

291+
public void testTLSHeader() throws IOException {
292+
BytesStreamOutput streamOutput = new BytesStreamOutput(1 << 14);
293+
294+
streamOutput.write(0x16);
295+
streamOutput.write(0x03);
296+
byte byte1 = randomByte();
297+
streamOutput.write(byte1);
298+
byte byte2 = randomByte();
299+
streamOutput.write(byte2);
300+
streamOutput.write(randomByte());
301+
streamOutput.write(randomByte());
302+
streamOutput.write(randomByte());
303+
304+
try {
305+
BytesReference bytes = streamOutput.bytes();
306+
TcpTransport.decodeFrame(bytes);
307+
fail("Expected exception");
308+
} catch (Exception ex) {
309+
assertThat(ex, instanceOf(StreamCorruptedException.class));
310+
String expected = "SSL/TLS request received but SSL/TLS is not enabled on this node, got (16,3,"
311+
+ Integer.toHexString(byte1 & 0xFF) + ","
312+
+ Integer.toHexString(byte2 & 0xFF) + ")";
313+
assertEquals(expected, ex.getMessage());
314+
}
315+
}
316+
291317
public void testHTTPHeader() throws IOException {
292318
String[] httpHeaders = {"GET", "POST", "PUT", "HEAD", "DELETE", "OPTIONS", "PATCH", "TRACE"};
293319

0 commit comments

Comments
 (0)