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
066 * Coprocessor 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    @Override
083    public void revoke(RpcController controller, RevokeRequest request,
084        RpcCallback<RevokeResponse> done) {}
085
086    @Override
087    public void getUserPermissions(RpcController controller, GetUserPermissionsRequest request,
088        RpcCallback<GetUserPermissionsResponse> done) {}
089
090    @Override
091    public void checkPermissions(RpcController controller, CheckPermissionsRequest request,
092        RpcCallback<CheckPermissionsResponse> done) {}
093
094    @Override
095    public void hasPermission(RpcController controller, HasPermissionRequest request,
096        RpcCallback<HasPermissionResponse> done) {
097    }
098  }
099
100  private static class MockVisibilityController implements VisibilityLabelsService.Interface,
101      MasterCoprocessor, RegionCoprocessor, MasterObserver, RegionObserver {
102
103    @Override
104    public void addLabels(RpcController controller, VisibilityLabelsRequest request,
105        RpcCallback<VisibilityLabelsResponse> done) {
106    }
107
108    @Override
109    public void setAuths(RpcController controller, SetAuthsRequest request,
110        RpcCallback<VisibilityLabelsResponse> done) {
111    }
112
113    @Override
114    public void clearAuths(RpcController controller, SetAuthsRequest request,
115        RpcCallback<VisibilityLabelsResponse> done) {
116    }
117
118    @Override
119    public void getAuths(RpcController controller, GetAuthsRequest request,
120        RpcCallback<GetAuthsResponse> done) {
121    }
122
123    @Override
124    public void listLabels(RpcController controller, ListLabelsRequest request,
125        RpcCallback<ListLabelsResponse> done) {
126    }
127  }
128
129  private MasterRpcServices masterServices;
130
131  @SuppressWarnings("unchecked")
132  @Before
133  public void setup() {
134    masterServices = mock(MasterRpcServices.class);
135    when(masterServices.hasAccessControlServiceCoprocessor(
136        any(MasterCoprocessorHost.class))).thenCallRealMethod();
137    when(masterServices.hasVisibilityLabelsServiceCoprocessor(
138        any(MasterCoprocessorHost.class))).thenCallRealMethod();
139    when(masterServices.checkCoprocessorWithService(
140        any(List.class), any(Class.class))).thenCallRealMethod();
141  }
142
143  @Test
144  public void testAccessControlServices() {
145    MasterCoprocessor defaultImpl = new AccessController();
146    MasterCoprocessor customImpl = new MockAccessController();
147    MasterCoprocessor unrelatedImpl = new JMXListener();
148    assertTrue(masterServices.checkCoprocessorWithService(
149        Collections.singletonList(defaultImpl), AccessControlService.Interface.class));
150    assertTrue(masterServices.checkCoprocessorWithService(
151        Collections.singletonList(customImpl), AccessControlService.Interface.class));
152    assertFalse(masterServices.checkCoprocessorWithService(
153        Collections.emptyList(), AccessControlService.Interface.class));
154    assertFalse(masterServices.checkCoprocessorWithService(
155        null, AccessControlService.Interface.class));
156    assertFalse(masterServices.checkCoprocessorWithService(
157        Collections.singletonList(unrelatedImpl), AccessControlService.Interface.class));
158    assertTrue(masterServices.checkCoprocessorWithService(
159        Arrays.asList(unrelatedImpl, customImpl), AccessControlService.Interface.class));
160    assertTrue(masterServices.checkCoprocessorWithService(
161        Arrays.asList(unrelatedImpl, defaultImpl), AccessControlService.Interface.class));
162  }
163
164  @Test
165  public void testVisibilityLabelServices() {
166    MasterCoprocessor defaultImpl = new VisibilityController();
167    MasterCoprocessor customImpl = new MockVisibilityController();
168    MasterCoprocessor unrelatedImpl = new JMXListener();
169    assertTrue(masterServices.checkCoprocessorWithService(
170        Collections.singletonList(defaultImpl), VisibilityLabelsService.Interface.class));
171    assertTrue(masterServices.checkCoprocessorWithService(
172        Collections.singletonList(customImpl), VisibilityLabelsService.Interface.class));
173    assertFalse(masterServices.checkCoprocessorWithService(
174        Collections.emptyList(), VisibilityLabelsService.Interface.class));
175    assertFalse(masterServices.checkCoprocessorWithService(
176        null, VisibilityLabelsService.Interface.class));
177    assertFalse(masterServices.checkCoprocessorWithService(
178        Collections.singletonList(unrelatedImpl), VisibilityLabelsService.Interface.class));
179    assertTrue(masterServices.checkCoprocessorWithService(
180        Arrays.asList(unrelatedImpl, customImpl), VisibilityLabelsService.Interface.class));
181    assertTrue(masterServices.checkCoprocessorWithService(
182        Arrays.asList(unrelatedImpl, defaultImpl), VisibilityLabelsService.Interface.class));
183  }
184}