KerberosOperationHandler { protected boolean createKeytabFile(File sourceKeytabFile, File destinationKeytabFile) throws KerberosOperationException { return createKeytabFile(readKeytabFile(sourceKeytabFile), destinationKeytabFile); }  abstract void open(PrincipalKeyCredential administratorCredential, String defaultRealm, Map<String, String> kerberosConfiguration); abstract void close(); abstract boolean principalExists(String principal); abstract Integer createPrincipal(String principal, String password, boolean service); abstract Integer setPrincipalPassword(String principal, String password); abstract boolean removePrincipal(String principal); boolean testAdministratorCredentials(); boolean createKeytabFile(Keytab keytab, File destinationKeytabFile); PrincipalKeyCredential getAdministratorCredential(); void setAdministratorCredential(PrincipalKeyCredential administratorCredential); String getDefaultRealm(); void setDefaultRealm(String defaultRealm); Set<EncryptionType> getKeyEncryptionTypes(); void setKeyEncryptionTypes(Set<EncryptionType> keyEncryptionTypes); String[] getExecutableSearchPaths(); void setExecutableSearchPaths(String[] executableSearchPaths); void setExecutableSearchPaths(String delimitedExecutableSearchPaths); boolean isOpen(); void setOpen(boolean open); final static String KERBEROS_ENV_LDAP_URL; final static String KERBEROS_ENV_PRINCIPAL_CONTAINER_DN; final static String KERBEROS_ENV_USER_PRINCIPAL_GROUP; final static String KERBEROS_ENV_PASSWORD_CHAT_TIMEOUT; final static int DEFAULT_PASSWORD_CHAT_TIMEOUT; final static String KERBEROS_ENV_SET_PASSWORD_EXPIRY; final static String KERBEROS_ENV_AD_CREATE_ATTRIBUTES_TEMPLATE; final static String KERBEROS_ENV_KDC_CREATE_ATTRIBUTES; final static String KERBEROS_ENV_ENCRYPTION_TYPES; final static String KERBEROS_ENV_KDC_HOSTS; final static String KERBEROS_ENV_ADMIN_SERVER_HOST; final static String KERBEROS_ENV_EXECUTABLE_SEARCH_PATHS; }
@Test public void testCreateKeytabFileOneAtATime() throws Exception { KerberosOperationHandler handler = createHandler(); File file = folder.newFile(); final String principal1 = "principal1@REALM.COM"; final String principal2 = "principal2@REALM.COM"; int count; Assert.assertTrue(handler.createKeytabFile(principal1, "some password", 0, file)); Keytab keytab = Keytab.read(file); Assert.assertNotNull(keytab); List<KeytabEntry> entries = keytab.getEntries(); Assert.assertNotNull(entries); Assert.assertFalse(entries.isEmpty()); count = entries.size(); for (KeytabEntry entry : entries) { Assert.assertEquals(principal1, entry.getPrincipalName()); } Assert.assertTrue(handler.createKeytabFile(principal2, "some password", 0, file)); keytab = Keytab.read(file); Assert.assertNotNull(keytab); entries = keytab.getEntries(); Assert.assertNotNull(entries); Assert.assertFalse(entries.isEmpty()); Assert.assertEquals(count * 2, entries.size()); }
@Test public void testEnsureKeytabFileContainsNoDuplicates() throws Exception { KerberosOperationHandler handler = createHandler(); File file = folder.newFile(); final String principal1 = "principal1@REALM.COM"; final String principal2 = "principal2@REALM.COM"; Set<String> seenEntries = new HashSet<String>(); Assert.assertTrue(handler.createKeytabFile(principal1, "some password", 0, file)); Assert.assertTrue(handler.createKeytabFile(principal2, "some password", 0, file)); Assert.assertTrue(handler.createKeytabFile(principal2, "some password", 0, file)); Keytab keytab = Keytab.read(file); Assert.assertNotNull(keytab); List<KeytabEntry> entries = keytab.getEntries(); Assert.assertNotNull(entries); Assert.assertFalse(entries.isEmpty()); for (KeytabEntry entry : entries) { String seenEntry = String.format("%s|%s", entry.getPrincipalName(), entry.getKey().getKeyType().toString()); Assert.assertFalse(seenEntries.contains(seenEntry)); seenEntries.add(seenEntry); } }
@Test public void testCreateKeytabFileExceptions() throws Exception { KerberosOperationHandler handler = createHandler(); File file = folder.newFile(); final String principal1 = "principal1@REALM.COM"; try { handler.createKeytabFile(null, "some password", 0, file); Assert.fail("KerberosOperationException not thrown with null principal"); } catch (Throwable t) { Assert.assertEquals(KerberosOperationException.class, t.getClass()); } try { handler.createKeytabFile(principal1, null, null, file); Assert.fail("KerberosOperationException not thrown with null password"); } catch (Throwable t) { Assert.assertEquals(KerberosOperationException.class, t.getClass()); } try { handler.createKeytabFile(principal1, "some password", 0, null); Assert.fail("KerberosOperationException not thrown with null file"); } catch (Throwable t) { Assert.assertEquals(KerberosOperationException.class, t.getClass()); } }