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 */
018
019package org.apache.hadoop.hbase.security.token;
020
021import java.io.File;
022import org.apache.hadoop.fs.Path;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.LocalHBaseCluster;
025import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
026import org.apache.hadoop.hbase.master.ServerManager;
027import org.apache.hadoop.hbase.security.HBaseKerberosUtils;
028import org.apache.hadoop.hbase.util.CommonFSUtils;
029import org.apache.hadoop.minikdc.MiniKdc;
030import org.apache.hadoop.security.UserGroupInformation;
031import org.junit.AfterClass;
032import org.junit.BeforeClass;
033
034/**
035 * The class for set up a security cluster with kerberos, hdfs, hbase.
036 */
037public class SecureTestCluster {
038  protected static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
039
040  protected static String USERNAME;
041
042  private static LocalHBaseCluster CLUSTER;
043
044  private static final File KEYTAB_FILE = new File(TEST_UTIL.getDataTestDir("keytab").toUri()
045      .getPath());
046  private static MiniKdc KDC;
047
048  private static String HOST = "localhost";
049
050  private static String PRINCIPAL;
051
052  private static String HTTP_PRINCIPAL;
053
054  //When extending SecureTestCluster on downstream projects that refer SecureTestCluster via
055  //hbase-server jar, we need to provide a way for the implementation to refer to its own class
056  //definition, so that KeyStoreTestUtil.getClasspathDir can resolve a valid path in the local FS
057  //to place required SSL config files.
058  private static Class testRunnerClass = SecureTestCluster.class;
059
060  /**
061   * SecureTestCluster extending classes can set their own <code>Class</code> reference type
062   * to be used as the target resource to be looked for on the class loader by
063   * <code>KeyStoreTestUtil</code>, when deciding where to place ssl related config files.
064   * @param testRunnerClass a <code>Class</code> reference from the
065   *                        <code>SecureTestCluster</code> extender.
066   */
067  protected static void setTestRunner(Class testRunnerClass){
068    SecureTestCluster.testRunnerClass = testRunnerClass;
069  }
070
071  /**
072   * Setup and start kerberos, hbase
073   */
074  @BeforeClass
075  public static void setUp() throws Exception {
076    // Can take a long time for the mini kdc to come up on loaded test cluster. Tolerate this in
077    // test by upping the skew time allowed from 30s to 90s.
078    TEST_UTIL.getConfiguration().setLong(ServerManager.MAX_CLOCK_SKEW_MS, 90000);
079    KDC = TEST_UTIL.setupMiniKdc(KEYTAB_FILE);
080    USERNAME = UserGroupInformation.getLoginUser().getShortUserName();
081    PRINCIPAL = USERNAME + "/" + HOST;
082    HTTP_PRINCIPAL = "HTTP/" + HOST;
083    KDC.createPrincipal(KEYTAB_FILE, PRINCIPAL, HTTP_PRINCIPAL);
084    TEST_UTIL.startMiniZKCluster();
085
086    HBaseKerberosUtils.setSecuredConfiguration(TEST_UTIL.getConfiguration(),
087        PRINCIPAL + "@" + KDC.getRealm(), HTTP_PRINCIPAL + "@" + KDC.getRealm());
088    HBaseKerberosUtils.setSSLConfiguration(TEST_UTIL, testRunnerClass);
089
090    TEST_UTIL.getConfiguration().setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
091        TokenProvider.class.getName());
092    TEST_UTIL.startMiniDFSCluster(1);
093    Path rootdir = TEST_UTIL.getDataTestDirOnTestFS("TestGenerateDelegationToken");
094    CommonFSUtils.setRootDir(TEST_UTIL.getConfiguration(), rootdir);
095    CLUSTER = new LocalHBaseCluster(TEST_UTIL.getConfiguration(), 1);
096    CLUSTER.startup();
097  }
098
099  @AfterClass
100  public static void tearDown() throws Exception {
101    try {
102      if (CLUSTER != null) {
103        CLUSTER.shutdown();
104      }
105      CLUSTER.join();
106      if (KDC != null) {
107        KDC.stop();
108      }
109      TEST_UTIL.shutdownMiniCluster();
110    } finally {
111      setTestRunner(SecureTestCluster.class);
112    }
113  }
114}