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