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);
}
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();
}
}
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);
}
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());
}
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()));
}
Aggregations