use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment in project knox by apache.
the class OozieServiceDefinitionTest method testOozieRewriteRulesForValuesRelativeToServiceRegistry.
@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testOozieRewriteRulesForValuesRelativeToServiceRegistry() throws Exception {
LOG_ENTER();
// This is a unique part of this test.
String testResource = "oozie-request-relative.xml";
// Mock out the service url registry which is required for several url rewrite functions to work.
ServiceRegistry registry = EasyMock.createNiceMock(ServiceRegistry.class);
EasyMock.expect(registry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-scheme://test-host:42").anyTimes();
// Mock out the gateway services registry which is required for several url rewrite functions to work.
GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(services.getService(ServiceType.SERVICE_REGISTRY_SERVICE)).andReturn(registry).anyTimes();
UrlRewriteProcessor rewriteProcessor = new UrlRewriteProcessor();
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(servletContext.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriteProcessor).anyTimes();
EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services).anyTimes();
EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster").anyTimes();
HttpServletRequest servletRequest = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(servletRequest.getInputStream()).andReturn(new MockServletInputStream(TestUtils.getResourceStream(OozieServiceDefinitionTest.class, testResource))).anyTimes();
EasyMock.expect(servletRequest.getContentType()).andReturn("text/xml").anyTimes();
EasyMock.expect(servletRequest.getContentLength()).andReturn(-1).anyTimes();
FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
EasyMock.expect(filterConfig.getInitParameter(UrlRewriteServletFilter.REQUEST_BODY_FILTER_PARAM)).andReturn("OOZIE/oozie/configuration").anyTimes();
EasyMock.replay(registry, services, servletContext, servletRequest, filterConfig);
UrlRewriteEnvironment rewriteEnvironment = new UrlRewriteServletEnvironment(servletContext);
Reader rulesReader = TestUtils.getResourceReader("services/oozie/4.0.0/rewrite.xml", StandardCharsets.UTF_8);
UrlRewriteRulesDescriptor rewriteRules = UrlRewriteRulesDescriptorFactory.load("xml", rulesReader);
rulesReader.close();
rewriteProcessor.initialize(rewriteEnvironment, rewriteRules);
UrlRewriteRequest rewriteRequest = new UrlRewriteRequest(filterConfig, servletRequest);
InputStream stream = rewriteRequest.getInputStream();
Document document = XmlUtils.readXml(stream);
assertThat(document, hasXPath("/configuration/property[name='oozie.wf.application.path']/value", equalTo("test-scheme://test-host:42/workflow.xml")));
LOG_EXIT();
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment in project knox by apache.
the class OozieServiceDefinitionTest method testOozieRewriteRulesForLiteralComplexTemplateValuesBugKnox394.
@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testOozieRewriteRulesForLiteralComplexTemplateValuesBugKnox394() throws Exception {
LOG_ENTER();
// This is a unique part of this test.
String testResource = "oozie-request-with-complex-var.xml";
// Mock out the service url registry which is required for several url rewrite functions to work.
ServiceRegistry registry = EasyMock.createNiceMock(ServiceRegistry.class);
EasyMock.expect(registry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-scheme://test-host:42").anyTimes();
// Mock out the gateway services registry which is required for several url rewrite functions to work.
GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(services.getService(ServiceType.SERVICE_REGISTRY_SERVICE)).andReturn(registry).anyTimes();
UrlRewriteProcessor rewriteProcessor = new UrlRewriteProcessor();
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(servletContext.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriteProcessor).anyTimes();
EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services).anyTimes();
EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster").anyTimes();
HttpServletRequest servletRequest = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(servletRequest.getInputStream()).andReturn(new MockServletInputStream(TestUtils.getResourceStream(OozieServiceDefinitionTest.class, testResource))).anyTimes();
EasyMock.expect(servletRequest.getContentType()).andReturn("text/xml").anyTimes();
FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
EasyMock.expect(filterConfig.getInitParameter(UrlRewriteServletFilter.REQUEST_BODY_FILTER_PARAM)).andReturn("OOZIE/oozie/configuration").anyTimes();
EasyMock.replay(registry, services, servletContext, servletRequest, filterConfig);
UrlRewriteEnvironment rewriteEnvironment = new UrlRewriteServletEnvironment(servletContext);
Reader rulesReader = TestUtils.getResourceReader("services/oozie/4.0.0/rewrite.xml", StandardCharsets.UTF_8);
UrlRewriteRulesDescriptor rewriteRules = UrlRewriteRulesDescriptorFactory.load("xml", rulesReader);
rulesReader.close();
rewriteProcessor.initialize(rewriteEnvironment, rewriteRules);
UrlRewriteRequest rewriteRequest = new UrlRewriteRequest(filterConfig, servletRequest);
InputStream stream = rewriteRequest.getInputStream();
Document document = XmlUtils.readXml(stream);
assertThat(document, hasXPath("/configuration/property[name='oozie.wf.application.path']/value", equalTo("${nameNode}/user/${user.name}/${examplesRoot}/apps/hive")));
LOG_EXIT();
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment 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);
DefaultCryptoService cryptoService = new DefaultCryptoService();
cryptoService.setAliasService(as);
GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(gatewayServices.getService(ServiceType.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.filter.rewrite.api.UrlRewriteEnvironment 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);
DefaultCryptoService cryptoService = new DefaultCryptoService();
cryptoService.setAliasService(as);
GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(gatewayServices.getService(ServiceType.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.filter.rewrite.api.UrlRewriteEnvironment in project knox by apache.
the class UrlRewriteRequestTest method testContentEncoding.
/*
* Test the case where a request has
* Content-Type:text/xml and Content-Encoding:gzip
*/
@Test
public void testContentEncoding() throws Exception {
/* copy results */
final ByteArrayOutputStream results = new ByteArrayOutputStream();
final InputStream input = Files.newInputStream(Paths.get(ClassLoader.getSystemResource("KNOX-1412.xml.gz").toURI()));
final ServletInputStream payload = new ServletInputStream() {
@Override
public int read() throws IOException {
return input.read();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
};
GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
EasyMock.expect(environment.getAttribute("org.apache.knox.gateway.frontend.uri")).andReturn(new URI("https://test-location")).anyTimes();
EasyMock.expect(environment.resolve("cluster.name")).andReturn(Collections.singletonList("test-cluster-name")).anyTimes();
EasyMock.replay(gatewayServices, environment);
UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
UrlRewriteRuleDescriptor rule = descriptor.addRule("test-location");
rule.pattern("{*}://{*}:{*}/{**}/?{**}");
UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
rewrite.template("{$inboundurl[host]}");
UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
rewriter.initialize(environment, descriptor);
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
EasyMock.expect(context.getServletContextName()).andReturn("test-cluster-name").anyTimes();
EasyMock.expect(context.getInitParameter("test-init-param-name")).andReturn("test-init-param-value").anyTimes();
EasyMock.expect(context.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriter).anyTimes();
FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.expect(config.getInitParameter("test-filter-init-param-name")).andReturn("test-filter-init-param-value").anyTimes();
EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();
/* Request wih Content-Type:text/xml and Content-Encoding:gzip */
HttpServletRequest request1 = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(request1.getScheme()).andReturn("https").anyTimes();
EasyMock.expect(request1.getServerName()).andReturn("targethost.com").anyTimes();
EasyMock.expect(request1.getServerPort()).andReturn(80).anyTimes();
EasyMock.expect(request1.getRequestURI()).andReturn("/").anyTimes();
EasyMock.expect(request1.getQueryString()).andReturn(null).anyTimes();
EasyMock.expect(request1.getInputStream()).andReturn(payload).anyTimes();
EasyMock.expect(request1.getContentLength()).andReturn(input.available()).anyTimes();
EasyMock.expect(request1.getContentType()).andReturn("text/xml").anyTimes();
EasyMock.expect(request1.getHeader("Content-Encoding")).andReturn("gzip").anyTimes();
EasyMock.expect(request1.getHeader("Host")).andReturn("sourcehost.com").anyTimes();
/* Request wih Content-Type:application/gzip and Content-Encoding:gzip */
HttpServletRequest request2 = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(request2.getScheme()).andReturn("https").anyTimes();
EasyMock.expect(request2.getServerName()).andReturn("targethost.com").anyTimes();
EasyMock.expect(request2.getServerPort()).andReturn(80).anyTimes();
EasyMock.expect(request2.getRequestURI()).andReturn("/").anyTimes();
EasyMock.expect(request2.getQueryString()).andReturn(null).anyTimes();
EasyMock.expect(request2.getInputStream()).andReturn(payload).anyTimes();
EasyMock.expect(request2.getContentLength()).andReturn(input.available()).anyTimes();
EasyMock.expect(request2.getContentType()).andReturn("application/gzip").anyTimes();
EasyMock.expect(request2.getHeader("Content-Encoding")).andReturn("gzip").anyTimes();
EasyMock.expect(request2.getHeader("Host")).andReturn("sourcehost.com").anyTimes();
/* Request wih Content-Type:application/gzip no content encoding */
HttpServletRequest request3 = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(request3.getScheme()).andReturn("https").anyTimes();
EasyMock.expect(request3.getServerName()).andReturn("targethost.com").anyTimes();
EasyMock.expect(request3.getServerPort()).andReturn(80).anyTimes();
EasyMock.expect(request3.getRequestURI()).andReturn("/").anyTimes();
EasyMock.expect(request3.getQueryString()).andReturn(null).anyTimes();
EasyMock.expect(request3.getInputStream()).andReturn(payload).anyTimes();
EasyMock.expect(request3.getContentLength()).andReturn(input.available()).anyTimes();
EasyMock.expect(request3.getContentType()).andReturn("application/gzip").anyTimes();
EasyMock.expect(request3.getHeader("Host")).andReturn("sourcehost.com").anyTimes();
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(context, config, response, request1, request2, request3);
/* make sure the following exception is not thrown
* java.lang.RuntimeException: com.ctc.wstx.exc.WstxUnexpectedCharException:
* Illegal character ((CTRL-CHAR, code 31))
*/
/* Test for condition where Content-Type:text/xml and Content-Encoding:gzip */
UrlRewriteRequest rewriteRequest = new UrlRewriteRequest(config, request1);
ServletInputStream inputStream = rewriteRequest.getInputStream();
HttpEntity entity = new InputStreamEntity(inputStream, request1.getContentLength(), ContentType.parse("text/xml"));
entity.writeTo(results);
/* Test for condition where Content-Type:application/gzip and Content-Encoding:gzip */
rewriteRequest = new UrlRewriteRequest(config, request2);
inputStream = rewriteRequest.getInputStream();
entity = new InputStreamEntity(inputStream, request1.getContentLength(), ContentType.parse("application/gzip"));
entity.writeTo(results);
/* Test for condition where Content-Type:application/gzip no content encoding */
rewriteRequest = new UrlRewriteRequest(config, request3);
inputStream = rewriteRequest.getInputStream();
entity = new InputStreamEntity(inputStream, request1.getContentLength(), ContentType.parse("application/gzip"));
entity.writeTo(results);
}
Aggregations