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;
021
022import java.io.IOException;
023import java.net.InetSocketAddress;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
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.MediumTests;
036import org.apache.hadoop.hbase.testclassification.RPCTests;
037import org.junit.After;
038import org.junit.Before;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
044import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
045import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
046import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
047import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
048
049import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestProtos;
050import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestRpcServiceProtos;
051
052@Category({ RPCTests.class, MediumTests.class })
053public class TestNettyIPCSslFailure {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057    HBaseClassTestRule.forClass(TestNettyIPCSslFailure.class);
058
059  private static final Configuration CONF = HBaseConfiguration.create();
060
061  private NioEventLoopGroup group;
062
063  private NettyRpcServer server;
064
065  private NettyRpcClient client;
066
067  private TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface stub;
068
069  @Before
070  public void setUp() throws IOException, SSLContextException {
071    CONF.set(X509Util.HBASE_SERVER_NETTY_TLS_ENABLED, "true");
072    CONF.unset(X509Util.TLS_CONFIG_KEYSTORE_LOCATION);
073    group = new NioEventLoopGroup();
074    server = new NettyRpcServer(null, getClass().getSimpleName(),
075      Lists.newArrayList(new RpcServer.BlockingServiceAndInterface(SERVICE, null)),
076      new InetSocketAddress("localhost", 0), CONF, new FifoRpcScheduler(CONF, 1), true);
077    NettyRpcClientConfigHelper.setEventLoopConfig(CONF, group, NioSocketChannel.class);
078    client = new NettyRpcClient(CONF);
079    server.start();
080    stub = TestProtobufRpcServiceImpl.newBlockingStub(client, server.getListenerAddress());
081  }
082
083  @After
084  public void tearDown() throws Exception {
085    Closeables.close(client, true);
086    server.stop();
087    group.shutdownGracefully().sync();
088  }
089
090  @Test(expected = ServiceException.class)
091  public void testInitSslThrowsException() throws ServiceException {
092    stub.echo(null, TestProtos.EchoRequestProto.newBuilder().setMessage("test").build())
093      .getMessage();
094  }
095
096}