Search in sources :

Example 6 with CorrelationContext

use of org.apache.knox.gateway.audit.api.CorrelationContext in project knox by apache.

the class Log4jCorrelationService method detachContext.

@Override
public CorrelationContext detachContext() {
    CorrelationContext context = (CorrelationContext) MDC.get(MDC_CORRELATION_CONTEXT_KEY);
    MDC.remove(MDC_CORRELATION_CONTEXT_KEY);
    return context;
}
Also used : CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext)

Example 7 with CorrelationContext

use of org.apache.knox.gateway.audit.api.CorrelationContext in project knox by apache.

the class Log4jCorrelationService method createContext.

@Override
public CorrelationContext createContext() {
    CorrelationContext context = getContext();
    if (context == null) {
        context = new Log4jCorrelationContext();
        attachContext(context);
    }
    return context;
}
Also used : CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext)

Example 8 with CorrelationContext

use of org.apache.knox.gateway.audit.api.CorrelationContext in project knox by apache.

the class Log4jCorrelationService method attachExternalizedContext.

@Override
public CorrelationContext attachExternalizedContext(byte[] externalizedContext) {
    CorrelationContext context = readExternalizedContext(externalizedContext);
    attachContext(context);
    return context;
}
Also used : CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext)

Example 9 with CorrelationContext

use of org.apache.knox.gateway.audit.api.CorrelationContext in project knox by apache.

the class GatewayFilter method assignCorrelationRequestId.

// Now creating the correlation context only if required since it may be created upstream in the CorrelationHandler.
private void assignCorrelationRequestId() {
    CorrelationContext correlationContext = CorrelationServiceFactory.getCorrelationService().getContext();
    if (correlationContext == null) {
        correlationContext = CorrelationServiceFactory.getCorrelationService().createContext();
    }
    String requestId = correlationContext.getRequestId();
    if (requestId == null) {
        correlationContext.setRequestId(UUID.randomUUID().toString());
    }
}
Also used : CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext)

Example 10 with CorrelationContext

use of org.apache.knox.gateway.audit.api.CorrelationContext 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)

Aggregations

CorrelationContext (org.apache.knox.gateway.audit.api.CorrelationContext)16 AuditContext (org.apache.knox.gateway.audit.api.AuditContext)7 LoggingEvent (org.apache.log4j.spi.LoggingEvent)5 Test (org.junit.Test)5 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ObjectInput (java.io.ObjectInput)1 ObjectInputStream (java.io.ObjectInputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 FilterChain (javax.servlet.FilterChain)1 FilterConfig (javax.servlet.FilterConfig)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CorrelationService (org.apache.knox.gateway.audit.api.CorrelationService)1 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)1