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