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