1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.zookeeper;
20
21 import java.io.IOException;
22 import java.util.List;
23
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hbase.HBaseConfiguration;
26 import org.apache.hadoop.hbase.HConstants;
27 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
28 import org.apache.hadoop.hbase.security.Superusers;
29 import org.apache.hadoop.hbase.testclassification.SmallTests;
30 import org.apache.zookeeper.KeeperException;
31 import org.apache.zookeeper.ZooDefs.Ids;
32 import org.apache.zookeeper.ZooDefs.Perms;
33 import org.apache.zookeeper.data.ACL;
34 import org.apache.zookeeper.data.Id;
35 import org.junit.Assert;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
38 import org.mockito.Mockito;
39
40
41
42
43 @Category({SmallTests.class})
44 public class TestZKUtil {
45
46 @Test
47 public void testUnsecure() throws ZooKeeperConnectionException, IOException {
48 Configuration conf = HBaseConfiguration.create();
49 conf.set(Superusers.SUPERUSER_CONF_KEY, "user1");
50 String node = "/hbase/testUnsecure";
51 ZooKeeperWatcher watcher = new ZooKeeperWatcher(conf, node, null, false);
52 List<ACL> aclList = ZKUtil.createACL(watcher, node, false);
53 Assert.assertEquals(aclList.size(), 1);
54 Assert.assertTrue(aclList.contains(Ids.OPEN_ACL_UNSAFE.iterator().next()));
55 }
56
57 @Test
58 public void testSecuritySingleSuperuser() throws ZooKeeperConnectionException, IOException {
59 Configuration conf = HBaseConfiguration.create();
60 conf.set(Superusers.SUPERUSER_CONF_KEY, "user1");
61 String node = "/hbase/testSecuritySingleSuperuser";
62 ZooKeeperWatcher watcher = new ZooKeeperWatcher(conf, node, null, false);
63 List<ACL> aclList = ZKUtil.createACL(watcher, node, true);
64 Assert.assertEquals(aclList.size(), 2);
65 Assert.assertTrue(aclList.contains(new ACL(Perms.ALL, new Id("sasl", "user1"))));
66 Assert.assertTrue(aclList.contains(Ids.CREATOR_ALL_ACL.iterator().next()));
67 }
68
69 @Test
70 public void testCreateACL() throws ZooKeeperConnectionException, IOException {
71 Configuration conf = HBaseConfiguration.create();
72 conf.set(Superusers.SUPERUSER_CONF_KEY, "user1,@group1,user2,@group2,user3");
73 String node = "/hbase/testCreateACL";
74 ZooKeeperWatcher watcher = new ZooKeeperWatcher(conf, node, null, false);
75 List<ACL> aclList = ZKUtil.createACL(watcher, node, true);
76 Assert.assertEquals(aclList.size(), 4);
77 Assert.assertFalse(aclList.contains(new ACL(Perms.ALL, new Id("sasl", "@group1"))));
78 Assert.assertFalse(aclList.contains(new ACL(Perms.ALL, new Id("sasl", "@group2"))));
79 Assert.assertTrue(aclList.contains(new ACL(Perms.ALL, new Id("sasl", "user1"))));
80 Assert.assertTrue(aclList.contains(new ACL(Perms.ALL, new Id("sasl", "user2"))));
81 Assert.assertTrue(aclList.contains(new ACL(Perms.ALL, new Id("sasl", "user3"))));
82 }
83
84 @Test
85 public void testInterruptedDuringAction()
86 throws ZooKeeperConnectionException, IOException, KeeperException, InterruptedException {
87 final RecoverableZooKeeper recoverableZk = Mockito.mock(RecoverableZooKeeper.class);
88 ZooKeeperWatcher zkw = new ZooKeeperWatcher(HBaseConfiguration.create(), "unittest", null) {
89 @Override
90 public RecoverableZooKeeper getRecoverableZooKeeper() {
91 return recoverableZk;
92 }
93 };
94 Mockito.doThrow(new InterruptedException()).when(recoverableZk)
95 .getChildren(zkw.baseZNode, null);
96 try {
97 ZKUtil.listChildrenNoWatch(zkw, zkw.baseZNode);
98 } catch (KeeperException.SystemErrorException e) {
99
100 return;
101 }
102 Assert.fail("Should have thrown KeeperException but not");
103 }
104 }