-
Notifications
You must be signed in to change notification settings - Fork 154
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
tls_available support #1127
tls_available support #1127
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1710,6 +1710,10 @@ else if (useDefaultTls) { | |
} | ||
} | ||
|
||
if (tlsFirst && sslContext == null) { | ||
throw new IllegalStateException("SSL context required for tls handshake first"); | ||
} | ||
|
||
if (credentialPath != null) { | ||
File file = new File(credentialPath).getAbsoluteFile(); | ||
authHandler = Nats.credentials(file.toString()); | ||
|
@@ -2101,10 +2105,10 @@ public int getMaxControlLine() { | |
|
||
/** | ||
* | ||
* @return true if there is an sslContext for this Options, otherwise false, see {@link Builder#secure() secure()} in the builder doc | ||
* @return true if there is an sslContext for these Options, otherwise false, see {@link Builder#secure() secure()} in the builder doc | ||
*/ | ||
public boolean isTLSRequired() { | ||
return tlsFirst || this.sslContext != null; | ||
return sslContext != null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reflects checking tls first and ssl context in the builder |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ public class ServerInfo { | |
private final boolean headersSupported; | ||
private final boolean authRequired; | ||
private final boolean tlsRequired; | ||
private final boolean tlsAvailable; | ||
private final long maxPayload; | ||
private final List<String> connectURLs; | ||
private final int protocolVersion; | ||
|
@@ -67,7 +68,8 @@ public ServerInfo(String json) { | |
headersSupported = readBoolean(jv, HEADERS); | ||
authRequired = readBoolean(jv, AUTH_REQUIRED); | ||
nonce = readBytes(jv, NONCE); | ||
tlsRequired = readBoolean(jv, TLS); | ||
tlsRequired = readBoolean(jv, TLS_REQUIRED); | ||
tlsAvailable = readBoolean(jv, TLS_AVAILABLE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 71: Better variable name |
||
lameDuckMode = readBoolean(jv, LAME_DUCK_MODE); | ||
jetStream = readBoolean(jv, JETSTREAM); | ||
port = readInteger(jv, PORT, 0); | ||
|
@@ -121,6 +123,10 @@ public boolean isTLSRequired() { | |
return this.tlsRequired; | ||
} | ||
|
||
public boolean isTLSAvailable() { | ||
return tlsAvailable; | ||
} | ||
|
||
public long getMaxPayload() { | ||
return this.maxPayload; | ||
} | ||
|
@@ -181,6 +187,7 @@ public String toString() { | |
", headersSupported=" + headersSupported + | ||
", authRequired=" + authRequired + | ||
", tlsRequired=" + tlsRequired + | ||
", tlsAvailable=" + tlsAvailable + | ||
", maxPayload=" + maxPayload + | ||
", connectURLs=" + connectURLs + | ||
", protocolVersion=" + protocolVersion + | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -584,36 +584,25 @@ void checkVersionRequirements() throws IOException { | |
} | ||
|
||
void upgradeToSecureIfNeeded(NatsUri nuri) throws IOException { | ||
Options clientOptions = getOptions(); | ||
if (clientOptions.isTlsFirst()) { | ||
this.dataPort.upgradeToSecure(); | ||
} | ||
else { | ||
ServerInfo serverInfo = getInfo(); | ||
boolean before2_9_19 = serverInfo.isOlderThanVersion("2.9.19"); | ||
|
||
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. | ||
if (before2_9_19) { | ||
isTLSRequired = false; | ||
} | ||
upgradeRequired = false; | ||
} | ||
if (isTLSRequired && !serverInfo.isTLSRequired()) { | ||
throw new IOException("SSL connection wanted by client."); | ||
} | ||
else if (!isTLSRequired && serverInfo.isTLSRequired()) { | ||
throw new IOException("SSL required by server."); | ||
// When already communicating over "https" websocket, do NOT try to upgrade to secure. | ||
if (!nuri.isWebsocket()) { | ||
if (options.isTlsFirst()) { | ||
dataPort.upgradeToSecure(); | ||
} | ||
if (upgradeRequired) { | ||
this.dataPort.upgradeToSecure(); | ||
else { | ||
ServerInfo serverInfo = getInfo(); | ||
if (options.isTLSRequired()) { | ||
if (!serverInfo.isTLSRequired() && !serverInfo.isTLSAvailable()) { | ||
throw new IOException("SSL connection wanted by client."); | ||
} | ||
dataPort.upgradeToSecure(); | ||
} | ||
else if (serverInfo.isTLSRequired()) { | ||
throw new IOException("SSL required by server."); | ||
} | ||
} | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole block is simplified.
|
||
// Called from reader/writer thread | ||
void handleCommunicationIssue(Exception io) { | ||
// If we are connecting or disconnecting, note exception and leave | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
INFO {"server_id": "serverId","server_name": "serverName","version": "1.2.3","go": "go0.0.0","host": "host","port": 7777,"headersSupported": true,"auth_required": true,"tls_required": true,"max_payload": 100000000000,"proto": 1,"ldm": true,"jetstream": true,"client_id": 42,"client_ip": "127.0.0.1","cluster": "cluster","connect_urls":["url0", "url1"],"nonce":"<encoded>","headers": true,} | ||
INFO {"server_id": "serverId","server_name": "serverName","version": "1.2.3","go": "go0.0.0","host": "host","port": 7777,"headersSupported": true,"auth_required": true,"tls_required": true,"tls_available": true,"max_payload": 100000000000,"proto": 1,"ldm": true,"jetstream": true,"client_id": 42,"client_ip": "127.0.0.1","cluster": "cluster","connect_urls":["url0", "url1"],"nonce":"<encoded>","headers": true,} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not used to check this before, but if there is no SSL Context, we can't do any tls anyway, so it's a fast fail essentially.