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.hamcrest.MatcherAssert.assertThat;
021import static org.hamcrest.Matchers.hasItem;
022import static org.junit.Assert.assertNotNull;
023
024import java.util.Set;
025import java.util.stream.Collectors;
026import java.util.stream.Stream;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.ipc.RpcServer;
030import org.apache.hadoop.hbase.ipc.RpcServer.BlockingServiceAndInterface;
031import org.apache.hadoop.hbase.ipc.RpcServerInterface;
032import org.apache.hadoop.hbase.testclassification.SecurityTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.apache.hadoop.security.authorize.Service;
035import org.junit.AfterClass;
036import org.junit.BeforeClass;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040
041/**
042 * Make sure that all rpc services for master and region server are properly configured in
043 * {@link SecurityInfo} and {@link HBasePolicyProvider}.
044 */
045@Category({ SecurityTests.class, SmallTests.class })
046public class TestSecurityInfoAndHBasePolicyProviderMatch {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestSecurityInfoAndHBasePolicyProviderMatch.class);
051
052  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
053
054  @BeforeClass
055  public static void setUpBeforeClass() throws Exception {
056    UTIL.startMiniCluster();
057  }
058
059  @AfterClass
060  public static void tearDownAfterClass() throws Exception {
061    UTIL.shutdownMiniCluster();
062  }
063
064  private void assertServiceMatches(RpcServerInterface rpcServer) {
065    HBasePolicyProvider provider = new HBasePolicyProvider();
066    Set<Class<?>> serviceClasses =
067      Stream.of(provider.getServices()).map(Service::getProtocol).collect(Collectors.toSet());
068    for (BlockingServiceAndInterface bsai : ((RpcServer) rpcServer).getServices()) {
069      assertNotNull(
070        "no security info for " + bsai.getBlockingService().getDescriptorForType().getName(),
071        SecurityInfo.getInfo(bsai.getBlockingService().getDescriptorForType().getName()));
072      assertThat(serviceClasses, hasItem(bsai.getServiceInterface()));
073    }
074  }
075
076  @Test
077  public void testMatches() {
078    assertServiceMatches(
079      UTIL.getMiniHBaseCluster().getMaster().getMasterRpcServices().getRpcServer());
080    assertServiceMatches(UTIL.getMiniHBaseCluster().getRegionServer(0).getRpcServer());
081  }
082}