001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.zookeeper;
019
020import java.util.List;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.conf.Configured;
023import org.apache.hadoop.hbase.HBaseConfiguration;
024import org.apache.hadoop.util.Tool;
025import org.apache.hadoop.util.ToolRunner;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.zookeeper.ZooDefs;
028import org.apache.zookeeper.ZooKeeper;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * You may add the jaas.conf option -Djava.security.auth.login.config=/PATH/jaas.conf You may also
034 * specify -D to set options "hbase.zookeeper.quorum" (it should be in hbase-site.xml)
035 * "zookeeper.znode.parent" (it should be in hbase-site.xml) Use -set-acls to set the ACLs, no
036 * option to erase ACLs
037 */
038@InterfaceAudience.Private
039public class ZKAclReset extends Configured implements Tool {
040  private static final Logger LOG = LoggerFactory.getLogger(ZKAclReset.class);
041
042  private static void resetAcls(final ZKWatcher zkw, final String znode, final boolean eraseAcls)
043    throws Exception {
044    List<String> children = ZKUtil.listChildrenNoWatch(zkw, znode);
045    if (children != null) {
046      for (String child : children) {
047        resetAcls(zkw, ZNodePaths.joinZNode(znode, child), eraseAcls);
048      }
049    }
050
051    ZooKeeper zk = zkw.getRecoverableZooKeeper().getZooKeeper();
052    if (eraseAcls) {
053      LOG.info(" - erase ACLs for {}", znode);
054      zk.setACL(znode, ZooDefs.Ids.OPEN_ACL_UNSAFE, -1);
055    } else {
056      LOG.info(" - set ACLs for {}", znode);
057      zk.setACL(znode, zkw.createACL(znode, true), -1);
058    }
059  }
060
061  private static void resetAcls(final Configuration conf, boolean eraseAcls) throws Exception {
062    try (ZKWatcher zkw = new ZKWatcher(conf, "ZKAclReset", null)) {
063      LOG.info((eraseAcls ? "Erase" : "Set") + " HBase ACLs for {} {}", zkw.getQuorum(),
064        zkw.getZNodePaths().baseZNode);
065      resetAcls(zkw, zkw.getZNodePaths().baseZNode, eraseAcls);
066    }
067  }
068
069  private void printUsageAndExit() {
070    System.err.printf("Usage: hbase %s [options]%n", getClass().getName());
071    System.err.println(" where [options] are:");
072    System.err.println("  -h|-help                Show this help and exit.");
073    System.err.println("  -set-acls               Setup the hbase znode ACLs for a secure cluster");
074    System.err.println();
075    System.err.println("Examples:");
076    System.err.println("  To reset the ACLs to the unsecure cluster behavior:");
077    System.err.println("  hbase " + getClass().getName());
078    System.err.println();
079    System.err.println("  To reset the ACLs to the secure cluster behavior:");
080    System.err.println("  hbase " + getClass().getName() + " -set-acls");
081    System.exit(1);
082  }
083
084  @Override
085  public int run(String[] args) throws Exception {
086    boolean eraseAcls = true;
087
088    for (String arg : args) {
089      switch (arg) {
090        case "-help": {
091          printUsageAndExit();
092          break;
093        }
094        case "-set-acls": {
095          eraseAcls = false;
096          break;
097        }
098        default: {
099          printUsageAndExit();
100          break;
101        }
102      }
103    }
104
105    resetAcls(getConf(), eraseAcls);
106    return (0);
107  }
108
109  public static void main(String[] args) throws Exception {
110    System.exit(ToolRunner.run(HBaseConfiguration.create(), new ZKAclReset(), args));
111  }
112}