Search in sources :

Example 6 with UrlRewriteEnvironment

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment in project knox by apache.

the class HostmapFunctionProcessorTest method testInvalidFunctionNameAndEmptyHostmapUseCase.

@Test
public void testInvalidFunctionNameAndEmptyHostmapUseCase() throws Exception {
    URL configUrl = TestUtils.getResourceUrl(this.getClass(), "empty-hostmap.txt");
    UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(environment.getResource("/WEB-INF/hostmap.txt")).andReturn(configUrl).anyTimes();
    Resolver resolver = EasyMock.createNiceMock(Resolver.class);
    EasyMock.expect(resolver.resolve("host")).andReturn(Arrays.asList("test-inbound-host")).anyTimes();
    EasyMock.replay(environment, resolver);
    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule");
    rule.pattern("{*}://{host}:{*}/{**}?{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{*}://{$invalid-function(host)}:{*}/{**}?{**}");
    UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
    rewriter.initialize(environment, descriptor);
    Template input = Parser.parseLiteral("test-scheme://test-inbound-host:42/test-path/test-file?test-name=test-value");
    Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.IN, null);
    // System.out.println( output );
    assertThat(output, notNullValue());
    assertThat(output.getHost().getFirstValue().getPattern(), is("$invalid-function(host)"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) URL(java.net.URL) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 7 with UrlRewriteEnvironment

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(GatewayServices.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", "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();
}
Also used : MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteRequest(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRequest) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) UrlRewriteServletEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletEnvironment) Document(org.w3c.dom.Document) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) Test(org.junit.Test)

Example 8 with UrlRewriteEnvironment

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(GatewayServices.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", "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();
}
Also used : MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteRequest(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRequest) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) UrlRewriteServletEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletEnvironment) Document(org.w3c.dom.Document) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) Test(org.junit.Test)

Example 9 with UrlRewriteEnvironment

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment in project knox by apache.

the class HostmapFunctionProcessorTest method testBasicUseCase.

@Test
public void testBasicUseCase() throws Exception {
    URL configUrl = TestUtils.getResourceUrl(this.getClass(), "hostmap.txt");
    HostMapper hm = EasyMock.createNiceMock(HostMapper.class);
    EasyMock.expect(hm.resolveInboundHostName("test-inbound-host")).andReturn("test-inbound-rewritten-host").anyTimes();
    HostMapperService hms = EasyMock.createNiceMock(HostMapperService.class);
    GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(gatewayServices.getService(GatewayServices.HOST_MAPPING_SERVICE)).andReturn(hms).anyTimes();
    UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
    EasyMock.expect(environment.resolve("cluster.name")).andReturn(Arrays.asList("test-cluster-name")).anyTimes();
    EasyMock.expect(environment.getResource("/WEB-INF/hostmap.txt")).andReturn(configUrl).anyTimes();
    Resolver resolver = EasyMock.createNiceMock(Resolver.class);
    EasyMock.expect(resolver.resolve("host")).andReturn(Arrays.asList("test-inbound-host")).anyTimes();
    EasyMock.replay(gatewayServices, hm, hms, environment, resolver);
    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule");
    rule.pattern("{*}://{host}:{*}/{**}?{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{*}://{$hostmap(host)}:{*}/{**}?{**}");
    UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
    rewriter.initialize(environment, descriptor);
    Template input = Parser.parseLiteral("test-scheme://test-inbound-host:42/test-path/test-file?test-name=test-value");
    Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.IN, null);
    // System.out.println( output );
    assertThat(output, notNullValue());
    assertThat(output.getHost().getFirstValue().getPattern(), is("test-inbound-rewritten-host"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) HostMapper(org.apache.knox.gateway.services.hostmap.HostMapper) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) HostMapperService(org.apache.knox.gateway.services.hostmap.HostMapperService) URL(java.net.URL) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 10 with UrlRewriteEnvironment

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment in project knox by apache.

the class HostmapFunctionProcessorTest method testHdfsUseCase.

@Test
public void testHdfsUseCase() throws Exception {
    URL configUrl = TestUtils.getResourceUrl(this.getClass(), "hdfs-hostmap.txt");
    UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(environment.getResource("/WEB-INF/hostmap.txt")).andReturn(configUrl).anyTimes();
    Resolver resolver = EasyMock.createNiceMock(Resolver.class);
    EasyMock.expect(resolver.resolve("host")).andReturn(Arrays.asList("test-internal-host")).anyTimes();
    EasyMock.replay(environment, resolver);
    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule");
    rule.pattern("{*}://{host}:{*}/{**}?{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{*}://test-static-host:{*}/{**}?server={$hostmap(host)}&{**}");
    UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
    rewriter.initialize(environment, descriptor);
    Template input = Parser.parseLiteral("test-scheme://test-external-host:42/test-path/test-file?test-name-1=test-value-1&test-name-2=test-value-2");
    Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.OUT, "test-rule");
    // System.out.println( output );
    assertThat(output, notNullValue());
    assertThat(output.getHost().getFirstValue().getPattern(), is("test-static-host"));
    assertThat(output.getQuery().get("server").getFirstValue().getPattern(), is("test-external-host"));
    assertThat(output.getQuery().get("server").getValues().size(), is(1));
    assertThat(output.getQuery().get("test-name-1").getFirstValue().getPattern(), is("test-value-1"));
    assertThat(output.getQuery().get("test-name-1").getValues().size(), is(1));
    assertThat(output.getQuery().get("test-name-2").getFirstValue().getPattern(), is("test-value-2"));
    assertThat(output.getQuery().get("test-name-2").getValues().size(), is(1));
    assertThat(output.getQuery().size(), is(3));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) URL(java.net.URL) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Aggregations

UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)22 Test (org.junit.Test)22 Template (org.apache.knox.gateway.util.urltemplate.Template)15 UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)11 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)11 GatewayServices (org.apache.knox.gateway.services.GatewayServices)11 Resolver (org.apache.knox.gateway.util.urltemplate.Resolver)9 UrlRewriteRuleDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor)8 UrlRewriteActionRewriteDescriptorExt (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt)8 URL (java.net.URL)7 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)6 AliasService (org.apache.knox.gateway.services.security.AliasService)6 CryptoService (org.apache.knox.gateway.services.security.CryptoService)6 DefaultCryptoService (org.apache.knox.gateway.services.security.impl.DefaultCryptoService)6 Capture (org.easymock.Capture)6 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 FilterConfig (javax.servlet.FilterConfig)3 ServletContext (javax.servlet.ServletContext)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3