AmbariManagementControllerImpl implements AmbariManagementController { @Override @Transactional public synchronized RequestStatusResponse updateClusters(Set<ClusterRequest> requests, Map<String, String> requestProperties) throws AmbariException, AuthorizationException { RequestStatusResponse response = null; for (ClusterRequest request : requests) { Cluster cluster; if (request.getClusterId() == null) { cluster = clusters.getCluster(request.getClusterName()); } else { cluster = clusters.getClusterById(request.getClusterId()); } if (cluster == null) { throw new AmbariException("The cluster may not be null"); } Map<String, Object> sessionAttributes = request.getSessionAttributes(); if((sessionAttributes != null) && !sessionAttributes.isEmpty()) { Map<String, Object> cleanedSessionAttributes = new HashMap<>(); String principal = null; char[] password = null; for(Map.Entry<String,Object> entry: sessionAttributes.entrySet()) { String name = entry.getKey(); Object value = entry.getValue(); if ("kerberos_admin/principal".equals(name)) { if(value instanceof String) { principal = (String)value; } } else if ("kerberos_admin/password".equals(name)) { if(value instanceof String) { password = ((String) value).toCharArray(); } } else { cleanedSessionAttributes.put(name, value); } } if(principal != null) { credentialStoreService.setCredential(cluster.getClusterName(), KerberosHelper.KDC_ADMINISTRATOR_CREDENTIAL_ALIAS, new PrincipalKeyCredential(principal, password), CredentialStoreType.TEMPORARY); } sessionAttributes = cleanedSessionAttributes; } cluster.addSessionAttributes(sessionAttributes); response = updateCluster(request, requestProperties); } return response; } @Inject  AmbariManagementControllerImpl(ActionManager actionManager,
      Clusters clusters, Injector injector); @Override String getAmbariServerURI(String path); @Override RoleCommandOrder getRoleCommandOrder(Cluster cluster); @Override void createCluster(ClusterRequest request); @Override synchronized void createHostComponents(Set<ServiceComponentHostRequest> requests); @Override void registerRackChange(String clusterName); @Override synchronized ConfigurationResponse createConfiguration(
      ConfigurationRequest request); @Override Config createConfig(Cluster cluster, String type, Map<String, String> properties,
                             String versionTag, Map<String, Map<String, String>> propertiesAttributes); @Override void createUsers(Set<UserRequest> requests); @Override void createGroups(Set<GroupRequest> requests); @Override void createMembers(Set<MemberRequest> requests); @Override Set<MemberResponse> getMembers(Set<MemberRequest> requests); @Override @SuppressWarnings("unchecked") synchronized void updateMembers(Set<MemberRequest> requests); @Override MaintenanceState getEffectiveMaintenanceState(ServiceComponentHost sch); @Override @Transactional synchronized RequestStatusResponse updateClusters(Set<ClusterRequest> requests,
                                                           Map<String, String> requestProperties); boolean isAttributeMapsEqual(Map<String, Map<String, String>> requestConfigAttributes,
          Map<String, Map<String, String>> clusterConfigAttributes); void saveClusterUpdate(ClusterRequest clusterRequest, ClusterResponse clusterResponse); @Override ClusterResponse getClusterUpdateResults(ClusterRequest clusterRequest); @Override String getJobTrackerHost(Cluster cluster); @Override Map<String, Map<String,String>> findConfigurationTagsWithOverrides(
          Cluster cluster, String hostName); @Override RequestExecutionFactory getRequestExecutionFactory(); @Override ExecutionScheduleManager getExecutionScheduleManager(); @Override List<ServiceOsSpecific.Package> getPackagesForServiceHost(ServiceInfo serviceInfo, Map<String, String> hostParams, String osFamily); @Override ExecutionCommand getExecutionCommand(Cluster cluster,
                                              ServiceComponentHost scHost,
                                              RoleCommand roleCommand); @Override Set<StackConfigurationDependencyResponse> getStackConfigurationDependencies(
          Set<StackConfigurationDependencyRequest> requests); @Override RequestStatusResponse createAndPersistStages(Cluster cluster, Map<String, String> requestProperties,
                                                      Map<String, String> requestParameters,
                                                      Map<State, List<Service>> changedServices,
                                                      Map<State, List<ServiceComponent>> changedComponents,
                                                      Map<String, Map<State, List<ServiceComponentHost>>> changedHosts,
                                                      Collection<ServiceComponentHost> ignoredHosts,
                                                      boolean runSmokeTest, boolean reconfigureClients); @Override RequestStageContainer addStages(RequestStageContainer requestStages, Cluster cluster, Map<String, String> requestProperties,
                                 Map<String, String> requestParameters, Map<State, List<Service>> changedServices,
                                 Map<State, List<ServiceComponent>> changedComponents,
                                 Map<String, Map<State, List<ServiceComponentHost>>> changedHosts,
                                 Collection<ServiceComponentHost> ignoredHosts, boolean runSmokeTest,
                                 boolean reconfigureClients); void validateServiceComponentHostRequest(ServiceComponentHostRequest request); @Override String findServiceName(Cluster cluster, String componentName); @Override synchronized void updateUsers(Set<UserRequest> requests); @Override synchronized void deleteCluster(ClusterRequest request); @Override DeleteStatusMetaData deleteHostComponents(
      Set<ServiceComponentHostRequest> requests); @Override void deleteUsers(Set<UserRequest> requests); @Override void deleteGroups(Set<GroupRequest> requests); @Override void deleteMembers(java.util.Set<MemberRequest> requests); @Override Set<ClusterResponse> getClusters(Set<ClusterRequest> requests); @Override Set<ServiceComponentHostResponse> getHostComponents(
      Set<ServiceComponentHostRequest> requests); @Override Set<ConfigurationResponse> getConfigurations(
      Set<ConfigurationRequest> requests); @Override Set<ServiceConfigVersionResponse> getServiceConfigVersions(Set<ServiceConfigVersionRequest> requests); @Override Set<UserResponse> getUsers(Set<UserRequest> requests); @Override Set<GroupResponse> getGroups(Set<GroupRequest> requests); @Override void updateGroups(Set<GroupRequest> requests); @Override List<String> selectHealthyHosts(Set<String> hostList); @Override String getHealthyHost(Set<String> hostList); @Override RequestStatusResponse createAction(ExecuteActionRequest actionRequest,
      Map<String, String> requestProperties); @Override Set<StackResponse> getStacks(Set<StackRequest> requests); @Override synchronized RequestStatusResponse updateStacks(); @Override Set<ExtensionResponse> getExtensions(Set<ExtensionRequest> requests); @Override Set<ExtensionVersionResponse> getExtensionVersions(
      Set<ExtensionVersionRequest> requests); @Override Set<RepositoryResponse> getRepositories(Set<RepositoryRequest> requests); @Override void updateRepositories(Set<RepositoryRequest> requests); @Override void verifyRepositories(Set<RepositoryRequest> requests); @Override Set<StackVersionResponse> getStackVersions(
      Set<StackVersionRequest> requests); @Override Set<StackServiceResponse> getStackServices(
      Set<StackServiceRequest> requests); @Override Set<StackConfigurationResponse> getStackLevelConfigurations(
      Set<StackLevelConfigurationRequest> requests); @Override Set<StackConfigurationResponse> getStackConfigurations(
      Set<StackConfigurationRequest> requests); @Override Set<StackServiceComponentResponse> getStackComponents(
      Set<StackServiceComponentRequest> requests); @Override Set<OperatingSystemResponse> getOperatingSystems(
      Set<OperatingSystemRequest> requests); @Override String getAuthName(); @Override int getAuthId(); @Override Set<RootServiceResponse> getRootServices(
      Set<RootServiceRequest> requests); @Override Set<RootServiceComponentResponse> getRootServiceComponents(
      Set<RootServiceComponentRequest> requests); @Override Clusters getClusters(); @Override ConfigHelper getConfigHelper(); @Override AmbariMetaInfo getAmbariMetaInfo(); @Override ServiceComponentFactory getServiceComponentFactory(); @Override ConfigGroupFactory getConfigGroupFactory(); @Override RoleGraphFactory getRoleGraphFactory(); @Override AbstractRootServiceResponseFactory getRootServiceResponseFactory(); @Override ActionManager getActionManager(); @Override String getJdkResourceUrl(); @Override String getJavaHome(); @Override String getJDKName(); @Override String getJCEName(); @Override String getServerDB(); @Override String getOjdbcUrl(); @Override String getMysqljdbcUrl(); @Override Map<String, String> getRcaParameters(); @Override boolean checkLdapConfigured(); @Override LdapSyncDto getLdapSyncInfo(); @Override boolean isLdapSyncInProgress(); @Override synchronized LdapBatchDto synchronizeLdapUsersAndGroups(
      LdapSyncRequest userRequest, LdapSyncRequest groupRequest); @SuppressWarnings("unchecked") @Override void initializeWidgetsAndLayouts(Cluster cluster, Service service); @Override TimelineMetricCacheProvider getTimelineMetricCacheProvider(); @Override MetricPropertyProviderFactory getMetricPropertyProviderFactory(); @Override LoggingSearchPropertyProvider getLoggingSearchPropertyProvider(); @Override LoggingService getLoggingService(String clusterName); @Override AmbariEventPublisher getAmbariEventPublisher(); @Override KerberosHelper getKerberosHelper(); @Override CredentialStoreService getCredentialStoreService(); Map<String,String> getCredentialStoreServiceProperties(); @Override MetricsCollectorHAManager getMetricsCollectorHAManager(); @Override void deleteExtensionLink(ExtensionLinkRequest request); @Override void createExtensionLink(ExtensionLinkRequest request); @Override void updateExtensionLink(ExtensionLinkRequest request); @Override void updateExtensionLink(ExtensionLinkEntity linkEntity); @Override QuickLinkVisibilityController getQuicklinkVisibilityController(); static final String SKIP_INSTALL_FOR_COMPONENTS; static final String DONT_SKIP_INSTALL_FOR_COMPONENTS; }
@Test public void testUpdateClusters() throws Exception { Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); Injector injector = createStrictMock(Injector.class); Cluster cluster = createNiceMock(Cluster.class); ActionManager actionManager = createNiceMock(ActionManager.class); ClusterRequest clusterRequest = createNiceMock(ClusterRequest.class); Set<ClusterRequest> setRequests = Collections.singleton(clusterRequest); KerberosHelper kerberosHelper = createStrictMock(KerberosHelper.class); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null); expect(injector.getInstance(KerberosHelper.class)).andReturn(kerberosHelper); expect(clusterRequest.getClusterName()).andReturn("clusterNew").times(3); expect(clusterRequest.getClusterId()).andReturn(1L).times(6); expect(clusters.getClusterById(1L)).andReturn(cluster).times(2); expect(cluster.getClusterName()).andReturn("clusterOld").times(1); cluster.addSessionAttributes(EasyMock.<Map<String, Object>>anyObject()); expectLastCall().once(); cluster.setClusterName("clusterNew"); expectLastCall(); replay(actionManager, cluster, clusters, injector, clusterRequest, sessionManager); AmbariManagementController controller = new AmbariManagementControllerImpl(actionManager, clusters, injector); controller.updateClusters(setRequests, null); assertSame(controller, controllerCapture.getValue()); verify(actionManager, cluster, clusters, injector, clusterRequest, sessionManager); }
@Test @Ignore public void testUpdateClustersWithNullConfigPropertyValues() throws Exception { Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); Injector injector = createStrictMock(Injector.class); Cluster cluster = createNiceMock(Cluster.class); ActionManager actionManager = createNiceMock(ActionManager.class); ClusterRequest clusterRequest = createNiceMock(ClusterRequest.class); Config config = createNiceMock(Config.class); Set<ClusterRequest> setRequests = Collections.singleton(clusterRequest); KerberosHelper kerberosHelper = createStrictMock(KerberosHelper.class); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null); expect(injector.getInstance(KerberosHelper.class)).andReturn(kerberosHelper); expect(clusterRequest.getClusterName()).andReturn("clusterNew").anyTimes(); expect(clusterRequest.getClusterId()).andReturn(1L).anyTimes(); ConfigurationRequest configReq = new ConfigurationRequest(); final Map<String, String> configReqProps = Maps.newHashMap(); configReqProps.put("p1", null); configReq.setProperties(configReqProps); expect(clusterRequest.getDesiredConfig()).andReturn(ImmutableList.of(configReq)).anyTimes(); expect(clusters.getClusterById(1L)).andReturn(cluster).anyTimes(); expect(cluster.getClusterName()).andReturn("clusterOld").anyTimes(); expect(cluster.getConfigPropertiesTypes(anyObject(String.class))).andReturn(Maps.<PropertyInfo.PropertyType, Set<String>>newHashMap()).anyTimes(); expect(config.getType()).andReturn("config-type").anyTimes(); expect(config.getProperties()).andReturn(configReqProps).anyTimes(); expect(config.getPropertiesAttributes()).andReturn(new HashMap<String,Map<String,String>>()).anyTimes(); expect(cluster.getDesiredConfigByType(anyObject(String.class))).andReturn(config).anyTimes(); cluster.addSessionAttributes(EasyMock.<Map<String, Object>>anyObject()); expectLastCall().once(); cluster.setClusterName("clusterNew"); expectLastCall(); replay(actionManager, cluster, clusters, config, injector, clusterRequest, sessionManager); AmbariManagementController controller = new AmbariManagementControllerImpl(actionManager, clusters, injector); controller.updateClusters(setRequests, null); assertSame(controller, controllerCapture.getValue()); verify(actionManager, cluster, clusters, config, injector, clusterRequest, sessionManager); }
@Test public void testUpdateClustersToggleKerberosNotInvoked() throws Exception { Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); Injector injector = createStrictMock(Injector.class); Cluster cluster = createNiceMock(Cluster.class); ActionManager actionManager = createNiceMock(ActionManager.class); ClusterRequest clusterRequest = createNiceMock(ClusterRequest.class); Set<ClusterRequest> setRequests = Collections.singleton(clusterRequest); KerberosHelper kerberosHelper = createStrictMock(KerberosHelper.class); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null); expect(injector.getInstance(KerberosHelper.class)).andReturn(kerberosHelper); expect(clusterRequest.getClusterId()).andReturn(1L).times(6); expect(clusters.getClusterById(1L)).andReturn(cluster).times(2); expect(cluster.getClusterName()).andReturn("cluster").times(1); cluster.addSessionAttributes(EasyMock.<Map<String, Object>>anyObject()); expectLastCall().once(); replay(actionManager, cluster, clusters, injector, clusterRequest, sessionManager, kerberosHelper); AmbariManagementController controller = new AmbariManagementControllerImpl(actionManager, clusters, injector); controller.updateClusters(setRequests, null); assertSame(controller, controllerCapture.getValue()); verify(actionManager, cluster, clusters, injector, clusterRequest, sessionManager, kerberosHelper); }
@Test public void testUpdateClustersToggleKerberosReenable() throws Exception { Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); Injector injector = createStrictMock(Injector.class); Cluster cluster = createNiceMock(Cluster.class); ActionManager actionManager = createNiceMock(ActionManager.class); ClusterRequest clusterRequest = createNiceMock(ClusterRequest.class); Set<ClusterRequest> setRequests = Collections.singleton(clusterRequest); KerberosHelper kerberosHelper = createStrictMock(KerberosHelper.class); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null); expect(injector.getInstance(KerberosHelper.class)).andReturn(kerberosHelper); expect(clusterRequest.getClusterId()).andReturn(1L).times(6); expect(clusterRequest.getSecurityType()).andReturn(SecurityType.KERBEROS).anyTimes(); expect(clusters.getClusterById(1L)).andReturn(cluster).times(2); expect(cluster.getClusterName()).andReturn("cluster").times(1); expect(cluster.getSecurityType()).andReturn(SecurityType.KERBEROS).anyTimes(); cluster.addSessionAttributes(EasyMock.<Map<String, Object>>anyObject()); expectLastCall().once(); expect(kerberosHelper.shouldExecuteCustomOperations(SecurityType.KERBEROS, null)) .andReturn(false) .once(); expect(kerberosHelper.getForceToggleKerberosDirective(EasyMock.<Map<String, String>>anyObject())) .andReturn(false) .once(); replay(actionManager, cluster, clusters, injector, clusterRequest, sessionManager, kerberosHelper); AmbariManagementController controller = new AmbariManagementControllerImpl(actionManager, clusters, injector); controller.updateClusters(setRequests, null); assertSame(controller, controllerCapture.getValue()); verify(actionManager, cluster, clusters, injector, clusterRequest, sessionManager, kerberosHelper); }
@Test public void testUpdateClustersToggleKerberosEnable() throws Exception { Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); Injector injector = createStrictMock(Injector.class); Cluster cluster = createNiceMock(Cluster.class); ActionManager actionManager = createNiceMock(ActionManager.class); ClusterRequest clusterRequest = createNiceMock(ClusterRequest.class); Set<ClusterRequest> setRequests = Collections.singleton(clusterRequest); KerberosHelper kerberosHelper = createStrictMock(KerberosHelper.class); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null); expect(injector.getInstance(KerberosHelper.class)).andReturn(kerberosHelper); expect(clusterRequest.getClusterId()).andReturn(1L).times(6); expect(clusterRequest.getSecurityType()).andReturn(SecurityType.KERBEROS).anyTimes(); expect(clusters.getClusterById(1L)).andReturn(cluster).times(2); expect(cluster.getClusterName()).andReturn("cluster").times(1); expect(cluster.getSecurityType()).andReturn(SecurityType.NONE).anyTimes(); cluster.addSessionAttributes(EasyMock.<Map<String, Object>>anyObject()); expectLastCall().once(); expect(kerberosHelper.shouldExecuteCustomOperations(SecurityType.KERBEROS, null)) .andReturn(false) .once(); expect(kerberosHelper.getForceToggleKerberosDirective(null)) .andReturn(false) .once(); expect(kerberosHelper.getManageIdentitiesDirective(null)) .andReturn(null) .once(); expect(kerberosHelper.toggleKerberos(anyObject(Cluster.class), anyObject(SecurityType.class), anyObject(RequestStageContainer.class), anyBoolean())) .andReturn(null) .once(); replay(actionManager, cluster, clusters, injector, clusterRequest, sessionManager, kerberosHelper); AmbariManagementController controller = new AmbariManagementControllerImpl(actionManager, clusters, injector); controller.updateClusters(setRequests, null); assertSame(controller, controllerCapture.getValue()); verify(actionManager, cluster, clusters, injector, clusterRequest, sessionManager, kerberosHelper); }
@Test public void testUpdateClustersToggleKerberos_Fail() throws Exception { Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); Injector injector = createStrictMock(Injector.class); Cluster cluster = createMock(Cluster.class); ActionManager actionManager = createNiceMock(ActionManager.class); ClusterRequest clusterRequest = createNiceMock(ClusterRequest.class); Set<ClusterRequest> setRequests = Collections.singleton(clusterRequest); KerberosHelper kerberosHelper = createStrictMock(KerberosHelper.class); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null); expect(injector.getInstance(KerberosHelper.class)).andReturn(kerberosHelper); expect(clusterRequest.getClusterId()).andReturn(1L).times(6); expect(clusterRequest.getSecurityType()).andReturn(SecurityType.NONE).anyTimes(); expect(clusters.getClusterById(1L)).andReturn(cluster).times(2); expect(cluster.getResourceId()).andReturn(1L).times(3); expect(cluster.getClusterName()).andReturn("cluster").times(1); expect(cluster.getSecurityType()).andReturn(SecurityType.KERBEROS).anyTimes(); expect(cluster.getCurrentClusterVersion()).andReturn(null).anyTimes(); expect(cluster.getCurrentStackVersion()).andReturn(null).anyTimes(); expect(cluster.getDesiredStackVersion()).andReturn(null).anyTimes(); cluster.setCurrentStackVersion(anyObject(StackId.class)); expectLastCall().once(); cluster.setClusterName(anyObject(String.class)); expectLastCall().once(); cluster.addSessionAttributes(EasyMock.<Map<String, Object>>anyObject()); expectLastCall().once(); expect(kerberosHelper.shouldExecuteCustomOperations(SecurityType.NONE, null)) .andReturn(false) .once(); expect(kerberosHelper.getForceToggleKerberosDirective(EasyMock.<Map<String, String>>anyObject())) .andReturn(false) .once(); expect(kerberosHelper.getManageIdentitiesDirective(EasyMock.<Map<String, String>>anyObject())) .andReturn(null) .once(); expect(kerberosHelper.toggleKerberos(anyObject(Cluster.class), anyObject(SecurityType.class), anyObject(RequestStageContainer.class), anyBoolean())) .andThrow(new IllegalArgumentException("bad args!")) .once(); replay(actionManager, cluster, clusters, injector, clusterRequest, sessionManager, kerberosHelper); AmbariManagementController controller = new AmbariManagementControllerImpl(actionManager, clusters, injector); try { controller.updateClusters(setRequests, null); Assert.fail("IllegalArgumentException not thrown"); } catch (IllegalArgumentException e) { } assertSame(controller, controllerCapture.getValue()); verify(actionManager, cluster, clusters, injector, clusterRequest, sessionManager, kerberosHelper); }
@Test public void testUpdateClusters__RollbackException() throws Exception { Capture<AmbariManagementController> controllerCapture = EasyMock.newCapture(); Injector injector = createStrictMock(Injector.class); Cluster cluster = createNiceMock(Cluster.class); ActionManager actionManager = createNiceMock(ActionManager.class); ClusterRequest clusterRequest = createNiceMock(ClusterRequest.class); Set<ClusterRequest> setRequests = Collections.singleton(clusterRequest); injector.injectMembers(capture(controllerCapture)); expect(injector.getInstance(Gson.class)).andReturn(null); expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null); expect(injector.getInstance(KerberosHelper.class)).andReturn(createNiceMock(KerberosHelper.class)); expect(clusterRequest.getClusterName()).andReturn("clusterNew").times(3); expect(clusterRequest.getClusterId()).andReturn(1L).times(6); expect(clusters.getClusterById(1L)).andReturn(cluster).times(2); expect(cluster.getClusterName()).andReturn("clusterOld").times(1); cluster.setClusterName("clusterNew"); expectLastCall().andThrow(new RollbackException()); replay(actionManager, cluster, clusters, injector, clusterRequest, sessionManager); AmbariManagementController controller = new AmbariManagementControllerImpl(actionManager, clusters, injector); try { controller.updateClusters(setRequests, null); fail("Expected RollbackException"); } catch (RollbackException e) { } assertSame(controller, controllerCapture.getValue()); verify(actionManager, cluster, clusters, injector, clusterRequest, sessionManager); }