UpgradeCatalog210 extends AbstractUpgradeCatalog { protected void updateHiveConfigs() throws AmbariException { AmbariManagementController ambariManagementController = injector.getInstance( AmbariManagementController.class); Clusters clusters = ambariManagementController.getClusters(); if (clusters != null) { Map<String, Cluster> clusterMap = clusters.getClusters(); if (clusterMap != null && !clusterMap.isEmpty()) { for (final Cluster cluster : clusterMap.values()) { String content = null; String hive_server2_auth = ""; if (cluster.getDesiredConfigByType("hive-site") != null && cluster.getDesiredConfigByType("hive-site").getProperties().containsKey("hive.server2.authentication")) { hive_server2_auth = cluster.getDesiredConfigByType("hive-site").getProperties().get("hive.server2.authentication"); } if(cluster.getDesiredConfigByType("hive-env") != null) { Map<String, String> hiveEnvProps = new HashMap<String, String>(); Set<String> hiveServerSiteRemoveProps = new HashSet<String>(); content = cluster.getDesiredConfigByType("hive-env").getProperties().get("content"); if(content != null) { content = updateHiveEnvContent(content); hiveEnvProps.put("content", content); } if (!cluster.getDesiredConfigByType("hive-env").getProperties().containsKey("hive.client.heapsize")) { hiveEnvProps.put("hive.client.heapsize", "512"); } if (!cluster.getDesiredConfigByType("hive-env").getProperties().containsKey("hive.metastore.heapsize")) { hiveEnvProps.put("hive.metastore.heapsize", "1024"); } boolean isHiveSecurityAuthPresent = cluster.getDesiredConfigByType("hive-env").getProperties().containsKey("hive_security_authorization"); String hiveSecurityAuth=""; if ("kerberos".equalsIgnoreCase(hive_server2_auth) && cluster.getServices().containsKey("KERBEROS")){ hiveSecurityAuth = "SQLStdAuth"; isHiveSecurityAuthPresent = true; hiveEnvProps.put("hive_security_authorization", hiveSecurityAuth); } else { if (isHiveSecurityAuthPresent) { hiveSecurityAuth = cluster.getDesiredConfigByType("hive-env").getProperties().get("hive_security_authorization"); } } if (isHiveSecurityAuthPresent && "none".equalsIgnoreCase(hiveSecurityAuth) && !isConfigEnabled(cluster, AbstractUpgradeCatalog.CONFIGURATION_TYPE_RANGER_HIVE_PLUGIN_PROPERTIES, AbstractUpgradeCatalog.PROPERTY_RANGER_HIVE_PLUGIN_ENABLED)) { hiveServerSiteRemoveProps.add("hive.security.authorization.manager"); hiveServerSiteRemoveProps.add("hive.security.authenticator.manager"); } updateConfigurationPropertiesForCluster(cluster, "hive-env", hiveEnvProps, true, true); removeConfigurationPropertiesFromCluster(cluster, "hiveserver2-site", hiveServerSiteRemoveProps); } if(cluster.getDesiredConfigByType("hive-site") != null) { Set<String> hiveSiteRemoveProps = new HashSet<String>(); Map<String, String> hiveSiteAddProps = new HashMap<String, String>(); if (!"pam".equalsIgnoreCase(hive_server2_auth)) { hiveSiteRemoveProps.add("hive.server2.authentication.pam.services"); } else { hiveSiteAddProps.put("hive.server2.authentication.pam.services", ""); } if (!"custom".equalsIgnoreCase(hive_server2_auth)) { hiveSiteRemoveProps.add("hive.server2.custom.authentication.class"); } else { hiveSiteAddProps.put("hive.server2.custom.authentication.class", ""); } if (!"ldap".equalsIgnoreCase(hive_server2_auth)) { hiveSiteRemoveProps.add("hive.server2.authentication.ldap.url"); } else { hiveSiteAddProps.put("hive.server2.authentication.ldap.url", ""); } if (!"kerberos".equalsIgnoreCase(hive_server2_auth) && !cluster.getServices().containsKey("KERBEROS")) { hiveSiteRemoveProps.add("hive.server2.authentication.kerberos.keytab"); hiveSiteRemoveProps.add("hive.server2.authentication.kerberos.principal"); } else { hiveSiteAddProps.put("hive.server2.authentication.kerberos.keytab", ""); hiveSiteAddProps.put("hive.server2.authentication.kerberos.principal", ""); } updateConfigurationPropertiesForCluster(cluster, "hive-site", hiveSiteAddProps, hiveSiteRemoveProps, false, true); } } } } } @Inject  UpgradeCatalog210(Injector injector); @Override String getSourceVersion(); @Override String getTargetVersion();  }
@Test public void testUpdateHiveConfigs() throws Exception { EasyMockSupport easyMockSupport = new EasyMockSupport(); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class); final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class); final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class); final ServiceConfigVersionResponse mockServiceConfigVersionResponse = easyMockSupport.createNiceMock(ServiceConfigVersionResponse.class); final Config mockHiveEnv = easyMockSupport.createNiceMock(Config.class); final Config mockHiveServerSite = easyMockSupport.createNiceMock(Config.class); final Config mockHiveSite = easyMockSupport.createNiceMock(Config.class); final Map<String, String> propertiesExpectedHiveEnv = new HashMap<String, String>() {{ put("hive_security_authorization", "none"); }}; final Map<String, String> propertiesExpectedHiveSite = new HashMap<String, String>() {{ put("hive.server2.authentication", "pam"); put("hive.server2.custom.authentication.class", ""); }}; final Map<String, String> propertiesExpectedHiveServerSite = new HashMap<String, String>() {{ put("hive.security.authorization.manager", ""); put("hive.security.authenticator.manager", ""); }}; final Map<String, Service> servicesExpected = new HashMap<String, Service>(); final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AmbariManagementController.class).toInstance(mockAmbariManagementController); bind(ConfigHelper.class).toInstance(mockConfigHelper); bind(Clusters.class).toInstance(mockClusters); bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class)); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); } }); expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{ put("normal", mockClusterExpected); }}).once(); Capture<String> configTypeEnv = EasyMock.newCapture(); Capture<String> configTypeSite = EasyMock.newCapture(); Capture<String> configTypeServerSite = EasyMock.newCapture(); expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnv).atLeastOnce(); expect(mockClusterExpected.getDesiredConfigByType("hiveserver2-site")).andReturn(mockHiveServerSite).atLeastOnce(); expect(mockHiveEnv.getProperties()).andReturn(propertiesExpectedHiveEnv).anyTimes(); expect(mockHiveServerSite.getProperties()).andReturn(propertiesExpectedHiveServerSite).anyTimes(); expect(mockClusterExpected.getConfig(capture(configTypeEnv), anyObject(String.class))).andReturn(mockHiveEnv).once(); expect(mockClusterExpected.getConfig(capture(configTypeServerSite), anyObject(String.class))).andReturn(mockHiveServerSite).once(); expect(mockClusterExpected.addDesiredConfig("ambari-upgrade", Collections.singleton(mockHiveEnv), "Updated hive-env during Ambari Upgrade from 2.0.0 to 2.1.0.")).andReturn(mockServiceConfigVersionResponse).once(); expect(mockClusterExpected.addDesiredConfig("ambari-upgrade", Collections.singleton(mockHiveServerSite), "Updated hiveserver2-site during Ambari Upgrade from 2.0.0 to 2.1.0.")).andReturn(mockServiceConfigVersionResponse).once(); expect(mockClusterExpected.getDesiredConfigByType("hive-site")).andReturn(mockHiveSite).atLeastOnce(); expect(mockHiveSite.getProperties()).andReturn(propertiesExpectedHiveSite).anyTimes(); expect(mockClusterExpected.getServices()).andReturn(servicesExpected).once(); expect(mockClusterExpected.getConfig(capture(configTypeSite), anyObject(String.class))).andReturn(mockHiveSite).once(); expect(mockClusterExpected.addDesiredConfig("ambari-upgrade", Collections.singleton(mockHiveSite), "Updated hive-site during Ambari Upgrade from 2.0.0 to 2.1.0.")).andReturn(mockServiceConfigVersionResponse).once(); easyMockSupport.replayAll(); mockInjector.getInstance(UpgradeCatalog210.class).updateHiveConfigs(); easyMockSupport.verifyAll(); assertEquals("hive-env", configTypeEnv.getValue()); assertEquals("hive-site", configTypeSite.getValue()); assertEquals("hiveserver2-site", configTypeServerSite.getValue()); }
@Test public void testUpdateHiveConfigsWithKerberos() throws Exception { EasyMockSupport easyMockSupport = new EasyMockSupport(); final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class); final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class); final Config mockHiveEnv = easyMockSupport.createNiceMock(Config.class); final Config mockHiveSite = easyMockSupport.createNiceMock(Config.class); final Config mockHiveServerSite = easyMockSupport.createNiceMock(Config.class); final Map<String, String> propertiesExpectedHiveEnv = new HashMap<String, String>(); final Map<String, String> propertiesExpectedHiveSite = new HashMap<String, String>() {{ put("hive.server2.authentication", "kerberos"); }}; final Map<String, String> propertiesExpectedHiveServerSite = new HashMap<>(); final Map<String, Service> servicesExpected = new HashMap<String, Service>(){{ put("KERBEROS", null); }}; final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AmbariManagementController.class).toInstance(mockAmbariManagementController); bind(ConfigHelper.class).toInstance(mockConfigHelper); bind(Clusters.class).toInstance(mockClusters); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class)); } }); final UpgradeCatalog210 upgradeCatalog210 = mockInjector.getInstance(UpgradeCatalog210.class); expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{ put("normal", mockClusterExpected); }}).once(); Capture<Map<String,String>> configCreation = Capture.newInstance(CaptureType.ALL); expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnv).atLeastOnce(); expect(mockClusterExpected.getDesiredConfigByType("hiveserver2-site")).andReturn(mockHiveServerSite).atLeastOnce(); expect(mockHiveEnv.getProperties()).andReturn(propertiesExpectedHiveEnv).anyTimes(); expect(mockHiveServerSite.getProperties()).andReturn(propertiesExpectedHiveServerSite).anyTimes(); expect(mockClusterExpected.getDesiredConfigByType("hive-site")).andReturn(mockHiveSite).atLeastOnce(); expect(mockHiveSite.getProperties()).andReturn(propertiesExpectedHiveSite).anyTimes(); expect(mockClusterExpected.getServices()).andReturn(servicesExpected).atLeastOnce(); expect(mockAmbariManagementController.createConfig((Cluster)anyObject(), anyString(), capture(configCreation), anyString(), EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(null).atLeastOnce(); easyMockSupport.replayAll(); upgradeCatalog210.updateHiveConfigs(); easyMockSupport.verifyAll(); Assert.assertEquals(2, configCreation.getValues().size()); boolean hiveSecFound = false; for (Map<String, String> cfg: configCreation.getValues()){ if (cfg.containsKey("hive_security_authorization")) { hiveSecFound = true; Assert.assertTrue("sqlstdauth".equalsIgnoreCase(cfg.get("hive_security_authorization"))); break; } } Assert.assertTrue(hiveSecFound); }
@Test public void testUpdateHiveConfigsWithRangerPlugin() throws Exception { EasyMockSupport easyMockSupport = new EasyMockSupport(); final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class); final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class); final Config mockHiveEnv = easyMockSupport.createNiceMock(Config.class); final Config mockHiveSite = easyMockSupport.createNiceMock(Config.class); final Config mockHiveServerSite = easyMockSupport.createNiceMock(Config.class); final Config mockHivePluginProperies = easyMockSupport.createNiceMock(Config.class); final Map<String, String> propertiesExpectedHiveEnv = new HashMap<String, String>() {{ put("hive_security_authorization", "none"); }}; final Map<String, String> propertiesExpectedHiveSite = new HashMap<>(); final Map<String, String> propertiesExpectedPluginProperies = new HashMap<String, String>() {{ put("ranger-hive-plugin-enabled", "yes"); }}; final Map<String, String> propertiesExpectedHiveServerSite = new HashMap<String, String>() {{ put("hive.security.authorization.manager", "test"); put("hive.security.authenticator.manager", "test"); }}; final Map<String, Service> servicesExpected = new HashMap<String, Service>() {{ put("RANGER", null); }}; final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AmbariManagementController.class).toInstance(mockAmbariManagementController); bind(ConfigHelper.class).toInstance(mockConfigHelper); bind(Clusters.class).toInstance(mockClusters); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class)); } }); final UpgradeCatalog210 upgradeCatalog210 = mockInjector.getInstance(UpgradeCatalog210.class); expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{ put("normal", mockClusterExpected); }}).once(); Capture<Map<String, String>> configCreation = Capture.newInstance(CaptureType.ALL); expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnv).atLeastOnce(); expect(mockClusterExpected.getDesiredConfigByType("hiveserver2-site")).andReturn(mockHiveServerSite).atLeastOnce(); expect(mockHiveEnv.getProperties()).andReturn(propertiesExpectedHiveEnv).anyTimes(); expect(mockHiveServerSite.getProperties()).andReturn(propertiesExpectedHiveServerSite).anyTimes(); expect(mockClusterExpected.getDesiredConfigByType("ranger-hive-plugin-properties")).andReturn(mockHivePluginProperies).once(); expect(mockClusterExpected.getDesiredConfigByType("hive-site")).andReturn(mockHiveSite).atLeastOnce(); expect(mockHiveSite.getProperties()).andReturn(propertiesExpectedHiveSite).anyTimes(); expect(mockHivePluginProperies.getProperties()).andReturn(propertiesExpectedPluginProperies).anyTimes(); expect(mockClusterExpected.getServices()).andReturn(servicesExpected).atLeastOnce(); expect(mockAmbariManagementController.createConfig((Cluster) anyObject(), anyString(), capture(configCreation), anyString(), EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(null).atLeastOnce(); easyMockSupport.replayAll(); upgradeCatalog210.updateHiveConfigs(); easyMockSupport.verifyAll(); Assert.assertEquals(1, configCreation.getValues().size()); boolean result = false; for (Map<String, String> cfg : configCreation.getValues()) { if (cfg.containsKey("hive.security.authorization.manager")) { result = true; break; } } Assert.assertFalse(result); result = false; for (Map<String, String> cfg : configCreation.getValues()) { if (cfg.containsKey("hive.security.authenticator.manager")) { result = true; break; } } Assert.assertFalse(result); }