KerberosUtil { static final String[] getPrincipalNames(String keytabFileName) throws IOException { Keytab keytab = Keytab.loadKeytab(new File(keytabFileName)); Set<String> principals = new HashSet<String>(); List<PrincipalName> entries = keytab.getPrincipals(); for (PrincipalName entry : entries) { principals.add(entry.getName().replace("\\", "/")); } return principals.toArray(new String[0]); }  static String getKrb5LoginModuleName(); static Oid getOidInstance(String oidName); static String getDefaultRealm(); static String getDefaultRealmProtected(); static String getDomainRealm(String shortprinc); static String getLocalHostName(); static final String getServicePrincipal(String service,
      String hostname); static final String[] getPrincipalNames(String keytab,
      Pattern pattern); static boolean hasKerberosKeyTab(Subject subject); static boolean hasKerberosTicket(Subject subject); static String getTokenServerName(byte[] rawToken); static final Oid GSS_SPNEGO_MECH_OID; static final Oid GSS_KRB5_MECH_OID; static final Oid NT_GSS_KRB5_PRINCIPAL_OID; }
@Test public void testGetPrincipalNamesMissingKeytab() { try { KerberosUtil.getPrincipalNames(testKeytab); Assert.fail("Exception should have been thrown"); } catch (IllegalArgumentException e) { } catch (IOException e) { } }
@Test public void testGetPrincipalNamesMissingPattern() throws IOException { createKeyTab(testKeytab, new String[]{"test/testhost@testRealm"}); try { KerberosUtil.getPrincipalNames(testKeytab, null); Assert.fail("Exception should have been thrown"); } catch (Exception e) { } }
@Test public void testGetPrincipalNamesFromKeytab() throws IOException { createKeyTab(testKeytab, testPrincipals); String[] principals = KerberosUtil.getPrincipalNames(testKeytab); Assert.assertNotNull("principals cannot be null", principals); int expectedSize = 0; List<String> principalList = Arrays.asList(principals); for (String principal : testPrincipals) { Assert.assertTrue("missing principal "+principal, principalList.contains(principal)); expectedSize++; } Assert.assertEquals(expectedSize, principals.length); }
@Test public void testGetPrincipalNamesFromKeytabWithPattern() throws IOException { createKeyTab(testKeytab, testPrincipals); Pattern httpPattern = Pattern.compile("HTTP/.*"); String[] httpPrincipals = KerberosUtil.getPrincipalNames(testKeytab, httpPattern); Assert.assertNotNull("principals cannot be null", httpPrincipals); int expectedSize = 0; List<String> httpPrincipalList = Arrays.asList(httpPrincipals); for (String principal : testPrincipals) { if (httpPattern.matcher(principal).matches()) { Assert.assertTrue("missing principal "+principal, httpPrincipalList.contains(principal)); expectedSize++; } } Assert.assertEquals(expectedSize, httpPrincipals.length); }