https://issues.apache.org/jira/browse/AMQ-3100 - audit logging provide more data in audit loge event object

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1060803 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bosanac Dejan 2011-01-19 13:13:50 +00:00
parent a0a1e643e4
commit b026971933
9 changed files with 166 additions and 23 deletions

View File

@ -16,9 +16,7 @@
*/ */
package org.apache.activemq.broker.jmx; package org.apache.activemq.broker.jmx;
import org.apache.activemq.broker.util.AuditLog; import org.apache.activemq.broker.util.*;
import org.apache.activemq.broker.util.AuditLogService;
import org.apache.activemq.broker.util.DefaultAuditLog;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -179,7 +177,14 @@ public class AnnotatedMBean extends StandardMBean {
caller += principal.getName() + " "; caller += principal.getName() + " ";
} }
} }
auditLog.log(caller.trim() + " called " + this.getMBeanInfo().getClassName() + "." + s + Arrays.toString(objects));
AuditLogEntry entry = new JMXAuditLogEntry();
entry.setUser(caller);
entry.setTimestamp(System.currentTimeMillis());
entry.setOperation(this.getMBeanInfo().getClassName() + "." + s);
entry.getParameters().put("arguments", objects);
auditLog.log(entry);
} }
return super.invoke(s, objects, strings); return super.invoke(s, objects, strings);
} }

View File

@ -19,6 +19,6 @@ package org.apache.activemq.broker.util;
public interface AuditLog { public interface AuditLog {
public void log(String message); public void log(AuditLogEntry entry);
} }

View File

@ -0,0 +1,79 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.broker.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class AuditLogEntry {
protected String user = "anonymous";
protected long timestamp;
protected String operation;
protected String remoteAddr;
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss,SSS");
protected Map<String, Object> parameters = new HashMap<String, Object>();
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getFormattedTime() {
return formatter.format(new Date(timestamp));
}
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public String getRemoteAddr() {
return remoteAddr;
}
public void setRemoteAddr(String remoteAddr) {
this.remoteAddr = remoteAddr;
}
public Map<String, Object> getParameters() {
return parameters;
}
public void setParameters(Map<String, Object> parameters) {
this.parameters = parameters;
}
}

View File

@ -33,9 +33,9 @@ public class AuditLogService {
factory = new DefaultAuditLogFactory(); factory = new DefaultAuditLogFactory();
} }
public void log(String message) { public void log(AuditLogEntry entry) {
for (AuditLog log : factory.getAuditLogs()) { for (AuditLog log : factory.getAuditLogs()) {
log.log(message); log.log(entry);
} }
} }

View File

@ -23,7 +23,7 @@ public class DefaultAuditLog implements AuditLog {
private static final Log LOG = LogFactory.getLog("org.apache.activemq.audit"); private static final Log LOG = LogFactory.getLog("org.apache.activemq.audit");
public void log(String message) { public void log(AuditLogEntry entry) {
LOG.info(message); LOG.info(entry.toString());
} }
} }

View File

@ -0,0 +1,27 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.broker.util;
import java.util.Arrays;
public class JMXAuditLogEntry extends AuditLogEntry {
@Override
public String toString() {
return user.trim() + " called " + operation + Arrays.toString((Object[])parameters.get("arguments")) + " at " + getFormattedTime();
}
}

View File

@ -16,16 +16,14 @@
*/ */
package org.apache.activemq.web; package org.apache.activemq.web;
import org.apache.activemq.broker.util.AuditLog; import org.apache.activemq.broker.util.AuditLogEntry;
import org.apache.activemq.broker.util.AuditLogService; import org.apache.activemq.broker.util.AuditLogService;
import org.apache.activemq.broker.util.DefaultAuditLog;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import javax.servlet.*; import javax.servlet.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
import java.util.Enumeration;
public class AuditFilter implements Filter { public class AuditFilter implements Filter {
@ -48,18 +46,15 @@ public class AuditFilter implements Filter {
if (audit && request instanceof HttpServletRequest) { if (audit && request instanceof HttpServletRequest) {
HttpServletRequest http = (HttpServletRequest)request; HttpServletRequest http = (HttpServletRequest)request;
Enumeration params = http.getParameterNames(); AuditLogEntry entry = new HttpAuditLogEntry();
String formattedParams = "";
while (params.hasMoreElements()) {
String paramName = (String)params.nextElement();
String paramValue = http.getParameter(paramName);
formattedParams += paramName + "='" + paramValue + "' ";
}
String user = "anonymous";
if (http.getRemoteUser() != null) { if (http.getRemoteUser() != null) {
user = http.getRemoteUser(); entry.setUser(http.getRemoteUser());
} }
auditLog.log(user + " requested " + http.getRequestURI() + " [" + formattedParams + "] from " + http.getRemoteAddr()); entry.setTimestamp(System.currentTimeMillis());
entry.setOperation(http.getRequestURI());
entry.setRemoteAddr(http.getRemoteAddr());
entry.getParameters().put("params", http.getParameterMap());
auditLog.log(entry);
} }
chain.doFilter(request, response); chain.doFilter(request, response);
} }

View File

@ -0,0 +1,37 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.web;
import org.apache.activemq.broker.util.AuditLogEntry;
import java.util.Arrays;
import java.util.Map;
public class HttpAuditLogEntry extends AuditLogEntry {
@Override
public String toString() {
String formattedParams = "";
Map<String, String[]> params = (Map<String, String[]>)parameters.get("params");
if (params != null) {
for (String paramName : params.keySet()) {
formattedParams += paramName + "='" + Arrays.toString(params.get(paramName)) + "' ";
}
}
return user + " requested " + operation + " [" + formattedParams + "] from " + remoteAddr +" at " + getFormattedTime();
}
}

View File

@ -63,4 +63,4 @@ log4j.appender.audit.maxFileSize=1024KB
log4j.appender.audit.maxBackupIndex=5 log4j.appender.audit.maxBackupIndex=5
log4j.appender.audit.append=true log4j.appender.audit.append=true
log4j.appender.audit.layout=org.apache.log4j.PatternLayout log4j.appender.audit.layout=org.apache.log4j.PatternLayout
log4j.appender.audit.layout.ConversionPattern=%d | %-5p | %m | %t%n log4j.appender.audit.layout.ConversionPattern=%-5p | %m | %t%n