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