Search in sources :

Example 41 with MiniKdc

use of org.apache.hadoop.minikdc.MiniKdc in project hbase by apache.

the class HBaseTestingUtility method setupMiniKdc.

/**
 * Sets up {@link MiniKdc} for testing security.
 * Uses {@link HBaseKerberosUtils} to set the given keytab file as
 * {@link HBaseKerberosUtils#KRB_KEYTAB_FILE}.
 * FYI, there is also the easier-to-use kerby KDC server and utility for using it,
 * {@link org.apache.hadoop.hbase.util.SimpleKdcServerUtil}. The kerby KDC server is preferred;
 * less baggage. It came in in HBASE-5291.
 */
public MiniKdc setupMiniKdc(File keytabFile) throws Exception {
    Properties conf = MiniKdc.createConf();
    conf.put(MiniKdc.DEBUG, true);
    MiniKdc kdc = null;
    File dir = null;
    // There is time lag between selecting a port and trying to bind with it. It's possible that
    // another service captures the port in between which'll result in BindException.
    boolean bindException;
    int numTries = 0;
    do {
        try {
            bindException = false;
            dir = new File(getDataTestDir("kdc").toUri().getPath());
            kdc = new MiniKdc(conf, dir);
            kdc.start();
        } catch (BindException e) {
            // clean directory
            FileUtils.deleteDirectory(dir);
            numTries++;
            if (numTries == 3) {
                LOG.error("Failed setting up MiniKDC. Tried " + numTries + " times.");
                throw e;
            }
            LOG.error("BindException encountered when setting up MiniKdc. Trying again.");
            bindException = true;
        }
    } while (bindException);
    HBaseKerberosUtils.setKeytabFileForTesting(keytabFile.getAbsolutePath());
    return kdc;
}
Also used : MiniKdc(org.apache.hadoop.minikdc.MiniKdc) BindException(java.net.BindException) Properties(java.util.Properties) File(java.io.File) HFile(org.apache.hadoop.hbase.io.hfile.HFile)

Aggregations

MiniKdc (org.apache.hadoop.minikdc.MiniKdc)41 File (java.io.File)33 Properties (java.util.Properties)18 BeforeClass (org.junit.BeforeClass)15 Configuration (org.apache.hadoop.conf.Configuration)10 FileWriter (java.io.FileWriter)5 IOException (java.io.IOException)5 Before (org.junit.Before)5 BindException (java.net.BindException)4 Closeable (java.io.Closeable)3 Writer (java.io.Writer)3 HBaseTestingUtil (org.apache.hadoop.hbase.HBaseTestingUtil)3 AuthenticationTokenIdentifier (org.apache.hadoop.hbase.security.token.AuthenticationTokenIdentifier)3 Text (org.apache.hadoop.io.Text)3 Job (org.apache.hadoop.mapreduce.Job)3 Credentials (org.apache.hadoop.security.Credentials)3 Token (org.apache.hadoop.security.token.Token)3 TokenIdentifier (org.apache.hadoop.security.token.TokenIdentifier)3 Test (org.junit.Test)3 ApplicationProperties (org.apache.atlas.ApplicationProperties)2