コード例 #1
0
  /**
   * Remove all the keys of the specified principal in the specified keytab file.
   *
   * @param keytabFile The keytab file
   * @param principalName The principal name
   * @throws KrbException
   */
  static void removeKeytabEntriesOf(File keytabFile, String principalName) throws KrbException {
    Keytab keytab = loadKeytab(keytabFile);

    keytab.removeKeytabEntries(new PrincipalName(principalName));

    storeKeytab(keytab, keytabFile);
  }
コード例 #2
0
  @Test
  public void testKeytab() {
    assertThat(keytab).isNotNull();

    List<PrincipalName> principals = keytab.getPrincipals();
    PrincipalName principal = principals.get(0);
    List<KeytabEntry> entries = keytab.getKeytabEntries(principal);
    for (KeytabEntry ke : entries) {
      assertThat(ke.getKvno() == 1).isTrue();
    }
  }
コード例 #3
0
 /**
  * Store the keytab to keytab file.
  *
  * @param keytab The keytab
  * @param keytabFile The keytab file
  * @throws KrbException
  */
 static void storeKeytab(Keytab keytab, File keytabFile) throws KrbException {
   try {
     keytab.store(keytabFile);
   } catch (IOException e) {
     throw new KrbException("Failed to store keytab", e);
   }
 }
コード例 #4
0
  /**
   * Load keytab from keytab file.
   *
   * @param keytabFile The keytab file
   * @return The keytab load from keytab file
   * @throws KrbException
   */
  static Keytab loadKeytab(File keytabFile) throws KrbException {
    Keytab keytab;
    try {
      keytab = Keytab.loadKeytab(keytabFile);
    } catch (IOException e) {
      throw new KrbException("Failed to load keytab", e);
    }

    return keytab;
  }
コード例 #5
0
  /**
   * Remove all the old keys of the specified principal in the specified keytab file.
   *
   * @param keytabFile The keytab file
   * @param principalName The principal name
   * @throws KrbException
   */
  static void removeOldKeytabEntriesOf(File keytabFile, String principalName) throws KrbException {
    Keytab keytab = loadKeytab(keytabFile);

    List<KeytabEntry> entries = keytab.getKeytabEntries(new PrincipalName(principalName));

    int maxKvno = 0;
    for (KeytabEntry entry : entries) {
      if (maxKvno < entry.getKvno()) {
        maxKvno = entry.getKvno();
      }
    }

    for (KeytabEntry entry : entries) {
      if (entry.getKvno() < maxKvno) {
        keytab.removeKeytabEntry(entry);
      }
    }

    storeKeytab(keytab, keytabFile);
  }
コード例 #6
0
  /**
   * Export all the keys of the specified identity into the keytab.
   *
   * @param keytab The keytab
   * @param identity The identity
   * @throws KrbException
   */
  static void exportToKeytab(Keytab keytab, KrbIdentity identity) throws KrbException {

    // Add principal to keytab.
    PrincipalName principal = identity.getPrincipal();
    KerberosTime timestamp = KerberosTime.now();
    for (EncryptionType encType : identity.getKeys().keySet()) {
      EncryptionKey ekey = identity.getKeys().get(encType);
      int keyVersion = ekey.getKvno();
      keytab.addEntry(new KeytabEntry(principal, timestamp, keyVersion, ekey));
    }
  }
コード例 #7
0
  /**
   * If keytab file does not exist, create a new keytab, otherwise load keytab from keytab file.
   *
   * @param keytabFile The keytab file
   * @return The keytab load from keytab file
   * @throws KrbException
   */
  static Keytab createOrLoadKeytab(File keytabFile) throws KrbException {

    Keytab keytab;
    try {
      if (!keytabFile.exists()) {
        if (!keytabFile.createNewFile()) {
          throw new KrbException("Failed to create keytab file " + keytabFile.getAbsolutePath());
        }
        keytab = new Keytab();
      } else {
        keytab = Keytab.loadKeytab(keytabFile);
      }
    } catch (IOException e) {
      throw new KrbException("Failed to load or create keytab", e);
    }

    return keytab;
  }
コード例 #8
0
 public static void main(String[] args) throws IOException {
   InputStream kis = KeytabTest.class.getResourceAsStream("test.keytab");
   Keytab keytab = Keytab.loadKeytab(kis);
   System.out.println("Principals:" + keytab.getPrincipals().size());
 }
コード例 #9
0
 @Before
 public void setUp() throws IOException {
   InputStream kis = KeytabTest.class.getResourceAsStream("/test.keytab");
   keytab = Keytab.loadKeytab(kis);
 }