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
036 * #setupMiniKdc. The Kerby KDC Server came in with HBASE-5291. Its preferred. Less baggage.
037 * @see #getRunningSimpleKdcServer(File, Supplier)
038 */
039public final class SimpleKdcServerUtil {
040  protected static final Logger LOG = LoggerFactory.getLogger(SimpleKdcServerUtil.class);
041
042  private SimpleKdcServerUtil() {}
043
044  /**
045   * Returns a running kdc server. Use this method rather than start the SimpleKdcServer
046   * yourself because it takes care of BindExceptions which can happen even though port-picking
047   * is random (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        if (ke.getCause() != null && ke.getCause() instanceof BindException) {
088          LOG.info("Clashed using port {}; getting a new random port", kdcPort);
089          continue;
090        } else {
091          throw ke;
092        }
093      } finally {
094        if (bsm != null) {
095          bsm.close();
096          bsm = null;
097        }
098      }
099    }
100    // If we get here, we exhausted our retries. Fail.
101    throw new KrbException("Failed create of SimpleKdcServer after " + retries + " attempts");
102  }
103}