[LOG4J2-2420] allow events without attributes
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/commit/d98904d9 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/tree/d98904d9 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/diff/d98904d9 Branch: refs/heads/master Commit: d98904d92329b7ffe2da5e59830d09200528c875 Parents: 02529bb Author: Andrei Ivanov <andrei.iva...@arnia.ro> Authored: Mon Sep 17 15:29:04 2018 +0300 Committer: Andrei Ivanov <andrei.iva...@arnia.ro> Committed: Mon Sep 17 15:29:04 2018 +0300 ---------------------------------------------------------------------- .../log4j/audit/AbstractEventLogger.java | 9 +++-- .../log4j/audit/catalog/CatalogManagerImpl.java | 33 ++++++++++------- .../logging/log4j/audit/AuditLoggerTest.java | 38 ++++++++++++++------ .../src/test/resources/basicCatalog.json | 5 +++ 4 files changed, 60 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/d98904d9/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/AbstractEventLogger.java ---------------------------------------------------------------------- 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.List; 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 @@ public abstract class AbstractEventLogger { 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()))) { http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/d98904d9/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/catalog/CatalogManagerImpl.java ---------------------------------------------------------------------- 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.Event; 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 class CatalogManagerImpl implements CatalogManager { 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 @@ public class CatalogManagerImpl implements CatalogManager { } 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); - } - Map<String, Attribute> attrMap = attributeMap.get(attr.getCatalogId()); - if (attrMap == null) { - attrMap = new HashMap<>(); - attributeMap.put(attr.getCatalogId(), attrMap); + + 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); } - attrMap.put(attr.getName(), attr); } + Map<String, Map<String, CatalogInfo>> map = new HashMap<>(); map.put(DEFAULT_CATALOG, new HashMap<>()); for (Event event : catalogData.getEvents()) { http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/d98904d9/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/AuditLoggerTest.java ---------------------------------------------------------------------- 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.audit.catalog.CatalogManagerImpl; 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.Before; 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 @@ import static org.junit.Assert.fail; */ 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 @@ public class AuditLoggerTest { 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 @@ public class AuditLoggerTest { } @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 @@ public class AuditLoggerTest { 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 http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/d98904d9/log4j-audit/log4j-audit-api/src/test/resources/basicCatalog.json ---------------------------------------------------------------------- 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