Search in sources :

Example 1 with GatewayConfig

use of org.apache.knox.gateway.config.GatewayConfig 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);
    }
}
Also used : ServletException(javax.servlet.ServletException) GatewayServices(org.apache.knox.gateway.services.GatewayServices) InputStreamReader(java.io.InputStreamReader) MetricsService(org.apache.knox.gateway.services.metrics.MetricsService) GatewayDescriptor(org.apache.knox.gateway.descriptor.GatewayDescriptor) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) AbstractGatewayFilter(org.apache.knox.gateway.filter.AbstractGatewayFilter) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig)

Example 2 with GatewayConfig

use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.

the class CMFKeystoreServiceTest method setup.

@Before
public void setup() {
    try {
        ks = new CMFKeystoreService(".", "ambari");
        ks.setMasterService(new MasterService() {

            public void init(GatewayConfig config, Map<String, String> options) throws ServiceLifecycleException {
            // TODO Auto-generated method stub
            }

            public void start() throws ServiceLifecycleException {
            // TODO Auto-generated method stub
            }

            public void stop() throws ServiceLifecycleException {
            // TODO Auto-generated method stub
            }

            public char[] getMasterSecret() {
                // TODO Auto-generated method stub
                return "testmaster".toCharArray();
            }
        });
    } catch (ServiceLifecycleException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) MasterService(org.apache.knox.gateway.services.security.MasterService) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Before(org.junit.Before)

Example 3 with GatewayConfig

use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.

the class DefaultDispatchTest method testUsingDefaultBufferSize.

@Test
public void testUsingDefaultBufferSize() throws URISyntaxException, IOException {
    DefaultDispatch defaultDispatch = new DefaultDispatch();
    ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
    GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.expect(gatewayConfig.isHadoopKerberosSecured()).andReturn(Boolean.TRUE).anyTimes();
    EasyMock.expect(gatewayConfig.getHttpServerRequestBuffer()).andReturn(16384).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).anyTimes();
    ServletInputStream inputStream = EasyMock.createNiceMock(ServletInputStream.class);
    HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(inboundRequest.getQueryString()).andReturn("a=123").anyTimes();
    EasyMock.expect(inboundRequest.getInputStream()).andReturn(inputStream).anyTimes();
    EasyMock.expect(inboundRequest.getServletContext()).andReturn(servletContext).anyTimes();
    EasyMock.replay(gatewayConfig, servletContext, inboundRequest);
    HttpEntity httpEntity = defaultDispatch.createRequestEntity(inboundRequest);
    assertTrue("not buffering in the absence of delegation token", (httpEntity instanceof PartiallyRepeatableHttpEntity));
    assertEquals(defaultDispatch.getReplayBufferSize(), 16);
    assertEquals(defaultDispatch.getReplayBufferSizeInBytes(), 16384);
    // also test normal setter and getters
    defaultDispatch.setReplayBufferSize(-1);
    assertEquals(defaultDispatch.getReplayBufferSizeInBytes(), -1);
    assertEquals(defaultDispatch.getReplayBufferSize(), -1);
    defaultDispatch.setReplayBufferSize(16);
    assertEquals(defaultDispatch.getReplayBufferSizeInBytes(), 16384);
    assertEquals(defaultDispatch.getReplayBufferSize(), 16);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletInputStream(javax.servlet.ServletInputStream) HttpEntity(org.apache.http.HttpEntity) ServletContext(javax.servlet.ServletContext) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 4 with GatewayConfig

use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.

the class DefaultDispatchTest method testCallToSecureClusterWithDelegationToken.

@Test
public void testCallToSecureClusterWithDelegationToken() throws URISyntaxException, IOException {
    DefaultDispatch defaultDispatch = new DefaultDispatch();
    ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
    GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.expect(gatewayConfig.isHadoopKerberosSecured()).andReturn(true).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).anyTimes();
    ServletInputStream inputStream = EasyMock.createNiceMock(ServletInputStream.class);
    HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(inboundRequest.getQueryString()).andReturn("delegation=123").anyTimes();
    EasyMock.expect(inboundRequest.getInputStream()).andReturn(inputStream).anyTimes();
    EasyMock.expect(inboundRequest.getServletContext()).andReturn(servletContext).anyTimes();
    EasyMock.replay(gatewayConfig, servletContext, inboundRequest);
    HttpEntity httpEntity = defaultDispatch.createRequestEntity(inboundRequest);
    assertFalse("buffering in the presence of delegation token", (httpEntity instanceof PartiallyRepeatableHttpEntity));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletInputStream(javax.servlet.ServletInputStream) HttpEntity(org.apache.http.HttpEntity) ServletContext(javax.servlet.ServletContext) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 5 with GatewayConfig

use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.

the class DefaultDispatch method createRequestEntity.

protected HttpEntity createRequestEntity(HttpServletRequest request) throws IOException {
    String contentType = request.getContentType();
    int contentLength = request.getContentLength();
    InputStream contentStream = request.getInputStream();
    HttpEntity entity;
    if (contentType == null) {
        entity = new InputStreamEntity(contentStream, contentLength);
    } else {
        entity = new InputStreamEntity(contentStream, contentLength, ContentType.parse(contentType));
    }
    GatewayConfig config = (GatewayConfig) request.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
    if (config != null && config.isHadoopKerberosSecured()) {
        // Check if delegation token is supplied in the request
        boolean delegationTokenPresent = false;
        String queryString = request.getQueryString();
        if (queryString != null) {
            delegationTokenPresent = queryString.startsWith("delegation=") || queryString.contains("&delegation=");
        }
        if (replayBufferSize < 0) {
            replayBufferSize = config.getHttpServerRequestBuffer();
        }
        if (!delegationTokenPresent && replayBufferSize > 0) {
            entity = new PartiallyRepeatableHttpEntity(entity, replayBufferSize);
        }
    }
    return entity;
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig)

Aggregations

GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)90 Test (org.junit.Test)67 File (java.io.File)31 HashMap (java.util.HashMap)24 GatewayConfigImpl (org.apache.knox.gateway.config.impl.GatewayConfigImpl)19 Topology (org.apache.knox.gateway.topology.Topology)17 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 AliasService (org.apache.knox.gateway.services.security.AliasService)12 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)11 IOException (java.io.IOException)9 Service (org.apache.knox.gateway.topology.Service)9 Document (org.w3c.dom.Document)9 ArrayList (java.util.ArrayList)8 GatewayTestConfig (org.apache.knox.gateway.GatewayTestConfig)8 MasterService (org.apache.knox.gateway.services.security.MasterService)8 TopologyService (org.apache.knox.gateway.services.topology.TopologyService)8 KeystoreService (org.apache.knox.gateway.services.security.KeystoreService)7 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)7 List (java.util.List)6