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