Search in sources :

Example 1 with CorrelationContext

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

the class AuditLoggingTest method verifyAuditEvent.

private void verifyAuditEvent(LoggingEvent event, String resourceName, String resourceType, String action, String outcome, String targetService, String message) {
    event.getMDCCopy();
    CorrelationContext cc = (CorrelationContext) event.getMDC(Log4jCorrelationService.MDC_CORRELATION_CONTEXT_KEY);
    assertThat(cc, notNullValue());
    assertThat(cc.getRequestId(), is(notNullValue()));
    AuditContext ac = (AuditContext) event.getMDC(Log4jAuditService.MDC_AUDIT_CONTEXT_KEY);
    assertThat(ac, notNullValue());
    assertThat(ac.getRemoteIp(), is(ADDRESS));
    assertThat(ac.getRemoteHostname(), is(HOST));
    assertThat((String) event.getMDC(AuditConstants.MDC_SERVICE_KEY), is(AuditConstants.KNOX_SERVICE_NAME));
    assertThat((String) event.getMDC(AuditConstants.MDC_COMPONENT_KEY), is(AuditConstants.KNOX_COMPONENT_NAME));
    assertThat((String) event.getLoggerName(), is(AuditConstants.DEFAULT_AUDITOR_NAME));
    verifyValue((String) event.getMDC(AuditConstants.MDC_RESOURCE_NAME_KEY), resourceName);
    verifyValue((String) event.getMDC(AuditConstants.MDC_RESOURCE_TYPE_KEY), resourceType);
    verifyValue((String) event.getMDC(AuditConstants.MDC_ACTION_KEY), action);
    verifyValue((String) event.getMDC(AuditConstants.MDC_OUTCOME_KEY), outcome);
    verifyValue(ac.getTargetServiceName(), targetService);
    verifyValue(event.getRenderedMessage(), message);
}
Also used : CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext) AuditContext(org.apache.knox.gateway.audit.api.AuditContext)

Example 2 with CorrelationContext

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

the class CorrelationHandler method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    CorrelationService correlationService = CorrelationServiceFactory.getCorrelationService();
    CorrelationContext correlationContext = correlationService.createContext();
    correlationContext.setRequestId(UUID.randomUUID().toString());
    try {
        super.handle(target, baseRequest, request, response);
    } finally {
        // Ensure that the correlationContext is destroyed between requests
        correlationContext.destroy();
        correlationService.detachContext();
    }
}
Also used : CorrelationService(org.apache.knox.gateway.audit.api.CorrelationService) CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext)

Example 3 with CorrelationContext

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

the class AuditServiceTest method testSequentialRequests.

@Test
public void testSequentialRequests() {
    AuditContext ac = auditService.createContext();
    ac.setUsername(username);
    ac.setProxyUsername(proxyUsername);
    ac.setRemoteIp(remoteIp);
    ac.setRemoteHostname(remoteHostname);
    ac.setTargetServiceName(targetServiceName);
    CorrelationContext cc = correlationService.createContext();
    cc.setRequestId(UUID.randomUUID().toString());
    cc.setParentRequestId(UUID.randomUUID().toString());
    cc.setRootRequestId(UUID.randomUUID().toString());
    auditor.audit("action", "resource", "resource type", "outcome", "message");
    auditService.detachContext();
    correlationService.detachContext();
    assertThat(CollectAppender.queue.size(), is(1));
    LoggingEvent event = CollectAppender.queue.iterator().next();
    checkLogEventContexts(event, cc, ac);
    CollectAppender.queue.clear();
    ac = auditService.createContext();
    ac.setUsername(username + "1");
    ac.setProxyUsername(proxyUsername + "1");
    ac.setRemoteIp(remoteIp + "1");
    ac.setRemoteHostname(remoteHostname + "1");
    ac.setTargetServiceName(targetServiceName + "1");
    cc = correlationService.createContext();
    cc.setRequestId(UUID.randomUUID().toString());
    cc.setParentRequestId(UUID.randomUUID().toString());
    cc.setRootRequestId(UUID.randomUUID().toString());
    auditor.audit("action", "resource", "resource type", "outcome", "message");
    auditService.detachContext();
    correlationService.detachContext();
    assertThat(CollectAppender.queue.size(), is(1));
    event = CollectAppender.queue.iterator().next();
    checkLogEventContexts(event, cc, ac);
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext) AuditContext(org.apache.knox.gateway.audit.api.AuditContext) Test(org.junit.Test)

Example 4 with CorrelationContext

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

the class AuditServiceTest method testMultipleRequestEvents.

@Test
public void testMultipleRequestEvents() {
    int iterations = 1000;
    AuditContext ac = auditService.createContext();
    ac.setUsername(username);
    ac.setProxyUsername(proxyUsername);
    ac.setRemoteIp(remoteIp);
    ac.setRemoteHostname(remoteHostname);
    ac.setTargetServiceName(targetServiceName);
    CorrelationContext cc = correlationService.createContext();
    cc.setRequestId(UUID.randomUUID().toString());
    cc.setParentRequestId(UUID.randomUUID().toString());
    cc.setRootRequestId(UUID.randomUUID().toString());
    CollectAppender.queue.clear();
    for (int i = 0; i < iterations; i++) {
        auditor.audit("action" + i, "resource" + i, "resource type" + i, "outcome" + i, "message" + i);
    }
    auditService.detachContext();
    correlationService.detachContext();
    assertThat(CollectAppender.queue.size(), is(iterations));
    // Verify events number and audit/correlation parameters in each event
    Iterator<LoggingEvent> iterator = CollectAppender.queue.iterator();
    int counter = 0;
    while (iterator.hasNext()) {
        LoggingEvent event = iterator.next();
        checkLogEventContexts(event, cc, ac);
        assertThat((String) event.getMDC(AuditConstants.MDC_ACTION_KEY), is("action" + counter));
        assertThat((String) event.getMDC(AuditConstants.MDC_RESOURCE_NAME_KEY), is("resource" + counter));
        assertThat((String) event.getMDC(AuditConstants.MDC_RESOURCE_TYPE_KEY), is("resource type" + counter));
        assertThat((String) event.getMDC(AuditConstants.MDC_OUTCOME_KEY), is("outcome" + counter));
        assertThat((String) event.getMDC(AuditConstants.MDC_SERVICE_KEY), is(AuditConstants.KNOX_SERVICE_NAME));
        assertThat((String) event.getMDC(AuditConstants.MDC_COMPONENT_KEY), is(AuditConstants.KNOX_COMPONENT_NAME));
        assertThat((String) event.getRenderedMessage(), is("message" + counter));
        counter++;
    }
    assertThat(auditService.getContext(), nullValue());
    assertThat(correlationService.getContext(), nullValue());
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext) AuditContext(org.apache.knox.gateway.audit.api.AuditContext) Test(org.junit.Test)

Example 5 with CorrelationContext

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

the class AuditServiceTest method checkLogEventContexts.

private void checkLogEventContexts(LoggingEvent event, CorrelationContext expectedCorrelationContext, AuditContext expectedAuditContext) {
    AuditContext context = (AuditContext) event.getMDC(Log4jAuditService.MDC_AUDIT_CONTEXT_KEY);
    assertThat(context.getUsername(), is(expectedAuditContext.getUsername()));
    assertThat(context.getProxyUsername(), is(expectedAuditContext.getProxyUsername()));
    assertThat(context.getSystemUsername(), is(expectedAuditContext.getSystemUsername()));
    assertThat(context.getRemoteIp(), is(expectedAuditContext.getRemoteIp()));
    assertThat(context.getRemoteHostname(), is(expectedAuditContext.getRemoteHostname()));
    assertThat(context.getTargetServiceName(), is(expectedAuditContext.getTargetServiceName()));
    CorrelationContext correlationContext = (CorrelationContext) event.getMDC(Log4jCorrelationService.MDC_CORRELATION_CONTEXT_KEY);
    assertThat(correlationContext.getRequestId(), is(expectedCorrelationContext.getRequestId()));
    assertThat(correlationContext.getRootRequestId(), is(expectedCorrelationContext.getRootRequestId()));
    assertThat(correlationContext.getParentRequestId(), is(expectedCorrelationContext.getParentRequestId()));
}
Also used : CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext) AuditContext(org.apache.knox.gateway.audit.api.AuditContext)

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