KerberosKeytabsAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { String clusterName = getExecutionCommand().getClusterName(); Cluster cluster = m_clusters.getCluster(clusterName); StringBuilder stdout = new StringBuilder(); stdout.append(String.format("Checking %s is secured by Kerberos... %s", clusterName, m_kerberosHelper.isClusterKerberosEnabled(cluster))).append(System.lineSeparator()); if (!m_kerberosHelper.isClusterKerberosEnabled(cluster)) { stdout.append(String.format("Cluster %s is not secured by Kerberos. No action required.", clusterName)); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", stdout.toString(), ""); } stdout.append(String.format("Loading %s for cluster %s", KERBEROS_ENV, clusterName)).append(System.lineSeparator()); Config kerberosEnv = cluster.getDesiredConfigByType(KERBEROS_ENV); if (kerberosEnv == null) { stdout.append(String.format("Configuration %s was not found. No action required.", KERBEROS_ENV)); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", stdout.toString(), ""); } Map<String, String> kerbProperties = kerberosEnv.getProperties(); KDCType kdcType = KDCType.NONE; if (null != kerbProperties && kerbProperties.containsKey(KDC_TYPE_KEY)) { kdcType = KDCType.translate(kerbProperties.get(KDC_TYPE_KEY)); } stdout.append(String.format("Checking KDC type... %s", kdcType)).append(System.lineSeparator()); if (KDCType.NONE == kdcType) { stdout.append(String.format("KDC Type is %s, keytabs are managed manually. No action required.", kdcType)); return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", stdout.toString(), ""); } stdout.append(String.format("Ambari is managing Kerberos keytabs. Regenerate " + "keytabs after upgrade is complete.")); return createCommandReport(0, HostRoleStatus.HOLDING, "{}", stdout.toString(), ""); }  @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext);  }
@Test public void testAction_NotKerberized() throws Exception { reset(m_kerberosHelper); expect(m_kerberosHelper.isClusterKerberosEnabled(EasyMock.anyObject(Cluster.class))).andReturn(Boolean.FALSE).atLeastOnce(); replay(m_kerberosHelper); Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put("clusterName", "c1"); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setCommandParams(commandParams); executionCommand.setClusterName("c1"); HostRoleCommand hrc = EasyMock.createMock(HostRoleCommand.class); expect(hrc.getRequestId()).andReturn(1L).anyTimes(); expect(hrc.getStageId()).andReturn(2L).anyTimes(); expect(hrc.getExecutionCommandWrapper()).andReturn(new ExecutionCommandWrapper(executionCommand)).anyTimes(); replay(hrc); KerberosKeytabsAction action = m_injector.getInstance(KerberosKeytabsAction.class); action.setExecutionCommand(executionCommand); action.setHostRoleCommand(hrc); CommandReport report = action.execute(null); assertNotNull(report); Assert.assertEquals(HostRoleStatus.COMPLETED.name(), report.getStatus()); Assert.assertTrue(StringUtils.contains(report.getStdOut(), "Cluster c1 is not secured by Kerberos")); Assert.assertTrue(StringUtils.contains(report.getStdOut(), "No action required.")); }
@Test public void testAction_NoKdcType() throws Exception { reset(m_kerberosHelper); expect(m_kerberosHelper.isClusterKerberosEnabled(EasyMock.anyObject(Cluster.class))).andReturn(Boolean.TRUE).atLeastOnce(); replay(m_kerberosHelper); Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put("clusterName", "c1"); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setCommandParams(commandParams); executionCommand.setClusterName("c1"); HostRoleCommand hrc = EasyMock.createMock(HostRoleCommand.class); expect(hrc.getRequestId()).andReturn(1L).anyTimes(); expect(hrc.getStageId()).andReturn(2L).anyTimes(); expect(hrc.getExecutionCommandWrapper()).andReturn(new ExecutionCommandWrapper(executionCommand)).anyTimes(); replay(hrc); KerberosKeytabsAction action = m_injector.getInstance(KerberosKeytabsAction.class); action.setExecutionCommand(executionCommand); action.setHostRoleCommand(hrc); CommandReport report = action.execute(null); assertNotNull(report); Assert.assertEquals(HostRoleStatus.COMPLETED.name(), report.getStatus()); Assert.assertTrue(StringUtils.contains(report.getStdOut(), "KDC Type is NONE")); Assert.assertTrue(StringUtils.contains(report.getStdOut(), "No action required.")); }
@Test public void testAction_Kerberized() throws Exception { reset(m_kerberosHelper); expect(m_kerberosHelper.isClusterKerberosEnabled(EasyMock.anyObject(Cluster.class))).andReturn(Boolean.TRUE).atLeastOnce(); replay(m_kerberosHelper); m_kerberosConfig.getProperties().put("kdc_type", "mit-kdc"); Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put("clusterName", "c1"); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setCommandParams(commandParams); executionCommand.setClusterName("c1"); HostRoleCommand hrc = EasyMock.createMock(HostRoleCommand.class); expect(hrc.getRequestId()).andReturn(1L).anyTimes(); expect(hrc.getStageId()).andReturn(2L).anyTimes(); expect(hrc.getExecutionCommandWrapper()).andReturn(new ExecutionCommandWrapper(executionCommand)).anyTimes(); replay(hrc); KerberosKeytabsAction action = m_injector.getInstance(KerberosKeytabsAction.class); action.setExecutionCommand(executionCommand); action.setHostRoleCommand(hrc); CommandReport report = action.execute(null); assertNotNull(report); Assert.assertEquals(HostRoleStatus.HOLDING.name(), report.getStatus()); Assert.assertTrue(StringUtils.contains(report.getStdOut(), "Checking KDC type... MIT_KDC")); Assert.assertTrue(StringUtils.contains(report.getStdOut(), "Regenerate keytabs after upgrade is complete.")); }