Skip to content
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

Address secure Websocket changes in server 2.9.19 #931

Merged
merged 5 commits into from
Jun 21, 2023
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
21 changes: 13 additions & 8 deletions src/main/java/io/nats/client/impl/NatsConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,22 +543,27 @@ void checkVersionRequirements() throws IOException {
}

void upgradeToSecureIfNeeded(NatsUri nuri) throws IOException {
Options opts = getOptions();
ServerInfo info = getInfo();
Options clientOptions = getOptions();
ServerInfo serverInfo = getInfo();
boolean before2_9_19 = serverInfo.isOlderThanVersion("2.9.19");

boolean isTLSRequired = opts.isTLSRequired();
boolean isTLSRequired = clientOptions.isTLSRequired();
boolean upgradeRequired = isTLSRequired;
if (isTLSRequired && nuri.isWebsocket()) {
// We are already communicating over "https" websocket, so
// do NOT try to upgrade to secure.
isTLSRequired = false;
if (before2_9_19) {
isTLSRequired = false;
}
upgradeRequired = false;
}
if (isTLSRequired && !info.isTLSRequired()) {
if (isTLSRequired && !serverInfo.isTLSRequired()) {
throw new IOException("SSL connection wanted by client.");
} else if (!isTLSRequired && info.isTLSRequired()) {
}
else if (!isTLSRequired && serverInfo.isTLSRequired()) {
throw new IOException("SSL required by server.");
}

if (isTLSRequired) {
if (upgradeRequired) {
this.dataPort.upgradeToSecure();
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/io/nats/client/ConnectTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void testConnectNoRandomize() throws Exception {
@Test
public void testFailWithMissingLineFeedAfterInfo() {
assertThrows(IOException.class, () -> {
String badInfo = "{\"server_id\":\"test\"}\rmore stuff";
String badInfo = "{\"server_id\":\"test\", \"version\":\"9.9.99\"}\rmore stuff";
try (NatsServerProtocolMock ts = new NatsServerProtocolMock(null, badInfo)) {
Options options = new Options.Builder().server(ts.getURI()).reconnectWait(Duration.ofDays(1)).build();
Nats.connect(options);
Expand All @@ -209,7 +209,7 @@ public void testFailWithMissingLineFeedAfterInfo() {
@Test
public void testFailWithStuffAfterInitialInfo() {
assertThrows(IOException.class, () -> {
String badInfo = "{\"server_id\":\"test\"}\r\nmore stuff";
String badInfo = "{\"server_id\":\"test\", \"version\":\"9.9.99\"}\r\nmore stuff";
try (NatsServerProtocolMock ts = new NatsServerProtocolMock(null, badInfo)) {
Options options = new Options.Builder().server(ts.getURI()).reconnectWait(Duration.ofDays(1)).build();
Nats.connect(options);
Expand All @@ -220,7 +220,7 @@ public void testFailWithStuffAfterInitialInfo() {
@Test
public void testFailWrongInitialInfoOP() {
assertThrows(IOException.class, () -> {
String badInfo = "PING {\"server_id\":\"test\"}\r\n"; // wrong op code
String badInfo = "PING {\"server_id\":\"test\", \"version\":\"9.9.99\"}\r\n"; // wrong op code
try (NatsServerProtocolMock ts = new NatsServerProtocolMock(null, badInfo)) {
ts.useCustomInfoAsFullInfo();
Options options = new Options.Builder().server(ts.getURI()).reconnectWait(Duration.ofDays(1)).build();
Expand Down Expand Up @@ -330,7 +330,7 @@ public void testSlowConnectionNoTimeout() throws Exception {

@Test
public void testTimeCheckCoverage() throws Exception {
try (NatsTestServer ts = new NatsTestServer(Options.DEFAULT_PORT, false)) {
try (NatsTestServer ts = new NatsTestServer(false)) {
Options options = new Options.Builder().server(ts.getURI()).traceConnection().build();
assertCanConnect(options);
}
Expand All @@ -348,7 +348,7 @@ public void testConnectExceptionHasURLS() {

@Test
public void testFlushBuffer() throws Exception {
try (NatsTestServer ts = new NatsTestServer(Options.DEFAULT_PORT, false)) {
try (NatsTestServer ts = new NatsTestServer(false)) {
Connection nc = standardConnection(ts.getURI());

// test connected
Expand All @@ -370,7 +370,7 @@ public void testFlushBuffer() throws Exception {

@Test
public void testFlushBufferThreadSafety() throws Exception {
try (NatsTestServer ts = new NatsTestServer(Options.DEFAULT_PORT, false)) {
try (NatsTestServer ts = new NatsTestServer(false)) {
Connection nc = standardConnection(ts.getURI());

// use two latches to sync the threads as close as
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/nats/client/NatsServerProtocolMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void accept() {
writer.write("INFO" + this.separator + customInfo + "\r\n");
}
} else {
writer.write("INFO" + this.separator + "{\"server_id\":\"test\", \"nonce\":\""+encoded+"\", \"headers\":true}\r\n");
writer.write("INFO" + this.separator + "{\"server_id\":\"test\", \"version\":\"9.9.99\", \"nonce\":\""+encoded+"\", \"headers\":true}\r\n");
}
writer.flush();
this.progress = Progress.SENT_INFO;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/nats/client/PublishTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void testThrowsWithoutSubject() {
@Test
public void testThrowsIfheadersNotSupported() {
assertThrows(IllegalArgumentException.class, () -> {
String customInfo = "{\"server_id\":\"test\"}";
String customInfo = "{\"server_id\":\"test\", \"version\":\"9.9.99\"}";

try (NatsServerProtocolMock ts = new NatsServerProtocolMock(null, customInfo);
Connection nc = Nats.connect(ts.getURI())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void testCloseCount() throws Exception {
public void testDiscoveredServersCountAndListenerInOptions() throws Exception {

try (NatsTestServer ts = new NatsTestServer()) {
String customInfo = "{\"server_id\":\"myid\",\"connect_urls\": [\""+ts.getURI()+"\"]}";
String customInfo = "{\"server_id\":\"myid\", \"version\":\"9.9.99\",\"connect_urls\": [\""+ts.getURI()+"\"]}";
try (NatsServerProtocolMock ts2 = new NatsServerProtocolMock(null, customInfo)) {
TestHandler handler = new TestHandler();
Options options = new Options.Builder().
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/io/nats/client/impl/InfoHandlerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class InfoHandlerTests {
@Test
public void testInitialInfo() throws IOException, InterruptedException {
String customInfo = "{\"server_id\":\"myid\"}";
String customInfo = "{\"server_id\":\"myid\", \"version\":\"9.9.99\"}";

try (NatsServerProtocolMock ts = new NatsServerProtocolMock(null, customInfo)) {
Connection nc = Nats.connect(ts.getURI());
Expand All @@ -46,7 +46,7 @@ public void testInitialInfo() throws IOException, InterruptedException {

@Test
public void testUnsolicitedInfo() throws IOException, InterruptedException, ExecutionException {
String customInfo = "{\"server_id\":\"myid\"}";
String customInfo = "{\"server_id\":\"myid\", \"version\":\"9.9.99\"}";
CompletableFuture<Boolean> gotPong = new CompletableFuture<>();
CompletableFuture<Boolean> sendInfo = new CompletableFuture<>();

Expand All @@ -62,7 +62,7 @@ public void testUnsolicitedInfo() throws IOException, InterruptedException, Exec
}

System.out.println("*** Mock Server @" + ts.getPort() + " sending INFO ...");
w.write("INFO {\"server_id\":\"replacement\"}\r\n");
w.write("INFO {\"server_id\":\"replacement\", \"version\":\"9.9.99\"}\r\n");
w.flush();

System.out.println("*** Mock Server @" + ts.getPort() + " sending PING ...");
Expand Down Expand Up @@ -108,7 +108,7 @@ public void testUnsolicitedInfo() throws IOException, InterruptedException, Exec

@Test
public void testLDM() throws IOException, InterruptedException, ExecutionException, TimeoutException {
String customInfo = "{\"server_id\":\"myid\", \"ldm\":true}";
String customInfo = "{\"server_id\":\"myid\", \"version\":\"9.9.99\", \"ldm\":true}";
CompletableFuture<Boolean> gotPong = new CompletableFuture<>();
CompletableFuture<Boolean> sendInfo = new CompletableFuture<>();
CompletableFuture<ConnectionListener.Events> connectLDM = new CompletableFuture<>();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/nats/client/impl/ReconnectTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public void testReconnectToSecondServerFromInfo() throws Exception {

try (NatsTestServer ts = new NatsTestServer()) {
String striped = ts.getURI().substring("nats://".length()); // info doesn't have protocol
String customInfo = "{\"server_id\":\"myid\",\"connect_urls\": [\""+striped+"\"]}";
String customInfo = "{\"server_id\":\"myid\", \"version\":\"9.9.99\",\"connect_urls\": [\""+striped+"\"]}";
try (NatsServerProtocolMock ts2 = new NatsServerProtocolMock(null, customInfo)) {
Options options = new Options.Builder().
server(ts2.getURI()).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testTLSRequestReply() throws Exception {
SSLContext ctx = TestSSLUtils.createTestSSLContext();
Options options = new Options.Builder().
httpRequestInterceptor(req -> {
// Ideally we could validate that this header was sent to NATS server 
// Ideally we could validate that this header was sent to NATS server
req.getHeaders().add("X-Ignored", "VALUE");
}).
server(ts.getLocalhostUri("wss")).
Expand Down