View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.ZooDefs.Ids;
31  import org.apache.zookeeper.ZooDefs.Perms;
32  import org.apache.zookeeper.data.ACL;
33  import org.apache.zookeeper.data.Id;
34  import org.junit.Assert;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  /**
39   *
40   */
41  @Category({SmallTests.class})
42  public class TestZKUtil {
43  
44    @Test
45    public void testUnsecure() throws ZooKeeperConnectionException, IOException {
46      Configuration conf = HBaseConfiguration.create();
47      conf.set(Superusers.SUPERUSER_CONF_KEY, "user1");
48      String node = "/hbase/testUnsecure";
49      ZooKeeperWatcher watcher = new ZooKeeperWatcher(conf, node, null, false);
50      List<ACL> aclList = ZKUtil.createACL(watcher, node, false);
51      Assert.assertEquals(aclList.size(), 1);
52      Assert.assertTrue(aclList.contains(Ids.OPEN_ACL_UNSAFE.iterator().next()));
53    }
54  
55    @Test
56    public void testSecuritySingleSuperuser() throws ZooKeeperConnectionException, IOException {
57      Configuration conf = HBaseConfiguration.create();
58      conf.set(Superusers.SUPERUSER_CONF_KEY, "user1");
59      String node = "/hbase/testSecuritySingleSuperuser";
60      ZooKeeperWatcher watcher = new ZooKeeperWatcher(conf, node, null, false);
61      List<ACL> aclList = ZKUtil.createACL(watcher, node, true);
62      Assert.assertEquals(aclList.size(), 2); // 1+1, since ACL will be set for the creator by default
63      Assert.assertTrue(aclList.contains(new ACL(Perms.ALL, new Id("sasl", "user1"))));
64      Assert.assertTrue(aclList.contains(Ids.CREATOR_ALL_ACL.iterator().next()));
65    }
66    
67    @Test
68    public void testCreateACL() throws ZooKeeperConnectionException, IOException {
69      Configuration conf = HBaseConfiguration.create();
70      conf.set(Superusers.SUPERUSER_CONF_KEY, "user1,@group1,user2,@group2,user3");
71      String node = "/hbase/testCreateACL";
72      ZooKeeperWatcher watcher = new ZooKeeperWatcher(conf, node, null, false);
73      List<ACL> aclList = ZKUtil.createACL(watcher, node, true);
74      Assert.assertEquals(aclList.size(), 4); // 3+1, since ACL will be set for the creator by default
75      Assert.assertFalse(aclList.contains(new ACL(Perms.ALL, new Id("sasl", "@group1"))));
76      Assert.assertFalse(aclList.contains(new ACL(Perms.ALL, new Id("sasl", "@group2"))));
77      Assert.assertTrue(aclList.contains(new ACL(Perms.ALL, new Id("sasl", "user1"))));
78      Assert.assertTrue(aclList.contains(new ACL(Perms.ALL, new Id("sasl", "user2"))));
79      Assert.assertTrue(aclList.contains(new ACL(Perms.ALL, new Id("sasl", "user3"))));
80    }
81  }