Search in sources :

Example 66 with GatewayConfig

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

the class KnoxCLI method getGatewayConfig.

private GatewayConfig getGatewayConfig() {
    GatewayConfig result;
    Configuration conf = getConf();
    if (conf != null && conf instanceof GatewayConfig) {
        result = (GatewayConfig) conf;
    } else {
        result = new GatewayConfigImpl();
    }
    return result;
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) GatewayConfigImpl(org.apache.knox.gateway.config.impl.GatewayConfigImpl) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig)

Example 67 with GatewayConfig

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

the class KnoxCLI method initializeServices.

private void initializeServices(boolean persisting) throws ServiceLifecycleException {
    GatewayConfig config = getGatewayConfig();
    Map<String, String> options = new HashMap<>();
    options.put(GatewayCommandLine.PERSIST_LONG, Boolean.toString(persisting));
    if (master != null) {
        options.put("master", master);
    }
    services.init(config, options);
}
Also used : HashMap(java.util.HashMap) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig)

Example 68 with GatewayConfig

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

the class AuditLoggingTest method testNoopFilter.

@Test
public /**
 * One NoOp filter in chain. Single audit event with same with specified request URI is expected:
 *
 * action=access request_type=uri outcome=unavailable
 */
void testNoopFilter() throws ServletException, IOException, URISyntaxException {
    FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
    EasyMock.replay(config);
    HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
    ServletContext context = EasyMock.createNiceMock(ServletContext.class);
    GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.expect(request.getMethod()).andReturn(METHOD).anyTimes();
    EasyMock.expect(request.getPathInfo()).andReturn(PATH).anyTimes();
    EasyMock.expect(request.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
    EasyMock.expect(request.getRemoteAddr()).andReturn(ADDRESS).anyTimes();
    EasyMock.expect(request.getRemoteHost()).andReturn(HOST).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.replay(request);
    EasyMock.replay(context);
    EasyMock.replay(gatewayConfig);
    HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.replay(response);
    FilterChain chain = EasyMock.createNiceMock(FilterChain.class);
    EasyMock.replay(chain);
    Filter filter = EasyMock.createNiceMock(Filter.class);
    EasyMock.replay(filter);
    GatewayFilter gateway = new GatewayFilter();
    gateway.addFilter("path", "filter", filter, null, null);
    gateway.init(config);
    gateway.doFilter(request, response, chain);
    gateway.destroy();
    assertThat(CollectAppender.queue.size(), is(1));
    Iterator<LoggingEvent> iterator = CollectAppender.queue.iterator();
    LoggingEvent accessEvent = iterator.next();
    verifyAuditEvent(accessEvent, CONTEXT_PATH + PATH, ResourceType.URI, Action.ACCESS, ActionOutcome.UNAVAILABLE, null, "Request method: GET");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) LoggingEvent(org.apache.log4j.spi.LoggingEvent) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterConfig(javax.servlet.FilterConfig) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 69 with GatewayConfig

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

the class AuditLoggingTest method testNoFiltersAudit.

@Test
public /**
 * Empty filter chain. Two events with same correlation ID are expected:
 *
 * action=access request_type=uri outcome=unavailable
 * action=access request_type=uri outcome=success message=Response status: 404
 */
void testNoFiltersAudit() throws Exception {
    FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
    EasyMock.replay(config);
    HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
    ServletContext context = EasyMock.createNiceMock(ServletContext.class);
    GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.expect(request.getMethod()).andReturn(METHOD).anyTimes();
    EasyMock.expect(request.getPathInfo()).andReturn(PATH).anyTimes();
    EasyMock.expect(request.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
    EasyMock.expect(request.getRemoteAddr()).andReturn(ADDRESS).anyTimes();
    EasyMock.expect(request.getRemoteHost()).andReturn(HOST).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.replay(request);
    EasyMock.replay(context);
    EasyMock.replay(gatewayConfig);
    HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.replay(response);
    FilterChain chain = EasyMock.createNiceMock(FilterChain.class);
    EasyMock.replay(chain);
    Random rnd = new Random();
    // Make number of total requests between 1-100
    int numberTotalRequests = rnd.nextInt(99) + 1;
    Set<Callable<Void>> callables = new HashSet<>(numberTotalRequests);
    for (int i = 0; i < numberTotalRequests; i++) {
        callables.add(() -> {
            GatewayFilter gateway = new GatewayFilter();
            gateway.init(config);
            gateway.doFilter(request, response, chain);
            gateway.destroy();
            return null;
        });
    }
    // Make number of concurrent requests between 1-10
    int numberConcurrentRequests = rnd.nextInt(9) + 1;
    LOG.info("Executing %d total requests with %d concurrently", numberTotalRequests, numberConcurrentRequests);
    ExecutorService executor = Executors.newFixedThreadPool(numberConcurrentRequests);
    executor.invokeAll(callables);
    executor.shutdown();
    executor.awaitTermination(5, TimeUnit.SECONDS);
    assertThat(executor.isTerminated(), is(true));
    assertThat(CollectAppender.queue.size(), is(numberTotalRequests));
    // Use a set to make sure to dedupe any requestIds to get only unique ones
    Set<String> requestIds = new HashSet<>();
    for (LoggingEvent accessEvent : CollectAppender.queue) {
        verifyAuditEvent(accessEvent, CONTEXT_PATH + PATH, ResourceType.URI, Action.ACCESS, ActionOutcome.UNAVAILABLE, null, "Request method: GET");
        CorrelationContext cc = (CorrelationContext) accessEvent.getMDC(Log4jCorrelationService.MDC_CORRELATION_CONTEXT_KEY);
        // There are some events that do not have a CorrelationContext associated (ie: deploy)
        if (cc != null) {
            requestIds.add(cc.getRequestId());
        }
    }
    // There should be a unique correlation id for each request
    assertThat(requestIds.size(), is(numberTotalRequests));
}
Also used : FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) Callable(java.util.concurrent.Callable) HttpServletRequest(javax.servlet.http.HttpServletRequest) LoggingEvent(org.apache.log4j.spi.LoggingEvent) ExecutorService(java.util.concurrent.ExecutorService) ServletContext(javax.servlet.ServletContext) CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext) FilterConfig(javax.servlet.FilterConfig) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 70 with GatewayConfig

use of org.apache.knox.gateway.config.GatewayConfig 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"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Topology(org.apache.knox.gateway.topology.Topology) AbstractGatewayFilter(org.apache.knox.gateway.filter.AbstractGatewayFilter) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

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