Search in sources :

Example 1 with AuditContext

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

use of org.apache.knox.gateway.audit.api.AuditContext 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 3 with AuditContext

use of org.apache.knox.gateway.audit.api.AuditContext 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 4 with AuditContext

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

Example 5 with AuditContext

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

the class Log4jAuditService method createContext.

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

Aggregations

AuditContext (org.apache.knox.gateway.audit.api.AuditContext)11 CorrelationContext (org.apache.knox.gateway.audit.api.CorrelationContext)7 LoggingEvent (org.apache.log4j.spi.LoggingEvent)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 URISyntaxException (java.net.URISyntaxException)1 Principal (java.security.Principal)1 PrivilegedActionException (java.security.PrivilegedActionException)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 FilterChain (javax.servlet.FilterChain)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 PrimaryPrincipal (org.apache.knox.gateway.security.PrimaryPrincipal)1 TokenServiceException (org.apache.knox.gateway.services.security.token.TokenServiceException)1 Topology (org.apache.knox.gateway.topology.Topology)1 Matcher (org.apache.knox.gateway.util.urltemplate.Matcher)1 Template (org.apache.knox.gateway.util.urltemplate.Template)1