use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class DefaultDispatchTest method testCallToNonSecureClusterWithoutDelegationToken.
@Test
public void testCallToNonSecureClusterWithoutDelegationToken() throws URISyntaxException, IOException {
DefaultDispatch defaultDispatch = new DefaultDispatch();
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(gatewayConfig.isHadoopKerberosSecured()).andReturn(false).anyTimes();
EasyMock.expect(servletContext.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).anyTimes();
ServletInputStream inputStream = EasyMock.createNiceMock(ServletInputStream.class);
HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(inboundRequest.getInputStream()).andReturn(inputStream).anyTimes();
EasyMock.expect(inboundRequest.getQueryString()).andReturn("a=123").anyTimes();
EasyMock.expect(inboundRequest.getServletContext()).andReturn(servletContext).anyTimes();
EasyMock.replay(gatewayConfig, servletContext, inboundRequest);
HttpEntity httpEntity = defaultDispatch.createRequestEntity(inboundRequest);
assertFalse("buffering in non secure cluster", (httpEntity instanceof PartiallyRepeatableHttpEntity));
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class DefaultDispatchTest method testCallToSecureClusterWithoutDelegationToken.
@Test
public void testCallToSecureClusterWithoutDelegationToken() throws URISyntaxException, IOException {
DefaultDispatch defaultDispatch = new DefaultDispatch();
defaultDispatch.setReplayBufferSize(10);
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(gatewayConfig.isHadoopKerberosSecured()).andReturn(Boolean.TRUE).anyTimes();
EasyMock.expect(servletContext.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).anyTimes();
ServletInputStream inputStream = EasyMock.createNiceMock(ServletInputStream.class);
HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(inboundRequest.getQueryString()).andReturn("a=123").anyTimes();
EasyMock.expect(inboundRequest.getInputStream()).andReturn(inputStream).anyTimes();
EasyMock.expect(inboundRequest.getServletContext()).andReturn(servletContext).anyTimes();
EasyMock.replay(gatewayConfig, servletContext, inboundRequest);
HttpEntity httpEntity = defaultDispatch.createRequestEntity(inboundRequest);
assertTrue("not buffering in the absence of delegation token", (httpEntity instanceof PartiallyRepeatableHttpEntity));
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class AmbariConfigurationMonitorTest method testPollingMonitor.
@Test
public void testPollingMonitor() throws Exception {
final String addr1 = "http://host1:8080";
final String addr2 = "http://host2:8080";
final String cluster1Name = "Cluster_One";
final String cluster2Name = "Cluster_Two";
GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(config.getGatewayDataDir()).andReturn(dataDir.getAbsolutePath()).anyTimes();
EasyMock.expect(config.getClusterMonitorPollingInterval(AmbariConfigurationMonitor.getType())).andReturn(10).anyTimes();
EasyMock.replay(config);
// Create the monitor
TestableAmbariConfigurationMonitor monitor = new TestableAmbariConfigurationMonitor(config);
// Clear the system property now that the monitor has been initialized
System.clearProperty(AmbariConfigurationMonitor.INTERVAL_PROPERTY_NAME);
// Sequence of config changes for testing monitoring for updates
Map<String, Map<String, List<List<AmbariCluster.ServiceConfiguration>>>> updateConfigurations = new HashMap<>();
updateConfigurations.put(addr1, new HashMap<>());
updateConfigurations.get(addr1).put(cluster1Name, Arrays.asList(Arrays.asList(createTestServiceConfig("zoo.cfg", "3"), createTestServiceConfig("hive-site", "2")), Arrays.asList(createTestServiceConfig("zoo.cfg", "3"), createTestServiceConfig("hive-site", "3")), Arrays.asList(createTestServiceConfig("zoo.cfg", "2"), createTestServiceConfig("hive-site", "1"))));
updateConfigurations.put(addr2, new HashMap<>());
updateConfigurations.get(addr2).put(cluster2Name, Arrays.asList(Arrays.asList(createTestServiceConfig("zoo.cfg", "1"), createTestServiceConfig("hive-site", "1")), Collections.singletonList(createTestServiceConfig("zoo.cfg", "1")), Arrays.asList(createTestServiceConfig("zoo.cfg", "1"), createTestServiceConfig("hive-site", "2"))));
updateConfigurations.get(addr2).put(cluster1Name, Arrays.asList(Arrays.asList(createTestServiceConfig("zoo.cfg", "2"), createTestServiceConfig("hive-site", "4")), Arrays.asList(createTestServiceConfig("zoo.cfg", "3"), createTestServiceConfig("hive-site", "4"), createTestServiceConfig("yarn-site", "1")), Arrays.asList(createTestServiceConfig("zoo.cfg", "1"), createTestServiceConfig("hive-site", "2"))));
Map<String, Map<String, Integer>> configChangeIndex = new HashMap<>();
configChangeIndex.put(addr1, new HashMap<>());
configChangeIndex.get(addr1).put(cluster1Name, 0);
configChangeIndex.get(addr1).put(cluster2Name, 0);
configChangeIndex.put(addr2, new HashMap<>());
configChangeIndex.get(addr2).put(cluster2Name, 0);
// Setup the initial test update data
// Cluster 1 data change
monitor.addTestConfigVersion(addr1, cluster1Name, "zoo.cfg", "2");
monitor.addTestConfigVersion(addr1, cluster1Name, "hive-site", "1");
// Cluster 2 NO data change
monitor.addTestConfigVersion(addr2, cluster1Name, "zoo.cfg", "1");
monitor.addTestConfigVersion(addr2, cluster1Name, "hive-site", "1");
// Cluster 3 data change
monitor.addTestConfigVersion(addr2, cluster2Name, "zoo.cfg", "1");
monitor.addTestConfigVersion(addr2, cluster2Name, "hive-site", "2");
Map<String, Map<String, AmbariCluster.ServiceConfiguration>> initialAmbariClusterConfigs = new HashMap<>();
Map<String, AmbariCluster.ServiceConfiguration> cluster1Configs = new HashMap<>();
AmbariCluster.ServiceConfiguration zooCfg = createTestServiceConfig("zoo.cfg", "1");
cluster1Configs.put("ZOOKEEPER", zooCfg);
AmbariCluster.ServiceConfiguration hiveSite = createTestServiceConfig("hive-site", "1");
cluster1Configs.put("Hive", hiveSite);
initialAmbariClusterConfigs.put(cluster1Name, cluster1Configs);
AmbariCluster cluster1 = createTestCluster(cluster1Name, initialAmbariClusterConfigs);
// Tell the monitor about the cluster configurations
monitor.addClusterConfigVersions(cluster1, createTestDiscoveryConfig(addr1));
monitor.addClusterConfigVersions(createTestCluster(cluster2Name, initialAmbariClusterConfigs), createTestDiscoveryConfig(addr2));
monitor.addClusterConfigVersions(createTestCluster(cluster1Name, initialAmbariClusterConfigs), createTestDiscoveryConfig(addr2));
final Map<String, Map<String, Integer>> changeNotifications = new HashMap<>();
monitor.addListener((src, cname) -> {
// System.out.println("Cluster config changed: " + cname + " @ " + src);
// Record the notification
Integer notificationCount = changeNotifications.computeIfAbsent(src, s -> new HashMap<>()).computeIfAbsent(cname, c -> Integer.valueOf(0));
changeNotifications.get(src).put(cname, (notificationCount += 1));
// Update the config version
int changeIndex = configChangeIndex.get(src).get(cname);
if (changeIndex < updateConfigurations.get(src).get(cname).size()) {
List<AmbariCluster.ServiceConfiguration> changes = updateConfigurations.get(src).get(cname).get(changeIndex);
// System.out.println("Applying config update " + changeIndex + " to " + cname + " @ " + src + " ...");
for (AmbariCluster.ServiceConfiguration change : changes) {
monitor.updateConfigState(src, cname, change.getType(), change.getVersion());
// System.out.println(" Updated " + change.getType() + " to version " + change.getVersion());
}
// Increment the change index
configChangeIndex.get(src).replace(cname, changeIndex + 1);
// System.out.println("Monitor config updated for " + cname + " @ " + src + " : " + changeIndex );
}
});
try {
monitor.start();
long expiration = System.currentTimeMillis() + (1000 * 30);
while (!areChangeUpdatesExhausted(updateConfigurations, configChangeIndex) && (System.currentTimeMillis() < expiration)) {
try {
Thread.sleep(5);
} catch (InterruptedException e) {
//
}
}
} finally {
monitor.stop();
}
assertNotNull("Expected changes to have been reported for source 1.", changeNotifications.get(addr1));
assertEquals("Expected changes to have been reported.", 3, changeNotifications.get(addr1).get(cluster1Name).intValue());
assertNotNull("Expected changes to have been reported for source 2.", changeNotifications.get(addr2));
assertEquals("Expected changes to have been reported.", 3, changeNotifications.get(addr2).get(cluster2Name).intValue());
assertNull("Expected changes to have been reported.", changeNotifications.get(addr2).get(cluster1Name));
// Verify the cache clearing behavior
Map<String, Map<String, String>> src2ClustersData = monitor.ambariClusterConfigVersions.get(addr2);
assertTrue("Expected data for this cluster.", src2ClustersData.containsKey(cluster1Name));
assertTrue("Expected data for this cluster.", src2ClustersData.containsKey(cluster2Name));
// Clear the cache for this source
monitor.clearCache(addr2, cluster1Name);
assertFalse("Expected NO data for this cluster.", src2ClustersData.containsKey(cluster1Name));
assertTrue("Expected data for this cluster.", src2ClustersData.containsKey(cluster2Name));
// Make sure the cache for the other source is unaffected
Map<String, Map<String, String>> src1ClustersData = monitor.ambariClusterConfigVersions.get(addr1);
assertTrue("Expected data for this cluster.", src1ClustersData.containsKey(cluster1Name));
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class AmbariServiceDiscoveryTest method testClusterDiscoveryWithExternalComponentConfigAugmentation.
@Test
public void testClusterDiscoveryWithExternalComponentConfigAugmentation() throws Exception {
final String discoveryAddress = "http://ambarihost:8080";
final String clusterName = "myCluster";
GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.replay(gc);
// Create component config mapping override
Properties compConfOverrideProps = new Properties();
compConfOverrideProps.setProperty("DISCOVERY_TEST", "test-site");
File compConfOverrides = File.createTempFile(getClass().getName() + "component-conf-overrides", ".properties");
compConfOverrideProps.store(new FileOutputStream(compConfOverrides), "Test Config Overrides");
System.setProperty(AmbariServiceDiscovery.COMPONENT_CONFIG_MAPPING_SYSTEM_PROPERTY, compConfOverrides.getAbsolutePath());
// Create URL mapping override
final String URL_MAPPING_OVERRIDES = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<service-discovery-url-mappings>\n" + " <service name=\"DISCOVERYTEST\">\n" + " <url-pattern>{TEST_ADDRESS}/discoveryTest</url-pattern>\n" + " <properties>\n" + " <property name=\"TEST_ADDRESS\">\n" + " <component>DISCOVERY_TEST</component>\n" + " <config-property>discovery.test.base.url</config-property>\n" + " </property>\n" + " </properties>\n" + " </service>\n" + "</service-discovery-url-mappings>\n";
File urlMappingOverrides = File.createTempFile(getClass().getName() + "_url-overrides", ".xml");
FileUtils.writeStringToFile(urlMappingOverrides, URL_MAPPING_OVERRIDES, java.nio.charset.Charset.forName("utf-8"));
System.setProperty(AmbariDynamicServiceURLCreator.MAPPING_CONFIG_OVERRIDE_PROPERTY, urlMappingOverrides.getAbsolutePath());
// Re-initialize the component config mappings to include the extension
AmbariServiceDiscovery.initializeComponentConfigMappings();
ServiceDiscovery sd = new TestAmbariServiceDiscovery(clusterName);
ServiceDiscoveryConfig sdc = EasyMock.createNiceMock(ServiceDiscoveryConfig.class);
EasyMock.expect(sdc.getAddress()).andReturn(discoveryAddress).anyTimes();
EasyMock.expect(sdc.getUser()).andReturn(null).anyTimes();
EasyMock.replay(sdc);
try {
ServiceDiscovery.Cluster cluster = sd.discover(gc, sdc, clusterName);
assertNotNull(cluster);
assertEquals(clusterName, cluster.getName());
assertTrue(AmbariCluster.class.isAssignableFrom(cluster.getClass()));
assertEquals(7, ((AmbariCluster) cluster).getComponents().size());
List<String> discTestURLs = cluster.getServiceURLs("DISCOVERYTEST");
assertNotNull(discTestURLs);
assertEquals(1, discTestURLs.size());
assertEquals("http://c6402.ambari.apache.org:11999/discoveryTest", discTestURLs.get(0));
} finally {
System.clearProperty(AmbariDynamicServiceURLCreator.MAPPING_CONFIG_OVERRIDE_PROPERTY);
System.clearProperty(AmbariServiceDiscovery.COMPONENT_CONFIG_MAPPING_SYSTEM_PROPERTY);
FileUtils.deleteQuietly(compConfOverrides);
// Re-initialize the component config mappings without the extension
AmbariServiceDiscovery.initializeComponentConfigMappings();
}
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class AmbariServiceDiscoveryTest method testSingleClusterDiscovery.
@Test
public void testSingleClusterDiscovery() throws Exception {
final String discoveryAddress = "http://ambarihost:8080";
final String clusterName = "testCluster";
ServiceDiscovery sd = new TestAmbariServiceDiscovery(clusterName);
GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.replay(gc);
ServiceDiscoveryConfig sdc = EasyMock.createNiceMock(ServiceDiscoveryConfig.class);
EasyMock.expect(sdc.getAddress()).andReturn(discoveryAddress).anyTimes();
EasyMock.expect(sdc.getUser()).andReturn(null).anyTimes();
EasyMock.replay(sdc);
ServiceDiscovery.Cluster cluster = sd.discover(gc, sdc, clusterName);
assertNotNull(cluster);
assertEquals(clusterName, cluster.getName());
assertTrue(AmbariCluster.class.isAssignableFrom(cluster.getClass()));
assertEquals(6, ((AmbariCluster) cluster).getComponents().size());
// printServiceURLs(cluster);
}
Aggregations