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.coprocessor; 019 020import static org.junit.Assert.assertEquals; 021 022import java.io.IOException; 023import java.net.InetAddress; 024import java.security.cert.X509Certificate; 025import java.util.Optional; 026import java.util.concurrent.atomic.AtomicInteger; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HBaseTestingUtil; 030import org.apache.hadoop.hbase.ipc.RpcCoprocessorHost; 031import org.apache.hadoop.hbase.testclassification.CoprocessorTests; 032import org.apache.hadoop.hbase.testclassification.MediumTests; 033import org.junit.AfterClass; 034import org.junit.BeforeClass; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038 039import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos; 040 041@Category({ CoprocessorTests.class, MediumTests.class }) 042public class TestRpcCoprocessor { 043 044 @ClassRule 045 public static final HBaseClassTestRule CLASS_RULE = 046 HBaseClassTestRule.forClass(TestRpcCoprocessor.class); 047 048 public static class AuthorizationRpcObserver implements RpcCoprocessor, RpcObserver { 049 final AtomicInteger ctPostAuthorization = new AtomicInteger(0); 050 final AtomicInteger ctPreAuthorization = new AtomicInteger(0); 051 String userName = null; 052 053 @Override 054 public Optional<RpcObserver> getRpcObserver() { 055 return Optional.of(this); 056 } 057 058 @Override 059 public void preAuthorizeConnection(ObserverContext<RpcCoprocessorEnvironment> ctx, 060 RPCProtos.ConnectionHeader connectionHeader, InetAddress remoteAddr) throws IOException { 061 ctPreAuthorization.incrementAndGet(); 062 } 063 064 @Override 065 public void postAuthorizeConnection(ObserverContext<RpcCoprocessorEnvironment> ctx, 066 String userName, X509Certificate[] clientCertificateChain) throws IOException { 067 ctPostAuthorization.incrementAndGet(); 068 this.userName = userName; 069 } 070 } 071 072 private static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 073 074 @BeforeClass 075 public static void setupBeforeClass() throws Exception { 076 // set configure to indicate which cp should be loaded 077 Configuration conf = TEST_UTIL.getConfiguration(); 078 conf.set(CoprocessorHost.RPC_COPROCESSOR_CONF_KEY, AuthorizationRpcObserver.class.getName()); 079 TEST_UTIL.getConfiguration().setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, false); 080 TEST_UTIL.startMiniCluster(); 081 } 082 083 @AfterClass 084 public static void teardownAfterClass() throws Exception { 085 TEST_UTIL.shutdownMiniCluster(); 086 } 087 088 @Test 089 public void testHooksCalledFromMaster() { 090 RpcCoprocessorHost coprocHostMaster = 091 TEST_UTIL.getMiniHBaseCluster().getMaster().getRpcServer().getRpcCoprocessorHost(); 092 AuthorizationRpcObserver observer = 093 coprocHostMaster.findCoprocessor(AuthorizationRpcObserver.class); 094 assertEquals(2, observer.ctPreAuthorization.get()); 095 assertEquals(2, observer.ctPostAuthorization.get()); 096 assertEquals(System.getProperty("user.name"), observer.userName); 097 } 098 099 @Test 100 public void testHooksCalledFromRegionServer() { 101 RpcCoprocessorHost coprocHostRs = 102 TEST_UTIL.getMiniHBaseCluster().getRegionServer(0).getRpcServer().getRpcCoprocessorHost(); 103 AuthorizationRpcObserver observer = 104 coprocHostRs.findCoprocessor(AuthorizationRpcObserver.class); 105 assertEquals(3, observer.ctPreAuthorization.get()); 106 assertEquals(3, observer.ctPostAuthorization.get()); 107 assertEquals(System.getProperty("user.name"), observer.userName); 108 } 109}