1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.security.access;
19
20 import static org.apache.hadoop.hbase.AuthUtil.toGroupEntry;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.security.PrivilegedAction;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.hadoop.conf.Configuration;
38 import org.apache.hadoop.fs.FileStatus;
39 import org.apache.hadoop.fs.FileSystem;
40 import org.apache.hadoop.fs.Path;
41 import org.apache.hadoop.fs.permission.FsPermission;
42 import org.apache.hadoop.hbase.Coprocessor;
43 import org.apache.hadoop.hbase.CoprocessorEnvironment;
44 import org.apache.hadoop.hbase.HBaseIOException;
45 import org.apache.hadoop.hbase.HBaseTestingUtility;
46 import org.apache.hadoop.hbase.HColumnDescriptor;
47 import org.apache.hadoop.hbase.HConstants;
48 import org.apache.hadoop.hbase.HRegionInfo;
49 import org.apache.hadoop.hbase.HRegionLocation;
50 import org.apache.hadoop.hbase.HTableDescriptor;
51 import org.apache.hadoop.hbase.KeyValue;
52 import org.apache.hadoop.hbase.MiniHBaseCluster;
53 import org.apache.hadoop.hbase.NamespaceDescriptor;
54 import org.apache.hadoop.hbase.ProcedureInfo;
55 import org.apache.hadoop.hbase.ServerName;
56 import org.apache.hadoop.hbase.TableName;
57 import org.apache.hadoop.hbase.TableNotFoundException;
58 import org.apache.hadoop.hbase.Tag;
59 import org.apache.hadoop.hbase.client.Admin;
60 import org.apache.hadoop.hbase.client.Append;
61 import org.apache.hadoop.hbase.client.Connection;
62 import org.apache.hadoop.hbase.client.ConnectionFactory;
63 import org.apache.hadoop.hbase.client.Delete;
64 import org.apache.hadoop.hbase.client.Get;
65 import org.apache.hadoop.hbase.client.Increment;
66 import org.apache.hadoop.hbase.client.Put;
67 import org.apache.hadoop.hbase.client.RegionLocator;
68 import org.apache.hadoop.hbase.client.Result;
69 import org.apache.hadoop.hbase.client.ResultScanner;
70 import org.apache.hadoop.hbase.client.Scan;
71 import org.apache.hadoop.hbase.client.Table;
72 import org.apache.hadoop.hbase.client.security.SecurityCapability;
73 import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
74 import org.apache.hadoop.hbase.coprocessor.CoprocessorService;
75 import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
76 import org.apache.hadoop.hbase.coprocessor.ObserverContext;
77 import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
78 import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessorEnvironment;
79 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.PingProtos.CountRequest;
80 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.PingProtos.CountResponse;
81 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.PingProtos.HelloRequest;
82 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.PingProtos.HelloResponse;
83 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.PingProtos.IncrementCountRequest;
84 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.PingProtos.IncrementCountResponse;
85 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.PingProtos.NoopRequest;
86 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.PingProtos.NoopResponse;
87 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.PingProtos.PingRequest;
88 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.PingProtos.PingResponse;
89 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.PingProtos.PingService;
90 import org.apache.hadoop.hbase.exceptions.HBaseException;
91 import org.apache.hadoop.hbase.io.hfile.CacheConfig;
92 import org.apache.hadoop.hbase.io.hfile.HFile;
93 import org.apache.hadoop.hbase.io.hfile.HFileContext;
94 import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
95 import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProcedureProtos;
96 import org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles;
97 import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
98 import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
99 import org.apache.hadoop.hbase.master.procedure.TableProcedureInterface;
100 import org.apache.hadoop.hbase.procedure2.Procedure;
101 import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
102 import org.apache.hadoop.hbase.procedure2.ProcedureYieldException;
103 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
104 import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos;
105 import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.AccessControlService;
106 import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.CheckPermissionsRequest;
107 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
108 import org.apache.hadoop.hbase.protobuf.generated.ProcedureProtos.ProcedureState;
109 import org.apache.hadoop.hbase.regionserver.HRegion;
110 import org.apache.hadoop.hbase.regionserver.HRegionServer;
111 import org.apache.hadoop.hbase.regionserver.Region;
112 import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost;
113 import org.apache.hadoop.hbase.regionserver.RegionServerCoprocessorHost;
114 import org.apache.hadoop.hbase.regionserver.ScanType;
115 import org.apache.hadoop.hbase.security.Superusers;
116 import org.apache.hadoop.hbase.security.User;
117 import org.apache.hadoop.hbase.security.access.Permission.Action;
118 import org.apache.hadoop.hbase.testclassification.LargeTests;
119 import org.apache.hadoop.hbase.testclassification.SecurityTests;
120 import org.apache.hadoop.hbase.util.Bytes;
121 import org.apache.hadoop.hbase.util.JVMClusterUtil;
122 import org.apache.log4j.Level;
123 import org.apache.log4j.Logger;
124 import org.junit.AfterClass;
125 import org.junit.BeforeClass;
126 import org.junit.Test;
127 import org.junit.experimental.categories.Category;
128
129 import com.google.protobuf.BlockingRpcChannel;
130 import com.google.protobuf.RpcCallback;
131 import com.google.protobuf.RpcController;
132 import com.google.protobuf.Service;
133 import com.google.protobuf.ServiceException;
134
135
136
137
138
139 @Category({SecurityTests.class, LargeTests.class})
140 public class TestAccessController extends SecureTestUtil {
141 private static final Log LOG = LogFactory.getLog(TestAccessController.class);
142
143 static {
144 Logger.getLogger(AccessController.class).setLevel(Level.TRACE);
145 Logger.getLogger(AccessControlFilter.class).setLevel(Level.TRACE);
146 Logger.getLogger(TableAuthManager.class).setLevel(Level.TRACE);
147 }
148
149 private static TableName TEST_TABLE = TableName.valueOf("testtable1");
150 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
151 private static Configuration conf;
152
153
154
155
156 private static Connection systemUserConnection;
157
158
159
160 private static User SUPERUSER;
161
162 private static User USER_ADMIN;
163
164 private static User USER_RW;
165
166 private static User USER_RO;
167
168 private static User USER_OWNER;
169
170 private static User USER_CREATE;
171
172 private static User USER_NONE;
173
174 private static User USER_ADMIN_CF;
175
176 private static final String GROUP_ADMIN = "group_admin";
177 private static final String GROUP_CREATE = "group_create";
178 private static final String GROUP_READ = "group_read";
179 private static final String GROUP_WRITE = "group_write";
180
181 private static User USER_GROUP_ADMIN;
182 private static User USER_GROUP_CREATE;
183 private static User USER_GROUP_READ;
184 private static User USER_GROUP_WRITE;
185
186
187
188
189
190 private static TableName TEST_TABLE2 = TableName.valueOf("testtable2");
191 private static byte[] TEST_FAMILY = Bytes.toBytes("f1");
192 private static byte[] TEST_QUALIFIER = Bytes.toBytes("q1");
193 private static byte[] TEST_ROW = Bytes.toBytes("r1");
194
195 private static MasterCoprocessorEnvironment CP_ENV;
196 private static AccessController ACCESS_CONTROLLER;
197 private static RegionServerCoprocessorEnvironment RSCP_ENV;
198 private static RegionCoprocessorEnvironment RCP_ENV;
199
200 @BeforeClass
201 public static void setupBeforeClass() throws Exception {
202
203 conf = TEST_UTIL.getConfiguration();
204
205 conf.setInt(HConstants.REGION_SERVER_HIGH_PRIORITY_HANDLER_COUNT, 10);
206
207 enableSecurity(conf);
208
209
210 conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, AccessController.class.getName());
211
212 verifyConfiguration(conf);
213
214
215 conf.setBoolean(AccessControlConstants.EXEC_PERMISSION_CHECKS_KEY, true);
216
217 TEST_UTIL.startMiniCluster();
218 MasterCoprocessorHost cpHost =
219 TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterCoprocessorHost();
220 cpHost.load(AccessController.class, Coprocessor.PRIORITY_HIGHEST, conf);
221 ACCESS_CONTROLLER = (AccessController) cpHost.findCoprocessor(AccessController.class.getName());
222 CP_ENV = cpHost.createEnvironment(AccessController.class, ACCESS_CONTROLLER,
223 Coprocessor.PRIORITY_HIGHEST, 1, conf);
224 RegionServerCoprocessorHost rsHost = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0)
225 .getRegionServerCoprocessorHost();
226 RSCP_ENV = rsHost.createEnvironment(AccessController.class, ACCESS_CONTROLLER,
227 Coprocessor.PRIORITY_HIGHEST, 1, conf);
228
229
230 TEST_UTIL.waitUntilAllRegionsAssigned(AccessControlLists.ACL_TABLE_NAME);
231
232
233 SUPERUSER = User.createUserForTesting(conf, "admin", new String[] { "supergroup" });
234 USER_ADMIN = User.createUserForTesting(conf, "admin2", new String[0]);
235 USER_RW = User.createUserForTesting(conf, "rwuser", new String[0]);
236 USER_RO = User.createUserForTesting(conf, "rouser", new String[0]);
237 USER_OWNER = User.createUserForTesting(conf, "owner", new String[0]);
238 USER_CREATE = User.createUserForTesting(conf, "tbl_create", new String[0]);
239 USER_NONE = User.createUserForTesting(conf, "nouser", new String[0]);
240 USER_ADMIN_CF = User.createUserForTesting(conf, "col_family_admin", new String[0]);
241
242 USER_GROUP_ADMIN =
243 User.createUserForTesting(conf, "user_group_admin", new String[] { GROUP_ADMIN });
244 USER_GROUP_CREATE =
245 User.createUserForTesting(conf, "user_group_create", new String[] { GROUP_CREATE });
246 USER_GROUP_READ =
247 User.createUserForTesting(conf, "user_group_read", new String[] { GROUP_READ });
248 USER_GROUP_WRITE =
249 User.createUserForTesting(conf, "user_group_write", new String[] { GROUP_WRITE });
250
251 systemUserConnection = TEST_UTIL.getConnection();
252 setUpTableAndUserPermissions();
253 }
254
255 @AfterClass
256 public static void tearDownAfterClass() throws Exception {
257 cleanUp();
258 TEST_UTIL.shutdownMiniCluster();
259 int total = TableAuthManager.getTotalRefCount();
260 assertTrue("Unexpected reference count: " + total, total == 0);
261 }
262
263 private static void setUpTableAndUserPermissions() throws Exception {
264 HTableDescriptor htd = new HTableDescriptor(TEST_TABLE);
265 HColumnDescriptor hcd = new HColumnDescriptor(TEST_FAMILY);
266 hcd.setMaxVersions(100);
267 htd.addFamily(hcd);
268 htd.setOwner(USER_OWNER);
269 createTable(TEST_UTIL, htd, new byte[][] { Bytes.toBytes("s") });
270
271 Region region = TEST_UTIL.getHBaseCluster().getRegions(TEST_TABLE).get(0);
272 RegionCoprocessorHost rcpHost = region.getCoprocessorHost();
273 RCP_ENV = rcpHost.createEnvironment(AccessController.class, ACCESS_CONTROLLER,
274 Coprocessor.PRIORITY_HIGHEST, 1, conf);
275
276
277
278 grantGlobal(TEST_UTIL, USER_ADMIN.getShortName(),
279 Permission.Action.ADMIN,
280 Permission.Action.CREATE,
281 Permission.Action.READ,
282 Permission.Action.WRITE);
283
284 grantOnTable(TEST_UTIL, USER_RW.getShortName(),
285 TEST_TABLE, TEST_FAMILY, null,
286 Permission.Action.READ,
287 Permission.Action.WRITE);
288
289
290 grantOnTable(TEST_UTIL, USER_CREATE.getShortName(),
291 TEST_TABLE, null, null,
292 Permission.Action.CREATE,
293 Permission.Action.READ,
294 Permission.Action.WRITE);
295
296 grantOnTable(TEST_UTIL, USER_RO.getShortName(),
297 TEST_TABLE, TEST_FAMILY, null,
298 Permission.Action.READ);
299
300 grantOnTable(TEST_UTIL, USER_ADMIN_CF.getShortName(),
301 TEST_TABLE, TEST_FAMILY,
302 null, Permission.Action.ADMIN, Permission.Action.CREATE);
303
304 grantGlobal(TEST_UTIL, toGroupEntry(GROUP_ADMIN), Permission.Action.ADMIN);
305 grantGlobal(TEST_UTIL, toGroupEntry(GROUP_CREATE), Permission.Action.CREATE);
306 grantGlobal(TEST_UTIL, toGroupEntry(GROUP_READ), Permission.Action.READ);
307 grantGlobal(TEST_UTIL, toGroupEntry(GROUP_WRITE), Permission.Action.WRITE);
308
309 assertEquals(5, AccessControlLists.getTablePermissions(conf, TEST_TABLE).size());
310 try {
311 assertEquals(5, AccessControlClient.getUserPermissions(systemUserConnection,
312 TEST_TABLE.toString()).size());
313 } catch (Throwable e) {
314 LOG.error("error during call of AccessControlClient.getUserPermissions. ", e);
315 }
316 }
317
318 private static void cleanUp() throws Exception {
319
320 try {
321 deleteTable(TEST_UTIL, TEST_TABLE);
322 } catch (TableNotFoundException ex) {
323
324 LOG.info("Test deleted table " + TEST_TABLE);
325 }
326
327 assertEquals(0, AccessControlLists.getTablePermissions(conf, TEST_TABLE).size());
328 assertEquals(
329 0,
330 AccessControlLists.getNamespacePermissions(conf,
331 TEST_TABLE.getNamespaceAsString()).size());
332 }
333
334 @Test (timeout=180000)
335 public void testSecurityCapabilities() throws Exception {
336 List<SecurityCapability> capabilities = TEST_UTIL.getConnection().getAdmin()
337 .getSecurityCapabilities();
338 assertTrue("AUTHORIZATION capability is missing",
339 capabilities.contains(SecurityCapability.AUTHORIZATION));
340 assertTrue("CELL_AUTHORIZATION capability is missing",
341 capabilities.contains(SecurityCapability.CELL_AUTHORIZATION));
342 }
343
344 @Test (timeout=180000)
345 public void testTableCreate() throws Exception {
346 AccessTestAction createTable = new AccessTestAction() {
347 @Override
348 public Object run() throws Exception {
349 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("testnewtable"));
350 htd.addFamily(new HColumnDescriptor(TEST_FAMILY));
351 ACCESS_CONTROLLER.preCreateTable(ObserverContext.createAndPrepare(CP_ENV, null), htd, null);
352 return null;
353 }
354 };
355
356
357 verifyAllowed(createTable, SUPERUSER, USER_ADMIN, USER_GROUP_CREATE);
358
359
360 verifyDenied(createTable, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_ADMIN,
361 USER_GROUP_READ, USER_GROUP_WRITE);
362 }
363
364 @Test (timeout=180000)
365 public void testTableModify() throws Exception {
366 AccessTestAction modifyTable = new AccessTestAction() {
367 @Override
368 public Object run() throws Exception {
369 HTableDescriptor htd = new HTableDescriptor(TEST_TABLE);
370 htd.addFamily(new HColumnDescriptor(TEST_FAMILY));
371 htd.addFamily(new HColumnDescriptor("fam_" + User.getCurrent().getShortName()));
372 ACCESS_CONTROLLER.preModifyTable(ObserverContext.createAndPrepare(CP_ENV, null),
373 TEST_TABLE, htd);
374 return null;
375 }
376 };
377
378 verifyAllowed(modifyTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_GROUP_CREATE,
379 USER_GROUP_ADMIN);
380 verifyDenied(modifyTable, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
381 }
382
383 @Test (timeout=180000)
384 public void testTableDelete() throws Exception {
385 AccessTestAction deleteTable = new AccessTestAction() {
386 @Override
387 public Object run() throws Exception {
388 ACCESS_CONTROLLER
389 .preDeleteTable(ObserverContext.createAndPrepare(CP_ENV, null), TEST_TABLE);
390 return null;
391 }
392 };
393
394 verifyAllowed(deleteTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_GROUP_CREATE,
395 USER_GROUP_ADMIN);
396 verifyDenied(deleteTable, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
397 }
398
399 @Test (timeout=180000)
400 public void testTableTruncate() throws Exception {
401 AccessTestAction truncateTable = new AccessTestAction() {
402 @Override
403 public Object run() throws Exception {
404 ACCESS_CONTROLLER
405 .preTruncateTable(ObserverContext.createAndPrepare(CP_ENV, null),
406 TEST_TABLE);
407 return null;
408 }
409 };
410
411 verifyAllowed(truncateTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_GROUP_CREATE,
412 USER_GROUP_ADMIN);
413 verifyDenied(truncateTable, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
414 }
415
416 @Test (timeout=180000)
417 public void testAddColumn() throws Exception {
418 final HColumnDescriptor hcd = new HColumnDescriptor("fam_new");
419 AccessTestAction action = new AccessTestAction() {
420 @Override
421 public Object run() throws Exception {
422 ACCESS_CONTROLLER.preAddColumnFamily(ObserverContext.createAndPrepare(CP_ENV, null), TEST_TABLE,
423 hcd);
424 return null;
425 }
426 };
427
428 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_GROUP_CREATE,
429 USER_GROUP_ADMIN);
430 verifyDenied(action, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
431 }
432
433 @Test (timeout=180000)
434 public void testModifyColumn() throws Exception {
435 final HColumnDescriptor hcd = new HColumnDescriptor(TEST_FAMILY);
436 hcd.setMaxVersions(10);
437 AccessTestAction action = new AccessTestAction() {
438 @Override
439 public Object run() throws Exception {
440 ACCESS_CONTROLLER.preModifyColumnFamily(ObserverContext.createAndPrepare(CP_ENV, null),
441 TEST_TABLE, hcd);
442 return null;
443 }
444 };
445
446 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_ADMIN_CF,
447 USER_GROUP_CREATE, USER_GROUP_ADMIN);
448 verifyDenied(action, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
449 }
450
451 @Test (timeout=180000)
452 public void testDeleteColumn() throws Exception {
453 AccessTestAction action = new AccessTestAction() {
454 @Override
455 public Object run() throws Exception {
456 ACCESS_CONTROLLER.preDeleteColumnFamily(ObserverContext.createAndPrepare(CP_ENV, null),
457 TEST_TABLE, TEST_FAMILY);
458 return null;
459 }
460 };
461
462 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_ADMIN_CF,
463 USER_GROUP_CREATE, USER_GROUP_ADMIN);
464 verifyDenied(action, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
465 }
466
467 @Test (timeout=180000)
468 public void testTableDisable() throws Exception {
469 AccessTestAction disableTable = new AccessTestAction() {
470 @Override
471 public Object run() throws Exception {
472 ACCESS_CONTROLLER.preDisableTable(ObserverContext.createAndPrepare(CP_ENV, null),
473 TEST_TABLE);
474 return null;
475 }
476 };
477
478 AccessTestAction disableAclTable = new AccessTestAction() {
479 @Override
480 public Object run() throws Exception {
481 ACCESS_CONTROLLER.preDisableTable(ObserverContext.createAndPrepare(CP_ENV, null),
482 AccessControlLists.ACL_TABLE_NAME);
483 return null;
484 }
485 };
486
487 verifyAllowed(disableTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_GROUP_CREATE,
488 USER_GROUP_ADMIN);
489 verifyDenied(disableTable, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
490
491
492 verifyDenied(disableAclTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_RW, USER_RO,
493 USER_GROUP_CREATE, USER_GROUP_ADMIN, USER_GROUP_READ, USER_GROUP_WRITE);
494 }
495
496 @Test (timeout=180000)
497 public void testTableEnable() throws Exception {
498 AccessTestAction enableTable = new AccessTestAction() {
499 @Override
500 public Object run() throws Exception {
501 ACCESS_CONTROLLER
502 .preEnableTable(ObserverContext.createAndPrepare(CP_ENV, null), TEST_TABLE);
503 return null;
504 }
505 };
506
507 verifyAllowed(enableTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_GROUP_CREATE,
508 USER_GROUP_ADMIN);
509 verifyDenied(enableTable, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
510 }
511
512 public static class TestTableDDLProcedure extends Procedure<MasterProcedureEnv>
513 implements TableProcedureInterface {
514 private TableName tableName;
515
516 public TestTableDDLProcedure() {
517 }
518
519 public TestTableDDLProcedure(final MasterProcedureEnv env, final TableName tableName)
520 throws IOException {
521 this.tableName = tableName;
522 this.setTimeout(180000);
523 this.setOwner(env.getRequestUser().getUGI().getShortUserName());
524 }
525
526 @Override
527 public TableName getTableName() {
528 return tableName;
529 }
530
531 @Override
532 public TableOperationType getTableOperationType() {
533 return null;
534 }
535
536 @Override
537 protected boolean abort(MasterProcedureEnv env) {
538 return true;
539 }
540
541 @Override
542 protected void serializeStateData(OutputStream stream) throws IOException {
543 TestProcedureProtos.TestTableDDLStateData.Builder testTableDDLMsg =
544 TestProcedureProtos.TestTableDDLStateData.newBuilder()
545 .setTableName(tableName.getNameAsString());
546 testTableDDLMsg.build().writeDelimitedTo(stream);
547 }
548
549 @Override
550 protected void deserializeStateData(InputStream stream) throws IOException {
551 TestProcedureProtos.TestTableDDLStateData testTableDDLMsg =
552 TestProcedureProtos.TestTableDDLStateData.parseDelimitedFrom(stream);
553 tableName = TableName.valueOf(testTableDDLMsg.getTableName());
554 }
555
556 @Override
557 protected Procedure[] execute(MasterProcedureEnv env) throws ProcedureYieldException,
558 InterruptedException {
559
560 setState(ProcedureState.WAITING_TIMEOUT);
561 return null;
562 }
563
564 @Override
565 protected void rollback(MasterProcedureEnv env) throws IOException, InterruptedException {
566 }
567 }
568
569 @Test
570 public void testAbortProcedure() throws Exception {
571 final TableName tableName = TableName.valueOf("testAbortProcedure");
572 final ProcedureExecutor<MasterProcedureEnv> procExec =
573 TEST_UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
574 Procedure proc = new TestTableDDLProcedure(procExec.getEnvironment(), tableName);
575 proc.setOwner(USER_OWNER.getShortName());
576 final long procId = procExec.submitProcedure(proc);
577
578 AccessTestAction abortProcedureAction = new AccessTestAction() {
579 @Override
580 public Object run() throws Exception {
581 ACCESS_CONTROLLER
582 .preAbortProcedure(ObserverContext.createAndPrepare(CP_ENV, null), procExec, procId);
583 return null;
584 }
585 };
586
587 verifyAllowed(abortProcedureAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
588 verifyAllowed(abortProcedureAction, USER_OWNER);
589 verifyDenied(
590 abortProcedureAction, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
591 }
592
593 @Test
594 public void testListProcedures() throws Exception {
595 final TableName tableName = TableName.valueOf("testAbortProcedure");
596 final ProcedureExecutor<MasterProcedureEnv> procExec =
597 TEST_UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
598 Procedure proc = new TestTableDDLProcedure(procExec.getEnvironment(), tableName);
599 proc.setOwner(USER_OWNER.getShortName());
600 final long procId = procExec.submitProcedure(proc);
601 final List<ProcedureInfo> procInfoList = procExec.listProcedures();
602
603 AccessTestAction listProceduresAction = new AccessTestAction() {
604 @Override
605 public Object run() throws Exception {
606 List<ProcedureInfo> procInfoListClone = new ArrayList<ProcedureInfo>(procInfoList.size());
607 for(ProcedureInfo pi : procInfoList) {
608 procInfoListClone.add(pi.clone());
609 }
610 ACCESS_CONTROLLER
611 .postListProcedures(ObserverContext.createAndPrepare(CP_ENV, null), procInfoListClone);
612 return null;
613 }
614 };
615
616 verifyAllowed(listProceduresAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
617 verifyAllowed(listProceduresAction, USER_OWNER);
618 verifyIfNull(
619 listProceduresAction, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
620 }
621
622 @Test (timeout=180000)
623 public void testMove() throws Exception {
624 List<HRegionLocation> regions;
625 try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE)) {
626 regions = locator.getAllRegionLocations();
627 }
628 HRegionLocation location = regions.get(0);
629 final HRegionInfo hri = location.getRegionInfo();
630 final ServerName server = location.getServerName();
631 AccessTestAction action = new AccessTestAction() {
632 @Override
633 public Object run() throws Exception {
634 ACCESS_CONTROLLER.preMove(ObserverContext.createAndPrepare(CP_ENV, null),
635 hri, server, server);
636 return null;
637 }
638 };
639
640 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
641 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
642 USER_GROUP_WRITE, USER_GROUP_CREATE);
643 }
644
645 @Test (timeout=180000)
646 public void testAssign() throws Exception {
647 List<HRegionLocation> regions;
648 try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE)) {
649 regions = locator.getAllRegionLocations();
650 }
651 HRegionLocation location = regions.get(0);
652 final HRegionInfo hri = location.getRegionInfo();
653 AccessTestAction action = new AccessTestAction() {
654 @Override
655 public Object run() throws Exception {
656 ACCESS_CONTROLLER.preAssign(ObserverContext.createAndPrepare(CP_ENV, null), hri);
657 return null;
658 }
659 };
660
661 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
662 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
663 USER_GROUP_WRITE, USER_GROUP_CREATE);
664 }
665
666 @Test (timeout=180000)
667 public void testUnassign() throws Exception {
668 List<HRegionLocation> regions;
669 try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE)) {
670 regions = locator.getAllRegionLocations();
671 }
672 HRegionLocation location = regions.get(0);
673 final HRegionInfo hri = location.getRegionInfo();
674 AccessTestAction action = new AccessTestAction() {
675 @Override
676 public Object run() throws Exception {
677 ACCESS_CONTROLLER.preUnassign(ObserverContext.createAndPrepare(CP_ENV, null), hri, false);
678 return null;
679 }
680 };
681
682 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
683 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
684 USER_GROUP_WRITE, USER_GROUP_CREATE);
685 }
686
687 @Test (timeout=180000)
688 public void testRegionOffline() throws Exception {
689 List<HRegionLocation> regions;
690 try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE)) {
691 regions = locator.getAllRegionLocations();
692 }
693 HRegionLocation location = regions.get(0);
694 final HRegionInfo hri = location.getRegionInfo();
695 AccessTestAction action = new AccessTestAction() {
696 @Override
697 public Object run() throws Exception {
698 ACCESS_CONTROLLER.preRegionOffline(ObserverContext.createAndPrepare(CP_ENV, null), hri);
699 return null;
700 }
701 };
702
703 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
704 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
705 USER_GROUP_WRITE, USER_GROUP_CREATE);
706 }
707
708 @Test (timeout=180000)
709 public void testBalance() throws Exception {
710 AccessTestAction action = new AccessTestAction() {
711 @Override
712 public Object run() throws Exception {
713 ACCESS_CONTROLLER.preBalance(ObserverContext.createAndPrepare(CP_ENV, null));
714 return null;
715 }
716 };
717
718 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
719 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
720 USER_GROUP_WRITE, USER_GROUP_CREATE);
721 }
722
723 @Test (timeout=180000)
724 public void testBalanceSwitch() throws Exception {
725 AccessTestAction action = new AccessTestAction() {
726 @Override
727 public Object run() throws Exception {
728 ACCESS_CONTROLLER.preBalanceSwitch(ObserverContext.createAndPrepare(CP_ENV, null), true);
729 return null;
730 }
731 };
732
733 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
734 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
735 USER_GROUP_WRITE, USER_GROUP_CREATE);
736 }
737
738 @Test (timeout=180000)
739 public void testShutdown() throws Exception {
740 AccessTestAction action = new AccessTestAction() {
741 @Override
742 public Object run() throws Exception {
743 ACCESS_CONTROLLER.preShutdown(ObserverContext.createAndPrepare(CP_ENV, null));
744 return null;
745 }
746 };
747
748 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
749 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
750 USER_GROUP_WRITE, USER_GROUP_CREATE);
751 }
752
753 @Test (timeout=180000)
754 public void testStopMaster() throws Exception {
755 AccessTestAction action = new AccessTestAction() {
756 @Override
757 public Object run() throws Exception {
758 ACCESS_CONTROLLER.preStopMaster(ObserverContext.createAndPrepare(CP_ENV, null));
759 return null;
760 }
761 };
762
763 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
764 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
765 USER_GROUP_WRITE, USER_GROUP_CREATE);
766 }
767
768 private void verifyWrite(AccessTestAction action) throws Exception {
769 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_RW,
770 USER_GROUP_WRITE);
771 verifyDenied(action, USER_NONE, USER_RO, USER_GROUP_ADMIN, USER_GROUP_READ, USER_GROUP_CREATE);
772 }
773
774 @Test (timeout=180000)
775 public void testSplit() throws Exception {
776 AccessTestAction action = new AccessTestAction() {
777 @Override
778 public Object run() throws Exception {
779 ACCESS_CONTROLLER.preSplit(ObserverContext.createAndPrepare(RCP_ENV, null));
780 return null;
781 }
782 };
783
784 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
785 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
786 USER_GROUP_WRITE, USER_GROUP_CREATE);
787 }
788
789 @Test (timeout=180000)
790 public void testSplitWithSplitRow() throws Exception {
791 AccessTestAction action = new AccessTestAction() {
792 @Override
793 public Object run() throws Exception {
794 ACCESS_CONTROLLER.preSplit(
795 ObserverContext.createAndPrepare(RCP_ENV, null),
796 TEST_ROW);
797 return null;
798 }
799 };
800
801 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
802 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
803 USER_GROUP_WRITE, USER_GROUP_CREATE);
804 }
805
806 @Test (timeout=180000)
807 public void testMergeRegions() throws Exception {
808 final TableName tname = TableName.valueOf("testMergeRegions");
809 createTestTable(tname);
810 try {
811 final List<HRegion> regions = TEST_UTIL.getHBaseCluster().findRegionsForTable(tname);
812 assertTrue("not enough regions: " + regions.size(), regions.size() >= 2);
813
814 AccessTestAction action = new AccessTestAction() {
815 @Override
816 public Object run() throws Exception {
817 ACCESS_CONTROLLER.preMerge(ObserverContext.createAndPrepare(RSCP_ENV, null),
818 regions.get(0), regions.get(1));
819 return null;
820 }
821 };
822
823 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
824 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
825 USER_GROUP_WRITE, USER_GROUP_CREATE);
826 } finally {
827 deleteTable(TEST_UTIL, tname);
828 }
829 }
830
831 @Test (timeout=180000)
832 public void testFlush() throws Exception {
833 AccessTestAction action = new AccessTestAction() {
834 @Override
835 public Object run() throws Exception {
836 ACCESS_CONTROLLER.preFlush(ObserverContext.createAndPrepare(RCP_ENV, null));
837 return null;
838 }
839 };
840
841 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_GROUP_CREATE,
842 USER_GROUP_ADMIN);
843 verifyDenied(action, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
844 }
845
846 @Test (timeout=180000)
847 public void testCompact() throws Exception {
848 AccessTestAction action = new AccessTestAction() {
849 @Override
850 public Object run() throws Exception {
851 ACCESS_CONTROLLER.preCompact(ObserverContext.createAndPrepare(RCP_ENV, null), null, null,
852 ScanType.COMPACT_RETAIN_DELETES);
853 return null;
854 }
855 };
856
857 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_GROUP_CREATE,
858 USER_GROUP_ADMIN);
859 verifyDenied(action, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
860 }
861
862 private void verifyRead(AccessTestAction action) throws Exception {
863 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_RW, USER_RO,
864 USER_GROUP_READ);
865 verifyDenied(action, USER_NONE, USER_GROUP_CREATE, USER_GROUP_ADMIN, USER_GROUP_WRITE);
866 }
867
868 private void verifyReadWrite(AccessTestAction action) throws Exception {
869 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_RW);
870 verifyDenied(action, USER_NONE, USER_RO, USER_GROUP_ADMIN, USER_GROUP_CREATE, USER_GROUP_READ,
871 USER_GROUP_WRITE);
872 }
873
874 @Test (timeout=180000)
875 public void testRead() throws Exception {
876
877 AccessTestAction getAction = new AccessTestAction() {
878 @Override
879 public Object run() throws Exception {
880 Get g = new Get(TEST_ROW);
881 g.addFamily(TEST_FAMILY);
882 try(Connection conn = ConnectionFactory.createConnection(conf);
883 Table t = conn.getTable(TEST_TABLE)) {
884 t.get(g);
885 }
886 return null;
887 }
888 };
889 verifyRead(getAction);
890
891
892 AccessTestAction scanAction = new AccessTestAction() {
893 @Override
894 public Object run() throws Exception {
895 Scan s = new Scan();
896 s.addFamily(TEST_FAMILY);
897 try(Connection conn = ConnectionFactory.createConnection(conf);
898 Table table = conn.getTable(TEST_TABLE)) {
899 ResultScanner scanner = table.getScanner(s);
900 try {
901 for (Result r = scanner.next(); r != null; r = scanner.next()) {
902
903 }
904 } catch (IOException e) {
905 } finally {
906 scanner.close();
907 }
908 }
909 return null;
910 }
911 };
912 verifyRead(scanAction);
913 }
914
915 @Test (timeout=180000)
916
917 public void testWrite() throws Exception {
918
919 AccessTestAction putAction = new AccessTestAction() {
920 @Override
921 public Object run() throws Exception {
922 Put p = new Put(TEST_ROW);
923 p.addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(1));
924 try(Connection conn = ConnectionFactory.createConnection(conf);
925 Table t = conn.getTable(TEST_TABLE)) {
926 t.put(p);
927 }
928 return null;
929 }
930 };
931 verifyWrite(putAction);
932
933
934 AccessTestAction deleteAction = new AccessTestAction() {
935 @Override
936 public Object run() throws Exception {
937 Delete d = new Delete(TEST_ROW);
938 d.addFamily(TEST_FAMILY);
939 try(Connection conn = ConnectionFactory.createConnection(conf);
940 Table t = conn.getTable(TEST_TABLE)) {
941 t.delete(d);
942 }
943 return null;
944 }
945 };
946 verifyWrite(deleteAction);
947
948
949 AccessTestAction incrementAction = new AccessTestAction() {
950 @Override
951 public Object run() throws Exception {
952 Increment inc = new Increment(TEST_ROW);
953 inc.addColumn(TEST_FAMILY, TEST_QUALIFIER, 1);
954 try(Connection conn = ConnectionFactory.createConnection(conf);
955 Table t = conn.getTable(TEST_TABLE);) {
956 t.increment(inc);
957 }
958 return null;
959 }
960 };
961 verifyWrite(incrementAction);
962 }
963
964 @Test (timeout=180000)
965 public void testReadWrite() throws Exception {
966
967 AccessTestAction checkAndDeleteAction = new AccessTestAction() {
968 @Override
969 public Object run() throws Exception {
970 Delete d = new Delete(TEST_ROW);
971 d.addFamily(TEST_FAMILY);
972 try(Connection conn = ConnectionFactory.createConnection(conf);
973 Table t = conn.getTable(TEST_TABLE);) {
974 t.checkAndDelete(TEST_ROW, TEST_FAMILY, TEST_QUALIFIER,
975 Bytes.toBytes("test_value"), d);
976 }
977 return null;
978 }
979 };
980 verifyReadWrite(checkAndDeleteAction);
981
982
983 AccessTestAction checkAndPut = new AccessTestAction() {
984 @Override
985 public Object run() throws Exception {
986 Put p = new Put(TEST_ROW);
987 p.addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(1));
988 try(Connection conn = ConnectionFactory.createConnection(conf);
989 Table t = conn.getTable(TEST_TABLE);) {
990 t.checkAndPut(TEST_ROW, TEST_FAMILY, TEST_QUALIFIER,
991 Bytes.toBytes("test_value"), p);
992 }
993 return null;
994 }
995 };
996 verifyReadWrite(checkAndPut);
997 }
998
999 @Test (timeout=180000)
1000 public void testBulkLoad() throws Exception {
1001 try {
1002 FileSystem fs = TEST_UTIL.getTestFileSystem();
1003 final Path dir = TEST_UTIL.getDataTestDirOnTestFS("testBulkLoad");
1004 fs.mkdirs(dir);
1005
1006
1007 fs.setPermission(dir, FsPermission.valueOf("-rwxrwxrwx"));
1008
1009 AccessTestAction bulkLoadAction = new AccessTestAction() {
1010 @Override
1011 public Object run() throws Exception {
1012 int numRows = 3;
1013
1014
1015 byte[][][] hfileRanges = { { { (byte) 0 }, { (byte) 9 } } };
1016
1017 Path bulkLoadBasePath = new Path(dir, new Path(User.getCurrent().getName()));
1018 new BulkLoadHelper(bulkLoadBasePath).bulkLoadHFile(TEST_TABLE, TEST_FAMILY,
1019 TEST_QUALIFIER, hfileRanges, numRows);
1020
1021 return null;
1022 }
1023 };
1024
1025
1026
1027 verifyAllowed(bulkLoadAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE,
1028 USER_GROUP_CREATE);
1029 verifyDenied(bulkLoadAction, USER_RW, USER_NONE, USER_RO, USER_GROUP_READ, USER_GROUP_WRITE,
1030 USER_GROUP_ADMIN);
1031 } finally {
1032
1033 TEST_UTIL.getHBaseAdmin().disableTable(TEST_TABLE);
1034 TEST_UTIL.getHBaseAdmin().enableTable(TEST_TABLE);
1035 }
1036 }
1037
1038 public class BulkLoadHelper {
1039 private final FileSystem fs;
1040 private final Path loadPath;
1041 private final Configuration conf;
1042
1043 public BulkLoadHelper(Path loadPath) throws IOException {
1044 fs = TEST_UTIL.getTestFileSystem();
1045 conf = TEST_UTIL.getConfiguration();
1046 loadPath = loadPath.makeQualified(fs);
1047 this.loadPath = loadPath;
1048 }
1049
1050 private void createHFile(Path path,
1051 byte[] family, byte[] qualifier,
1052 byte[] startKey, byte[] endKey, int numRows) throws IOException {
1053
1054 HFile.Writer writer = null;
1055 long now = System.currentTimeMillis();
1056 try {
1057 HFileContext context = new HFileContextBuilder().build();
1058 writer = HFile.getWriterFactory(conf, new CacheConfig(conf))
1059 .withPath(fs, path)
1060 .withFileContext(context)
1061 .create();
1062
1063 for (byte[] key : Bytes.iterateOnSplits(startKey, endKey, true, numRows-2)) {
1064 KeyValue kv = new KeyValue(key, family, qualifier, now, key);
1065 writer.append(kv);
1066 }
1067 } finally {
1068 if(writer != null)
1069 writer.close();
1070 }
1071 }
1072
1073 private void bulkLoadHFile(
1074 TableName tableName,
1075 byte[] family,
1076 byte[] qualifier,
1077 byte[][][] hfileRanges,
1078 int numRowsPerRange) throws Exception {
1079
1080 Path familyDir = new Path(loadPath, Bytes.toString(family));
1081 fs.mkdirs(familyDir);
1082 int hfileIdx = 0;
1083 for (byte[][] range : hfileRanges) {
1084 byte[] from = range[0];
1085 byte[] to = range[1];
1086 createHFile(new Path(familyDir, "hfile_"+(hfileIdx++)),
1087 family, qualifier, from, to, numRowsPerRange);
1088 }
1089
1090 setPermission(loadPath, FsPermission.valueOf("-rwxrwxrwx"));
1091
1092
1093 try (Connection conn = ConnectionFactory.createConnection(conf);
1094 Admin admin = conn.getAdmin();
1095 RegionLocator locator = conn.getRegionLocator(tableName);
1096 Table table = conn.getTable(tableName)) {
1097 TEST_UTIL.waitUntilAllRegionsAssigned(tableName);
1098 LoadIncrementalHFiles loader = new LoadIncrementalHFiles(conf);
1099 loader.doBulkLoad(loadPath, admin, table, locator);
1100 }
1101 }
1102
1103 public void setPermission(Path dir, FsPermission perm) throws IOException {
1104 if(!fs.getFileStatus(dir).isDirectory()) {
1105 fs.setPermission(dir,perm);
1106 }
1107 else {
1108 for(FileStatus el : fs.listStatus(dir)) {
1109 fs.setPermission(el.getPath(), perm);
1110 setPermission(el.getPath() , perm);
1111 }
1112 }
1113 }
1114 }
1115
1116 @Test (timeout=180000)
1117 public void testAppend() throws Exception {
1118
1119 AccessTestAction appendAction = new AccessTestAction() {
1120 @Override
1121 public Object run() throws Exception {
1122 byte[] row = TEST_ROW;
1123 byte[] qualifier = TEST_QUALIFIER;
1124 Put put = new Put(row);
1125 put.addColumn(TEST_FAMILY, qualifier, Bytes.toBytes(1));
1126 Append append = new Append(row);
1127 append.add(TEST_FAMILY, qualifier, Bytes.toBytes(2));
1128 try(Connection conn = ConnectionFactory.createConnection(conf);
1129 Table t = conn.getTable(TEST_TABLE)) {
1130 t.put(put);
1131 t.append(append);
1132 }
1133 return null;
1134 }
1135 };
1136
1137 verifyAllowed(appendAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_RW,
1138 USER_GROUP_WRITE);
1139 verifyDenied(appendAction, USER_RO, USER_NONE, USER_GROUP_CREATE, USER_GROUP_READ,
1140 USER_GROUP_ADMIN);
1141 }
1142
1143 @Test (timeout=180000)
1144 public void testGrantRevoke() throws Exception {
1145 AccessTestAction grantAction = new AccessTestAction() {
1146 @Override
1147 public Object run() throws Exception {
1148 try(Connection conn = ConnectionFactory.createConnection(conf);
1149 Table acl = conn.getTable(AccessControlLists.ACL_TABLE_NAME)) {
1150 BlockingRpcChannel service = acl.coprocessorService(TEST_TABLE.getName());
1151 AccessControlService.BlockingInterface protocol =
1152 AccessControlService.newBlockingStub(service);
1153 ProtobufUtil.grant(protocol, USER_RO.getShortName(), TEST_TABLE, TEST_FAMILY, null,
1154 Action.READ);
1155 }
1156 return null;
1157 }
1158 };
1159
1160 AccessTestAction revokeAction = new AccessTestAction() {
1161 @Override
1162 public Object run() throws Exception {
1163 try(Connection conn = ConnectionFactory.createConnection(conf);
1164 Table acl = conn.getTable(AccessControlLists.ACL_TABLE_NAME)) {
1165 BlockingRpcChannel service = acl.coprocessorService(TEST_TABLE.getName());
1166 AccessControlService.BlockingInterface protocol =
1167 AccessControlService.newBlockingStub(service);
1168 ProtobufUtil.revoke(protocol, USER_RO.getShortName(), TEST_TABLE, TEST_FAMILY, null,
1169 Action.READ);
1170 }
1171 return null;
1172 }
1173 };
1174
1175 AccessTestAction getTablePermissionsAction = new AccessTestAction() {
1176 @Override
1177 public Object run() throws Exception {
1178 try(Connection conn = ConnectionFactory.createConnection(conf);
1179 Table acl = conn.getTable(AccessControlLists.ACL_TABLE_NAME)){
1180 BlockingRpcChannel service = acl.coprocessorService(TEST_TABLE.getName());
1181 AccessControlService.BlockingInterface protocol =
1182 AccessControlService.newBlockingStub(service);
1183 ProtobufUtil.getUserPermissions(protocol, TEST_TABLE);
1184 }
1185 return null;
1186 }
1187 };
1188
1189 AccessTestAction getGlobalPermissionsAction = new AccessTestAction() {
1190 @Override
1191 public Object run() throws Exception {
1192 try(Connection conn = ConnectionFactory.createConnection(conf);
1193 Table acl = conn.getTable(AccessControlLists.ACL_TABLE_NAME);) {
1194 BlockingRpcChannel service = acl.coprocessorService(HConstants.EMPTY_START_ROW);
1195 AccessControlService.BlockingInterface protocol =
1196 AccessControlService.newBlockingStub(service);
1197 ProtobufUtil.getUserPermissions(protocol);
1198 }
1199 return null;
1200 }
1201 };
1202
1203 verifyAllowed(grantAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
1204 verifyDenied(grantAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
1205 USER_GROUP_WRITE, USER_GROUP_CREATE);
1206 try {
1207 verifyAllowed(revokeAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
1208 verifyDenied(revokeAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
1209 USER_GROUP_WRITE, USER_GROUP_CREATE);
1210
1211 verifyAllowed(getTablePermissionsAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
1212 verifyDenied(getTablePermissionsAction, USER_CREATE, USER_RW, USER_RO, USER_NONE,
1213 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
1214
1215 verifyAllowed(getGlobalPermissionsAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
1216 verifyDenied(getGlobalPermissionsAction, USER_CREATE, USER_OWNER, USER_RW, USER_RO,
1217 USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
1218 } finally {
1219
1220 grantOnTable(TEST_UTIL, USER_RO.getShortName(), TEST_TABLE, TEST_FAMILY, null,
1221 Permission.Action.READ);
1222 }
1223 }
1224
1225 @Test (timeout=180000)
1226 public void testPostGrantRevoke() throws Exception {
1227 final TableName tableName =
1228 TableName.valueOf("TempTable");
1229 final byte[] family1 = Bytes.toBytes("f1");
1230 final byte[] family2 = Bytes.toBytes("f2");
1231 final byte[] qualifier = Bytes.toBytes("q");
1232
1233
1234 Admin admin = TEST_UTIL.getHBaseAdmin();
1235 if (admin.tableExists(tableName)) {
1236 deleteTable(TEST_UTIL, tableName);
1237 }
1238 HTableDescriptor htd = new HTableDescriptor(tableName);
1239 htd.addFamily(new HColumnDescriptor(family1));
1240 htd.addFamily(new HColumnDescriptor(family2));
1241 createTable(TEST_UTIL, htd);
1242 try {
1243
1244 User tblUser =
1245 User.createUserForTesting(TEST_UTIL.getConfiguration(), "tbluser", new String[0]);
1246 User gblUser =
1247 User.createUserForTesting(TEST_UTIL.getConfiguration(), "gbluser", new String[0]);
1248
1249
1250 AccessTestAction putActionAll = new AccessTestAction() {
1251 @Override
1252 public Object run() throws Exception {
1253 Put p = new Put(Bytes.toBytes("a"));
1254 p.addColumn(family1, qualifier, Bytes.toBytes("v1"));
1255 p.addColumn(family2, qualifier, Bytes.toBytes("v2"));
1256
1257 try (Connection conn = ConnectionFactory.createConnection(conf);
1258 Table t = conn.getTable(tableName);) {
1259 t.put(p);
1260 }
1261 return null;
1262 }
1263 };
1264
1265 AccessTestAction putAction1 = new AccessTestAction() {
1266 @Override
1267 public Object run() throws Exception {
1268 Put p = new Put(Bytes.toBytes("a"));
1269 p.addColumn(family1, qualifier, Bytes.toBytes("v1"));
1270
1271 try (Connection conn = ConnectionFactory.createConnection(conf);
1272 Table t = conn.getTable(tableName)) {
1273 t.put(p);
1274 }
1275 return null;
1276 }
1277 };
1278
1279 AccessTestAction putAction2 = new AccessTestAction() {
1280 @Override
1281 public Object run() throws Exception {
1282 Put p = new Put(Bytes.toBytes("a"));
1283 p.addColumn(family2, qualifier, Bytes.toBytes("v2"));
1284 try (Connection conn = ConnectionFactory.createConnection(conf);
1285 Table t = conn.getTable(tableName);) {
1286 t.put(p);
1287 }
1288 return null;
1289 }
1290 };
1291
1292 AccessTestAction getActionAll = new AccessTestAction() {
1293 @Override
1294 public Object run() throws Exception {
1295 Get g = new Get(TEST_ROW);
1296 g.addFamily(family1);
1297 g.addFamily(family2);
1298 try (Connection conn = ConnectionFactory.createConnection(conf);
1299 Table t = conn.getTable(tableName);) {
1300 t.get(g);
1301 }
1302 return null;
1303 }
1304 };
1305
1306 AccessTestAction getAction1 = new AccessTestAction() {
1307 @Override
1308 public Object run() throws Exception {
1309 Get g = new Get(TEST_ROW);
1310 g.addFamily(family1);
1311 try (Connection conn = ConnectionFactory.createConnection(conf);
1312 Table t = conn.getTable(tableName)) {
1313 t.get(g);
1314 }
1315 return null;
1316 }
1317 };
1318
1319 AccessTestAction getAction2 = new AccessTestAction() {
1320 @Override
1321 public Object run() throws Exception {
1322 Get g = new Get(TEST_ROW);
1323 g.addFamily(family2);
1324 try (Connection conn = ConnectionFactory.createConnection(conf);
1325 Table t = conn.getTable(tableName)) {
1326 t.get(g);
1327 }
1328 return null;
1329 }
1330 };
1331
1332 AccessTestAction deleteActionAll = new AccessTestAction() {
1333 @Override
1334 public Object run() throws Exception {
1335 Delete d = new Delete(TEST_ROW);
1336 d.addFamily(family1);
1337 d.addFamily(family2);
1338 try (Connection conn = ConnectionFactory.createConnection(conf);
1339 Table t = conn.getTable(tableName)) {
1340 t.delete(d);
1341 }
1342 return null;
1343 }
1344 };
1345
1346 AccessTestAction deleteAction1 = new AccessTestAction() {
1347 @Override
1348 public Object run() throws Exception {
1349 Delete d = new Delete(TEST_ROW);
1350 d.addFamily(family1);
1351 try (Connection conn = ConnectionFactory.createConnection(conf);
1352 Table t = conn.getTable(tableName)) {
1353 t.delete(d);
1354 }
1355 return null;
1356 }
1357 };
1358
1359 AccessTestAction deleteAction2 = new AccessTestAction() {
1360 @Override
1361 public Object run() throws Exception {
1362 Delete d = new Delete(TEST_ROW);
1363 d.addFamily(family2);
1364 try (Connection conn = ConnectionFactory.createConnection(conf);
1365 Table t = conn.getTable(tableName)) {
1366 t.delete(d);
1367 }
1368 return null;
1369 }
1370 };
1371
1372
1373 verifyDenied(tblUser, getActionAll, getAction1, getAction2);
1374 verifyDenied(tblUser, putActionAll, putAction1, putAction2);
1375 verifyDenied(tblUser, deleteActionAll, deleteAction1, deleteAction2);
1376
1377 verifyDenied(gblUser, getActionAll, getAction1, getAction2);
1378 verifyDenied(gblUser, putActionAll, putAction1, putAction2);
1379 verifyDenied(gblUser, deleteActionAll, deleteAction1, deleteAction2);
1380
1381
1382 grantGlobal(TEST_UTIL, gblUser.getShortName(), Permission.Action.READ);
1383 grantOnTable(TEST_UTIL, tblUser.getShortName(), tableName, null, null, Permission.Action.READ);
1384
1385
1386 verifyAllowed(tblUser, getActionAll, getAction1, getAction2);
1387 verifyDenied(tblUser, putActionAll, putAction1, putAction2);
1388 verifyDenied(tblUser, deleteActionAll, deleteAction1, deleteAction2);
1389
1390 verifyAllowed(gblUser, getActionAll, getAction1, getAction2);
1391 verifyDenied(gblUser, putActionAll, putAction1, putAction2);
1392 verifyDenied(gblUser, deleteActionAll, deleteAction1, deleteAction2);
1393
1394
1395 grantGlobal(TEST_UTIL, gblUser.getShortName(), Permission.Action.WRITE);
1396 grantOnTable(TEST_UTIL, tblUser.getShortName(), tableName, null, null,
1397 Permission.Action.WRITE);
1398
1399 verifyDenied(tblUser, getActionAll, getAction1, getAction2);
1400 verifyAllowed(tblUser, putActionAll, putAction1, putAction2);
1401 verifyAllowed(tblUser, deleteActionAll, deleteAction1, deleteAction2);
1402
1403 verifyDenied(gblUser, getActionAll, getAction1, getAction2);
1404 verifyAllowed(gblUser, putActionAll, putAction1, putAction2);
1405 verifyAllowed(gblUser, deleteActionAll, deleteAction1, deleteAction2);
1406
1407
1408 revokeGlobal(TEST_UTIL, gblUser.getShortName());
1409 revokeFromTable(TEST_UTIL, tblUser.getShortName(), tableName, null, null);
1410
1411 verifyDenied(tblUser, getActionAll, getAction1, getAction2);
1412 verifyDenied(tblUser, putActionAll, putAction1, putAction2);
1413 verifyDenied(tblUser, deleteActionAll, deleteAction1, deleteAction2);
1414
1415 verifyDenied(gblUser, getActionAll, getAction1, getAction2);
1416 verifyDenied(gblUser, putActionAll, putAction1, putAction2);
1417 verifyDenied(gblUser, deleteActionAll, deleteAction1, deleteAction2);
1418
1419
1420 grantGlobal(TEST_UTIL, gblUser.getShortName(), Permission.Action.READ);
1421 grantOnTable(TEST_UTIL, tblUser.getShortName(), tableName, family1, null,
1422 Permission.Action.READ);
1423
1424
1425 verifyAllowed(tblUser, getActionAll, getAction1);
1426 verifyDenied(tblUser, getAction2);
1427 verifyDenied(tblUser, putActionAll, putAction1, putAction2);
1428 verifyDenied(tblUser, deleteActionAll, deleteAction1, deleteAction2);
1429
1430 verifyAllowed(gblUser, getActionAll, getAction1, getAction2);
1431 verifyDenied(gblUser, putActionAll, putAction1, putAction2);
1432 verifyDenied(gblUser, deleteActionAll, deleteAction1, deleteAction2);
1433
1434
1435 grantGlobal(TEST_UTIL, gblUser.getShortName(), Permission.Action.WRITE);
1436 grantOnTable(TEST_UTIL, tblUser.getShortName(), tableName, family2, null,
1437 Permission.Action.WRITE);
1438
1439
1440 verifyAllowed(tblUser, getActionAll, getAction1);
1441 verifyAllowed(tblUser, putAction2, deleteAction2);
1442 verifyDenied(tblUser, getAction2);
1443 verifyDenied(tblUser, putActionAll, putAction1);
1444 verifyDenied(tblUser, deleteActionAll, deleteAction1);
1445
1446 verifyDenied(gblUser, getActionAll, getAction1, getAction2);
1447 verifyAllowed(gblUser, putActionAll, putAction1, putAction2);
1448 verifyAllowed(gblUser, deleteActionAll, deleteAction1, deleteAction2);
1449
1450
1451 revokeGlobal(TEST_UTIL, gblUser.getShortName());
1452 revokeFromTable(TEST_UTIL, tblUser.getShortName(), tableName, family2, null);
1453
1454
1455 verifyAllowed(tblUser, getActionAll, getAction1);
1456 verifyDenied(tblUser, getAction2);
1457 verifyDenied(tblUser, putActionAll, putAction1, putAction2);
1458 verifyDenied(tblUser, deleteActionAll, deleteAction1, deleteAction2);
1459
1460
1461 verifyDenied(gblUser, getActionAll, getAction1, getAction2);
1462 verifyDenied(gblUser, putActionAll, putAction1, putAction2);
1463 verifyDenied(gblUser, deleteActionAll, deleteAction1, deleteAction2);
1464 } finally {
1465
1466 deleteTable(TEST_UTIL, tableName);
1467 }
1468 }
1469
1470 private boolean hasFoundUserPermission(List<UserPermission> userPermissions,
1471 List<UserPermission> perms) {
1472 return perms.containsAll(userPermissions);
1473 }
1474
1475 private boolean hasFoundUserPermission(UserPermission userPermission, List<UserPermission> perms) {
1476 return perms.contains(userPermission);
1477 }
1478
1479 @Test (timeout=180000)
1480 public void testPostGrantRevokeAtQualifierLevel() throws Exception {
1481 final TableName tableName =
1482 TableName.valueOf("testGrantRevokeAtQualifierLevel");
1483 final byte[] family1 = Bytes.toBytes("f1");
1484 final byte[] family2 = Bytes.toBytes("f2");
1485 final byte[] qualifier = Bytes.toBytes("q");
1486
1487
1488 Admin admin = TEST_UTIL.getHBaseAdmin();
1489 if (admin.tableExists(tableName)) {
1490 deleteTable(TEST_UTIL, tableName);
1491 }
1492 HTableDescriptor htd = new HTableDescriptor(tableName);
1493 htd.addFamily(new HColumnDescriptor(family1));
1494 htd.addFamily(new HColumnDescriptor(family2));
1495 createTable(TEST_UTIL, htd);
1496
1497 try {
1498
1499 User user = User.createUserForTesting(TEST_UTIL.getConfiguration(), "user", new String[0]);
1500
1501 AccessTestAction getQualifierAction = new AccessTestAction() {
1502 @Override
1503 public Object run() throws Exception {
1504 Get g = new Get(TEST_ROW);
1505 g.addColumn(family1, qualifier);
1506 try (Connection conn = ConnectionFactory.createConnection(conf);
1507 Table t = conn.getTable(tableName)) {
1508 t.get(g);
1509 }
1510 return null;
1511 }
1512 };
1513
1514 AccessTestAction putQualifierAction = new AccessTestAction() {
1515 @Override
1516 public Object run() throws Exception {
1517 Put p = new Put(TEST_ROW);
1518 p.addColumn(family1, qualifier, Bytes.toBytes("v1"));
1519 try (Connection conn = ConnectionFactory.createConnection(conf);
1520 Table t = conn.getTable(tableName)) {
1521 t.put(p);
1522 }
1523 return null;
1524 }
1525 };
1526
1527 AccessTestAction deleteQualifierAction = new AccessTestAction() {
1528 @Override
1529 public Object run() throws Exception {
1530 Delete d = new Delete(TEST_ROW);
1531 d.addColumn(family1, qualifier);
1532
1533 try (Connection conn = ConnectionFactory.createConnection(conf);
1534 Table t = conn.getTable(tableName)) {
1535 t.delete(d);
1536 }
1537 return null;
1538 }
1539 };
1540
1541 revokeFromTable(TEST_UTIL, user.getShortName(), tableName, family1, null);
1542
1543 verifyDenied(user, getQualifierAction);
1544 verifyDenied(user, putQualifierAction);
1545 verifyDenied(user, deleteQualifierAction);
1546
1547 grantOnTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier,
1548 Permission.Action.READ);
1549
1550 verifyAllowed(user, getQualifierAction);
1551 verifyDenied(user, putQualifierAction);
1552 verifyDenied(user, deleteQualifierAction);
1553
1554
1555
1556 grantOnTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier,
1557 Permission.Action.WRITE);
1558
1559 verifyDenied(user, getQualifierAction);
1560 verifyAllowed(user, putQualifierAction);
1561 verifyAllowed(user, deleteQualifierAction);
1562
1563
1564 grantOnTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier,
1565 Permission.Action.READ, Permission.Action.WRITE);
1566
1567 verifyAllowed(user, getQualifierAction);
1568 verifyAllowed(user, putQualifierAction);
1569 verifyAllowed(user, deleteQualifierAction);
1570
1571
1572 revokeFromTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier);
1573
1574 verifyDenied(user, getQualifierAction);
1575 verifyDenied(user, putQualifierAction);
1576 verifyDenied(user, deleteQualifierAction);
1577 } finally {
1578
1579 deleteTable(TEST_UTIL, tableName);
1580 }
1581 }
1582
1583 @Test (timeout=180000)
1584 public void testPermissionList() throws Exception {
1585 final TableName tableName =
1586 TableName.valueOf("testPermissionList");
1587 final byte[] family1 = Bytes.toBytes("f1");
1588 final byte[] family2 = Bytes.toBytes("f2");
1589 final byte[] qualifier = Bytes.toBytes("q");
1590
1591
1592 Admin admin = TEST_UTIL.getHBaseAdmin();
1593 if (admin.tableExists(tableName)) {
1594 deleteTable(TEST_UTIL, tableName);
1595 }
1596 HTableDescriptor htd = new HTableDescriptor(tableName);
1597 htd.addFamily(new HColumnDescriptor(family1));
1598 htd.addFamily(new HColumnDescriptor(family2));
1599 htd.setOwner(USER_OWNER);
1600 createTable(TEST_UTIL, htd);
1601 try {
1602 List<UserPermission> perms;
1603 Table acl = systemUserConnection.getTable(AccessControlLists.ACL_TABLE_NAME);
1604 try {
1605 BlockingRpcChannel service = acl.coprocessorService(tableName.getName());
1606 AccessControlService.BlockingInterface protocol =
1607 AccessControlService.newBlockingStub(service);
1608 perms = ProtobufUtil.getUserPermissions(protocol, tableName);
1609 } finally {
1610 acl.close();
1611 }
1612
1613 UserPermission ownerperm =
1614 new UserPermission(Bytes.toBytes(USER_OWNER.getName()), tableName, null, Action.values());
1615 assertTrue("Owner should have all permissions on table",
1616 hasFoundUserPermission(ownerperm, perms));
1617
1618 User user = User.createUserForTesting(TEST_UTIL.getConfiguration(), "user", new String[0]);
1619 byte[] userName = Bytes.toBytes(user.getShortName());
1620
1621 UserPermission up =
1622 new UserPermission(userName, tableName, family1, qualifier, Permission.Action.READ);
1623 assertFalse("User should not be granted permission: " + up.toString(),
1624 hasFoundUserPermission(up, perms));
1625
1626
1627 grantOnTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier,
1628 Permission.Action.READ);
1629
1630 acl = systemUserConnection.getTable(AccessControlLists.ACL_TABLE_NAME);
1631 try {
1632 BlockingRpcChannel service = acl.coprocessorService(tableName.getName());
1633 AccessControlService.BlockingInterface protocol =
1634 AccessControlService.newBlockingStub(service);
1635 perms = ProtobufUtil.getUserPermissions(protocol, tableName);
1636 } finally {
1637 acl.close();
1638 }
1639
1640 UserPermission upToVerify =
1641 new UserPermission(userName, tableName, family1, qualifier, Permission.Action.READ);
1642 assertTrue("User should be granted permission: " + upToVerify.toString(),
1643 hasFoundUserPermission(upToVerify, perms));
1644
1645 upToVerify =
1646 new UserPermission(userName, tableName, family1, qualifier, Permission.Action.WRITE);
1647 assertFalse("User should not be granted permission: " + upToVerify.toString(),
1648 hasFoundUserPermission(upToVerify, perms));
1649
1650
1651 grantOnTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier,
1652 Permission.Action.WRITE, Permission.Action.READ);
1653
1654 acl = systemUserConnection.getTable(AccessControlLists.ACL_TABLE_NAME);
1655 try {
1656 BlockingRpcChannel service = acl.coprocessorService(tableName.getName());
1657 AccessControlService.BlockingInterface protocol =
1658 AccessControlService.newBlockingStub(service);
1659 perms = ProtobufUtil.getUserPermissions(protocol, tableName);
1660 } finally {
1661 acl.close();
1662 }
1663
1664 upToVerify =
1665 new UserPermission(userName, tableName, family1, qualifier, Permission.Action.WRITE,
1666 Permission.Action.READ);
1667 assertTrue("User should be granted permission: " + upToVerify.toString(),
1668 hasFoundUserPermission(upToVerify, perms));
1669
1670
1671 revokeFromTable(TEST_UTIL, user.getShortName(), tableName, family1, qualifier,
1672 Permission.Action.WRITE, Permission.Action.READ);
1673
1674 acl = systemUserConnection.getTable(AccessControlLists.ACL_TABLE_NAME);
1675 try {
1676 BlockingRpcChannel service = acl.coprocessorService(tableName.getName());
1677 AccessControlService.BlockingInterface protocol =
1678 AccessControlService.newBlockingStub(service);
1679 perms = ProtobufUtil.getUserPermissions(protocol, tableName);
1680 } finally {
1681 acl.close();
1682 }
1683
1684 assertFalse("User should not be granted permission: " + upToVerify.toString(),
1685 hasFoundUserPermission(upToVerify, perms));
1686
1687
1688 admin.disableTable(tableName);
1689
1690 User newOwner = User.createUserForTesting(conf, "new_owner", new String[] {});
1691 htd.setOwner(newOwner);
1692 admin.modifyTable(tableName, htd);
1693
1694 acl = systemUserConnection.getTable(AccessControlLists.ACL_TABLE_NAME);
1695 try {
1696 BlockingRpcChannel service = acl.coprocessorService(tableName.getName());
1697 AccessControlService.BlockingInterface protocol =
1698 AccessControlService.newBlockingStub(service);
1699 perms = ProtobufUtil.getUserPermissions(protocol, tableName);
1700 } finally {
1701 acl.close();
1702 }
1703
1704 UserPermission newOwnerperm =
1705 new UserPermission(Bytes.toBytes(newOwner.getName()), tableName, null, Action.values());
1706 assertTrue("New owner should have all permissions on table",
1707 hasFoundUserPermission(newOwnerperm, perms));
1708 } finally {
1709
1710 deleteTable(TEST_UTIL, tableName);
1711 }
1712 }
1713
1714 @Test (timeout=180000)
1715 public void testGlobalPermissionList() throws Exception {
1716 List<UserPermission> perms;
1717 Table acl = systemUserConnection.getTable(AccessControlLists.ACL_TABLE_NAME);
1718 try {
1719 BlockingRpcChannel service = acl.coprocessorService(HConstants.EMPTY_START_ROW);
1720 AccessControlService.BlockingInterface protocol =
1721 AccessControlService.newBlockingStub(service);
1722 perms = ProtobufUtil.getUserPermissions(protocol);
1723 } finally {
1724 acl.close();
1725 }
1726 List<UserPermission> adminPerms = new ArrayList<UserPermission>();
1727 adminPerms.add(new UserPermission(Bytes.toBytes(USER_ADMIN.getShortName()),
1728 AccessControlLists.ACL_TABLE_NAME, null, null, Bytes.toBytes("ACRW")));
1729 List<String> superUsers = Superusers.getSuperUsers();
1730 for(String user: superUsers) {
1731 adminPerms.add(new UserPermission(Bytes.toBytes(user), AccessControlLists.ACL_TABLE_NAME,
1732 null, null, Action.values()));
1733 }
1734 assertTrue("Only super users, global users and user admin has permission on table hbase:acl " +
1735 "per setup", perms.size() == 5 + superUsers.size() &&
1736 hasFoundUserPermission(adminPerms, perms));
1737 }
1738
1739
1740 private void verifyGlobal(AccessTestAction action) throws Exception {
1741 verifyAllowed(action, SUPERUSER);
1742
1743 verifyDenied(action, USER_CREATE, USER_RW, USER_NONE, USER_RO);
1744 }
1745
1746 @Test (timeout=180000)
1747 public void testCheckPermissions() throws Exception {
1748
1749
1750 AccessTestAction globalAdmin = new AccessTestAction() {
1751 @Override
1752 public Void run() throws Exception {
1753 checkGlobalPerms(TEST_UTIL, Permission.Action.ADMIN);
1754 return null;
1755 }
1756 };
1757
1758 verifyGlobal(globalAdmin);
1759
1760
1761
1762 AccessTestAction globalReadWrite = new AccessTestAction() {
1763 @Override
1764 public Void run() throws Exception {
1765 checkGlobalPerms(TEST_UTIL, Permission.Action.READ, Permission.Action.WRITE);
1766 return null;
1767 }
1768 };
1769
1770 verifyGlobal(globalReadWrite);
1771
1772
1773
1774 final byte[] TEST_Q1 = Bytes.toBytes("q1");
1775 final byte[] TEST_Q2 = Bytes.toBytes("q2");
1776
1777 User userTable = User.createUserForTesting(conf, "user_check_perms_table", new String[0]);
1778 User userColumn = User.createUserForTesting(conf, "user_check_perms_family", new String[0]);
1779 User userQualifier = User.createUserForTesting(conf, "user_check_perms_q", new String[0]);
1780
1781 grantOnTable(TEST_UTIL, userTable.getShortName(),
1782 TEST_TABLE, null, null,
1783 Permission.Action.READ);
1784 grantOnTable(TEST_UTIL, userColumn.getShortName(),
1785 TEST_TABLE, TEST_FAMILY, null,
1786 Permission.Action.READ);
1787 grantOnTable(TEST_UTIL, userQualifier.getShortName(),
1788 TEST_TABLE, TEST_FAMILY, TEST_Q1,
1789 Permission.Action.READ);
1790
1791 try {
1792 AccessTestAction tableRead = new AccessTestAction() {
1793 @Override
1794 public Void run() throws Exception {
1795 checkTablePerms(TEST_UTIL, TEST_TABLE, null, null, Permission.Action.READ);
1796 return null;
1797 }
1798 };
1799
1800 AccessTestAction columnRead = new AccessTestAction() {
1801 @Override
1802 public Void run() throws Exception {
1803 checkTablePerms(TEST_UTIL, TEST_TABLE, TEST_FAMILY, null, Permission.Action.READ);
1804 return null;
1805 }
1806 };
1807
1808 AccessTestAction qualifierRead = new AccessTestAction() {
1809 @Override
1810 public Void run() throws Exception {
1811 checkTablePerms(TEST_UTIL, TEST_TABLE, TEST_FAMILY, TEST_Q1, Permission.Action.READ);
1812 return null;
1813 }
1814 };
1815
1816 AccessTestAction multiQualifierRead = new AccessTestAction() {
1817 @Override
1818 public Void run() throws Exception {
1819 checkTablePerms(TEST_UTIL, TEST_TABLE, new Permission[] {
1820 new TablePermission(TEST_TABLE, TEST_FAMILY, TEST_Q1, Permission.Action.READ),
1821 new TablePermission(TEST_TABLE, TEST_FAMILY, TEST_Q2, Permission.Action.READ), });
1822 return null;
1823 }
1824 };
1825
1826 AccessTestAction globalAndTableRead = new AccessTestAction() {
1827 @Override
1828 public Void run() throws Exception {
1829 checkTablePerms(TEST_UTIL, TEST_TABLE, new Permission[] {
1830 new Permission(Permission.Action.READ),
1831 new TablePermission(TEST_TABLE, null, (byte[]) null, Permission.Action.READ), });
1832 return null;
1833 }
1834 };
1835
1836 AccessTestAction noCheck = new AccessTestAction() {
1837 @Override
1838 public Void run() throws Exception {
1839 checkTablePerms(TEST_UTIL, TEST_TABLE, new Permission[0]);
1840 return null;
1841 }
1842 };
1843
1844 verifyAllowed(tableRead, SUPERUSER, userTable);
1845 verifyDenied(tableRead, userColumn, userQualifier);
1846
1847 verifyAllowed(columnRead, SUPERUSER, userTable, userColumn);
1848 verifyDenied(columnRead, userQualifier);
1849
1850 verifyAllowed(qualifierRead, SUPERUSER, userTable, userColumn, userQualifier);
1851
1852 verifyAllowed(multiQualifierRead, SUPERUSER, userTable, userColumn);
1853 verifyDenied(multiQualifierRead, userQualifier);
1854
1855 verifyAllowed(globalAndTableRead, SUPERUSER);
1856 verifyDenied(globalAndTableRead, userTable, userColumn, userQualifier);
1857
1858 verifyAllowed(noCheck, SUPERUSER, userTable, userColumn, userQualifier);
1859
1860
1861
1862 AccessTestAction familyReadWrite = new AccessTestAction() {
1863 @Override
1864 public Void run() throws Exception {
1865 checkTablePerms(TEST_UTIL, TEST_TABLE, TEST_FAMILY, null, Permission.Action.READ,
1866 Permission.Action.WRITE);
1867 return null;
1868 }
1869 };
1870
1871 verifyAllowed(familyReadWrite, SUPERUSER, USER_OWNER, USER_CREATE, USER_RW);
1872 verifyDenied(familyReadWrite, USER_NONE, USER_RO);
1873
1874
1875
1876 CheckPermissionsRequest checkRequest =
1877 CheckPermissionsRequest
1878 .newBuilder()
1879 .addPermission(
1880 AccessControlProtos.Permission
1881 .newBuilder()
1882 .setType(AccessControlProtos.Permission.Type.Table)
1883 .setTablePermission(
1884 AccessControlProtos.TablePermission.newBuilder()
1885 .setTableName(ProtobufUtil.toProtoTableName(TEST_TABLE))
1886 .addAction(AccessControlProtos.Permission.Action.CREATE))).build();
1887 Table acl = systemUserConnection.getTable(AccessControlLists.ACL_TABLE_NAME);
1888 try {
1889 BlockingRpcChannel channel = acl.coprocessorService(new byte[0]);
1890 AccessControlService.BlockingInterface protocol =
1891 AccessControlService.newBlockingStub(channel);
1892 try {
1893
1894 protocol.checkPermissions(null, checkRequest);
1895 fail("this should have thrown CoprocessorException");
1896 } catch (ServiceException ex) {
1897
1898 }
1899 } finally {
1900 acl.close();
1901 }
1902
1903 } finally {
1904 revokeFromTable(TEST_UTIL, userTable.getShortName(), TEST_TABLE, null, null,
1905 Permission.Action.READ);
1906 revokeFromTable(TEST_UTIL, userColumn.getShortName(), TEST_TABLE, TEST_FAMILY, null,
1907 Permission.Action.READ);
1908 revokeFromTable(TEST_UTIL, userQualifier.getShortName(), TEST_TABLE, TEST_FAMILY, TEST_Q1,
1909 Permission.Action.READ);
1910 }
1911 }
1912
1913 @Test (timeout=180000)
1914 public void testStopRegionServer() throws Exception {
1915 AccessTestAction action = new AccessTestAction() {
1916 @Override
1917 public Object run() throws Exception {
1918 ACCESS_CONTROLLER.preStopRegionServer(ObserverContext.createAndPrepare(RSCP_ENV, null));
1919 return null;
1920 }
1921 };
1922
1923 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
1924 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
1925 USER_GROUP_WRITE, USER_GROUP_CREATE);
1926 }
1927
1928 @Test (timeout=180000)
1929 public void testRollWALWriterRequest() throws Exception {
1930 AccessTestAction action = new AccessTestAction() {
1931 @Override
1932 public Object run() throws Exception {
1933 ACCESS_CONTROLLER.preRollWALWriterRequest(ObserverContext.createAndPrepare(RSCP_ENV, null));
1934 return null;
1935 }
1936 };
1937
1938 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
1939 verifyDenied(action, USER_CREATE, USER_OWNER, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
1940 USER_GROUP_WRITE, USER_GROUP_CREATE);
1941 }
1942
1943 @Test (timeout=180000)
1944 public void testOpenRegion() throws Exception {
1945 AccessTestAction action = new AccessTestAction() {
1946 @Override
1947 public Object run() throws Exception {
1948 ACCESS_CONTROLLER.preOpen(ObserverContext.createAndPrepare(RCP_ENV, null));
1949 return null;
1950 }
1951 };
1952
1953 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
1954 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, USER_GROUP_CREATE,
1955 USER_GROUP_READ, USER_GROUP_WRITE);
1956 }
1957
1958 @Test (timeout=180000)
1959 public void testCloseRegion() throws Exception {
1960 AccessTestAction action = new AccessTestAction() {
1961 @Override
1962 public Object run() throws Exception {
1963 ACCESS_CONTROLLER.preClose(ObserverContext.createAndPrepare(RCP_ENV, null), false);
1964 return null;
1965 }
1966 };
1967
1968 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
1969 verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER, USER_GROUP_CREATE,
1970 USER_GROUP_READ, USER_GROUP_WRITE);
1971 }
1972
1973 @Test (timeout=180000)
1974 public void testSnapshot() throws Exception {
1975 Admin admin = TEST_UTIL.getHBaseAdmin();
1976 final HTableDescriptor htd = admin.getTableDescriptor(TEST_TABLE);
1977 SnapshotDescription.Builder builder = SnapshotDescription.newBuilder();
1978 builder.setName(TEST_TABLE.getNameAsString() + "-snapshot");
1979 builder.setTable(TEST_TABLE.getNameAsString());
1980 final SnapshotDescription snapshot = builder.build();
1981 AccessTestAction snapshotAction = new AccessTestAction() {
1982 @Override
1983 public Object run() throws Exception {
1984 ACCESS_CONTROLLER.preSnapshot(ObserverContext.createAndPrepare(CP_ENV, null),
1985 snapshot, htd);
1986 return null;
1987 }
1988 };
1989
1990 AccessTestAction deleteAction = new AccessTestAction() {
1991 @Override
1992 public Object run() throws Exception {
1993 ACCESS_CONTROLLER.preDeleteSnapshot(ObserverContext.createAndPrepare(CP_ENV, null),
1994 snapshot);
1995 return null;
1996 }
1997 };
1998
1999 AccessTestAction restoreAction = new AccessTestAction() {
2000 @Override
2001 public Object run() throws Exception {
2002 ACCESS_CONTROLLER.preRestoreSnapshot(ObserverContext.createAndPrepare(CP_ENV, null),
2003 snapshot, htd);
2004 return null;
2005 }
2006 };
2007
2008 AccessTestAction cloneAction = new AccessTestAction() {
2009 @Override
2010 public Object run() throws Exception {
2011 ACCESS_CONTROLLER.preCloneSnapshot(ObserverContext.createAndPrepare(CP_ENV, null),
2012 null, null);
2013 return null;
2014 }
2015 };
2016
2017 verifyAllowed(snapshotAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
2018 verifyDenied(snapshotAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
2019 USER_GROUP_WRITE, USER_GROUP_CREATE);
2020
2021 verifyAllowed(cloneAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
2022 verifyDenied(deleteAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER,
2023 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
2024
2025 verifyAllowed(restoreAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
2026 verifyDenied(restoreAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER,
2027 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
2028
2029 verifyAllowed(deleteAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
2030 verifyDenied(cloneAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER,
2031 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
2032 }
2033
2034 @Test (timeout=180000)
2035 public void testSnapshotWithOwner() throws Exception {
2036 Admin admin = TEST_UTIL.getHBaseAdmin();
2037 final HTableDescriptor htd = admin.getTableDescriptor(TEST_TABLE);
2038 SnapshotDescription.Builder builder = SnapshotDescription.newBuilder();
2039 builder.setName(TEST_TABLE.getNameAsString() + "-snapshot");
2040 builder.setTable(TEST_TABLE.getNameAsString());
2041 builder.setOwner(USER_OWNER.getName());
2042 final SnapshotDescription snapshot = builder.build();
2043 AccessTestAction snapshotAction = new AccessTestAction() {
2044 @Override
2045 public Object run() throws Exception {
2046 ACCESS_CONTROLLER.preSnapshot(ObserverContext.createAndPrepare(CP_ENV, null),
2047 snapshot, htd);
2048 return null;
2049 }
2050 };
2051 verifyAllowed(snapshotAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
2052 verifyDenied(snapshotAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
2053 USER_GROUP_WRITE, USER_GROUP_CREATE);
2054
2055 AccessTestAction deleteAction = new AccessTestAction() {
2056 @Override
2057 public Object run() throws Exception {
2058 ACCESS_CONTROLLER.preDeleteSnapshot(ObserverContext.createAndPrepare(CP_ENV, null),
2059 snapshot);
2060 return null;
2061 }
2062 };
2063 verifyAllowed(deleteAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
2064 verifyDenied(deleteAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
2065 USER_GROUP_WRITE, USER_GROUP_CREATE);
2066
2067 AccessTestAction restoreAction = new AccessTestAction() {
2068 @Override
2069 public Object run() throws Exception {
2070 ACCESS_CONTROLLER.preRestoreSnapshot(ObserverContext.createAndPrepare(CP_ENV, null),
2071 snapshot, htd);
2072 return null;
2073 }
2074 };
2075 verifyAllowed(restoreAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
2076 verifyDenied(restoreAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
2077 USER_GROUP_WRITE, USER_GROUP_CREATE);
2078
2079 AccessTestAction cloneAction = new AccessTestAction() {
2080 @Override
2081 public Object run() throws Exception {
2082 ACCESS_CONTROLLER.preCloneSnapshot(ObserverContext.createAndPrepare(CP_ENV, null),
2083 null, null);
2084 return null;
2085 }
2086 };
2087
2088
2089 verifyAllowed(cloneAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
2090 verifyDenied(cloneAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER,
2091 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
2092 }
2093
2094 @Test (timeout=180000)
2095 public void testGlobalAuthorizationForNewRegisteredRS() throws Exception {
2096 LOG.debug("Test for global authorization for a new registered RegionServer.");
2097 MiniHBaseCluster hbaseCluster = TEST_UTIL.getHBaseCluster();
2098
2099 final Admin admin = TEST_UTIL.getHBaseAdmin();
2100 HTableDescriptor htd = new HTableDescriptor(TEST_TABLE2);
2101 htd.addFamily(new HColumnDescriptor(TEST_FAMILY));
2102 createTable(TEST_UTIL, htd);
2103
2104
2105 JVMClusterUtil.RegionServerThread newRsThread = hbaseCluster
2106 .startRegionServer();
2107 final HRegionServer newRs = newRsThread.getRegionServer();
2108
2109
2110 List<HRegionLocation> regions;
2111 try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE2)) {
2112 regions = locator.getAllRegionLocations();
2113 }
2114 HRegionLocation location = regions.get(0);
2115 final HRegionInfo hri = location.getRegionInfo();
2116 final ServerName server = location.getServerName();
2117 try (Table table = systemUserConnection.getTable(TEST_TABLE2)) {
2118 AccessTestAction moveAction = new AccessTestAction() {
2119 @Override
2120 public Object run() throws Exception {
2121 admin.move(hri.getEncodedNameAsBytes(),
2122 Bytes.toBytes(newRs.getServerName().getServerName()));
2123 return null;
2124 }
2125 };
2126 SUPERUSER.runAs(moveAction);
2127
2128 final int RETRIES_LIMIT = 10;
2129 int retries = 0;
2130 while (newRs.getOnlineRegions(TEST_TABLE2).size() < 1 && retries < RETRIES_LIMIT) {
2131 LOG.debug("Waiting for region to be opened. Already retried " + retries
2132 + " times.");
2133 try {
2134 Thread.sleep(1000);
2135 } catch (InterruptedException e) {
2136 }
2137 retries++;
2138 if (retries == RETRIES_LIMIT - 1) {
2139 fail("Retry exhaust for waiting region to be opened.");
2140 }
2141 }
2142
2143
2144 AccessTestAction putAction = new AccessTestAction() {
2145 @Override
2146 public Object run() throws Exception {
2147 Put put = new Put(Bytes.toBytes("test"));
2148 put.addColumn(TEST_FAMILY, Bytes.toBytes("qual"), Bytes.toBytes("value"));
2149 table.put(put);
2150 return null;
2151 }
2152 };
2153 USER_ADMIN.runAs(putAction);
2154 }
2155 }
2156
2157 @Test (timeout=180000)
2158 public void testTableDescriptorsEnumeration() throws Exception {
2159 User TABLE_ADMIN = User.createUserForTesting(conf, "UserA", new String[0]);
2160
2161
2162 grantOnTable(TEST_UTIL, TABLE_ADMIN.getShortName(), TEST_TABLE, null, null,
2163 Permission.Action.ADMIN);
2164 try {
2165 AccessTestAction listTablesAction = new AccessTestAction() {
2166 @Override
2167 public Object run() throws Exception {
2168 try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
2169 Admin admin = conn.getAdmin()) {
2170 return Arrays.asList(admin.listTables());
2171 }
2172 }
2173 };
2174
2175 AccessTestAction getTableDescAction = new AccessTestAction() {
2176 @Override
2177 public Object run() throws Exception {
2178 try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
2179 Admin admin = conn.getAdmin();) {
2180 return admin.getTableDescriptor(TEST_TABLE);
2181 }
2182 }
2183 };
2184
2185 verifyAllowed(listTablesAction, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, TABLE_ADMIN,
2186 USER_GROUP_CREATE, USER_GROUP_ADMIN);
2187 verifyIfEmptyList(listTablesAction, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
2188 USER_GROUP_WRITE);
2189
2190 verifyAllowed(getTableDescAction, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER,
2191 TABLE_ADMIN, USER_GROUP_CREATE, USER_GROUP_ADMIN);
2192 verifyDenied(getTableDescAction, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
2193 USER_GROUP_WRITE);
2194 } finally {
2195
2196 revokeFromTable(TEST_UTIL, TABLE_ADMIN.getShortName(), TEST_TABLE, null, null,
2197 Permission.Action.ADMIN);
2198 }
2199 }
2200
2201 @Test (timeout=180000)
2202 public void testTableNameEnumeration() throws Exception {
2203 AccessTestAction listTablesAction = new AccessTestAction() {
2204 @Override
2205 public Object run() throws Exception {
2206 Connection unmanagedConnection =
2207 ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
2208 Admin admin = unmanagedConnection.getAdmin();
2209 try {
2210 return Arrays.asList(admin.listTableNames());
2211 } finally {
2212 admin.close();
2213 unmanagedConnection.close();
2214 }
2215 }
2216 };
2217
2218 verifyAllowed(listTablesAction, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_RW,
2219 USER_RO, USER_GROUP_CREATE, USER_GROUP_ADMIN, USER_GROUP_READ, USER_GROUP_WRITE);
2220 verifyIfEmptyList(listTablesAction, USER_NONE);
2221 }
2222
2223 @Test (timeout=180000)
2224 public void testTableDeletion() throws Exception {
2225 User TABLE_ADMIN = User.createUserForTesting(conf, "TestUser", new String[0]);
2226 final TableName tname = TableName.valueOf("testTableDeletion");
2227 createTestTable(tname);
2228
2229
2230 grantOnTable(TEST_UTIL, TABLE_ADMIN.getShortName(), tname, null, null, Permission.Action.ADMIN);
2231
2232 AccessTestAction deleteTableAction = new AccessTestAction() {
2233 @Override
2234 public Object run() throws Exception {
2235 Connection unmanagedConnection =
2236 ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
2237 Admin admin = unmanagedConnection.getAdmin();
2238 try {
2239 deleteTable(TEST_UTIL, admin, tname);
2240 } finally {
2241 admin.close();
2242 unmanagedConnection.close();
2243 }
2244 return null;
2245 }
2246 };
2247
2248 verifyDenied(deleteTableAction, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
2249 USER_GROUP_WRITE);
2250 verifyAllowed(deleteTableAction, TABLE_ADMIN);
2251 }
2252
2253 private void createTestTable(TableName tname) throws Exception {
2254 HTableDescriptor htd = new HTableDescriptor(tname);
2255 HColumnDescriptor hcd = new HColumnDescriptor(TEST_FAMILY);
2256 hcd.setMaxVersions(100);
2257 htd.addFamily(hcd);
2258 htd.setOwner(USER_OWNER);
2259 createTable(TEST_UTIL, htd, new byte[][] { Bytes.toBytes("s") });
2260 }
2261
2262 @Test (timeout=180000)
2263 public void testNamespaceUserGrant() throws Exception {
2264 AccessTestAction getAction = new AccessTestAction() {
2265 @Override
2266 public Object run() throws Exception {
2267 try(Connection conn = ConnectionFactory.createConnection(conf);
2268 Table t = conn.getTable(TEST_TABLE);) {
2269 return t.get(new Get(TEST_ROW));
2270 }
2271 }
2272 };
2273
2274 String namespace = TEST_TABLE.getNamespaceAsString();
2275
2276
2277 grantOnNamespace(TEST_UTIL, USER_NONE.getShortName(), namespace, Permission.Action.READ);
2278
2279 verifyAllowed(getAction, USER_NONE);
2280
2281
2282 revokeFromNamespace(TEST_UTIL, USER_NONE.getShortName(), namespace, Permission.Action.READ);
2283 verifyDenied(getAction, USER_NONE);
2284 }
2285
2286 @Test (timeout=180000)
2287 public void testAccessControlClientGrantRevoke() throws Exception {
2288
2289 User testGrantRevoke = User.createUserForTesting(conf, "testGrantRevoke", new String[0]);
2290 AccessTestAction getAction = new AccessTestAction() {
2291 @Override
2292 public Object run() throws Exception {
2293 try(Connection conn = ConnectionFactory.createConnection(conf);
2294 Table t = conn.getTable(TEST_TABLE);) {
2295 return t.get(new Get(TEST_ROW));
2296 }
2297 }
2298 };
2299
2300 verifyDenied(getAction, testGrantRevoke);
2301
2302
2303 try {
2304 grantOnTableUsingAccessControlClient(TEST_UTIL, systemUserConnection,
2305 testGrantRevoke.getShortName(), TEST_TABLE, null, null, Permission.Action.READ);
2306 } catch (Throwable e) {
2307 LOG.error("error during call of AccessControlClient.grant. ", e);
2308 }
2309
2310
2311 verifyAllowed(getAction, testGrantRevoke);
2312
2313
2314 try {
2315 revokeFromTableUsingAccessControlClient(TEST_UTIL, systemUserConnection,
2316 testGrantRevoke.getShortName(), TEST_TABLE, null, null, Permission.Action.READ);
2317 } catch (Throwable e) {
2318 LOG.error("error during call of AccessControlClient.revoke ", e);
2319 }
2320
2321
2322 verifyDenied(getAction, testGrantRevoke);
2323 }
2324
2325 @Test (timeout=180000)
2326 public void testAccessControlClientGlobalGrantRevoke() throws Exception {
2327
2328 User testGlobalGrantRevoke = User.createUserForTesting(conf,
2329 "testGlobalGrantRevoke", new String[0]);
2330 AccessTestAction getAction = new AccessTestAction() {
2331 @Override
2332 public Object run() throws Exception {
2333 try(Connection conn = ConnectionFactory.createConnection(conf);
2334 Table t = conn.getTable(TEST_TABLE)) {
2335 return t.get(new Get(TEST_ROW));
2336 }
2337 }
2338 };
2339
2340 verifyDenied(getAction, testGlobalGrantRevoke);
2341
2342
2343 String userName = testGlobalGrantRevoke.getShortName();
2344 try {
2345 grantGlobalUsingAccessControlClient(TEST_UTIL, systemUserConnection,
2346 userName, Permission.Action.READ);
2347 } catch (Throwable e) {
2348 LOG.error("error during call of AccessControlClient.grant. ", e);
2349 }
2350 try {
2351
2352 verifyAllowed(getAction, testGlobalGrantRevoke);
2353
2354
2355 try {
2356 revokeGlobalUsingAccessControlClient(TEST_UTIL, systemUserConnection,
2357 userName, Permission.Action.READ);
2358 } catch (Throwable e) {
2359 LOG.error("error during call of AccessControlClient.revoke ", e);
2360 }
2361
2362
2363 verifyDenied(getAction, testGlobalGrantRevoke);
2364 } finally {
2365 revokeGlobal(TEST_UTIL, userName, Permission.Action.READ);
2366 }
2367 }
2368
2369 @Test (timeout=180000)
2370 public void testAccessControlClientGrantRevokeOnNamespace() throws Exception {
2371
2372 User testNS = User.createUserForTesting(conf, "testNS", new String[0]);
2373 AccessTestAction getAction = new AccessTestAction() {
2374 @Override
2375 public Object run() throws Exception {
2376 try(Connection conn = ConnectionFactory.createConnection(conf);
2377 Table t = conn.getTable(TEST_TABLE);) {
2378 return t.get(new Get(TEST_ROW));
2379 }
2380 }
2381 };
2382
2383 verifyDenied(getAction, testNS);
2384
2385 String userName = testNS.getShortName();
2386 String namespace = TEST_TABLE.getNamespaceAsString();
2387
2388 try {
2389 grantOnNamespaceUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName,
2390 namespace, Permission.Action.READ);
2391 } catch (Throwable e) {
2392 LOG.error("error during call of AccessControlClient.grant. ", e);
2393 }
2394 try {
2395
2396 verifyAllowed(getAction, testNS);
2397
2398
2399 try {
2400 revokeFromNamespaceUsingAccessControlClient(TEST_UTIL, systemUserConnection, userName,
2401 namespace, Permission.Action.READ);
2402 } catch (Throwable e) {
2403 LOG.error("error during call of AccessControlClient.revoke ", e);
2404 }
2405
2406
2407 verifyDenied(getAction, testNS);
2408 } finally {
2409 revokeFromNamespace(TEST_UTIL, userName, namespace, Permission.Action.READ);
2410 }
2411 }
2412
2413
2414 public static class PingCoprocessor extends PingService implements Coprocessor,
2415 CoprocessorService {
2416
2417 @Override
2418 public void start(CoprocessorEnvironment env) throws IOException { }
2419
2420 @Override
2421 public void stop(CoprocessorEnvironment env) throws IOException { }
2422
2423 @Override
2424 public Service getService() {
2425 return this;
2426 }
2427
2428 @Override
2429 public void ping(RpcController controller, PingRequest request,
2430 RpcCallback<PingResponse> callback) {
2431 callback.run(PingResponse.newBuilder().setPong("Pong!").build());
2432 }
2433
2434 @Override
2435 public void count(RpcController controller, CountRequest request,
2436 RpcCallback<CountResponse> callback) {
2437 callback.run(CountResponse.newBuilder().build());
2438 }
2439
2440 @Override
2441 public void increment(RpcController controller, IncrementCountRequest requet,
2442 RpcCallback<IncrementCountResponse> callback) {
2443 callback.run(IncrementCountResponse.newBuilder().build());
2444 }
2445
2446 @Override
2447 public void hello(RpcController controller, HelloRequest request,
2448 RpcCallback<HelloResponse> callback) {
2449 callback.run(HelloResponse.newBuilder().setResponse("Hello!").build());
2450 }
2451
2452 @Override
2453 public void noop(RpcController controller, NoopRequest request,
2454 RpcCallback<NoopResponse> callback) {
2455 callback.run(NoopResponse.newBuilder().build());
2456 }
2457 }
2458
2459 @Test (timeout=180000)
2460 public void testCoprocessorExec() throws Exception {
2461
2462 for (JVMClusterUtil.RegionServerThread thread:
2463 TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads()) {
2464 HRegionServer rs = thread.getRegionServer();
2465 for (Region region: rs.getOnlineRegions(TEST_TABLE)) {
2466 region.getCoprocessorHost().load(PingCoprocessor.class,
2467 Coprocessor.PRIORITY_USER, conf);
2468 }
2469 }
2470
2471
2472
2473 User userA = User.createUserForTesting(conf, "UserA", new String[0]);
2474 User userB = User.createUserForTesting(conf, "UserB", new String[0]);
2475
2476 grantOnTable(TEST_UTIL, userA.getShortName(),
2477 TEST_TABLE, null, null,
2478 Permission.Action.EXEC);
2479 try {
2480
2481 AccessTestAction execEndpointAction = new AccessTestAction() {
2482 @Override
2483 public Object run() throws Exception {
2484 try (Connection conn = ConnectionFactory.createConnection(conf);
2485 Table t = conn.getTable(TEST_TABLE);) {
2486 BlockingRpcChannel service = t.coprocessorService(HConstants.EMPTY_BYTE_ARRAY);
2487 PingCoprocessor.newBlockingStub(service).noop(null, NoopRequest.newBuilder().build());
2488 }
2489 return null;
2490 }
2491 };
2492
2493 String namespace = TEST_TABLE.getNamespaceAsString();
2494
2495 grantOnNamespace(TEST_UTIL, userB.getShortName(), namespace, Permission.Action.EXEC);
2496
2497 verifyAllowed(execEndpointAction, userA, userB);
2498
2499 revokeFromNamespace(TEST_UTIL, userB.getShortName(), namespace, Permission.Action.EXEC);
2500
2501 verifyDenied(execEndpointAction, userB);
2502 verifyAllowed(execEndpointAction, userA);
2503 } finally {
2504
2505 revokeFromTable(TEST_UTIL, userA.getShortName(), TEST_TABLE, null, null,
2506 Permission.Action.EXEC);
2507 }
2508 }
2509
2510 @Test (timeout=180000)
2511 public void testReservedCellTags() throws Exception {
2512 AccessTestAction putWithReservedTag = new AccessTestAction() {
2513 @Override
2514 public Object run() throws Exception {
2515 try(Connection conn = ConnectionFactory.createConnection(conf);
2516 Table t = conn.getTable(TEST_TABLE);) {
2517 KeyValue kv = new KeyValue(TEST_ROW, TEST_FAMILY, TEST_QUALIFIER,
2518 HConstants.LATEST_TIMESTAMP, HConstants.EMPTY_BYTE_ARRAY,
2519 new Tag[] { new Tag(AccessControlLists.ACL_TAG_TYPE,
2520 ProtobufUtil.toUsersAndPermissions(USER_OWNER.getShortName(),
2521 new Permission(Permission.Action.READ)).toByteArray()) });
2522 t.put(new Put(TEST_ROW).add(kv));
2523 }
2524 return null;
2525 }
2526 };
2527
2528
2529 verifyAllowed(putWithReservedTag, User.getCurrent());
2530
2531 verifyDenied(putWithReservedTag, USER_OWNER, USER_ADMIN, USER_CREATE, USER_RW, USER_RO);
2532 }
2533
2534 @Test (timeout=180000)
2535 public void testSetQuota() throws Exception {
2536 AccessTestAction setUserQuotaAction = new AccessTestAction() {
2537 @Override
2538 public Object run() throws Exception {
2539 ACCESS_CONTROLLER.preSetUserQuota(ObserverContext.createAndPrepare(CP_ENV, null),
2540 null, null);
2541 return null;
2542 }
2543 };
2544
2545 AccessTestAction setUserTableQuotaAction = new AccessTestAction() {
2546 @Override
2547 public Object run() throws Exception {
2548 ACCESS_CONTROLLER.preSetUserQuota(ObserverContext.createAndPrepare(CP_ENV, null), null,
2549 TEST_TABLE, null);
2550 return null;
2551 }
2552 };
2553
2554 AccessTestAction setUserNamespaceQuotaAction = new AccessTestAction() {
2555 @Override
2556 public Object run() throws Exception {
2557 ACCESS_CONTROLLER.preSetUserQuota(ObserverContext.createAndPrepare(CP_ENV, null),
2558 null, (String)null, null);
2559 return null;
2560 }
2561 };
2562
2563 AccessTestAction setTableQuotaAction = new AccessTestAction() {
2564 @Override
2565 public Object run() throws Exception {
2566 ACCESS_CONTROLLER.preSetTableQuota(ObserverContext.createAndPrepare(CP_ENV, null),
2567 TEST_TABLE, null);
2568 return null;
2569 }
2570 };
2571
2572 AccessTestAction setNamespaceQuotaAction = new AccessTestAction() {
2573 @Override
2574 public Object run() throws Exception {
2575 ACCESS_CONTROLLER.preSetNamespaceQuota(ObserverContext.createAndPrepare(CP_ENV, null),
2576 null, null);
2577 return null;
2578 }
2579 };
2580
2581 verifyAllowed(setUserQuotaAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
2582 verifyDenied(setUserQuotaAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER,
2583 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
2584
2585 verifyAllowed(setUserTableQuotaAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
2586 verifyDenied(setUserTableQuotaAction, USER_CREATE, USER_RW, USER_RO, USER_NONE,
2587 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
2588
2589 verifyAllowed(setUserNamespaceQuotaAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
2590 verifyDenied(setUserNamespaceQuotaAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER,
2591 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
2592
2593 verifyAllowed(setTableQuotaAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
2594 verifyDenied(setTableQuotaAction, USER_CREATE, USER_RW, USER_RO, USER_NONE);
2595
2596 verifyAllowed(setNamespaceQuotaAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
2597 verifyDenied(setNamespaceQuotaAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER,
2598 USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
2599 }
2600
2601 @Test (timeout=180000)
2602 public void testGetNamespacePermission() throws Exception {
2603 String namespace = "testGetNamespacePermission";
2604 NamespaceDescriptor desc = NamespaceDescriptor.create(namespace).build();
2605 createNamespace(TEST_UTIL, desc);
2606 grantOnNamespace(TEST_UTIL, USER_NONE.getShortName(), namespace, Permission.Action.READ);
2607 try {
2608 List<UserPermission> namespacePermissions = AccessControlClient.getUserPermissions(
2609 systemUserConnection, AccessControlLists.toNamespaceEntry(namespace));
2610 assertTrue(namespacePermissions != null);
2611 assertTrue(namespacePermissions.size() == 1);
2612 } catch (Throwable thw) {
2613 throw new HBaseException(thw);
2614 }
2615 deleteNamespace(TEST_UTIL, namespace);
2616 }
2617
2618 @Test (timeout=180000)
2619 public void testTruncatePerms() throws Exception {
2620 try {
2621 List<UserPermission> existingPerms = AccessControlClient.getUserPermissions(
2622 systemUserConnection, TEST_TABLE.getNameAsString());
2623 assertTrue(existingPerms != null);
2624 assertTrue(existingPerms.size() > 1);
2625 TEST_UTIL.getHBaseAdmin().disableTable(TEST_TABLE);
2626 TEST_UTIL.truncateTable(TEST_TABLE);
2627 TEST_UTIL.waitTableAvailable(TEST_TABLE);
2628 List<UserPermission> perms = AccessControlClient.getUserPermissions(
2629 systemUserConnection, TEST_TABLE.getNameAsString());
2630 assertTrue(perms != null);
2631 assertEquals(existingPerms.size(), perms.size());
2632 } catch (Throwable e) {
2633 throw new HBaseIOException(e);
2634 }
2635 }
2636
2637 private PrivilegedAction<List<UserPermission>> getPrivilegedAction(final String regex) {
2638 return new PrivilegedAction<List<UserPermission>>() {
2639 @Override
2640 public List<UserPermission> run() {
2641 try(Connection conn = ConnectionFactory.createConnection(conf);) {
2642 return AccessControlClient.getUserPermissions(conn, regex);
2643 } catch (Throwable e) {
2644 LOG.error("error during call of AccessControlClient.getUserPermissions.", e);
2645 return null;
2646 }
2647 }
2648 };
2649 }
2650
2651 @Test (timeout=180000)
2652 public void testAccessControlClientUserPerms() throws Exception {
2653 TableName tname = TableName.valueOf("testAccessControlClientUserPerms");
2654 createTestTable(tname);
2655 try {
2656 final String regex = tname.getNameWithNamespaceInclAsString();
2657 User testUserPerms = User.createUserForTesting(conf, "testUserPerms", new String[0]);
2658 assertEquals(0, testUserPerms.runAs(getPrivilegedAction(regex)).size());
2659
2660 grantOnTable(TEST_UTIL, testUserPerms.getShortName(), tname, null, null, Action.ADMIN);
2661 List<UserPermission> perms = testUserPerms.runAs(getPrivilegedAction(regex));
2662 assertNotNull(perms);
2663
2664 assertEquals(2, perms.size());
2665 } finally {
2666 deleteTable(TEST_UTIL, tname);
2667 }
2668 }
2669
2670 @Test (timeout=180000)
2671 public void testAccessControllerUserPermsRegexHandling() throws Exception {
2672 User testRegexHandler = User.createUserForTesting(conf, "testRegexHandling", new String[0]);
2673
2674 final String REGEX_ALL_TABLES = ".*";
2675 final String tableName = "testRegex";
2676 final TableName table1 = TableName.valueOf(tableName);
2677 final byte[] family = Bytes.toBytes("f1");
2678
2679
2680 Admin admin = TEST_UTIL.getHBaseAdmin();
2681 HTableDescriptor htd = new HTableDescriptor(table1);
2682 htd.addFamily(new HColumnDescriptor(family));
2683 createTable(TEST_UTIL, htd);
2684
2685
2686 String ns = "testNamespace";
2687 NamespaceDescriptor desc = NamespaceDescriptor.create(ns).build();
2688 final TableName table2 = TableName.valueOf(ns, tableName);
2689 createNamespace(TEST_UTIL, desc);
2690 htd = new HTableDescriptor(table2);
2691 htd.addFamily(new HColumnDescriptor(family));
2692 createTable(TEST_UTIL, htd);
2693
2694
2695 String aclTableName = AccessControlLists.ACL_TABLE_NAME.getNameAsString();
2696 assertEquals(5, SUPERUSER.runAs(getPrivilegedAction(aclTableName)).size());
2697 assertEquals(0, testRegexHandler.runAs(getPrivilegedAction(aclTableName)).size());
2698
2699
2700 assertEquals(0, testRegexHandler.runAs(getPrivilegedAction(REGEX_ALL_TABLES)).size());
2701 grantOnTable(TEST_UTIL, testRegexHandler.getShortName(), table1, null, null, Action.ADMIN);
2702 assertEquals(2, testRegexHandler.runAs(getPrivilegedAction(REGEX_ALL_TABLES)).size());
2703 grantOnTable(TEST_UTIL, testRegexHandler.getShortName(), table2, null, null, Action.ADMIN);
2704 assertEquals(4, testRegexHandler.runAs(getPrivilegedAction(REGEX_ALL_TABLES)).size());
2705
2706
2707 assertEquals(2, testRegexHandler.runAs(getPrivilegedAction(tableName)).size());
2708 assertEquals(2, testRegexHandler.runAs(getPrivilegedAction(
2709 NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR + TableName.NAMESPACE_DELIM + tableName)
2710 ).size());
2711 assertEquals(2, testRegexHandler.runAs(getPrivilegedAction(
2712 ns + TableName.NAMESPACE_DELIM + tableName)).size());
2713 assertEquals(0, testRegexHandler.runAs(getPrivilegedAction("notMatchingAny")).size());
2714
2715 deleteTable(TEST_UTIL, table1);
2716 deleteTable(TEST_UTIL, table2);
2717 deleteNamespace(TEST_UTIL, ns);
2718 }
2719
2720 private void verifyAnyCreate(AccessTestAction action) throws Exception {
2721 verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_ADMIN_CF,
2722 USER_GROUP_CREATE);
2723 verifyDenied(action, USER_NONE, USER_RO, USER_RW, USER_GROUP_READ, USER_GROUP_WRITE,
2724 USER_GROUP_ADMIN);
2725 }
2726
2727 @Test (timeout=180000)
2728 public void testPrepareAndCleanBulkLoad() throws Exception {
2729 AccessTestAction prepareBulkLoadAction = new AccessTestAction() {
2730 @Override
2731 public Object run() throws Exception {
2732 ACCESS_CONTROLLER.prePrepareBulkLoad(ObserverContext.createAndPrepare(RCP_ENV, null),
2733 null);
2734 return null;
2735 }
2736 };
2737 AccessTestAction cleanupBulkLoadAction = new AccessTestAction() {
2738 @Override
2739 public Object run() throws Exception {
2740 ACCESS_CONTROLLER.preCleanupBulkLoad(ObserverContext.createAndPrepare(RCP_ENV, null),
2741 null);
2742 return null;
2743 }
2744 };
2745 verifyAnyCreate(prepareBulkLoadAction);
2746 verifyAnyCreate(cleanupBulkLoadAction);
2747 }
2748
2749 @Test (timeout=180000)
2750 public void testReplicateLogEntries() throws Exception {
2751 AccessTestAction replicateLogEntriesAction = new AccessTestAction() {
2752 @Override
2753 public Object run() throws Exception {
2754 ACCESS_CONTROLLER.preReplicateLogEntries(ObserverContext.createAndPrepare(RSCP_ENV, null),
2755 null, null);
2756 ACCESS_CONTROLLER.postReplicateLogEntries(ObserverContext.createAndPrepare(RSCP_ENV, null),
2757 null, null);
2758 return null;
2759 }
2760 };
2761
2762 verifyAllowed(replicateLogEntriesAction, SUPERUSER, USER_ADMIN, USER_GROUP_WRITE);
2763 verifyDenied(replicateLogEntriesAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER,
2764 USER_GROUP_READ, USER_GROUP_ADMIN, USER_GROUP_CREATE);
2765 }
2766 }