spnegoPrincipals = KerberosUtil.getPrincipalNames(keytab, Pattern.compile("HTTP/.*")); if (spnegoPrincipals.length == 0) { throw new ServletException("Principals do not exist in the keytab");
/** * Get all the unique principals from keytabfile which matches a pattern. * * @param keytab Name of the keytab file to be read. * @param pattern pattern to be matched. * @return list of unique principals which matches the pattern. * @throws IOException if cannot get the principal name */ public static final String[] getPrincipalNames(String keytab, Pattern pattern) throws IOException { String[] principals = getPrincipalNames(keytab); if (principals.length != 0) { List<String> matchingPrincipals = new ArrayList<String>(); for (String principal : principals) { if (pattern.matcher(principal).matches()) { matchingPrincipals.add(principal); } } principals = matchingPrincipals.toArray(new String[0]); } return principals; }
/** * Get all the unique principals from keytabfile which matches a pattern. * * @param keytab Name of the keytab file to be read. * @param pattern pattern to be matched. * @return list of unique principals which matches the pattern. * @throws IOException if cannot get the principal name */ public static final String[] getPrincipalNames(String keytab, Pattern pattern) throws IOException { String[] principals = getPrincipalNames(keytab); if (principals.length != 0) { List<String> matchingPrincipals = new ArrayList<String>(); for (String principal : principals) { if (pattern.matcher(principal).matches()) { matchingPrincipals.add(principal); } } principals = matchingPrincipals.toArray(new String[0]); } return principals; }
/** * Get all the unique principals from keytabfile which matches a pattern. * * @param keytab Name of the keytab file to be read. * @param pattern pattern to be matched. * @return list of unique principals which matches the pattern. * @throws IOException if cannot get the principal name */ public static final String[] getPrincipalNames(String keytab, Pattern pattern) throws IOException { String[] principals = getPrincipalNames(keytab); if (principals.length != 0) { List<String> matchingPrincipals = new ArrayList<String>(); for (String principal : principals) { if (pattern.matcher(principal).matches()) { matchingPrincipals.add(principal); } } principals = matchingPrincipals.toArray(new String[0]); } return principals; } }
/** * Get all the unique principals from keytabfile which matches a pattern. * * @param keytab * Name of the keytab file to be read. * @param pattern * pattern to be matched. * @return list of unique principals which matches the pattern. * @throws IOException */ public static final String[] getPrincipalNames(String keytab, Pattern pattern) throws IOException { String[] principals = getPrincipalNames(keytab); if (principals.length != 0) { List<String> matchingPrincipals = new ArrayList<String>(); for (String principal : principals) { if (pattern.matcher(principal).matches()) { matchingPrincipals.add(principal); } } principals = matchingPrincipals.toArray(new String[0]); } return principals; } }
/** * Get all the unique principals from keytabfile which matches a pattern. * * @param keytab Name of the keytab file to be read. * @param pattern pattern to be matched. * @return list of unique principals which matches the pattern. * @throws IOException if cannot get the principal name */ public static final String[] getPrincipalNames(String keytab, Pattern pattern) throws IOException { String[] principals = getPrincipalNames(keytab); if (principals.length != 0) { List<String> matchingPrincipals = new ArrayList<String>(); for (String principal : principals) { if (pattern.matcher(principal).matches()) { matchingPrincipals.add(principal); } } principals = matchingPrincipals.toArray(new String[0]); } return principals; }
/** * Dump a keytab: list all principals. * @param keytabFile the keytab file * @throws IOException IO problems */ public void dumpKeytab(File keytabFile) throws IOException { title("Examining keytab %s", keytabFile); File kt = keytabFile.getCanonicalFile(); failif(!kt.exists(), CAT_CONFIG, "Keytab not found: %s", kt); failif(!kt.isFile(), CAT_CONFIG, "Keytab is not a valid file: %s", kt); String[] names = getPrincipalNames(keytabFile.getCanonicalPath(), Pattern.compile(".*")); println("keytab entry count: %d", names.length); for (String name : names) { println(" %s", name); } println("-----"); }
/** * Dump a keytab: list all principals. * @param keytabFile the keytab file * @throws IOException IO problems */ public void dumpKeytab(File keytabFile) throws IOException { title("Examining keytab %s", keytabFile); File kt = keytabFile.getCanonicalFile(); failif(!kt.exists(), CAT_CONFIG, "Keytab not found: %s", kt); failif(!kt.isFile(), CAT_CONFIG, "Keytab is not a valid file: %s", kt); String[] names = getPrincipalNames(keytabFile.getCanonicalPath(), Pattern.compile(".*")); println("keytab entry count: %d", names.length); for (String name : names) { println(" %s", name); } println("-----"); }
@Test public void testGetPrincipalNamesMissingKeytab() { try { KerberosUtil.getPrincipalNames(testKeytab); Assert.fail("Exception should have been thrown"); } catch (IOException e) { //expects exception } }
@Test public void testGetPrincipalNamesMissingKeytab() { try { KerberosUtil.getPrincipalNames(testKeytab); Assert.fail("Exception should have been thrown"); } catch (IOException e) { //expects exception } }
@Test public void testGetPrincipalNamesMissingKeytab() { try { KerberosUtil.getPrincipalNames(testKeytab); Assert.fail("Exception should have been thrown"); } catch (IOException e) { //expects exception } }
@Test public void testGetPrincipalNamesFromKeytabWithPattern() throws IOException { createKeyTab(testKeytab, testPrincipals); // read the keytab file // look for principals with HTTP as the first part 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); }
@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) { //expects exception } }
@Test public void testGetPrincipalNamesFromKeytabWithPattern() throws IOException { createKeyTab(testKeytab, testPrincipals); // read the keytab file // look for principals with HTTP as the first part 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); }
@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) { //expects exception } }
@Test public void testGetPrincipalNamesFromKeytabWithPattern() throws IOException { createKeyTab(testKeytab, testPrincipals); // read the keytab file // look for principals with HTTP as the first part 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); }
@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) { //expects exception } }
@Test public void testGetPrincipalNamesFromKeytab() throws IOException { createKeyTab(testKeytab, testPrincipals); // read all principals in the keytab file 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 testGetPrincipalNamesFromKeytab() throws IOException { createKeyTab(testKeytab, testPrincipals); // read all principals in the keytab file 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 testGetPrincipalNamesFromKeytab() throws IOException { createKeyTab(testKeytab, testPrincipals); // read all principals in the keytab file 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); }