|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.ipc; |
| 19 | + |
| 20 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | +import static org.hamcrest.Matchers.instanceOf; |
| 22 | +import static org.hamcrest.Matchers.startsWith; |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | +import static org.mockito.ArgumentMatchers.any; |
| 25 | +import static org.mockito.Mockito.doAnswer; |
| 26 | +import static org.mockito.Mockito.mock; |
| 27 | +import static org.mockito.Mockito.verify; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.net.ServerSocket; |
| 32 | +import java.net.Socket; |
| 33 | +import java.util.Random; |
| 34 | +import java.util.concurrent.atomic.AtomicReference; |
| 35 | +import org.apache.hadoop.conf.Configuration; |
| 36 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 37 | +import org.apache.hadoop.hbase.HBaseConfiguration; |
| 38 | +import org.apache.hadoop.hbase.Waiter; |
| 39 | +import org.apache.hadoop.hbase.client.MetricsConnection.CallStats; |
| 40 | +import org.apache.hadoop.hbase.io.crypto.tls.X509Util; |
| 41 | +import org.apache.hadoop.hbase.net.Address; |
| 42 | +import org.apache.hadoop.hbase.security.User; |
| 43 | +import org.apache.hadoop.hbase.testclassification.ClientTests; |
| 44 | +import org.apache.hadoop.hbase.testclassification.SmallTests; |
| 45 | +import org.junit.After; |
| 46 | +import org.junit.Before; |
| 47 | +import org.junit.ClassRule; |
| 48 | +import org.junit.Test; |
| 49 | +import org.junit.experimental.categories.Category; |
| 50 | +import org.mockito.invocation.InvocationOnMock; |
| 51 | +import org.mockito.stubbing.Answer; |
| 52 | +import org.slf4j.Logger; |
| 53 | +import org.slf4j.LoggerFactory; |
| 54 | + |
| 55 | +import org.apache.hbase.thirdparty.com.google.common.io.Closeables; |
| 56 | +import org.apache.hbase.thirdparty.io.netty.handler.ssl.NotSslRecordException; |
| 57 | + |
| 58 | +/** |
| 59 | + * A simple UT to make sure that we do not leak the SslExceptions to netty's TailContext, where it |
| 60 | + * will generate a confusing WARN message. |
| 61 | + * <p> |
| 62 | + * See HBASE-27782 for more details. |
| 63 | + */ |
| 64 | +@Category({ ClientTests.class, SmallTests.class }) |
| 65 | +public class TestTLSHandshadeFailure { |
| 66 | + |
| 67 | + @ClassRule |
| 68 | + public static final HBaseClassTestRule CLASS_RULE = |
| 69 | + HBaseClassTestRule.forClass(TestTLSHandshadeFailure.class); |
| 70 | + |
| 71 | + private static final Logger LOG = LoggerFactory.getLogger(TestTLSHandshadeFailure.class); |
| 72 | + |
| 73 | + private final Configuration conf = HBaseConfiguration.create(); |
| 74 | + |
| 75 | + // use a pre set seed to make the random bytes stable |
| 76 | + private final Random rand = new Random(1); |
| 77 | + |
| 78 | + private ServerSocket server; |
| 79 | + |
| 80 | + private Thread serverThread; |
| 81 | + |
| 82 | + private NettyRpcClient client; |
| 83 | + |
| 84 | + private org.apache.logging.log4j.core.Appender mockAppender; |
| 85 | + |
| 86 | + private void serve() { |
| 87 | + Socket socket = null; |
| 88 | + try { |
| 89 | + socket = server.accept(); |
| 90 | + byte[] bytes = new byte[128]; |
| 91 | + rand.nextBytes(bytes); |
| 92 | + socket.getOutputStream().write(bytes); |
| 93 | + socket.getOutputStream().flush(); |
| 94 | + } catch (Exception e) { |
| 95 | + LOG.warn("failed to process request", e); |
| 96 | + } finally { |
| 97 | + if (socket != null) { |
| 98 | + try { |
| 99 | + socket.close(); |
| 100 | + } catch (IOException e1) { |
| 101 | + LOG.warn("failed to close socket"); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Before |
| 108 | + public void setUp() throws IOException { |
| 109 | + server = new ServerSocket(0); |
| 110 | + serverThread = new Thread(this::serve); |
| 111 | + serverThread.setDaemon(true); |
| 112 | + serverThread.setName("Error-Server-Thread"); |
| 113 | + serverThread.start(); |
| 114 | + conf.setBoolean(X509Util.HBASE_CLIENT_NETTY_TLS_ENABLED, true); |
| 115 | + client = new NettyRpcClient(conf); |
| 116 | + |
| 117 | + mockAppender = mock(org.apache.logging.log4j.core.Appender.class); |
| 118 | + when(mockAppender.getName()).thenReturn("mockAppender"); |
| 119 | + when(mockAppender.isStarted()).thenReturn(true); |
| 120 | + ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager |
| 121 | + .getLogger(BufferCallBeforeInitHandler.class)).addAppender(mockAppender); |
| 122 | + } |
| 123 | + |
| 124 | + @After |
| 125 | + public void tearDown() throws IOException { |
| 126 | + ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager |
| 127 | + .getLogger(BufferCallBeforeInitHandler.class)).removeAppender(mockAppender); |
| 128 | + Closeables.close(client, true); |
| 129 | + Closeables.close(server, true); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void test() throws Exception { |
| 134 | + AtomicReference<org.apache.logging.log4j.Level> level = new AtomicReference<>(); |
| 135 | + AtomicReference<String> msg = new AtomicReference<String>(); |
| 136 | + doAnswer(new Answer<Void>() { |
| 137 | + |
| 138 | + @Override |
| 139 | + public Void answer(InvocationOnMock invocation) throws Throwable { |
| 140 | + org.apache.logging.log4j.core.LogEvent logEvent = |
| 141 | + invocation.getArgument(0, org.apache.logging.log4j.core.LogEvent.class); |
| 142 | + level.set(logEvent.getLevel()); |
| 143 | + msg.set(logEvent.getMessage().getFormattedMessage()); |
| 144 | + return null; |
| 145 | + } |
| 146 | + }).when(mockAppender).append(any()); |
| 147 | + ConnectionId id = new ConnectionId(User.getCurrent(), "test", |
| 148 | + Address.fromParts("127.0.0.1", server.getLocalPort())); |
| 149 | + NettyRpcConnection conn = client.createConnection(id); |
| 150 | + BlockingRpcCallback<Call> done = new BlockingRpcCallback<>(); |
| 151 | + Call call = new Call(1, null, null, null, null, 0, 0, done, new CallStats()); |
| 152 | + HBaseRpcController hrc = new HBaseRpcControllerImpl(); |
| 153 | + conn.sendRequest(call, hrc); |
| 154 | + done.get(); |
| 155 | + assertThat(call.error, instanceOf(NotSslRecordException.class)); |
| 156 | + Waiter.waitFor(conf, 5000, () -> msg.get() != null); |
| 157 | + verify(mockAppender).append(any()); |
| 158 | + // make sure that it has been logged by BufferCallBeforeInitHandler |
| 159 | + assertEquals(org.apache.logging.log4j.Level.DEBUG, level.get()); |
| 160 | + assertThat(msg.get(), |
| 161 | + startsWith("got ssl exception, which should have already been proceeded")); |
| 162 | + } |
| 163 | +} |
0 commit comments