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