use of org.apache.knox.gateway.audit.api.CorrelationContext in project knox by apache.
the class TraceUtil method appendCorrelationContext.
static final void appendCorrelationContext(final StringBuilder sb) {
CorrelationContext cc = cs.getContext();
if (cc == null) {
sb.append("||");
} else {
append(sb, cc.getRootRequestId());
sb.append("|");
append(sb, cc.getParentRequestId());
sb.append("|");
append(sb, cc.getRequestId());
}
}
use of org.apache.knox.gateway.audit.api.CorrelationContext in project knox by apache.
the class GatewayCorrelationIdTest method testTestService.
@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testTestService() throws Exception {
LOG_ENTER();
String username = "guest";
String password = "guest-password";
String serviceUrl = clusterUrl + "/test-service-path/test-service-resource";
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(() -> {
given().auth().preemptive().basic(username, password).then().statusCode(HttpStatus.SC_OK).contentType("text/plain").body(is("test-service-response")).when().get(serviceUrl);
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));
// 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) {
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));
LOG_EXIT();
}
use of org.apache.knox.gateway.audit.api.CorrelationContext in project knox by apache.
the class AuditLayoutTest method testFullyFilledAuditEvent.
@Test
public void testFullyFilledAuditEvent() {
AuditContext auditContext = auditService.createContext();
auditContext.setProxyUsername(PROXYUSERNAME);
auditContext.setSystemUsername(SYSTEMUSERNAME);
auditContext.setUsername(USERNAME);
auditContext.setRemoteHostname(HOST_NAME);
auditContext.setRemoteIp(HOST_ADDRESS);
auditContext.setTargetServiceName(TARGET_SERVICE);
CorrelationContext correlationContext = correlationService.createContext();
correlationContext.setRequestId(REQUEST_ID);
correlationContext.setParentRequestId(PARENT_REQUEST_ID);
correlationContext.setRootRequestId(ROOT_REQUEST_ID);
auditor.audit(ACTION, RESOURCE_NAME, RESOURCE_TYPE, OUTCOME, MESSAGE);
assertThat(CollectAppender.queue.size(), is(1));
LoggingEvent event = CollectAppender.queue.iterator().next();
SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
String formatedDate = format.format(new Date(event.getTimeStamp()));
// 14/01/24 12:40:24 1|2|3|audit.forward|hostaddress|WEBHDFS|username|proxy_username|system_username|action|resource_type|resource_name|outcome|message
String expectedOutput = String.format(RECORD_PATTERN, formatedDate, ROOT_REQUEST_ID, PARENT_REQUEST_ID, REQUEST_ID, "audit.forward", HOST_ADDRESS, TARGET_SERVICE, USERNAME, PROXYUSERNAME, SYSTEMUSERNAME, ACTION, RESOURCE_TYPE, RESOURCE_NAME, OUTCOME, MESSAGE, AuditLayout.LINE_SEP);
String auditOutput = layout.format(event);
assertThat(auditOutput, is(expectedOutput));
}
use of org.apache.knox.gateway.audit.api.CorrelationContext in project knox by apache.
the class Log4jCorrelationService method readExternalizedContext.
@Override
public CorrelationContext readExternalizedContext(byte[] externalizedContext) {
ByteArrayInputStream bais = new ByteArrayInputStream(externalizedContext);
ObjectInput oi = null;
CorrelationContext context = null;
try {
oi = new ObjectInputStream(bais);
context = (CorrelationContext) oi.readObject();
} catch (IOException e) {
throw new IllegalArgumentException(e);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
} finally {
try {
bais.close();
if (oi != null) {
oi.close();
}
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
return context;
}
use of org.apache.knox.gateway.audit.api.CorrelationContext in project knox by apache.
the class AuditLayout method format.
@Override
public String format(LoggingEvent event) {
sb.setLength(0);
dateFormat(sb, event);
CorrelationContext cc = (CorrelationContext) event.getMDC(Log4jCorrelationService.MDC_CORRELATION_CONTEXT_KEY);
AuditContext ac = (AuditContext) event.getMDC(Log4jAuditService.MDC_AUDIT_CONTEXT_KEY);
appendParameter(cc == null ? null : cc.getRootRequestId());
appendParameter(cc == null ? null : cc.getParentRequestId());
appendParameter(cc == null ? null : cc.getRequestId());
appendParameter(event.getLoggerName());
appendParameter(ac == null ? null : ac.getRemoteIp());
appendParameter(ac == null ? null : ac.getTargetServiceName());
appendParameter(ac == null ? null : ac.getUsername());
appendParameter(ac == null ? null : ac.getProxyUsername());
appendParameter(ac == null ? null : ac.getSystemUsername());
appendParameter((String) event.getMDC(AuditConstants.MDC_ACTION_KEY));
appendParameter((String) event.getMDC(AuditConstants.MDC_RESOURCE_TYPE_KEY));
appendParameter((String) event.getMDC(AuditConstants.MDC_RESOURCE_NAME_KEY));
appendParameter((String) event.getMDC(AuditConstants.MDC_OUTCOME_KEY));
String message = event.getRenderedMessage();
sb.append(message == null ? "" : message).append(LINE_SEP);
return sb.toString();
}
Aggregations