View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * You may add the jaas.conf option
39   *    -Djava.security.auth.login.config=/PATH/jaas.conf
40   *
41   * You may also specify -D to set options
42   *    "hbase.zookeeper.quorum"    (it should be in hbase-site.xml)
43   *    "zookeeper.znode.parent"    (it should be in hbase-site.xml)
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 }