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.util; 019 020import java.io.File; 021import java.io.IOException; 022import java.net.InetAddress; 023import java.util.function.Supplier; 024import org.apache.hadoop.hbase.net.BoundSocketMaker; 025import org.apache.kerby.kerberos.kerb.KrbException; 026import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; 031 032/** 033 * Utility for running {@link SimpleKdcServer}. Kerby KDC server is favored over Hadoop 034 * org.apache.hadoop.minikdc server which has support in the HBaseTestingUtility at #setupMiniKdc. 035 * The Kerby KDC Server came in with HBASE-5291. Its preferred. Less baggage. 036 * @see #getRunningSimpleKdcServer(File, Supplier) 037 */ 038public final class SimpleKdcServerUtil { 039 static final Logger LOG = LoggerFactory.getLogger(SimpleKdcServerUtil.class); 040 041 private SimpleKdcServerUtil() { 042 } 043 044 /** 045 * Returns a running kdc server. Use this method rather than start the SimpleKdcServer yourself 046 * because it takes care of BindExceptions which can happen even though port-picking is random 047 * (between the choice of port number and bind, it could have been used elsewhere). 048 * @return A SimpleKdcServer on which 'start' has been called; be sure to call stop on this 049 * instance when done. 050 */ 051 public static SimpleKdcServer getRunningSimpleKdcServer(File testDir, 052 Supplier<Integer> randomPortGenerator) throws KrbException, IOException { 053 return getRunningSimpleKdcServer(testDir, randomPortGenerator, false); 054 } 055 056 /** 057 * Internal method for testing. 058 * @param portClash True if we want to generate BindException (for testing). 059 * @return A running SimpleKdcServer on loopback/'localhost' on a random port 060 * @see #getRunningSimpleKdcServer(File, Supplier) 061 */ 062 static SimpleKdcServer getRunningSimpleKdcServer(File testDir, 063 Supplier<Integer> randomPortGenerator, final boolean portClash) 064 throws KrbException, IOException { 065 File kdcDir = new File(testDir, SimpleKdcServer.class.getSimpleName()); 066 Preconditions.checkArgument(kdcDir.mkdirs(), "Failed create of " + kdcDir); 067 String hostName = InetAddress.getLoopbackAddress().getHostName(); 068 BoundSocketMaker bsm = portClash ? new BoundSocketMaker(randomPortGenerator) : null; 069 final int retries = 10; 070 for (int i = 0; i < retries; i++) { 071 SimpleKdcServer kdc = new SimpleKdcServer(); 072 kdc.setWorkDir(kdcDir); 073 kdc.setKdcHost(hostName); 074 kdc.setAllowTcp(true); 075 kdc.setAllowUdp(false); 076 int kdcPort = bsm != null ? bsm.getPort() : randomPortGenerator.get(); 077 try { 078 kdc.setKdcTcpPort(kdcPort); 079 LOG.info("Starting KDC server at {}:{}", hostName, kdcPort); 080 kdc.init(); 081 kdc.start(); 082 return kdc; 083 } catch (KrbException ke) { 084 if (kdc != null) { 085 kdc.stop(); 086 } 087 // new kerby just eats the cause exception so we can not test for BindException any more. 088 // the only way is to always retry... 089 LOG.info("Failed to init/start kdc server, retry = {}", i, ke); 090 } finally { 091 if (bsm != null) { 092 bsm.close(); 093 bsm = null; 094 } 095 } 096 } 097 // If we get here, we exhausted our retries. Fail. 098 throw new KrbException("Failed create of SimpleKdcServer after " + retries + " attempts"); 099 } 100}