Skip to content

Commit ddee000

Browse files
committed
Use ChannelCustomizer to set SNI
References #11, #12
1 parent f6bcc34 commit ddee000

File tree

10 files changed

+57
-125
lines changed

10 files changed

+57
-125
lines changed

src/docs/asciidoc/api.adoc

-4
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,6 @@ The server certificate chain and the client private key are the typical
201201
elements that need to be configured.
202202
|The JDK trust manager and no client private key.
203203

204-
|`tls#sslParameters`
205-
|Set the `SSLParameters` for the `SSLEngine`. The provided parameters will be merged into the parameters returned by `SSLEngine#getSSLParameters()`. Can be used to set SNI information with `SSLParameters#setServerNames(List)`.
206-
|`null`
207-
208204
|`tls#trustEverything`
209205
|Helper to configure a `SslContext` that trusts all server certificates
210206
and does not use a client private key. **Only for development**.

src/main/java/com/rabbitmq/stream/ChannelCustomizer.java

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.rabbitmq.stream;
1515

1616
import io.netty.channel.Channel;
17+
import java.util.Objects;
1718

1819
/**
1920
* An extension point to customize Netty's {@link io.netty.channel.Channel}s used for connection.
@@ -23,4 +24,12 @@
2324
public interface ChannelCustomizer {
2425

2526
void customize(Channel channel);
27+
28+
default ChannelCustomizer andThen(ChannelCustomizer after) {
29+
Objects.requireNonNull(after);
30+
return ch -> {
31+
customize(ch);
32+
after.customize(ch);
33+
};
34+
}
2635
}

src/main/java/com/rabbitmq/stream/EnvironmentBuilder.java

-17
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import java.util.List;
2626
import java.util.Map;
2727
import java.util.concurrent.ScheduledExecutorService;
28-
import javax.net.ssl.SSLEngine;
29-
import javax.net.ssl.SSLParameters;
3028

3129
/**
3230
* API to configure and create an {@link Environment}.
@@ -334,21 +332,6 @@ interface TlsConfiguration {
334332
*/
335333
TlsConfiguration sslContext(SslContext sslContext);
336334

337-
/**
338-
* Set {@link SSLParameters} for the {@link javax.net.ssl.SSLEngine}.
339-
*
340-
* <p>Provided {@link SSLParameters} will be merged into the {@link SSLParameters} returned by
341-
* {@link SSLEngine#getSSLParameters()}, that is non-null property values from the provided
342-
* instance will override those in the original instance.
343-
*
344-
* <p>This is typically use to provide SNI information with {@link
345-
* SSLParameters#setServerNames(List)}.
346-
*
347-
* @param sslParameters
348-
* @return the TLS configuration helper
349-
*/
350-
TlsConfiguration sslParameters(SSLParameters sslParameters);
351-
352335
/**
353336
* Convenience method to set a {@link SslContext} that trusts all servers.
354337
*

src/main/java/com/rabbitmq/stream/impl/Client.java

+4-12
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,13 @@ public void write(
285285
if (parameters.sslContext != null) {
286286
SslHandler sslHandler =
287287
parameters.sslContext.newHandler(ch.alloc(), parameters.host, parameters.port);
288-
SSLEngine sslEngine = sslHandler.engine();
289-
SSLParameters sslParameters = sslEngine.getSSLParameters();
288+
290289
if (parameters.tlsHostnameVerification) {
290+
SSLEngine sslEngine = sslHandler.engine();
291+
SSLParameters sslParameters = sslEngine.getSSLParameters();
291292
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
293+
sslEngine.setSSLParameters(sslParameters);
292294
}
293-
if (parameters.sslParameters != null) {
294-
Utils.mergeSslParameters(sslParameters, parameters.sslParameters);
295-
}
296-
sslEngine.setSSLParameters(sslParameters);
297295

298296
ch.pipeline().addFirst("ssl", sslHandler);
299297
}
@@ -1923,7 +1921,6 @@ public static class ClientParameters {
19231921
private ChunkChecksum chunkChecksum = JdkChunkChecksum.CRC32_SINGLETON;
19241922
private MetricsCollector metricsCollector = NoOpMetricsCollector.SINGLETON;
19251923
private SslContext sslContext;
1926-
private SSLParameters sslParameters;
19271924
private boolean tlsHostnameVerification = true;
19281925
private ByteBufAllocator byteBufAllocator;
19291926
private Duration rpcTimeout;
@@ -2070,11 +2067,6 @@ public ClientParameters sslContext(SslContext sslContext) {
20702067
return this;
20712068
}
20722069

2073-
public ClientParameters sslParameters(SSLParameters sslParameters) {
2074-
this.sslParameters = sslParameters;
2075-
return this;
2076-
}
2077-
20782070
public ClientParameters tlsHostnameVerification(boolean tlsHostnameVerification) {
20792071
this.tlsHostnameVerification = tlsHostnameVerification;
20802072
return this;

src/main/java/com/rabbitmq/stream/impl/StreamEnvironment.java

-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ class StreamEnvironment implements Environment {
145145
clientParametersPrototype.sslContext(sslContext);
146146
clientParametersPrototype.tlsHostnameVerification(
147147
tlsConfiguration.hostnameVerificationEnabled());
148-
clientParametersPrototype.sslParameters(tlsConfiguration.sslParameters());
149148

150149
} catch (SSLException e) {
151150
throw new StreamException("Error while creating Netty SSL context", e);

src/main/java/com/rabbitmq/stream/impl/StreamEnvironmentBuilder.java

-12
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.concurrent.ScheduledExecutorService;
3939
import java.util.stream.Collectors;
4040
import javax.net.ssl.SSLException;
41-
import javax.net.ssl.SSLParameters;
4241
import org.slf4j.Logger;
4342
import org.slf4j.LoggerFactory;
4443

@@ -301,7 +300,6 @@ static final class DefaultTlsConfiguration implements TlsConfiguration {
301300
private boolean enabled = false;
302301
private boolean hostnameVerification = true;
303302
private SslContext sslContext;
304-
private SSLParameters sslParameters;
305303

306304
private DefaultTlsConfiguration(EnvironmentBuilder environmentBuilder) {
307305
this.environmentBuilder = environmentBuilder;
@@ -325,12 +323,6 @@ public TlsConfiguration sslContext(SslContext sslContext) {
325323
return this;
326324
}
327325

328-
@Override
329-
public TlsConfiguration sslParameters(SSLParameters sslParameters) {
330-
this.sslParameters = sslParameters;
331-
return this;
332-
}
333-
334326
@Override
335327
public TlsConfiguration trustEverything() {
336328
LOGGER.warn(
@@ -368,9 +360,5 @@ public boolean hostnameVerificationEnabled() {
368360
public SslContext sslContext() {
369361
return sslContext;
370362
}
371-
372-
public SSLParameters sslParameters() {
373-
return sslParameters;
374-
}
375363
}
376364
}

src/main/java/com/rabbitmq/stream/impl/Utils.java

-67
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.function.LongConsumer;
2828
import java.util.function.Predicate;
2929
import java.util.stream.Collectors;
30-
import javax.net.ssl.SSLParameters;
3130
import javax.net.ssl.X509TrustManager;
3231
import org.slf4j.Logger;
3332
import org.slf4j.LoggerFactory;
@@ -216,72 +215,6 @@ public X509Certificate[] getAcceptedIssuers() {
216215
}
217216
}
218217

219-
static void mergeSslParameters(SSLParameters original, SSLParameters provided) {
220-
if (notEmptyArray(provided.getCipherSuites())) {
221-
LOGGER.debug(
222-
"Setting SSLParameters cipherSuites from {} to {}",
223-
arrayToString(original.getCipherSuites()),
224-
arrayToString(provided.getCipherSuites()));
225-
original.setCipherSuites(provided.getCipherSuites());
226-
}
227-
if (notEmptyArray(provided.getProtocols())) {
228-
LOGGER.debug(
229-
"Setting SSLParameters protocols from {} to {}",
230-
arrayToString(original.getProtocols()),
231-
arrayToString(provided.getProtocols()));
232-
original.setProtocols(provided.getProtocols());
233-
}
234-
if (original.getWantClientAuth() != provided.getWantClientAuth()) {
235-
LOGGER.debug(
236-
"Setting SSLParameters wantClientAuth from {} to {}",
237-
original.getWantClientAuth(),
238-
provided.getWantClientAuth());
239-
original.setWantClientAuth(provided.getWantClientAuth());
240-
}
241-
if (original.getNeedClientAuth() != provided.getNeedClientAuth()) {
242-
LOGGER.debug(
243-
"Setting SSLParameters needClientAuth from {} to {}",
244-
original.getNeedClientAuth(),
245-
provided.getNeedClientAuth());
246-
original.setNeedClientAuth(provided.getNeedClientAuth());
247-
}
248-
if (notNullOrBlank(provided.getEndpointIdentificationAlgorithm())) {
249-
LOGGER.debug(
250-
"Setting SSLParameters endpointIdentificationAlgorithm from {} to {}",
251-
original.getEndpointIdentificationAlgorithm(),
252-
provided.getEndpointIdentificationAlgorithm());
253-
original.setEndpointIdentificationAlgorithm(provided.getEndpointIdentificationAlgorithm());
254-
}
255-
if (provided.getAlgorithmConstraints() != null) {
256-
LOGGER.debug(
257-
"Setting SSLParameters algorithmConstraints from {} to {}",
258-
original.getAlgorithmConstraints(),
259-
provided.getAlgorithmConstraints());
260-
original.setAlgorithmConstraints(provided.getAlgorithmConstraints());
261-
}
262-
if (provided.getServerNames() != null) {
263-
LOGGER.debug(
264-
"Setting SSLParameters serverNames from {} to {}",
265-
original.getServerNames(),
266-
provided.getServerNames());
267-
original.setServerNames(provided.getServerNames());
268-
}
269-
if (provided.getSNIMatchers() != null) {
270-
LOGGER.debug(
271-
"Setting SSLParameters SNIMatchers from {} to {}",
272-
original.getSNIMatchers(),
273-
provided.getSNIMatchers());
274-
original.setSNIMatchers(provided.getSNIMatchers());
275-
}
276-
if (original.getUseCipherSuitesOrder() != provided.getUseCipherSuitesOrder()) {
277-
LOGGER.debug(
278-
"Setting SSLParameters useCipherSuitesOrder from {} to {}",
279-
original.getUseCipherSuitesOrder(),
280-
provided.getUseCipherSuitesOrder());
281-
original.setUseCipherSuitesOrder(provided.getUseCipherSuitesOrder());
282-
}
283-
}
284-
285218
private static boolean notNullOrBlank(String str) {
286219
return str != null && !str.trim().isEmpty();
287220
}

src/main/java/com/rabbitmq/stream/perf/StreamPerfTest.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.rabbitmq.stream.Address;
2020
import com.rabbitmq.stream.AddressResolver;
2121
import com.rabbitmq.stream.ByteCapacity;
22+
import com.rabbitmq.stream.ChannelCustomizer;
2223
import com.rabbitmq.stream.Codec;
2324
import com.rabbitmq.stream.ConfirmationHandler;
2425
import com.rabbitmq.stream.Constants;
@@ -46,6 +47,7 @@
4647
import io.netty.buffer.ByteBufAllocatorMetric;
4748
import io.netty.buffer.ByteBufAllocatorMetricProvider;
4849
import io.netty.handler.ssl.SslContextBuilder;
50+
import io.netty.handler.ssl.SslHandler;
4951
import io.netty.util.internal.PlatformDependent;
5052
import java.io.PrintStream;
5153
import java.io.PrintWriter;
@@ -501,22 +503,31 @@ public Integer call() throws Exception {
501503
.maxTrackingConsumersByConnection(this.trackingConsumersByConnection)
502504
.maxConsumersByConnection(this.consumersByConnection);
503505

506+
ChannelCustomizer channelCustomizer = channel -> {};
507+
504508
if (tls) {
505509
TlsConfiguration tlsConfiguration = environmentBuilder.tls();
506510
tlsConfiguration =
507511
tlsConfiguration.sslContext(
508512
SslContextBuilder.forClient()
509513
.trustManager(Utils.TRUST_EVERYTHING_TRUST_MANAGER)
510514
.build());
515+
environmentBuilder = tlsConfiguration.environmentBuilder();
511516
if (!this.sniServerNames.isEmpty()) {
512-
SSLParameters sslParameters = new SSLParameters();
513-
sslParameters.setServerNames(this.sniServerNames);
514-
tlsConfiguration = tlsConfiguration.sslParameters(sslParameters);
517+
channelCustomizer =
518+
channelCustomizer.andThen(
519+
ch -> {
520+
SslHandler sslHandler = ch.pipeline().get(SslHandler.class);
521+
if (sslHandler != null) {
522+
SSLParameters sslParameters = sslHandler.engine().getSSLParameters();
523+
sslParameters.setServerNames(this.sniServerNames);
524+
sslHandler.engine().setSSLParameters(sslParameters);
525+
}
526+
});
515527
}
516-
environmentBuilder = tlsConfiguration.environmentBuilder();
517528
}
518529

519-
Environment environment = environmentBuilder.build();
530+
Environment environment = environmentBuilder.channelCustomizer(channelCustomizer).build();
520531
shutdownService.wrap(closeStep("Closing environment(s)", () -> environment.close()));
521532

522533
streams = Utils.streams(this.streamCount, this.streams);

src/test/java/com/rabbitmq/stream/impl/StreamEnvironmentTest.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.rabbitmq.stream.Address;
2626
import com.rabbitmq.stream.AuthenticationFailureException;
2727
import com.rabbitmq.stream.BackOffDelayPolicy;
28+
import com.rabbitmq.stream.ChannelCustomizer;
2829
import com.rabbitmq.stream.Constants;
2930
import com.rabbitmq.stream.Consumer;
3031
import com.rabbitmq.stream.Environment;
@@ -37,6 +38,7 @@
3738
import com.rabbitmq.stream.impl.TestUtils.DisabledIfTlsNotEnabled;
3839
import io.netty.channel.EventLoopGroup;
3940
import io.netty.channel.nio.NioEventLoopGroup;
41+
import io.netty.handler.ssl.SslHandler;
4042
import java.net.ConnectException;
4143
import java.nio.charset.StandardCharsets;
4244
import java.time.Duration;
@@ -137,13 +139,20 @@ void environmentCreationShouldSucceedWithUrlContainingAllCorrectInformation() {
137139
@DisabledIfTlsNotEnabled
138140
@Test
139141
void environmentCreationShouldSucceedWhenUsingTls() {
140-
SSLParameters sslParameters = new SSLParameters();
141-
sslParameters.setServerNames(Collections.singletonList(new SNIHostName("localhost")));
142+
ChannelCustomizer channelCustomizer =
143+
ch -> {
144+
SslHandler sslHandler = ch.pipeline().get(SslHandler.class);
145+
if (sslHandler != null) {
146+
SSLParameters sslParameters = sslHandler.engine().getSSLParameters();
147+
sslParameters.setServerNames(Collections.singletonList(new SNIHostName("localhost")));
148+
sslHandler.engine().setSSLParameters(sslParameters);
149+
}
150+
};
142151
environmentBuilder
143152
.uri("rabbitmq-stream+tls://guest:guest@localhost:5551/%2f")
153+
.channelCustomizer(channelCustomizer)
144154
.tls()
145155
.trustEverything()
146-
.sslParameters(sslParameters)
147156
.environmentBuilder()
148157
.build()
149158
.close();

src/test/java/com/rabbitmq/stream/impl/TlsTest.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.assertj.core.api.Assertions.assertThat;
2121
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2222

23+
import com.rabbitmq.stream.ChannelCustomizer;
2324
import com.rabbitmq.stream.ConfirmationHandler;
2425
import com.rabbitmq.stream.Environment;
2526
import com.rabbitmq.stream.OffsetSpecification;
@@ -29,6 +30,7 @@
2930
import com.rabbitmq.stream.impl.TestUtils.DisabledIfTlsNotEnabled;
3031
import io.netty.handler.ssl.SslContext;
3132
import io.netty.handler.ssl.SslContextBuilder;
33+
import io.netty.handler.ssl.SslHandler;
3234
import java.io.File;
3335
import java.io.FileInputStream;
3436
import java.nio.charset.Charset;
@@ -175,10 +177,20 @@ void unverifiedConnection() {
175177
}
176178

177179
@Test
178-
void unverifiedConnectionWithSslParameters() {
179-
SSLParameters sslParameters = new SSLParameters();
180-
sslParameters.setServerNames(Collections.singletonList(new SNIHostName("localhost")));
181-
cf.get(new ClientParameters().sslContext(alwaysTrustSslContext()).sslParameters(sslParameters));
180+
void unverifiedConnectionWithSni() {
181+
ChannelCustomizer channelCustomizer =
182+
ch -> {
183+
SslHandler sslHandler = ch.pipeline().get(SslHandler.class);
184+
if (sslHandler != null) {
185+
SSLParameters sslParameters = sslHandler.engine().getSSLParameters();
186+
sslParameters.setServerNames(Collections.singletonList(new SNIHostName("localhost")));
187+
sslHandler.engine().setSSLParameters(sslParameters);
188+
}
189+
};
190+
cf.get(
191+
new ClientParameters()
192+
.sslContext(alwaysTrustSslContext())
193+
.channelCustomizer(channelCustomizer));
182194
}
183195

184196
@Test

0 commit comments

Comments
 (0)