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;
019import java.io.File;
020import java.io.IOException;
021import java.net.BindException;
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;
029import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
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
035 * #setupMiniKdc. The Kerby KDC Server came in with HBASE-5291. Its preferred. Less baggage.
036 * @see #getRunningSimpleKdcServer(File, Supplier)
037 */
038public final class SimpleKdcServerUtil {
039  protected static final Logger LOG = LoggerFactory.getLogger(SimpleKdcServerUtil.class);
040
041  private SimpleKdcServerUtil() {}
042
043  /**
044   * Returns a running kdc server. Use this method rather than start the SimpleKdcServer
045   * yourself because it takes care of BindExceptions which can happen even though port-picking
046   * is random (between the choice of port number and bind, it could have been used elsewhere).
047   * @return A SimpleKdcServer on which 'start' has been called; be sure to call stop on this
048   *   instance when done.
049   */
050  public static SimpleKdcServer getRunningSimpleKdcServer(File testDir,
051      Supplier<Integer> randomPortGenerator) throws KrbException, IOException {
052    return getRunningSimpleKdcServer(testDir, randomPortGenerator, false);
053  }
054
055  /**
056   * Internal method for testing.
057   * @param portClash True if we want to generate BindException (for testing).
058   * @return A running SimpleKdcServer on loopback/'localhost' on a random port
059   * @see #getRunningSimpleKdcServer(File, Supplier)
060   */
061  @VisibleForTesting
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}