use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class ServiceRegistryFunctionsTest method setUp.
public void setUp(String username, Map<String, String> initParams) throws Exception {
ServiceRegistry mockServiceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-nn-scheme://test-nn-host:411").anyTimes();
EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "JOBTRACKER")).andReturn("test-jt-scheme://test-jt-host:511").anyTimes();
GatewayServices mockGatewayServices = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(mockGatewayServices.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(mockServiceRegistry).anyTimes();
EasyMock.replay(mockServiceRegistry, mockGatewayServices);
String descriptorUrl = getTestResource("rewrite.xml").toExternalForm();
Log.setLog(new NoOpLogger());
server = new ServletTester();
server.setContextPath("/");
server.getContext().addEventListener(new UrlRewriteServletContextListener());
server.getContext().setInitParameter(UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl);
server.getContext().setAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE, "test-cluster");
server.getContext().setAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE, mockGatewayServices);
FilterHolder setupFilter = server.addFilter(SetupFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
setupFilter.setFilter(new SetupFilter(username));
FilterHolder rewriteFilter = server.addFilter(UrlRewriteServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
if (initParams != null) {
for (Map.Entry<String, String> entry : initParams.entrySet()) {
rewriteFilter.setInitParameter(entry.getKey(), entry.getValue());
}
}
rewriteFilter.setFilter(new UrlRewriteServletFilter());
interactions = new ArrayQueue<MockInteraction>();
ServletHolder servlet = server.addServlet(MockServlet.class, "/");
servlet.setServlet(new MockServlet("mock-servlet", interactions));
server.start();
interaction = new MockInteraction();
request = HttpTester.newRequest();
response = null;
}
use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class EncryptUriDeploymentContributorTest method testDeployment.
@Test
public void testDeployment() throws IOException {
WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test-acrhive");
Provider provider = new Provider();
provider.setEnabled(true);
provider.setName(EncryptUriDeploymentContributor.PROVIDER_ROLE_NAME);
Topology topology = new Topology();
topology.setName("Sample");
DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class);
EasyMock.expect(context.getWebArchive()).andReturn(webArchive).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();
UrlRewriteEnvironment encEnvironment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
EasyMock.expect(encEnvironment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
EncryptUriDeploymentContributor contributor = new EncryptUriDeploymentContributor();
contributor.setAliasService(as);
assertThat(contributor.getRole(), is(EncryptUriDeploymentContributor.PROVIDER_ROLE_NAME));
assertThat(contributor.getName(), is(EncryptUriDeploymentContributor.PROVIDER_IMPL_NAME));
// Just make sure it doesn't blow up.
contributor.contributeFilter(null, null, null, null, null);
// 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);
}
use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class SecureQueryDeploymentContributorTest method testDeployment.
@Test
public void testDeployment() throws IOException {
WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test-acrhive");
// UrlRewriteRulesDescriptorImpl rewriteRules = new UrlRewriteRulesDescriptorImpl();
Map<String, String> providerParams = new HashMap<>();
// providerParams.put( "test-host-external", "test-host-internal" );
Provider provider = new Provider();
provider.setEnabled(true);
provider.setName("secure-query");
provider.setParams(providerParams);
Topology topology = new Topology();
topology.setName("Sample");
DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class);
// EasyMock.expect( context.getDescriptor( "rewrite" ) ).andReturn( rewriteRules ).anyTimes();
EasyMock.expect(context.getWebArchive()).andReturn(webArchive).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();
UrlRewriteEnvironment encEnvironment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
EasyMock.expect(encEnvironment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
SecureQueryDeploymentContributor contributor = new SecureQueryDeploymentContributor();
contributor.setAliasService(as);
assertThat(contributor.getRole(), is("secure-query"));
assertThat(contributor.getName(), is("default"));
// Just make sure it doesn't blow up.
contributor.contributeFilter(null, null, null, null, null);
// Just make sure it doesn't blow up.
contributor.initializeContribution(context);
contributor.contributeProvider(context, provider);
// HostmapFunctionDescriptor funcDesc = rewriteRules.getFunction( "hostmap" );
// assertThat( funcDesc.config(), is( "/WEB-INF/hostmap.txt" ) );
//
// Node node = webArchive.get( "/WEB-INF/hostmap.txt" );
// String asset = IOUtils.toString( node.getAsset().openStream() );
// assertThat( asset, containsString( "test-host-external=test-host-internal" ) );
// Just make sure it doesn't blow up.
contributor.finalizeContribution(context);
}
use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class AccessTokenFederationFilter method init.
@Override
public void init(FilterConfig filterConfig) throws ServletException {
GatewayServices services = (GatewayServices) filterConfig.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
authority = (JWTokenAuthority) services.getService(GatewayServices.TOKEN_SERVICE);
}
use of org.apache.knox.gateway.services.GatewayServices in project knox by apache.
the class GatewayServlet method createFilter.
private static GatewayFilter createFilter(InputStream stream, ServletContext servletContext) throws ServletException {
try {
GatewayFilter filter = null;
if (stream != null) {
try {
GatewayDescriptor descriptor = GatewayDescriptorFactory.load("xml", new InputStreamReader(stream));
filter = GatewayFactory.create(descriptor);
} finally {
stream.close();
}
}
GatewayConfig gatewayConfig = (GatewayConfig) servletContext.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
if (gatewayConfig.isMetricsEnabled()) {
GatewayServices gatewayServices = (GatewayServices) servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
MetricsService metricsService = gatewayServices.getService(GatewayServices.METRICS_SERVICE);
if (metricsService != null) {
GatewayFilter instrumentedFilter = metricsService.getInstrumented(filter);
if (instrumentedFilter != null) {
filter = instrumentedFilter;
}
}
}
return filter;
} catch (IOException e) {
throw new ServletException(e);
} catch (URISyntaxException e) {
throw new ServletException(e);
}
}
Aggregations