001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.security;
019
020import static org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.SERVICE;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022
023import java.io.IOException;
024import java.net.InetSocketAddress;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseConfiguration;
027import org.apache.hadoop.hbase.exceptions.SSLContextException;
028import org.apache.hadoop.hbase.io.crypto.tls.X509Util;
029import org.apache.hadoop.hbase.ipc.FifoRpcScheduler;
030import org.apache.hadoop.hbase.ipc.NettyRpcClient;
031import org.apache.hadoop.hbase.ipc.NettyRpcClientConfigHelper;
032import org.apache.hadoop.hbase.ipc.NettyRpcServer;
033import org.apache.hadoop.hbase.ipc.RpcServer;
034import org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl;
035import org.apache.hadoop.hbase.testclassification.RPCTests;
036import org.apache.hadoop.hbase.testclassification.SmallTests;
037import org.junit.jupiter.api.AfterEach;
038import org.junit.jupiter.api.BeforeEach;
039import org.junit.jupiter.api.Tag;
040import org.junit.jupiter.api.Test;
041
042import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
043import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
044import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
045import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
046import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
047
048import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestProtos;
049import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestRpcServiceProtos;
050
051@Tag(RPCTests.TAG)
052@Tag(SmallTests.TAG)
053public class TestNettyIPCSslFailure {
054
055  private static final Configuration CONF = HBaseConfiguration.create();
056
057  private NioEventLoopGroup group;
058
059  private NettyRpcServer server;
060
061  private NettyRpcClient client;
062
063  private TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface stub;
064
065  @BeforeEach
066  public void setUp() throws IOException, SSLContextException {
067    CONF.set(X509Util.HBASE_SERVER_NETTY_TLS_ENABLED, "true");
068    CONF.unset(X509Util.TLS_CONFIG_KEYSTORE_LOCATION);
069    group = new NioEventLoopGroup();
070    server = new NettyRpcServer(null, getClass().getSimpleName(),
071      Lists.newArrayList(new RpcServer.BlockingServiceAndInterface(SERVICE, null)),
072      new InetSocketAddress("localhost", 0), CONF, new FifoRpcScheduler(CONF, 1), true);
073    NettyRpcClientConfigHelper.setEventLoopConfig(CONF, group, NioSocketChannel.class);
074    client = new NettyRpcClient(CONF);
075    server.start();
076    stub = TestProtobufRpcServiceImpl.newBlockingStub(client, server.getListenerAddress());
077  }
078
079  @AfterEach
080  public void tearDown() throws Exception {
081    Closeables.close(client, true);
082    server.stop();
083    group.shutdownGracefully().sync();
084  }
085
086  @Test
087  public void testInitSslThrowsException() throws ServiceException {
088    assertThrows(ServiceException.class,
089      () -> stub.echo(null, TestProtos.EchoRequestProto.newBuilder().setMessage("test").build())
090        .getMessage());
091  }
092
093}