KerberosServerAction extends AbstractServerAction { protected CommandReport processIdentities(Map<String, Object> requestSharedDataContext) throws AmbariException { CommandReport commandReport = null; Map<String, String> commandParameters = getCommandParameters(); actionLog.writeStdOut("Processing identities..."); LOG.info("Processing identities..."); if (commandParameters != null) { PrincipalKeyCredential administratorCredential = kerberosHelper.getKDCAdministratorCredentials(getClusterName()); String defaultRealm = getDefaultRealm(commandParameters); KDCType kdcType = getKDCType(commandParameters); String dataDirectoryPath = getDataDirectoryPath(commandParameters); if (dataDirectoryPath != null) { File dataDirectory = new File(dataDirectoryPath); if (dataDirectory.exists()) { if (!dataDirectory.isDirectory() || !dataDirectory.canRead()) { String message = String.format("Failed to process the identities, the data directory is not accessible: %s", dataDirectory.getAbsolutePath()); actionLog.writeStdErr(message); LOG.error(message); throw new AmbariException(message); } File identityDataFile = new File(dataDirectory, DATA_FILE_NAME); if (identityDataFile.exists()) { if (!identityDataFile.canRead()) { String message = String.format("Failed to process the identities, cannot read the index file: %s", identityDataFile.getAbsolutePath()); actionLog.writeStdErr(message); LOG.error(message); throw new AmbariException(message); } KerberosOperationHandler handler = kerberosOperationHandlerFactory.getKerberosOperationHandler(kdcType); if (handler == null) { String message = String.format("Failed to process the identities, a KDC operation handler was not found for the KDC type of : %s", kdcType.toString()); actionLog.writeStdErr(message); LOG.error(message); throw new AmbariException(message); } Map<String, String> kerberosConfiguration = getConfiguration("kerberos-env"); try { handler.open(administratorCredential, defaultRealm, kerberosConfiguration); } catch (KerberosOperationException e) { String message = String.format("Failed to process the identities, could not properly open the KDC operation handler: %s", e.getMessage()); actionLog.writeStdErr(message); LOG.error(message); throw new AmbariException(message, e); } KerberosIdentityDataFileReader reader = null; try { reader = kerberosIdentityDataFileReaderFactory.createKerberosIdentityDataFileReader(identityDataFile); for (Map<String, String> record : reader) { commandReport = processRecord(record, defaultRealm, handler, kerberosConfiguration, requestSharedDataContext); if (commandReport != null) { break; } } } catch (AmbariException e) { throw new AmbariException(e.getMessage(), e); } catch (IOException e) { String message = String.format("Failed to process the identities, cannot read the index file: %s", identityDataFile.getAbsolutePath()); actionLog.writeStdErr(message); LOG.error(message, e); throw new AmbariException(message, e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { } } try { handler.close(); } catch (KerberosOperationException e) { } } } } } } actionLog.writeStdOut("Processing identities completed."); LOG.info("Processing identities completed."); return (commandReport == null) ? createCommandReport(0, HostRoleStatus.COMPLETED, "{}", actionLog.getStdOut(), actionLog.getStdErr()) : commandReport; }   static final String AUTHENTICATED_USER_NAME; static final String DATA_DIRECTORY; static final String DEFAULT_REALM; static final String SERVICE_COMPONENT_FILTER; static final String HOST_FILTER; static final String IDENTITY_FILTER; static final String KDC_TYPE; static final String UPDATE_CONFIGURATIONS; static final String UPDATE_CONFIGURATION_NOTE; static final String DATA_DIRECTORY_PREFIX; static final String KEYTAB_CONTENT_BASE64; static final String REGENERATE_ALL; static final String INCLUDE_AMBARI_IDENTITY; }
@Test public void testProcessIdentitiesSuccess() throws Exception { KerberosHelper kerberosHelper = injector.getInstance(KerberosHelper.class); expect(kerberosHelper.getKDCAdministratorCredentials(EasyMock.anyObject(String.class))) .andReturn(new PrincipalKeyCredential("principal", "password")) .anyTimes(); replay(kerberosHelper); ConcurrentMap<String, Object> sharedMap = new ConcurrentHashMap<String, Object>(); CommandReport report = action.processIdentities(sharedMap); Assert.assertNotNull(report); Assert.assertEquals(HostRoleStatus.COMPLETED.toString(), report.getStatus()); for (Map.Entry<String, Object> entry : sharedMap.entrySet()) { Assert.assertEquals(entry.getValue(), entry.getKey().replace("_HOST", "hostName").replace("_REALM", "REALM.COM")); } verify(kerberosHelper); }
@Test public void testProcessIdentitiesFail() throws Exception { KerberosHelper kerberosHelper = injector.getInstance(KerberosHelper.class); expect(kerberosHelper.getKDCAdministratorCredentials(EasyMock.anyObject(String.class))) .andReturn(new PrincipalKeyCredential("principal", "password")) .anyTimes(); replay(kerberosHelper); ConcurrentMap<String, Object> sharedMap = new ConcurrentHashMap<String, Object>(); sharedMap.put("FAIL", "true"); CommandReport report = action.processIdentities(sharedMap); Assert.assertNotNull(report); Assert.assertEquals(HostRoleStatus.FAILED.toString(), report.getStatus()); verify(kerberosHelper); }