[LOG4J2-2420] allow events without attributes
diff --git a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/AbstractEventLogger.java b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/AbstractEventLogger.java
index da2254a..cda7207 100644
--- a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/AbstractEventLogger.java
+++ b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/AbstractEventLogger.java
@@ -32,7 +32,7 @@
import java.util.Map;
import java.util.Set;
-import static org.apache.logging.log4j.catalog.api.constant.Constants.*;
+import static java.util.Collections.*;
/**
* This class is used to log events generated remotely.
@@ -109,10 +109,15 @@
AuditExceptionHandler exceptionHandler) {
AuditMessage msg = new AuditMessage(eventName, maxLength);
+ if (attributes == null) {
+ attributes = emptyMap();
+ }
+
StringBuilder missingAttributes = new StringBuilder();
StringBuilder errors = new StringBuilder();
- for (EventAttribute eventAttribute : event.getAttributes()) {
+ List<EventAttribute> eventAttributes = event.getAttributes() == null ? emptyList() : event.getAttributes();
+ for (EventAttribute eventAttribute : eventAttributes) {
Attribute attr = catalogManager.getAttribute(eventAttribute.getName(), event.getCatalogId());
if ((!attr.isRequestContext() && (attr.isRequired()) ||
(eventAttribute.isRequired() != null && eventAttribute.isRequired()))) {
diff --git a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManagerImpl.java b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManagerImpl.java
index 538d4ca..1cd9831 100644
--- a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManagerImpl.java
+++ b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManagerImpl.java
@@ -36,6 +36,7 @@
import org.apache.logging.log4j.catalog.api.CatalogReader;
import org.apache.logging.log4j.catalog.api.EventAttribute;
+import static java.util.Collections.emptyList;
import static org.apache.logging.log4j.catalog.api.constant.Constants.DEFAULT_CATALOG;
/**
@@ -83,11 +84,12 @@
public Map<String, Attribute> getAttributes(String eventName, String catalogId) {
Event event = getEvent(eventName, catalogId);
if (event == null) {
- logger.warn("No event named {} counld be found in catalog {}", eventName, catalogId);
+ logger.warn("The event named {} could not be found in catalog {}", eventName, catalogId);
return null;
}
- Map<String, Attribute> attributes = new HashMap<>(event.getAttributes().size());
- for (EventAttribute eventAttribute : event.getAttributes()) {
+ List<EventAttribute> eventAttributes = event.getAttributes() == null ? emptyList() : event.getAttributes();
+ Map<String, Attribute> attributes = new HashMap<>(eventAttributes.size());
+ for (EventAttribute eventAttribute : eventAttributes) {
Attribute attr = getAttribute(eventAttribute.getName(), event.getCatalogId());
if (attr != null) {
attributes.put(attr.getName(), attr);
@@ -128,22 +130,27 @@
}
private Map<String, Map<String, CatalogInfo>> initializeData(CatalogReader catalogReader) throws Exception {
- String catalog = catalogReader.readCatalog();
JsonFactory factory = new JsonFactory();
factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
ObjectMapper mapper = new ObjectMapper(factory);
+
+ String catalog = catalogReader.readCatalog();
catalogData = mapper.readValue(catalog, CatalogData.class);
- for (Attribute attr : catalogData.getAttributes()) {
- if (attr.isRequestContext()) {
- requestContextAttributes.put(attr.getName(), attr);
+
+ if (catalogData.getAttributes() != null) {
+ for (Attribute attr : catalogData.getAttributes()) {
+ if (attr.isRequestContext()) {
+ requestContextAttributes.put(attr.getName(), attr);
+ }
+ Map<String, Attribute> attrMap = attributeMap.get(attr.getCatalogId());
+ if (attrMap == null) {
+ attrMap = new HashMap<>();
+ attributeMap.put(attr.getCatalogId(), attrMap);
+ }
+ attrMap.put(attr.getName(), attr);
}
- Map<String, Attribute> attrMap = attributeMap.get(attr.getCatalogId());
- if (attrMap == null) {
- attrMap = new HashMap<>();
- attributeMap.put(attr.getCatalogId(), attrMap);
- }
- attrMap.put(attr.getName(), attr);
}
+
Map<String, Map<String, CatalogInfo>> map = new HashMap<>();
map.put(DEFAULT_CATALOG, new HashMap<>());
for (Event event : catalogData.getEvents()) {
diff --git a/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/AuditLoggerTest.java b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/AuditLoggerTest.java
index abe6eb8..b497740 100644
--- a/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/AuditLoggerTest.java
+++ b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/AuditLoggerTest.java
@@ -23,6 +23,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.audit.catalog.StringCatalogReader;
import org.apache.logging.log4j.audit.exception.AuditException;
+import org.apache.logging.log4j.catalog.api.CatalogReader;
+import org.apache.logging.log4j.catalog.api.dao.ClassPathCatalogReader;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
@@ -31,6 +33,7 @@
import org.junit.BeforeClass;
import org.junit.Test;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -44,18 +47,15 @@
*/
public class AuditLoggerTest {
- private static AbstractEventLogger auditLogger;
-
- private static CatalogManager catalogManager;
-
+ private static CatalogReader catalogReader;
private static LoggerContext ctx;
private static ListAppender app;
+ private AbstractEventLogger auditLogger;
+
@BeforeClass
public static void setupClass() throws Exception {
- catalogManager = new CatalogManagerImpl(new StringCatalogReader());
- auditLogger = new AuditLogger();
- auditLogger.setCatalogManager(catalogManager);
+ catalogReader = new StringCatalogReader();
ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
for (Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
@@ -67,13 +67,22 @@
assertNotNull("No Appender", app);
}
+ private AbstractEventLogger buildAuditLogger(CatalogReader catalogReader) throws Exception {
+ CatalogManager catalogManager = new CatalogManagerImpl(catalogReader);
+ AuditLogger auditLogger = new AuditLogger();
+ auditLogger.setCatalogManager(catalogManager);
+ return auditLogger;
+ }
+
@Before
public void before() {
app.clear();
}
@Test
- public void testAuditLogger() {
+ public void testAuditLogger() throws Exception {
+ auditLogger = buildAuditLogger(catalogReader);
+
ThreadContext.put("companyId", "12345");
ThreadContext.put("ipAddress", "127.0.0.1");
ThreadContext.put("environment", "dev");
@@ -100,7 +109,9 @@
}
@Test(expected = AuditException.class)
- public void testBadAttribute() {
+ public void testBadAttribute() throws Exception {
+ auditLogger = buildAuditLogger(catalogReader);
+
ThreadContext.put("companyId", "12345");
ThreadContext.put("ipAddress", "127.0.0.1");
ThreadContext.put("environment", "dev");
@@ -112,4 +123,11 @@
properties.put("amount", "111.55");
auditLogger.logEvent("transfer", properties);
}
-}
+
+ @Test
+ public void testAuditLoggerWithBasicCatalog() throws Exception {
+ auditLogger = buildAuditLogger(new ClassPathCatalogReader(Collections.singletonMap("catalogFile", "basicCatalog.json")));
+
+ auditLogger.logEvent("login", null);
+ }
+}
\ No newline at end of file
diff --git a/log4j-audit/log4j-audit-api/src/test/resources/basicCatalog.json b/log4j-audit/log4j-audit-api/src/test/resources/basicCatalog.json
new file mode 100644
index 0000000..297a36b
--- /dev/null
+++ b/log4j-audit/log4j-audit-api/src/test/resources/basicCatalog.json
@@ -0,0 +1,5 @@
+{
+ "events" : [ {
+ "name" : "login"
+ } ]
+}
\ No newline at end of file