use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class DefaultTopologyServiceTest method testConfigurationCRUDAPI.
/**
* KNOX-1039
*/
@Test
public void testConfigurationCRUDAPI() throws Exception {
File dir = createDir();
File topologyDir = new File(dir, "topologies");
topologyDir.mkdirs();
File descriptorsDir = new File(dir, "descriptors");
descriptorsDir.mkdirs();
File sharedProvidersDir = new File(dir, "shared-providers");
sharedProvidersDir.mkdirs();
try {
TestTopologyListener topoListener = new TestTopologyListener();
FileAlterationMonitor monitor = new FileAlterationMonitor(Long.MAX_VALUE);
TopologyService ts = new DefaultTopologyService();
Map<String, String> c = new HashMap<>();
GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(config.getGatewayTopologyDir()).andReturn(topologyDir.getAbsolutePath()).anyTimes();
EasyMock.expect(config.getGatewayConfDir()).andReturn(descriptorsDir.getParentFile().getAbsolutePath()).anyTimes();
EasyMock.replay(config);
ts.init(config, c);
ts.addTopologyChangeListener(topoListener);
ts.reloadTopologies();
java.lang.reflect.Field dmField = ts.getClass().getDeclaredField("descriptorsMonitor");
dmField.setAccessible(true);
DefaultTopologyService.DescriptorsMonitor dm = (DefaultTopologyService.DescriptorsMonitor) dmField.get(ts);
final String simpleDescName = "six.json";
final String provConfOne = "provider-config-one.xml";
final String provConfTwo = "ambari-cluster-policy.xml";
// "Deploy" the referenced provider configs first
boolean isDeployed = ts.deployProviderConfiguration(provConfOne, FileUtils.readFileToString(new File(ClassLoader.getSystemResource("org/apache/knox/gateway/topology/file/provider-config-one.xml").toURI())));
assertTrue(isDeployed);
File provConfOneFile = new File(sharedProvidersDir, provConfOne);
assertTrue(provConfOneFile.exists());
isDeployed = ts.deployProviderConfiguration(provConfTwo, FileUtils.readFileToString(new File(ClassLoader.getSystemResource("org/apache/knox/gateway/topology/file/ambari-cluster-policy.xml").toURI())));
assertTrue(isDeployed);
File provConfTwoFile = new File(sharedProvidersDir, provConfTwo);
assertTrue(provConfTwoFile.exists());
// Validate the provider configurations known by the topology service
Collection<File> providerConfigurations = ts.getProviderConfigurations();
assertNotNull(providerConfigurations);
assertEquals(2, providerConfigurations.size());
assertTrue(providerConfigurations.contains(provConfOneFile));
assertTrue(providerConfigurations.contains(provConfTwoFile));
// "Deploy" the simple descriptor, which depends on provConfOne
isDeployed = ts.deployDescriptor(simpleDescName, FileUtils.readFileToString(new File(ClassLoader.getSystemResource("org/apache/knox/gateway/topology/file/simple-descriptor-six.json").toURI())));
assertTrue(isDeployed);
File simpleDesc = new File(descriptorsDir, simpleDescName);
assertTrue(simpleDesc.exists());
// Validate the simple descriptors known by the topology service
Collection<File> descriptors = ts.getDescriptors();
assertNotNull(descriptors);
assertEquals(1, descriptors.size());
assertTrue(descriptors.contains(simpleDesc));
// "Notice" the simple descriptor, so the provider configuration dependency relationship is recorded
dm.onFileChange(simpleDesc);
// Attempt to delete the referenced provConfOne
assertFalse("Should not be able to delete a provider configuration that is referenced by one or more descriptors", ts.deleteProviderConfiguration(FilenameUtils.getBaseName(provConfOne)));
// Overwrite the simple descriptor with content that changes the provider config reference to provConfTwo
isDeployed = ts.deployDescriptor(simpleDescName, FileUtils.readFileToString(new File(ClassLoader.getSystemResource("org/apache/knox/gateway/topology/file/simple-descriptor-five.json").toURI())));
assertTrue(isDeployed);
assertTrue(simpleDesc.exists());
ts.getProviderConfigurations();
// "Notice" the simple descriptor, so the provider configuration dependency relationship is updated
dm.onFileChange(simpleDesc);
// Attempt to delete the referenced provConfOne
assertTrue("Should be able to delete the provider configuration, now that it's not referenced by any descriptors", ts.deleteProviderConfiguration(FilenameUtils.getBaseName(provConfOne)));
// Re-validate the provider configurations known by the topology service
providerConfigurations = ts.getProviderConfigurations();
assertNotNull(providerConfigurations);
assertEquals(1, providerConfigurations.size());
assertFalse(providerConfigurations.contains(provConfOneFile));
assertTrue(providerConfigurations.contains(provConfTwoFile));
// Attempt to delete the referenced provConfTwo
assertFalse("Should not be able to delete a provider configuration that is referenced by one or more descriptors", ts.deleteProviderConfiguration(FilenameUtils.getBaseName(provConfTwo)));
// Delete the referencing simple descriptor
assertTrue(ts.deleteDescriptor(FilenameUtils.getBaseName(simpleDescName)));
assertFalse(simpleDesc.exists());
// Re-validate the simple descriptors known by the topology service
descriptors = ts.getDescriptors();
assertNotNull(descriptors);
assertTrue(descriptors.isEmpty());
// "Notice" the simple descriptor, so the provider configuration dependency relationship is updated
dm.onFileDelete(simpleDesc);
// Attempt to delete the referenced provConfTwo
assertTrue("Should be able to delete the provider configuration, now that it's not referenced by any descriptors", ts.deleteProviderConfiguration(FilenameUtils.getBaseName(provConfTwo)));
// Re-validate the provider configurations known by the topology service
providerConfigurations = ts.getProviderConfigurations();
assertNotNull(providerConfigurations);
assertTrue(providerConfigurations.isEmpty());
} finally {
FileUtils.deleteQuietly(dir);
}
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class DefaultTopologyServiceTest method testTopologiesUpdateFromProviderConfigChange.
/**
* KNOX-1014
*
* Test the lifecycle relationship between provider configuration files, simple descriptors, and topology files.
*
* N.B. This test depends on the DummyServiceDiscovery extension being configured:
* org.apache.knox.gateway.topology.discovery.test.extension.DummyServiceDiscovery
*/
@Test
public void testTopologiesUpdateFromProviderConfigChange() throws Exception {
File dir = createDir();
File topologyDir = new File(dir, "topologies");
topologyDir.mkdirs();
File descriptorsDir = new File(dir, "descriptors");
descriptorsDir.mkdirs();
File sharedProvidersDir = new File(dir, "shared-providers");
sharedProvidersDir.mkdirs();
try {
TestTopologyListener topoListener = new TestTopologyListener();
FileAlterationMonitor monitor = new FileAlterationMonitor(Long.MAX_VALUE);
TopologyService ts = new DefaultTopologyService();
Map<String, String> c = new HashMap<>();
GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(config.getGatewayTopologyDir()).andReturn(topologyDir.getAbsolutePath()).anyTimes();
EasyMock.expect(config.getGatewayConfDir()).andReturn(descriptorsDir.getParentFile().getAbsolutePath()).anyTimes();
EasyMock.replay(config);
ts.init(config, c);
ts.addTopologyChangeListener(topoListener);
ts.reloadTopologies();
java.lang.reflect.Field dmField = ts.getClass().getDeclaredField("descriptorsMonitor");
dmField.setAccessible(true);
DefaultTopologyService.DescriptorsMonitor dm = (DefaultTopologyService.DescriptorsMonitor) dmField.get(ts);
// Write out the referenced provider configs first
createFile(sharedProvidersDir, "provider-config-one.xml", "org/apache/knox/gateway/topology/file/provider-config-one.xml", System.currentTimeMillis());
// Create the simple descriptor, which depends on provider-config-one.xml
File simpleDesc = createFile(descriptorsDir, "six.json", "org/apache/knox/gateway/topology/file/simple-descriptor-six.json", System.currentTimeMillis());
// "Notice" the simple descriptor change, and generate a topology based on it
dm.onFileChange(simpleDesc);
// Load the generated topology
ts.reloadTopologies();
Collection<Topology> topologies = ts.getTopologies();
assertThat(topologies.size(), is(1));
Iterator<Topology> iterator = topologies.iterator();
Topology topology = iterator.next();
assertFalse("The Shiro provider is disabled in provider-config-one.xml", topology.getProvider("authentication", "ShiroProvider").isEnabled());
// Overwrite the referenced provider configuration with a different ShiroProvider config, and check that the
// changes are propagated to the associated topology
File providerConfig = createFile(sharedProvidersDir, "provider-config-one.xml", "org/apache/knox/gateway/topology/file/ambari-cluster-policy.xml", System.currentTimeMillis());
// "Notice" the simple descriptor change as a result of the referenced config change
dm.onFileChange(simpleDesc);
// Load the generated topology
ts.reloadTopologies();
topologies = ts.getTopologies();
assertFalse(topologies.isEmpty());
topology = topologies.iterator().next();
assertTrue("The Shiro provider is enabled in ambari-cluster-policy.xml", topology.getProvider("authentication", "ShiroProvider").isEnabled());
// Delete the provider configuration, and make sure that the associated topology file is unaffected.
// The topology file should not be affected because the simple descriptor handling will fail to resolve the
// referenced provider configuration.
// Delete the file
providerConfig.delete();
// The provider config deletion will trigger a descriptor change notification
dm.onFileChange(simpleDesc);
ts.reloadTopologies();
topologies = ts.getTopologies();
assertFalse(topologies.isEmpty());
assertTrue("The Shiro provider is enabled in ambari-cluster-policy.xml", topology.getProvider("authentication", "ShiroProvider").isEnabled());
} finally {
FileUtils.deleteQuietly(dir);
}
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class PropertiesFileServiceDiscoveryTest method testPropertiesFileServiceDiscovery.
@Test
public void testPropertiesFileServiceDiscovery() throws Exception {
ServiceDiscovery sd = ServiceDiscoveryFactory.get("PROPERTIES_FILE");
assertNotNull(sd);
GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.replay(gc);
String discoveryAddress = this.getClass().getName() + "__test-discovery-source.properties";
File discoverySource = new File(discoveryAddress);
try {
config.store(new FileOutputStream(discoverySource), "Test discovery source for PropertiesFileServiceDiscovery");
ServiceDiscovery.Cluster c = sd.discover(gc, new DefaultServiceDiscoveryConfig(discoverySource.getAbsolutePath()), "mycluster");
assertNotNull(c);
for (String name : clusterProperties.keySet()) {
assertEquals(clusterProperties.get(name), c.getServiceURLs(name.split("\\.")[1]).get(0));
}
} finally {
discoverySource.delete();
}
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class GatewayFilter method getRemoteAddress.
private String getRemoteAddress(ServletRequest servletRequest) {
GatewayConfig gatewayConfig = (GatewayConfig) servletRequest.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
String addrHeaderName = gatewayConfig.getHeaderNameForRemoteAddress();
String addr = ((HttpServletRequest) servletRequest).getHeader(addrHeaderName);
if (addr == null || addr.trim().isEmpty()) {
addr = servletRequest.getRemoteAddr();
}
return addr;
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class GatewayServer method main.
public static void main(String[] args) {
try {
configureLogging();
logSysProps();
CommandLine cmd = GatewayCommandLine.parse(args);
if (cmd.hasOption(GatewayCommandLine.HELP_LONG)) {
GatewayCommandLine.printHelp();
} else if (cmd.hasOption(GatewayCommandLine.VERSION_LONG)) {
printVersion();
} else if (cmd.hasOption(GatewayCommandLine.REDEPLOY_LONG)) {
redeployTopologies(cmd.getOptionValue(GatewayCommandLine.REDEPLOY_LONG));
} else {
buildProperties = loadBuildProperties();
services = instantiateGatewayServices();
if (services == null) {
log.failedToInstantiateGatewayServices();
}
GatewayConfig config = new GatewayConfigImpl();
if (config.isHadoopKerberosSecured()) {
configureKerberosSecurity(config);
}
Map<String, String> options = new HashMap<>();
options.put(GatewayCommandLine.PERSIST_LONG, Boolean.toString(cmd.hasOption(GatewayCommandLine.PERSIST_LONG)));
services.init(config, options);
if (!cmd.hasOption(GatewayCommandLine.NOSTART_LONG)) {
startGateway(config, services);
}
}
} catch (ParseException e) {
log.failedToParseCommandLine(e);
GatewayCommandLine.printHelp();
} catch (Exception e) {
log.failedToStartGateway(e);
// Make sure the process exits.
System.exit(1);
}
}
Aggregations