protected Keytab createKeytab(String principal, String password, Integer keyNumber)
      throws KerberosOperationException {

    if (StringUtils.isEmpty(principal)) {
      throw new KerberosOperationException("Failed to create keytab file, missing principal");
    }

    if (password == null) {
      throw new KerberosOperationException(String.format("Failed to create keytab file for %s, missing password", principal));
    }

    Set<EncryptionType> ciphers = new HashSet<>(keyEncryptionTypes);
    List<KeytabEntry> keytabEntries = new ArrayList<>();
    Keytab keytab = new Keytab();


    if (!ciphers.isEmpty()) {
      // Create a set of keys and relevant keytab entries
      Map<EncryptionType, EncryptionKey> keys = KerberosKeyFactory.getKerberosKeys(principal, password, ciphers);

      if (keys != null) {
        byte keyVersion = (keyNumber == null) ? 0 : keyNumber.byteValue();
        KerberosTime timestamp = new KerberosTime();

        for (EncryptionKey encryptionKey : keys.values()) {
          keytabEntries.add(new KeytabEntry(principal, 1, timestamp, keyVersion, encryptionKey));
        }

        keytab.setEntries(keytabEntries);
      }
    }

    return keytab;
  }
Create a keytab using the specified principal and password.

@param principal a String containing the principal to test
@param password  a String containing the password to use when creating the principal
@param keyNumber a Integer indicating the key number for the keytab entries
@return the created Keytab
@throws KerberosOperationException
ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/KerberosOperationHandler.java