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