Search in sources :

Example 11 with CorrelationContext

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());
    }
}
Also used : CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext)

Example 12 with CorrelationContext

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();
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext) Test(org.junit.Test)

Example 13 with CorrelationContext

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));
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext) SimpleDateFormat(java.text.SimpleDateFormat) AuditContext(org.apache.knox.gateway.audit.api.AuditContext) Date(java.util.Date) Test(org.junit.Test)

Example 14 with CorrelationContext

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CorrelationContext(org.apache.knox.gateway.audit.api.CorrelationContext) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 15 with CorrelationContext

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