use of co.cask.cdap.common.logging.AuditLogEntry in project cdap by caskdata.
the class AuditLogHandler method logRequest.
private void logRequest(HttpServletRequest request, HttpServletResponse response) throws UnknownHostException {
AuditLogEntry logEntry = new AuditLogEntry();
logEntry.setUserName(request.getRemoteUser());
logEntry.setClientIP(InetAddress.getByName(request.getRemoteAddr()));
logEntry.setRequestLine(request.getMethod(), request.getRequestURI(), request.getProtocol());
logEntry.setResponseCode(response.getStatus());
logEntry.setResponseContentLength(((Response) response).getContentCount());
logger.trace(logEntry.toString());
}
use of co.cask.cdap.common.logging.AuditLogEntry in project cdap by caskdata.
the class SecurityAuthenticationHttpHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, final MessageEvent event) throws Exception {
Object msg = event.getMessage();
if (!(msg instanceof HttpRequest)) {
super.messageReceived(ctx, event);
} else {
AuditLogEntry logEntry = new AuditLogEntry();
ctx.setAttachment(logEntry);
HttpRequest req = (HttpRequest) msg;
if (matchBypassPattern(req) || validateSecuredInterception(ctx, req, event.getChannel(), logEntry)) {
Channels.fireMessageReceived(ctx, msg, event.getRemoteAddress());
}
// we write the response directly for authentication failure, so nothing to do for else
}
}
use of co.cask.cdap.common.logging.AuditLogEntry in project cdap by caskdata.
the class SecurityAuthenticationHttpHandler method getLogEntry.
private AuditLogEntry getLogEntry(ChannelHandlerContext ctx) {
Object entryObject = ctx.getAttachment();
AuditLogEntry logEntry;
if (entryObject != null && entryObject instanceof AuditLogEntry) {
logEntry = (AuditLogEntry) entryObject;
} else {
logEntry = new AuditLogEntry();
ctx.setAttachment(logEntry);
}
return logEntry;
}
use of co.cask.cdap.common.logging.AuditLogEntry in project cdap by caskdata.
the class SecurityAuthenticationHttpHandler method writeRequested.
@Override
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
AuditLogEntry logEntry = getLogEntry(ctx);
boolean isLogResponseBody = logEntry.isLogResponseBody();
Object message = e.getMessage();
if (message instanceof HttpResponse) {
HttpResponse response = (HttpResponse) message;
logEntry.setResponseCode(response.getStatus().getCode());
if (isLogResponseBody) {
ChannelBuffer body = response.getContent();
if (body.readable()) {
logEntry.setResponseBody(body.toString(Charsets.UTF_8));
}
}
if (response.containsHeader(HttpHeaders.Names.CONTENT_LENGTH)) {
String lengthString = response.getHeader(HttpHeaders.Names.CONTENT_LENGTH);
try {
logEntry.setResponseContentLength(Long.valueOf(lengthString));
} catch (NumberFormatException nfe) {
LOG.warn("Invalid value for content length in HTTP response message: {}", lengthString, nfe);
}
}
} else if (message instanceof ChannelBuffer) {
// for chunked responses the response code will only be present on the first chunk
// so we only look for it the first time around
ChannelBuffer channelBuffer = (ChannelBuffer) message;
if (logEntry.getResponseCode() == null) {
logEntry.setResponseCode(findResponseCode(channelBuffer));
if (logEntry.getResponseCode() != null) {
// we currently only look for a Content-Length header in the first buffer on an HTTP response
// this is a limitation of the implementation that simplifies header parsing
logEntry.setResponseContentLength(findContentLength(channelBuffer));
if (isLogResponseBody) {
logEntry.setResponseBody(findResponseBody(channelBuffer, true));
}
}
} else if (isLogResponseBody) {
logEntry.appendResponseBody(findResponseBody(channelBuffer, false));
}
} else {
LOG.debug("Unhandled response message type: {}", message.getClass());
}
super.writeRequested(ctx, e);
}
use of co.cask.cdap.common.logging.AuditLogEntry in project cdap by caskdata.
the class AuthorizationHandler method createLogEntry.
private void createLogEntry(HttpRequest httpRequest, @Nullable AuthorizationRequest request, HttpResponseStatus responseStatus) throws UnknownHostException {
AuditLogEntry logEntry = new AuditLogEntry();
logEntry.setUserName(Objects.firstNonNull(authenticationContext.getPrincipal().getName(), "-"));
logEntry.setClientIP(InetAddress.getByName(Objects.firstNonNull(SecurityRequestContext.getUserIP(), "0.0.0.0")));
logEntry.setRequestLine(httpRequest.getMethod(), httpRequest.getUri(), httpRequest.getProtocolVersion());
if (request != null) {
logEntry.setRequestBody(String.format("[%s %s %s]", request.getPrincipal(), request.getEntity(), request.getActions()));
}
logEntry.setResponseCode(responseStatus.getCode());
AUDIT_LOG.trace(logEntry.toString());
}
Aggregations