use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class ShiroDeploymentContributorTest method testDeployment.
@Test
public void testDeployment() throws IOException {
WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test-archive");
Map<String, String> providerParams = new HashMap<>();
Provider provider = new Provider();
provider.setEnabled(true);
provider.setName("shiro");
provider.setParams(providerParams);
Topology topology = new Topology();
topology.setName("Sample");
DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class);
EasyMock.expect(context.getWebArchive()).andReturn(webArchive).anyTimes();
EasyMock.expect(context.getWebAppDescriptor()).andReturn(Descriptors.create(WebAppDescriptor.class)).anyTimes();
EasyMock.expect(context.getTopology()).andReturn(topology).anyTimes();
EasyMock.replay(context);
AliasService as = EasyMock.createNiceMock(AliasService.class);
CryptoService cryptoService = new DefaultCryptoService();
((DefaultCryptoService) cryptoService).setAliasService(as);
GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(gatewayServices.getService(GatewayServices.CRYPTO_SERVICE)).andReturn(cryptoService).anyTimes();
ShiroDeploymentContributor contributor = new ShiroDeploymentContributor();
assertThat(contributor.getRole(), is("authentication"));
assertThat(contributor.getName(), is("ShiroProvider"));
// Just make sure it doesn't blow up.
contributor.initializeContribution(context);
contributor.contributeProvider(context, provider);
// Just make sure it doesn't blow up.
contributor.finalizeContribution(context);
assertThat(context.getWebAppDescriptor().getOrCreateSessionConfig().getOrCreateCookieConfig().isHttpOnly(), is(true));
assertThat(context.getWebAppDescriptor().getOrCreateSessionConfig().getOrCreateCookieConfig().isSecure(), is(true));
}
use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class HaProviderDeploymentContributorTest method testServiceLevelParamOverrides_MultipleMixed.
@Test
public void testServiceLevelParamOverrides_MultipleMixed() throws Exception {
// Define some provider params
Map<String, String> providerParams = new HashMap<>();
// Specify a subset of the possible HaProvider-level params for TestRoleOne
providerParams.put("TestRoleOne", "enabled=true;maxRetryAttempts=1;retrySleep=10;maxFailoverAttempts=2;failoverSleep=20");
// Specify all the possible params at the HaProvider level for TestRoleTwo
providerParams.put("TestRoleTwo", "enabled=false;" + "maxRetryAttempts=3;" + "retrySleep=30;" + "maxFailoverAttempts=4;" + "failoverSleep=40;" + "zookeeperNamespace=testRoleTwo;" + "zookeeperEnsemble=http://host1:2181,http://host2:2181");
Provider testHaProvider = createHaProvider(providerParams);
// Define the topology content (e.g., services)
Collection<Service> topologyServices = new HashSet<>();
// A service with no param overrides
Service testRoleOneService = EasyMock.createNiceMock(Service.class);
EasyMock.expect(testRoleOneService.getRole()).andReturn("TestRoleOne").anyTimes();
EasyMock.expect(testRoleOneService.getName()).andReturn("TestRoleOneService").anyTimes();
EasyMock.expect(testRoleOneService.getParams()).andReturn(Collections.emptyMap()).anyTimes();
EasyMock.replay(testRoleOneService);
topologyServices.add(testRoleOneService);
// Override all the possible params in the TestRoleTwo service level
Map<String, String> testRoleTwoParams = new HashMap<>();
testRoleTwoParams.put("enabled", "true");
testRoleTwoParams.put("maxRetryAttempts", "6");
testRoleTwoParams.put("retrySleep", "60");
testRoleTwoParams.put("maxFailoverAttempts", "8");
testRoleTwoParams.put("failoverSleep", "80");
testRoleTwoParams.put("zookeeperNamespace", "testRoleTwoOverride");
testRoleTwoParams.put("zookeeperEnsemble", "http://host3:2181,http://host4:2181");
Service testRoleTwoService = EasyMock.createNiceMock(Service.class);
EasyMock.expect(testRoleTwoService.getRole()).andReturn("TestRoleTwo").anyTimes();
EasyMock.expect(testRoleTwoService.getName()).andReturn("TestRoleTwoService").anyTimes();
EasyMock.expect(testRoleTwoService.getParams()).andReturn(testRoleTwoParams).anyTimes();
EasyMock.replay(testRoleTwoService);
topologyServices.add(testRoleTwoService);
Topology topology = EasyMock.createNiceMock(Topology.class);
EasyMock.expect(topology.getServices()).andReturn(topologyServices).anyTimes();
EasyMock.replay(topology);
WebArchive war = EasyMock.createNiceMock(WebArchive.class);
EasyMock.replay(war);
DeploymentContext context = new DescriptorCaptureDeploymentContext(topology, war);
// Invoke the contributor
HaProviderDeploymentContributor haPDC = new HaProviderDeploymentContributor();
haPDC.contributeProvider(context, testHaProvider);
HaDescriptor descriptor = context.getDescriptor("ha.provider.descriptor");
assertNotNull(descriptor);
assertEquals(2, descriptor.getServiceConfigs().size());
// Validate the service with no-overrides, checking that the provider-level defaults are applied
validateServiceHaConfig(descriptor.getServiceConfig("TestRoleOne"), true, 20, 2, 10, 1, null, null);
// Validate the service with all-overrides, checking that the service-level defaults are applied
validateServiceHaConfig(descriptor.getServiceConfig("TestRoleTwo"), true, 80, 8, 60, 6, "testRoleTwoOverride", "http://host3:2181,http://host4:2181");
}
use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class GatewayFilterTest method testDefaultServicePathTopologyRequestAttribute.
@Test
public void testDefaultServicePathTopologyRequestAttribute() throws Exception {
FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.replay(config);
Topology topology = EasyMock.createNiceMock(Topology.class);
topology.setDefaultServicePath("test-role/");
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(topology.getDefaultServicePath()).andReturn("test-role").anyTimes();
EasyMock.expect(request.getPathInfo()).andReturn("/test-path/test-resource").anyTimes();
EasyMock.expect(request.getServletContext()).andReturn(context).anyTimes();
EasyMock.expect(context.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).anyTimes();
EasyMock.expect(gatewayConfig.getHeaderNameForRemoteAddress()).andReturn("Custom-Forwarded-For").anyTimes();
EasyMock.expect(request.getRequestURL()).andReturn(new StringBuffer("http://host:8443/gateway/sandbox/test-path/test-resource/")).anyTimes();
EasyMock.expect(context.getAttribute("org.apache.knox.gateway.topology")).andReturn(topology).anyTimes();
EasyMock.replay(request);
EasyMock.replay(context);
EasyMock.replay(topology);
EasyMock.replay(gatewayConfig);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(response);
TestRoleFilter filter = new TestRoleFilter();
GatewayFilter gateway = new GatewayFilter();
gateway.addFilter("test-role/**/**", "test-filter", filter, null, "test-role");
gateway.init(config);
gateway.doFilter(request, response);
gateway.destroy();
assertThat((String) filter.defaultServicePath, is("test-role"));
assertThat((String) filter.url, is("http://host:8443/gateway/sandbox/test-role/test-path/test-resource"));
}
use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class DefaultTopologyService method deployTopology.
public void deployTopology(Topology t) {
try {
File temp = new File(topologiesDirectory.getAbsolutePath() + "/" + t.getName() + ".xml.temp");
Package topologyPkg = Topology.class.getPackage();
String pkgName = topologyPkg.getName();
String bindingFile = pkgName.replace(".", "/") + "/topology_binding-xml.xml";
Map<String, Object> properties = new HashMap<>(1);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, bindingFile);
JAXBContext jc = JAXBContext.newInstance(pkgName, Topology.class.getClassLoader(), properties);
Marshaller mr = jc.createMarshaller();
mr.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
mr.marshal(t, temp);
File topology = new File(topologiesDirectory.getAbsolutePath() + "/" + t.getName() + ".xml");
if (!temp.renameTo(topology)) {
FileUtils.forceDelete(temp);
throw new IOException("Could not rename temp file");
}
// This code will check if the topology is valid, and retrieve the errors if it is not.
TopologyValidator validator = new TopologyValidator(topology.getAbsolutePath());
if (!validator.validateTopology()) {
throw new SAXException(validator.getErrorString());
}
} catch (JAXBException e) {
auditor.audit(Action.DEPLOY, t.getName(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
log.failedToDeployTopology(t.getName(), e);
} catch (IOException io) {
auditor.audit(Action.DEPLOY, t.getName(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
log.failedToDeployTopology(t.getName(), io);
} catch (SAXException sx) {
auditor.audit(Action.DEPLOY, t.getName(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
log.failedToDeployTopology(t.getName(), sx);
}
reloadTopologies();
}
use of org.apache.knox.gateway.topology.Topology in project knox by apache.
the class DefaultTopologyService method loadTopologies.
private Map<File, Topology> loadTopologies(File directory) {
Map<File, Topology> map = new HashMap<>();
if (directory.isDirectory() && directory.canRead()) {
File[] existingTopologies = directory.listFiles(this);
if (existingTopologies != null) {
for (File file : existingTopologies) {
try {
Topology loadTopology = loadTopology(file);
if (null != loadTopology) {
map.put(file, loadTopology);
} else {
auditor.audit(Action.LOAD, file.getAbsolutePath(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
log.failedToLoadTopology(file.getAbsolutePath());
}
} catch (IOException e) {
// Maybe it makes sense to throw exception
auditor.audit(Action.LOAD, file.getAbsolutePath(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
log.failedToLoadTopology(file.getAbsolutePath(), e);
} catch (SAXException e) {
// Maybe it makes sense to throw exception
auditor.audit(Action.LOAD, file.getAbsolutePath(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
log.failedToLoadTopology(file.getAbsolutePath(), e);
} catch (Exception e) {
// Maybe it makes sense to throw exception
auditor.audit(Action.LOAD, file.getAbsolutePath(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
log.failedToLoadTopology(file.getAbsolutePath(), e);
}
}
}
}
return map;
}
Aggregations