AbstractPrepareKerberosServerAction extends KerberosServerAction { void processServiceComponentHosts(Cluster cluster, KerberosDescriptor kerberosDescriptor, List<ServiceComponentHost> schToProcess, Collection<String> identityFilter, String dataDirectory, Map<String, Map<String, String>> currentConfigurations, Map<String, Map<String, String>> kerberosConfigurations, boolean includeAmbariIdentity, Map<String, Set<String>> propertiesToBeIgnored) throws AmbariException { actionLog.writeStdOut("Processing Kerberos identities and configurations"); if (!schToProcess.isEmpty()) { if (dataDirectory == null) { String message = "The data directory has not been set. Generated data can not be stored."; LOG.error(message); throw new AmbariException(message); } File identityDataFile = new File(dataDirectory, KerberosIdentityDataFileWriter.DATA_FILE_NAME); KerberosIdentityDataFileWriter kerberosIdentityDataFileWriter; Map<String, Object> filterContext = new HashMap<String, Object>(); filterContext.put("configurations", currentConfigurations); filterContext.put("services", cluster.getServices().keySet()); actionLog.writeStdOut(String.format("Writing Kerberos identity data metadata file to %s", identityDataFile.getAbsolutePath())); try { kerberosIdentityDataFileWriter = kerberosIdentityDataFileWriterFactory.createKerberosIdentityDataFileWriter(identityDataFile); } catch (IOException e) { String message = String.format("Failed to write index file - %s", identityDataFile.getAbsolutePath()); LOG.error(message, e); actionLog.writeStdOut(message); actionLog.writeStdErr(message + "\n" + e.getLocalizedMessage()); throw new AmbariException(message, e); } try { Map<String, Set<String>> propertiesToIgnore = null; for (ServiceComponentHost sch : schToProcess) { String hostName = sch.getHostName(); String serviceName = sch.getServiceName(); String componentName = sch.getServiceComponentName(); KerberosServiceDescriptor serviceDescriptor = kerberosDescriptor.getService(serviceName); if (serviceDescriptor != null) { List<KerberosIdentityDescriptor> serviceIdentities = serviceDescriptor.getIdentities(true, filterContext); kerberosHelper.addIdentities(kerberosIdentityDataFileWriter, serviceIdentities, identityFilter, hostName, serviceName, componentName, kerberosConfigurations, currentConfigurations); propertiesToIgnore = gatherPropertiesToIgnore(serviceIdentities, propertiesToIgnore); KerberosComponentDescriptor componentDescriptor = serviceDescriptor.getComponent(componentName); if (componentDescriptor != null) { List<KerberosIdentityDescriptor> componentIdentities = componentDescriptor.getIdentities(true, filterContext); kerberosHelper.mergeConfigurations(kerberosConfigurations, componentDescriptor.getConfigurations(true), currentConfigurations); kerberosHelper.addIdentities(kerberosIdentityDataFileWriter, componentIdentities, identityFilter, hostName, serviceName, componentName, kerberosConfigurations, currentConfigurations); propertiesToIgnore = gatherPropertiesToIgnore(componentIdentities, propertiesToIgnore); } } } if (includeAmbariIdentity && kerberosHelper.createAmbariIdentities(currentConfigurations.get("kerberos-env"))) { List<KerberosIdentityDescriptor> ambariIdentities = kerberosHelper.getAmbariServerIdentities(kerberosDescriptor); if (!ambariIdentities.isEmpty()) { for (KerberosIdentityDescriptor identity : ambariIdentities) { String componentName = KerberosHelper.AMBARI_SERVER_KERBEROS_IDENTITY_NAME.equals(identity.getName()) ? "AMBARI_SERVER_SELF" : "AMBARI_SERVER"; List<KerberosIdentityDescriptor> componentIdentities = Collections.singletonList(identity); kerberosHelper.addIdentities(kerberosIdentityDataFileWriter, componentIdentities, identityFilter, KerberosHelper.AMBARI_SERVER_HOST_NAME, "AMBARI", componentName, kerberosConfigurations, currentConfigurations); propertiesToIgnore = gatherPropertiesToIgnore(componentIdentities, propertiesToIgnore); } } } if ((propertiesToBeIgnored != null) && (propertiesToIgnore != null)) { propertiesToBeIgnored.putAll(propertiesToIgnore); } } catch (IOException e) { String message = String.format("Failed to write index file - %s", identityDataFile.getAbsolutePath()); LOG.error(message, e); actionLog.writeStdOut(message); actionLog.writeStdErr(message + "\n" + e.getLocalizedMessage()); throw new AmbariException(message, e); } finally { if (kerberosIdentityDataFileWriter != null) { try { kerberosIdentityDataFileWriter.close(); } catch (IOException e) { String message = "Failed to close the index file writer"; LOG.warn(message, e); actionLog.writeStdOut(message); actionLog.writeStdErr(message + "\n" + e.getLocalizedMessage()); } } } } }    }
@Test @SuppressWarnings("unchecked") public void testProcessServiceComponentHosts() throws Exception { final Cluster cluster = EasyMock.createNiceMock(Cluster.class); final KerberosIdentityDataFileWriter kerberosIdentityDataFileWriter = EasyMock.createNiceMock(KerberosIdentityDataFileWriter.class); final KerberosDescriptor kerberosDescriptor = EasyMock.createNiceMock(KerberosDescriptor.class); final ServiceComponentHost serviceComponentHostHDFS = EasyMock.createNiceMock(ServiceComponentHost.class); final ServiceComponentHost serviceComponentHostZK = EasyMock.createNiceMock(ServiceComponentHost.class); final KerberosServiceDescriptor serviceDescriptor = EasyMock.createNiceMock(KerberosServiceDescriptor.class); final KerberosComponentDescriptor componentDescriptor = EasyMock.createNiceMock(KerberosComponentDescriptor.class); final String hdfsService = "HDFS"; final String zookeeperService = "ZOOKEEPER"; final String hostName = "host1"; final String hdfsComponent = "DATANODE"; final String zkComponent = "ZK"; Collection<String> identityFilter = new ArrayList<>(); Map<String, Map<String, String>> kerberosConfigurations = new HashMap<>(); Map<String, Set<String>> propertiesToIgnore = new HashMap<>(); Map<String, String> descriptorProperties = new HashMap<>(); Map<String, Map<String, String>> configurations = new HashMap<>(); List<ServiceComponentHost> serviceComponentHosts = new ArrayList<ServiceComponentHost>() {{ add(serviceComponentHostHDFS); add(serviceComponentHostZK); }}; Map<String, Service> clusterServices = new HashMap<String, Service>(){{ put(hdfsService, null); put(zookeeperService, null); }}; expect(kerberosDescriptor.getProperties()).andReturn(descriptorProperties).atLeastOnce(); expect(kerberosIdentityDataFileWriterFactory.createKerberosIdentityDataFileWriter((File)anyObject())).andReturn(kerberosIdentityDataFileWriter); expect(cluster.getServices()).andReturn(new HashMap<>(clusterServices)).atLeastOnce(); expect(serviceComponentHostHDFS.getHostName()).andReturn(hostName).atLeastOnce(); expect(serviceComponentHostHDFS.getServiceName()).andReturn(hdfsService).atLeastOnce(); expect(serviceComponentHostHDFS.getServiceComponentName()).andReturn(hdfsComponent).atLeastOnce(); expect(serviceComponentHostZK.getHostName()).andReturn(hostName).atLeastOnce(); expect(serviceComponentHostZK.getServiceName()).andReturn(zookeeperService).atLeastOnce(); expect(serviceComponentHostZK.getServiceComponentName()).andReturn(zkComponent).atLeastOnce(); expect(kerberosDescriptor.getService(hdfsService)).andReturn(serviceDescriptor).once(); expect(serviceDescriptor.getComponent(hdfsComponent)).andReturn(componentDescriptor).once(); expect(componentDescriptor.getConfigurations(anyBoolean())).andReturn(null); replay(kerberosDescriptor, kerberosHelper, kerberosIdentityDataFileWriterFactory, cluster, serviceComponentHostHDFS, serviceComponentHostZK, serviceDescriptor, componentDescriptor); prepareKerberosServerAction.processServiceComponentHosts(cluster, kerberosDescriptor, serviceComponentHosts, identityFilter, "", configurations, kerberosConfigurations, false, propertiesToIgnore); verify(kerberosHelper); }