Last active
August 29, 2015 14:21
-
-
Save matteobertozzi/cb7740e4cc152216f419 to your computer and use it in GitHub Desktop.
ZKAclReset erase/set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKUtil.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKUtil.java | |
| index 413bc98..16357c1 100644 | |
| --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKUtil.java | |
| +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKUtil.java | |
| @@ -1013,10 +1013,15 @@ public class ZKUtil { | |
| } | |
| private static ArrayList<ACL> createACL(ZooKeeperWatcher zkw, String node) { | |
| + return createACL(zkw, node, isSecureZooKeeper(zkw.getConfiguration())); | |
| + } | |
| + | |
| + protected static ArrayList<ACL> createACL(ZooKeeperWatcher zkw, String node, | |
| + boolean isSecureZooKeeper) { | |
| if (!node.startsWith(zkw.baseZNode)) { | |
| return Ids.OPEN_ACL_UNSAFE; | |
| } | |
| - if (isSecureZooKeeper(zkw.getConfiguration())) { | |
| + if (isSecureZooKeeper) { | |
| String superUser = zkw.getConfiguration().get("hbase.superuser"); | |
| ArrayList<ACL> acls = new ArrayList<ACL>(); | |
| // add permission to hbase supper user | |
| diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/zookeeper/ZkAclReset.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/zookeeper/ZkAclReset.java | |
| index f5a41df..6dc2986 100644 | |
| --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/zookeeper/ZkAclReset.java | |
| +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/zookeeper/ZkAclReset.java | |
| @@ -46,61 +46,48 @@ import org.apache.zookeeper.WatchedEvent; | |
| public class ZkAclReset extends Configured implements Tool { | |
| private static final Log LOG = LogFactory.getLog(ZkAclReset.class); | |
| - private static final int ZK_SESSION_TIMEOUT_DEFAULT = 5 * 1000; | |
| - | |
| - private static class ZkWatcher implements Watcher { | |
| - public ZkWatcher() { | |
| - } | |
| - | |
| - @Override | |
| - public void process(WatchedEvent event) { | |
| - LOG.info("Received ZooKeeper Event, " + | |
| - "type=" + event.getType() + ", " + | |
| - "state=" + event.getState() + ", " + | |
| - "path=" + event.getPath()); | |
| - } | |
| - } | |
| - | |
| - private static void resetAcls(final ZooKeeper zk, final String znode) | |
| - throws Exception { | |
| - List<String> children = zk.getChildren(znode, false); | |
| + private static void resetAcls(final ZooKeeperWatcher zkw, final String znode, | |
| + final boolean eraseAcls) throws Exception { | |
| + List<String> children = ZKUtil.listChildrenNoWatch(zkw, znode); | |
| if (children != null) { | |
| for (String child: children) { | |
| - resetAcls(zk, znode + '/' + child); | |
| + resetAcls(zkw, ZKUtil.joinZNode(znode, child), eraseAcls); | |
| } | |
| } | |
| - LOG.info(" - reset acl for " + znode); | |
| - zk.setACL(znode, ZooDefs.Ids.OPEN_ACL_UNSAFE, -1); | |
| + | |
| + ZooKeeper zk = zkw.getRecoverableZooKeeper().getZooKeeper(); | |
| + if (eraseAcls) { | |
| + LOG.info(" - erase ACLs for " + znode); | |
| + zk.setACL(znode, ZooDefs.Ids.OPEN_ACL_UNSAFE, -1); | |
| + } else { | |
| + LOG.info(" - set ACLs for " + znode); | |
| + zk.setACL(znode, ZKUtil.createACL(zkw, znode, true), -1); | |
| + } | |
| } | |
| - private static void resetAcls(final String quorumServers, final int zkTimeout, final String znode) | |
| + private static void resetAcls(final Configuration conf, boolean eraseAcls) | |
| throws Exception { | |
| - ZooKeeper zk = new ZooKeeper(quorumServers, zkTimeout, new ZkWatcher()); | |
| + ZooKeeperWatcher zkw = new ZooKeeperWatcher(conf, "ZkAclReset", null); | |
| try { | |
| - resetAcls(zk, znode); | |
| + LOG.info((eraseAcls ? "Erase" : "Set") + " HBase ACLs for " + | |
| + zkw.getQuorum() + " " + zkw.getBaseZNode()); | |
| + resetAcls(zkw, zkw.getBaseZNode(), eraseAcls); | |
| } finally { | |
| - zk.close(); | |
| + zkw.close(); | |
| } | |
| } | |
| - private void resetHBaseAcls(final Configuration conf) throws Exception { | |
| - String quorumServers = conf.get("hbase.zookeeper.quorum", HConstants.LOCALHOST); | |
| - int sessionTimeout = conf.getInt("zookeeper.session.timeout", ZK_SESSION_TIMEOUT_DEFAULT); | |
| - String znode = conf.get("zookeeper.znode.parent", HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT); | |
| - if (quorumServers == null) { | |
| - LOG.error("Unable to load hbase.zookeeper.quorum (try with: -conf hbase-site.xml)"); | |
| - return; | |
| - } | |
| - | |
| - LOG.info("Reset HBase ACLs for " + quorumServers + " " + znode); | |
| - resetAcls(quorumServers, sessionTimeout, znode); | |
| - } | |
| - | |
| - | |
| @Override | |
| public int run(String[] args) throws Exception { | |
| - Configuration conf = getConf(); | |
| - resetHBaseAcls(conf); | |
| + boolean eraseAcls = true; | |
| + | |
| + for (int i = 0; i < args.length; ++i) { | |
| + if (args[i].equals("-set-acls")) { | |
| + eraseAcls = false; | |
| + } | |
| + } | |
| + | |
| + resetAcls(getConf(), eraseAcls); | |
| return(0); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment