1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.zookeeper;
21
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.conf.Configured;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.classification.InterfaceAudience;
30 import org.apache.hadoop.util.Tool;
31 import org.apache.hadoop.util.ToolRunner;
32 import org.apache.zookeeper.ZooDefs;
33 import org.apache.zookeeper.ZooKeeper;
34 import org.apache.zookeeper.Watcher;
35 import org.apache.zookeeper.WatchedEvent;
36
37
38
39
40
41
42
43
44
45 @InterfaceAudience.Private
46 public class ZkAclReset extends Configured implements Tool {
47 private static final Log LOG = LogFactory.getLog(ZkAclReset.class);
48
49 private static final int ZK_SESSION_TIMEOUT_DEFAULT = 5 * 1000;
50
51 private static class ZkWatcher implements Watcher {
52 public ZkWatcher() {
53 }
54
55 @Override
56 public void process(WatchedEvent event) {
57 LOG.info("Received ZooKeeper Event, " +
58 "type=" + event.getType() + ", " +
59 "state=" + event.getState() + ", " +
60 "path=" + event.getPath());
61 }
62 }
63
64 private static void resetAcls(final ZooKeeper zk, final String znode)
65 throws Exception {
66 List<String> children = zk.getChildren(znode, false);
67 if (children != null) {
68 for (String child: children) {
69 resetAcls(zk, znode + '/' + child);
70 }
71 }
72 LOG.info(" - reset acl for " + znode);
73 zk.setACL(znode, ZooDefs.Ids.OPEN_ACL_UNSAFE, -1);
74 }
75
76 private static void resetAcls(final String quorumServers, final int zkTimeout, final String znode)
77 throws Exception {
78 ZooKeeper zk = new ZooKeeper(quorumServers, zkTimeout, new ZkWatcher());
79 try {
80 resetAcls(zk, znode);
81 } finally {
82 zk.close();
83 }
84 }
85
86 private void resetHBaseAcls(final Configuration conf) throws Exception {
87 String quorumServers = conf.get("hbase.zookeeper.quorum", HConstants.LOCALHOST);
88 int sessionTimeout = conf.getInt("zookeeper.session.timeout", ZK_SESSION_TIMEOUT_DEFAULT);
89 String znode = conf.get("zookeeper.znode.parent", HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
90 if (quorumServers == null) {
91 LOG.error("Unable to load hbase.zookeeper.quorum (try with: -conf hbase-site.xml)");
92 return;
93 }
94
95 LOG.info("Reset HBase ACLs for " + quorumServers + " " + znode);
96 resetAcls(quorumServers, sessionTimeout, znode);
97 }
98
99
100 @Override
101 public int run(String[] args) throws Exception {
102 Configuration conf = getConf();
103 resetHBaseAcls(conf);
104 return(0);
105 }
106
107 public static void main(String[] args) throws Exception {
108 System.exit(ToolRunner.run(new Configuration(), new ZkAclReset(), args));
109 }
110 }