@Test public void testMergeKeytabs() throws KerberosOperationException { KerberosOperationHandler handler = createHandler(); Keytab keytab1 = handler.createKeytab("principal@EXAMPLE.COM", "password", 1); Keytab keytab2 = handler.createKeytab("principal@EXAMPLE.COM", "password1", 1); Keytab keytab3 = handler.createKeytab("principal1@EXAMPLE.COM", "password", 4); Keytab merged; merged = handler.mergeKeytabs(keytab1, keytab2); Assert.assertEquals(keytab1.getEntries().size(), merged.getEntries().size()); merged = handler.mergeKeytabs(keytab1, keytab3); Assert.assertEquals(keytab1.getEntries().size() + keytab3.getEntries().size(), merged.getEntries().size()); merged = handler.mergeKeytabs(keytab2, keytab3); Assert.assertEquals(keytab2.getEntries().size() + keytab3.getEntries().size(), merged.getEntries().size()); merged = handler.mergeKeytabs(keytab2, merged); Assert.assertEquals(keytab2.getEntries().size() + keytab3.getEntries().size(), merged.getEntries().size()); }
protected Keytab mergeKeytabs(Keytab keytab, Keytab updates) { List<KeytabEntry> keytabEntries = (keytab == null) ? Collections.<KeytabEntry>emptyList() : new ArrayList<KeytabEntry>(keytab.getEntries()); List<KeytabEntry> updateEntries = (updates == null) ? Collections.<KeytabEntry>emptyList() : new ArrayList<KeytabEntry>(updates.getEntries()); List<KeytabEntry> mergedEntries = new ArrayList<KeytabEntry>(); if (keytabEntries.isEmpty()) { mergedEntries.addAll(updateEntries); } else if (updateEntries.isEmpty()) { mergedEntries.addAll(keytabEntries); } else { Iterator<KeytabEntry> iterator = keytabEntries.iterator(); while (iterator.hasNext()) { KeytabEntry keytabEntry = iterator.next(); for (KeytabEntry entry : updateEntries) { if (entry.getPrincipalName().equals(keytabEntry.getPrincipalName()) && entry.getKey().getKeyType().equals(keytabEntry.getKey().getKeyType())) { iterator.remove(); break; } } } mergedEntries.addAll(keytabEntries); mergedEntries.addAll(updateEntries); } Keytab mergedKeytab = new Keytab(); mergedKeytab.setEntries(mergedEntries); return mergedKeytab; }
KerberosOperationHandler { protected Keytab mergeKeytabs(Keytab keytab, Keytab updates) { List<KeytabEntry> keytabEntries = (keytab == null) ? Collections.<KeytabEntry>emptyList() : new ArrayList<KeytabEntry>(keytab.getEntries()); List<KeytabEntry> updateEntries = (updates == null) ? Collections.<KeytabEntry>emptyList() : new ArrayList<KeytabEntry>(updates.getEntries()); List<KeytabEntry> mergedEntries = new ArrayList<KeytabEntry>(); if (keytabEntries.isEmpty()) { mergedEntries.addAll(updateEntries); } else if (updateEntries.isEmpty()) { mergedEntries.addAll(keytabEntries); } else { Iterator<KeytabEntry> iterator = keytabEntries.iterator(); while (iterator.hasNext()) { KeytabEntry keytabEntry = iterator.next(); for (KeytabEntry entry : updateEntries) { if (entry.getPrincipalName().equals(keytabEntry.getPrincipalName()) && entry.getKey().getKeyType().equals(keytabEntry.getKey().getKeyType())) { iterator.remove(); break; } } } mergedEntries.addAll(keytabEntries); mergedEntries.addAll(updateEntries); } Keytab mergedKeytab = new Keytab(); mergedKeytab.setEntries(mergedEntries); return mergedKeytab; } }
KerberosOperationHandler { protected Keytab mergeKeytabs(Keytab keytab, Keytab updates) { List<KeytabEntry> keytabEntries = (keytab == null) ? Collections.<KeytabEntry>emptyList() : new ArrayList<KeytabEntry>(keytab.getEntries()); List<KeytabEntry> updateEntries = (updates == null) ? Collections.<KeytabEntry>emptyList() : new ArrayList<KeytabEntry>(updates.getEntries()); List<KeytabEntry> mergedEntries = new ArrayList<KeytabEntry>(); if (keytabEntries.isEmpty()) { mergedEntries.addAll(updateEntries); } else if (updateEntries.isEmpty()) { mergedEntries.addAll(keytabEntries); } else { Iterator<KeytabEntry> iterator = keytabEntries.iterator(); while (iterator.hasNext()) { KeytabEntry keytabEntry = iterator.next(); for (KeytabEntry entry : updateEntries) { if (entry.getPrincipalName().equals(keytabEntry.getPrincipalName()) && entry.getKey().getKeyType().equals(keytabEntry.getKey().getKeyType())) { iterator.remove(); break; } } } mergedEntries.addAll(keytabEntries); mergedEntries.addAll(updateEntries); } Keytab mergedKeytab = new Keytab(); mergedKeytab.setEntries(mergedEntries); return mergedKeytab; }  }
KerberosOperationHandler { protected Keytab mergeKeytabs(Keytab keytab, Keytab updates) { List<KeytabEntry> keytabEntries = (keytab == null) ? Collections.<KeytabEntry>emptyList() : new ArrayList<KeytabEntry>(keytab.getEntries()); List<KeytabEntry> updateEntries = (updates == null) ? Collections.<KeytabEntry>emptyList() : new ArrayList<KeytabEntry>(updates.getEntries()); List<KeytabEntry> mergedEntries = new ArrayList<KeytabEntry>(); if (keytabEntries.isEmpty()) { mergedEntries.addAll(updateEntries); } else if (updateEntries.isEmpty()) { mergedEntries.addAll(keytabEntries); } else { Iterator<KeytabEntry> iterator = keytabEntries.iterator(); while (iterator.hasNext()) { KeytabEntry keytabEntry = iterator.next(); for (KeytabEntry entry : updateEntries) { if (entry.getPrincipalName().equals(keytabEntry.getPrincipalName()) && entry.getKey().getKeyType().equals(keytabEntry.getKey().getKeyType())) { iterator.remove(); break; } } } mergedEntries.addAll(keytabEntries); mergedEntries.addAll(updateEntries); } Keytab mergedKeytab = new Keytab(); mergedKeytab.setEntries(mergedEntries); return mergedKeytab; }  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); }
KerberosOperationHandler { protected Keytab mergeKeytabs(Keytab keytab, Keytab updates) { List<KeytabEntry> keytabEntries = (keytab == null) ? Collections.<KeytabEntry>emptyList() : new ArrayList<KeytabEntry>(keytab.getEntries()); List<KeytabEntry> updateEntries = (updates == null) ? Collections.<KeytabEntry>emptyList() : new ArrayList<KeytabEntry>(updates.getEntries()); List<KeytabEntry> mergedEntries = new ArrayList<KeytabEntry>(); if (keytabEntries.isEmpty()) { mergedEntries.addAll(updateEntries); } else if (updateEntries.isEmpty()) { mergedEntries.addAll(keytabEntries); } else { Iterator<KeytabEntry> iterator = keytabEntries.iterator(); while (iterator.hasNext()) { KeytabEntry keytabEntry = iterator.next(); for (KeytabEntry entry : updateEntries) { if (entry.getPrincipalName().equals(keytabEntry.getPrincipalName()) && entry.getKey().getKeyType().equals(keytabEntry.getKey().getKeyType())) { iterator.remove(); break; } } } mergedEntries.addAll(keytabEntries); mergedEntries.addAll(updateEntries); } Keytab mergedKeytab = new Keytab(); mergedKeytab.setEntries(mergedEntries); return mergedKeytab; }  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; }