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.master; 019 020import static org.junit.Assert.assertFalse; 021import static org.junit.Assert.assertTrue; 022import static org.mockito.Mockito.any; 023import static org.mockito.Mockito.mock; 024import static org.mockito.Mockito.when; 025 026import com.google.protobuf.RpcCallback; 027import com.google.protobuf.RpcController; 028import java.util.Arrays; 029import java.util.Collections; 030import java.util.List; 031import org.apache.hadoop.hbase.HBaseClassTestRule; 032import org.apache.hadoop.hbase.JMXListener; 033import org.apache.hadoop.hbase.coprocessor.MasterCoprocessor; 034import org.apache.hadoop.hbase.coprocessor.MasterObserver; 035import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor; 036import org.apache.hadoop.hbase.coprocessor.RegionObserver; 037import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.AccessControlService; 038import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.CheckPermissionsRequest; 039import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.CheckPermissionsResponse; 040import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.GetUserPermissionsRequest; 041import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.GetUserPermissionsResponse; 042import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.GrantRequest; 043import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.GrantResponse; 044import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.HasPermissionRequest; 045import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.HasPermissionResponse; 046import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.RevokeRequest; 047import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.RevokeResponse; 048import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.GetAuthsRequest; 049import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.GetAuthsResponse; 050import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.ListLabelsRequest; 051import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse; 052import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.SetAuthsRequest; 053import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsRequest; 054import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsResponse; 055import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsService; 056import org.apache.hadoop.hbase.security.access.AccessController; 057import org.apache.hadoop.hbase.security.visibility.VisibilityController; 058import org.apache.hadoop.hbase.testclassification.SmallTests; 059import org.junit.Before; 060import org.junit.ClassRule; 061import org.junit.Test; 062import org.junit.experimental.categories.Category; 063 064/** 065 * Tests that the MasterRpcServices is correctly searching for implementations of the Coprocessor 066 * Service and not just the "default" implementations of those services. 067 */ 068@Category({ SmallTests.class }) 069public class TestMasterCoprocessorServices { 070 071 @ClassRule 072 public static final HBaseClassTestRule CLASS_RULE = 073 HBaseClassTestRule.forClass(TestMasterCoprocessorServices.class); 074 075 private static class MockAccessController implements AccessControlService.Interface, 076 MasterCoprocessor, RegionCoprocessor, MasterObserver, RegionObserver { 077 078 @Override 079 public void grant(RpcController controller, GrantRequest request, 080 RpcCallback<GrantResponse> done) { 081 } 082 083 @Override 084 public void revoke(RpcController controller, RevokeRequest request, 085 RpcCallback<RevokeResponse> done) { 086 } 087 088 @Override 089 public void getUserPermissions(RpcController controller, GetUserPermissionsRequest request, 090 RpcCallback<GetUserPermissionsResponse> done) { 091 } 092 093 @Override 094 public void checkPermissions(RpcController controller, CheckPermissionsRequest request, 095 RpcCallback<CheckPermissionsResponse> done) { 096 } 097 098 @Override 099 public void hasPermission(RpcController controller, HasPermissionRequest request, 100 RpcCallback<HasPermissionResponse> done) { 101 } 102 } 103 104 private static class MockVisibilityController implements VisibilityLabelsService.Interface, 105 MasterCoprocessor, RegionCoprocessor, MasterObserver, RegionObserver { 106 107 @Override 108 public void addLabels(RpcController controller, VisibilityLabelsRequest request, 109 RpcCallback<VisibilityLabelsResponse> done) { 110 } 111 112 @Override 113 public void setAuths(RpcController controller, SetAuthsRequest request, 114 RpcCallback<VisibilityLabelsResponse> done) { 115 } 116 117 @Override 118 public void clearAuths(RpcController controller, SetAuthsRequest request, 119 RpcCallback<VisibilityLabelsResponse> done) { 120 } 121 122 @Override 123 public void getAuths(RpcController controller, GetAuthsRequest request, 124 RpcCallback<GetAuthsResponse> done) { 125 } 126 127 @Override 128 public void listLabels(RpcController controller, ListLabelsRequest request, 129 RpcCallback<ListLabelsResponse> done) { 130 } 131 } 132 133 private MasterRpcServices masterServices; 134 135 @SuppressWarnings("unchecked") 136 @Before 137 public void setup() { 138 masterServices = mock(MasterRpcServices.class); 139 when(masterServices.hasAccessControlServiceCoprocessor(any(MasterCoprocessorHost.class))) 140 .thenCallRealMethod(); 141 when(masterServices.hasVisibilityLabelsServiceCoprocessor(any(MasterCoprocessorHost.class))) 142 .thenCallRealMethod(); 143 when(masterServices.checkCoprocessorWithService(any(List.class), any(Class.class))) 144 .thenCallRealMethod(); 145 } 146 147 @Test 148 public void testAccessControlServices() { 149 MasterCoprocessor defaultImpl = new AccessController(); 150 MasterCoprocessor customImpl = new MockAccessController(); 151 MasterCoprocessor unrelatedImpl = new JMXListener(); 152 assertTrue(masterServices.checkCoprocessorWithService(Collections.singletonList(defaultImpl), 153 AccessControlService.Interface.class)); 154 assertTrue(masterServices.checkCoprocessorWithService(Collections.singletonList(customImpl), 155 AccessControlService.Interface.class)); 156 assertFalse(masterServices.checkCoprocessorWithService(Collections.emptyList(), 157 AccessControlService.Interface.class)); 158 assertFalse( 159 masterServices.checkCoprocessorWithService(null, AccessControlService.Interface.class)); 160 assertFalse(masterServices.checkCoprocessorWithService(Collections.singletonList(unrelatedImpl), 161 AccessControlService.Interface.class)); 162 assertTrue(masterServices.checkCoprocessorWithService(Arrays.asList(unrelatedImpl, customImpl), 163 AccessControlService.Interface.class)); 164 assertTrue(masterServices.checkCoprocessorWithService(Arrays.asList(unrelatedImpl, defaultImpl), 165 AccessControlService.Interface.class)); 166 } 167 168 @Test 169 public void testVisibilityLabelServices() { 170 MasterCoprocessor defaultImpl = new VisibilityController(); 171 MasterCoprocessor customImpl = new MockVisibilityController(); 172 MasterCoprocessor unrelatedImpl = new JMXListener(); 173 assertTrue(masterServices.checkCoprocessorWithService(Collections.singletonList(defaultImpl), 174 VisibilityLabelsService.Interface.class)); 175 assertTrue(masterServices.checkCoprocessorWithService(Collections.singletonList(customImpl), 176 VisibilityLabelsService.Interface.class)); 177 assertFalse(masterServices.checkCoprocessorWithService(Collections.emptyList(), 178 VisibilityLabelsService.Interface.class)); 179 assertFalse( 180 masterServices.checkCoprocessorWithService(null, VisibilityLabelsService.Interface.class)); 181 assertFalse(masterServices.checkCoprocessorWithService(Collections.singletonList(unrelatedImpl), 182 VisibilityLabelsService.Interface.class)); 183 assertTrue(masterServices.checkCoprocessorWithService(Arrays.asList(unrelatedImpl, customImpl), 184 VisibilityLabelsService.Interface.class)); 185 assertTrue(masterServices.checkCoprocessorWithService(Arrays.asList(unrelatedImpl, defaultImpl), 186 VisibilityLabelsService.Interface.class)); 187 } 188}