|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.client.impl; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import io.netty.channel.EventLoopGroup; |
| 23 | +import io.netty.channel.embedded.EmbeddedChannel; |
| 24 | +import io.netty.handler.codec.DecoderException; |
| 25 | +import io.netty.handler.ssl.SslHandshakeCompletionEvent; |
| 26 | +import io.netty.util.concurrent.DefaultThreadFactory; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import javax.net.ssl.SSLException; |
| 30 | +import javax.net.ssl.SSLHandshakeException; |
| 31 | +import org.apache.pulsar.client.api.PulsarClientException; |
| 32 | +import org.apache.pulsar.client.impl.conf.ClientConfigurationData; |
| 33 | +import org.apache.pulsar.client.impl.metrics.InstrumentProvider; |
| 34 | +import org.apache.pulsar.common.util.FutureUtil; |
| 35 | +import org.apache.pulsar.common.util.netty.EventLoopUtil; |
| 36 | +import org.testng.annotations.DataProvider; |
| 37 | +import org.testng.annotations.Test; |
| 38 | + |
| 39 | +public class ClientCnxTlsTest { |
| 40 | + |
| 41 | + @DataProvider |
| 42 | + public Object[][] tlsAuthenticationAlerts() { |
| 43 | + return new Object[][]{ |
| 44 | + {"Received fatal alert: certificate_required"}, |
| 45 | + {"Received fatal alert: bad_certificate"}, |
| 46 | + {"Received fatal alert: unsupported_certificate"}, |
| 47 | + {"Received fatal alert: certificate_revoked"}, |
| 48 | + {"Received fatal alert: certificate_expired"}, |
| 49 | + {"Received fatal alert: certificate_unknown"}, |
| 50 | + {"Received fatal alert: unknown_ca"}, |
| 51 | + {"error:1000045c:SSL routines:OPENSSL_internal:TLSV1_ALERT_CERTIFICATE_REQUIRED"}, |
| 52 | + {"error:10000412:SSL routines:OPENSSL_internal:SSLV3_ALERT_BAD_CERTIFICATE"}, |
| 53 | + {"error:10000418:SSL routines:OPENSSL_internal:TLSV1_ALERT_UNKNOWN_CA"} |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + @Test(dataProvider = "tlsAuthenticationAlerts") |
| 58 | + public void testTlsAuthenticationAlertIsNotRetriable(String message) throws Exception { |
| 59 | + SSLException alert = new SSLHandshakeException(message); |
| 60 | + assertTlsFailure(alert, true, false); |
| 61 | + // TLS 1.3 can finish the local handshake before the server rejects the client certificate. |
| 62 | + assertTlsFailure(alert, false, false); |
| 63 | + } |
| 64 | + |
| 65 | + @DataProvider |
| 66 | + public Object[][] otherConnectionFailures() { |
| 67 | + return new Object[][]{ |
| 68 | + {new IOException("Connection reset")}, |
| 69 | + {new SSLHandshakeException("Remote host terminated the handshake")}, |
| 70 | + {new SSLHandshakeException("Received fatal alert: handshake_failure")}, |
| 71 | + {new SSLHandshakeException("Received fatal alert: protocol_version")}, |
| 72 | + {new SSLHandshakeException("Received fatal alert: bad_certificate_status_response")}, |
| 73 | + {new SSLHandshakeException("PKIX path building failed: unable to find valid certification path")}, |
| 74 | + {new SSLException("TLS handshake timed out")}, |
| 75 | + {new SSLException("certificate_expired")}, |
| 76 | + {new SSLException((String) null)}, |
| 77 | + {new IOException("Received fatal alert: certificate_required")} |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + @Test(dataProvider = "otherConnectionFailures") |
| 82 | + public void testOtherConnectionFailuresRemainRetriable(Throwable cause) throws Exception { |
| 83 | + assertTlsFailure(cause, true, true); |
| 84 | + assertTlsFailure(cause, false, true); |
| 85 | + } |
| 86 | + |
| 87 | + private void assertTlsFailure(Throwable cause, boolean handshakeEvent, boolean retriable) throws Exception { |
| 88 | + EventLoopGroup eventLoop = EventLoopUtil.newEventLoopGroup(1, false, |
| 89 | + new DefaultThreadFactory("client-cnx-tls-test")); |
| 90 | + ClientConfigurationData conf = new ClientConfigurationData(); |
| 91 | + conf.setKeepAliveIntervalSeconds(0); |
| 92 | + ClientCnx cnx = new ClientCnx(InstrumentProvider.NOOP, conf, eventLoop); |
| 93 | + EmbeddedChannel channel = new EmbeddedChannel(cnx); |
| 94 | + try { |
| 95 | + if (handshakeEvent) { |
| 96 | + channel.pipeline().fireUserEventTriggered(new SslHandshakeCompletionEvent(cause)); |
| 97 | + } else { |
| 98 | + channel.pipeline().fireUserEventTriggered(SslHandshakeCompletionEvent.SUCCESS); |
| 99 | + channel.pipeline().fireExceptionCaught(new DecoderException(cause)); |
| 100 | + } |
| 101 | + channel.close().sync(); |
| 102 | + Throwable failure = cnx.connectionFuture().handle((result, error) -> error).get(5, TimeUnit.SECONDS); |
| 103 | + assertThat(failure).isNotNull(); |
| 104 | + Throwable connectionError = FutureUtil.unwrapCompletionException(failure); |
| 105 | + assertThat(connectionError).isInstanceOf(PulsarClientException.class); |
| 106 | + assertThat(PulsarClientException.isRetriableError(connectionError)).isEqualTo(retriable); |
| 107 | + if (!retriable) { |
| 108 | + assertThat(connectionError.getCause()).isSameAs(cause); |
| 109 | + assertThat(connectionError).hasMessageContaining(cause.getMessage()); |
| 110 | + } |
| 111 | + } finally { |
| 112 | + channel.finishAndReleaseAll(); |
| 113 | + eventLoop.shutdownGracefully(0, 5, TimeUnit.SECONDS).sync(); |
| 114 | + } |
| 115 | + } |
| 116 | +} |