Skip to content

Commit 689e45e

Browse files
committed
Propagate SSL failures for SSL disabled/required
If a client requires SSL but the server has SSL disabled, the failure is now correctly propagated to the client. Previously, the connection was hanging. [resolves #187]
1 parent 5688a12 commit 689e45e

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/main/java/io/r2dbc/postgresql/client/SSLSessionHandlerAdapter.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.netty.util.concurrent.Future;
2626
import io.netty.util.concurrent.GenericFutureListener;
2727
import io.r2dbc.postgresql.message.frontend.SSLRequest;
28+
import io.r2dbc.spi.R2dbcPermissionDeniedException;
2829
import reactor.core.publisher.Mono;
2930

3031
import javax.net.ssl.SSLEngine;
@@ -87,21 +88,26 @@ public void operationComplete(Future<Channel> future) throws Exception {
8788
if (this.sslConfig.getHostnameVerifier().verify(hostName, this.sslEngine.getSession())) {
8889
this.handshakeFuture.complete(null);
8990
} else {
90-
this.handshakeFuture.completeExceptionally(new IllegalStateException(String.format("The hostname '%s' could not be verified.", socketAddress.getAddress().toString())));
91+
this.handshakeFuture.completeExceptionally(new PostgresqlSslException(String.format("The hostname '%s' could not be verified.", socketAddress.getAddress().toString())));
9192
}
9293
}
9394

9495
private void processSslDisabled(ChannelHandlerContext ctx, Object msg) {
9596
if (this.sslConfig.getSslMode().requireSsl()) {
96-
throw new IllegalStateException("Server support for SSL connection is disabled, but client was configured with SSL mode " + this.sslConfig.getSslMode());
97+
PostgresqlSslException e =
98+
new PostgresqlSslException("Server support for SSL connection is disabled, but client was configured with SSL mode " + this.sslConfig.getSslMode());
99+
this.handshakeFuture.completeExceptionally(e);
97100
} else {
98101
this.handshakeFuture.complete(null);
99102
}
100103
}
101104

102105
private void processSslEnabled(ChannelHandlerContext ctx, Object msg) {
103106
if (this.sslConfig.getSslMode() == SSLMode.DISABLE) {
104-
throw new IllegalStateException("Server requires SSL handshake, but client was configured with SSL mode DISABLE");
107+
108+
PostgresqlSslException e = new PostgresqlSslException("Server requires SSL handshake, but client was configured with SSL mode DISABLE");
109+
this.handshakeFuture.completeExceptionally(e);
110+
return;
105111
}
106112
ctx.channel().pipeline()
107113
.addFirst(this.sslHandler)
@@ -117,4 +123,15 @@ public void handlerAdded(ChannelHandlerContext ctx) {
117123
Mono<Void> getHandshake() {
118124
return Mono.fromFuture(this.handshakeFuture);
119125
}
126+
127+
/**
128+
* Postgres-specific {@link R2dbcPermissionDeniedException}.
129+
*/
130+
static final class PostgresqlSslException extends R2dbcPermissionDeniedException {
131+
132+
133+
PostgresqlSslException(String msg) {
134+
super(msg);
135+
}
136+
}
120137
}

src/test/java/io/r2dbc/postgresql/client/ReactorNettyClientIntegrationTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,13 @@ void constructorNoHost() {
161161

162162
@BeforeEach
163163
void createTable() {
164+
dropTable();
164165
SERVER.getJdbcOperations().execute("CREATE TABLE test ( value INTEGER )");
165166
}
166167

167168
@AfterEach
168169
void dropTable() {
169-
SERVER.getJdbcOperations().execute("DROP TABLE test");
170+
SERVER.getJdbcOperations().execute("DROP TABLE IF EXISTS test");
170171
}
171172

172173
@Test
@@ -396,7 +397,7 @@ void invalidServerCertificate() {
396397
.sslRootCert(SERVER.getClientCrt()),
397398
c -> c
398399
.as(StepVerifier::create)
399-
.expectError()
400+
.expectError(R2dbcNonTransientResourceException.class)
400401
.verify());
401402
}
402403

@@ -664,7 +665,7 @@ void verifyFullFailedWithWrongHost() {
664665
.sslMode(SSLMode.VERIFY_FULL),
665666
c -> c
666667
.as(StepVerifier::create)
667-
.verifyError(R2dbcNonTransientResourceException.class));
668+
.verifyError(R2dbcPermissionDeniedException.class));
668669
}
669670

670671
@Test

0 commit comments

Comments
 (0)