UpdateKerberosConfigsServerAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { CommandReport commandReport = null; String clusterName = getExecutionCommand().getClusterName(); Clusters clusters = controller.getClusters(); Cluster cluster = clusters.getCluster(clusterName); String authenticatedUserName = getCommandParameterValue(getCommandParameters(), KerberosServerAction.AUTHENTICATED_USER_NAME); String dataDirectoryPath = getCommandParameterValue(getCommandParameters(), KerberosServerAction.DATA_DIRECTORY); HashMap<String, Map<String, String>> propertiesToSet = new HashMap<String, Map<String, String>>(); HashMap<String, Collection<String>> propertiesToRemove = new HashMap<String, Collection<String>>(); if (dataDirectoryPath != null) { File dataDirectory = new File(dataDirectoryPath); if (dataDirectory.exists()) { KerberosConfigDataFileReader configReader = null; Set<String> configTypes = new HashSet<String>(); try { File configFile = new File(dataDirectory, KerberosConfigDataFileReader.DATA_FILE_NAME); if (configFile.exists()) { configReader = kerberosConfigDataFileReaderFactory.createKerberosConfigDataFileReader(configFile); for (Map<String, String> record : configReader) { String configType = record.get(KerberosConfigDataFileReader.CONFIGURATION_TYPE); String configKey = record.get(KerberosConfigDataFileReader.KEY); String configOp = record.get(KerberosConfigDataFileReader.OPERATION); configTypes.add(configType); if (KerberosConfigDataFileReader.OPERATION_TYPE_REMOVE.equals(configOp)) { removeConfigTypeProp(propertiesToRemove, configType, configKey); } else { String configVal = record.get(KerberosConfigDataFileReader.VALUE); addConfigTypePropVal(propertiesToSet, configType, configKey, configVal); } } } final String securityEnabled = cluster.getSecurityType() == SecurityType.KERBEROS ? "true" : "false"; if(!configTypes.contains("cluster-env")) { configTypes.add("cluster-env"); } Map<String, String> clusterEnvProperties = propertiesToSet.get("cluster-env"); if(clusterEnvProperties == null) { clusterEnvProperties = new HashMap<>(); propertiesToSet.put("cluster-env", clusterEnvProperties); } clusterEnvProperties.put("security_enabled", securityEnabled); if (!configTypes.isEmpty()) { String configNote = getCommandParameterValue(getCommandParameters(), KerberosServerAction.UPDATE_CONFIGURATION_NOTE); if((configNote == null) || configNote.isEmpty()) { configNote = cluster.getSecurityType() == SecurityType.KERBEROS ? "Enabling Kerberos" : "Disabling Kerberos"; } for (String configType : configTypes) { configHelper.updateConfigType(cluster, controller, configType, propertiesToSet.get(configType), propertiesToRemove.get(configType), authenticatedUserName, configNote); } } } catch (IOException e) { String message = "Could not update services configs to enable kerberos"; actionLog.writeStdErr(message); LOG.error(message, e); commandReport = createCommandReport(1, HostRoleStatus.FAILED, "{}", actionLog.getStdOut(), actionLog.getStdErr()); } finally { if (configReader != null && !configReader.isClosed()) { try { configReader.close(); } catch (Throwable t) { } } } } } return (commandReport == null) ? createCommandReport(0, HostRoleStatus.COMPLETED, "{}", actionLog.getStdOut(), actionLog.getStdErr()) : commandReport; }  @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext);  }
@Test public void testUpdateConfig() throws Exception { Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put(KerberosServerAction.DATA_DIRECTORY, dataDir); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setCommandParams(commandParams); ConfigHelper configHelper = injector.getInstance(ConfigHelper.class); configHelper.updateConfigType(anyObject(Cluster.class), anyObject(AmbariManagementController.class), anyObject(String.class), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Collection<String>>anyObject(), anyObject(String.class), anyObject(String.class)); expectLastCall().atLeastOnce(); replayAll(); action.setExecutionCommand(executionCommand); action.execute(null); verifyAll(); }
@Test public void testUpdateConfigMissingDataDirectory() throws Exception { ExecutionCommand executionCommand = new ExecutionCommand(); Map<String, String> commandParams = new HashMap<String, String>(); executionCommand.setCommandParams(commandParams); replayAll(); action.setExecutionCommand(executionCommand); action.execute(null); verifyAll(); }
@Test public void testUpdateConfigEmptyDataDirectory() throws Exception { ExecutionCommand executionCommand = new ExecutionCommand(); Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put(KerberosServerAction.DATA_DIRECTORY, testFolder.newFolder().getAbsolutePath()); executionCommand.setCommandParams(commandParams); replayAll(); action.setExecutionCommand(executionCommand); action.execute(null); verifyAll(); }
@Test public void testUpdateConfigForceSecurityEnabled() throws Exception { Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put(KerberosServerAction.DATA_DIRECTORY, dataDir); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setCommandParams(commandParams); ConfigHelper configHelper = injector.getInstance(ConfigHelper.class); Capture<String> configTypes = Capture.newInstance(CaptureType.ALL); Capture<Map<String, String>> configUpdates = Capture.newInstance(CaptureType.ALL); configHelper.updateConfigType(anyObject(Cluster.class), anyObject(AmbariManagementController.class), capture(configTypes), capture(configUpdates), anyObject(Collection.class), anyObject(String.class), anyObject(String.class)); expectLastCall().atLeastOnce(); replayAll(); action.setExecutionCommand(executionCommand); action.execute(null); assertTrue(configTypes.getValues().contains("cluster-env")); boolean containsSecurityEnabled = false; for(Map<String, String> properties: configUpdates.getValues()) { if(properties.containsKey("security_enabled")) { containsSecurityEnabled = true; break; } } assertTrue(containsSecurityEnabled); verifyAll(); }