From 0c8f9d7d2aabc717660e13166f564382bf4d5553 Mon Sep 17 00:00:00 2001 From: maximthomas Date: Fri, 15 Dec 2023 15:34:08 +0300 Subject: [PATCH] added JSONStdout audit logger --- .../openam-audit-configuration/pom.xml | 5 ++ .../JsonStdoutAuditEventHandlerFactory.java | 44 ++++++++++ .../resources/JSONStdout.section.properties | 32 ++++++++ .../src/main/resources/audit.properties | 16 ++++ .../src/main/resources/audit.xml | 80 ++++++++++++++++++- ...sonStdoutAuditEventHandlerFactoryTest.java | 71 ++++++++++++++++ pom.xml | 2 +- 7 files changed, 247 insertions(+), 3 deletions(-) create mode 100644 openam-audit/openam-audit-configuration/src/main/java/org/forgerock/openam/audit/events/handlers/JsonStdoutAuditEventHandlerFactory.java create mode 100644 openam-audit/openam-audit-configuration/src/main/resources/JSONStdout.section.properties create mode 100644 openam-audit/openam-audit-configuration/src/test/java/org/forgerock/openam/audit/events/handlers/JsonStdoutAuditEventHandlerFactoryTest.java diff --git a/openam-audit/openam-audit-configuration/pom.xml b/openam-audit/openam-audit-configuration/pom.xml index 8caea440a3..b24a6f4c85 100644 --- a/openam-audit/openam-audit-configuration/pom.xml +++ b/openam-audit/openam-audit-configuration/pom.xml @@ -13,6 +13,7 @@ * information: "Portions copyright [year] [name of copyright owner]". * * Copyright 2014-2016 ForgeRock AS. + * Portions copyright 2023 3A Systems LLC --> @@ -63,6 +64,10 @@ org.openidentityplatform.commons.audit handler-jms + + org.openidentityplatform.commons.audit + handler-json + com.zaxxer HikariCP diff --git a/openam-audit/openam-audit-configuration/src/main/java/org/forgerock/openam/audit/events/handlers/JsonStdoutAuditEventHandlerFactory.java b/openam-audit/openam-audit-configuration/src/main/java/org/forgerock/openam/audit/events/handlers/JsonStdoutAuditEventHandlerFactory.java new file mode 100644 index 0000000000..d840257925 --- /dev/null +++ b/openam-audit/openam-audit-configuration/src/main/java/org/forgerock/openam/audit/events/handlers/JsonStdoutAuditEventHandlerFactory.java @@ -0,0 +1,44 @@ +/* + * The contents of this file are subject to the terms of the Common Development and + * Distribution License (the License). You may not use this file except in compliance with the + * License. + * + * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the + * specific language governing permission and limitations under the License. + * + * When distributing Covered Software, include this CDDL Header Notice in each file and include + * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL + * Header, with the fields enclosed by brackets [] replaced by your own identifying + * information: "Portions copyright [year] [name of copyright owner]". + * + * Copyright 2023 3A Systems LLC + */ + +package org.forgerock.openam.audit.events.handlers; + +import org.forgerock.audit.AuditException; +import org.forgerock.audit.events.handlers.AuditEventHandler; +import org.forgerock.audit.handlers.json.JsonStdoutAuditEventHandler; +import org.forgerock.audit.handlers.json.JsonStdoutAuditEventHandlerConfiguration; +import org.forgerock.openam.audit.AuditEventHandlerFactory; +import org.forgerock.openam.audit.configuration.AuditEventHandlerConfiguration; + +import java.util.Map; +import java.util.Set; + +import static com.sun.identity.shared.datastruct.CollectionHelper.getBooleanMapAttr; + +public class JsonStdoutAuditEventHandlerFactory implements AuditEventHandlerFactory { + + @Override + public AuditEventHandler create(AuditEventHandlerConfiguration configuration) throws AuditException { + Map> attributes = configuration.getAttributes(); + + JsonStdoutAuditEventHandlerConfiguration handlerConfig = new JsonStdoutAuditEventHandlerConfiguration(); + handlerConfig.setTopics(attributes.get("topics")); + handlerConfig.setName(configuration.getHandlerName()); + handlerConfig.setEnabled(getBooleanMapAttr(attributes, "enabled", false)); + handlerConfig.setElasticsearchCompatible(getBooleanMapAttr(attributes, "elasticsearchCompatible", false)); + return new JsonStdoutAuditEventHandler(handlerConfig, configuration.getEventTopicsMetaData()); + } +} diff --git a/openam-audit/openam-audit-configuration/src/main/resources/JSONStdout.section.properties b/openam-audit/openam-audit-configuration/src/main/resources/JSONStdout.section.properties new file mode 100644 index 0000000000..5afe073d4f --- /dev/null +++ b/openam-audit/openam-audit-configuration/src/main/resources/JSONStdout.section.properties @@ -0,0 +1,32 @@ +# +# The contents of this file are subject to the terms of the Common Development and +# Distribution License (the License). You may not use this file except in compliance with the +# License. +# +# You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the +# specific language governing permission and limitations under the License. +# +# When distributing Covered Software, include this CDDL Header Notice in each file and include +# the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL +# Header, with the fields enclosed by brackets [] replaced by your own identifying +# information: "Portions copyright [year] [name of copyright owner]". +# +# Copyright 2016 ForgeRock AS. +# Portions copyright 2023 3A Systems LLC +# + +######################################################################################################################## +# Common handler section properties +######################################################################################################################## +commonHandler=enabled +commonHandler=topics + +######################################################################################################################## +# Common handler plugin section properties +######################################################################################################################## +commonHandlerPlugin=handlerFactory + +######################################################################################################################## +# JMS handler section properties +######################################################################################################################## +jsonStdoutConfig=elasticsearchCompatible diff --git a/openam-audit/openam-audit-configuration/src/main/resources/audit.properties b/openam-audit/openam-audit-configuration/src/main/resources/audit.properties index aa86c0fc18..e24f27cbeb 100644 --- a/openam-audit/openam-audit-configuration/src/main/resources/audit.properties +++ b/openam-audit/openam-audit-configuration/src/main/resources/audit.properties @@ -11,6 +11,7 @@ # information: "Portions copyright [year] [name of copyright owner]". # # Copyright 2015-2016 ForgeRock AS. +# Portions copyright 2023 3A Systems LLC ######################################################################################################################## @@ -41,6 +42,7 @@ handler.name.jdbc=JDBC handler.name.syslog=Syslog handler.name.elasticsearch=Elasticsearch handler.name.jms=JMS +handler.name.jsonstdout=JSONStdout # Additions to handler properties. Other properties are stored in commons audit translation.properties. audit.handlers.jdbc.databaseType.help=Select the database to use for logging audit events. @@ -194,6 +196,17 @@ section.label.AuditService.Global.splunkBuffering=Buffering section.label.AuditService.Organization.splunkConfig=Splunk Configuration section.label.AuditService.Organization.splunkBuffering=Buffering +######################################################################################################################## +# JSONStdout handler section properties +######################################################################################################################## +sections.JSONStdout=\ + commonHandler \ + jsonStdoutConfig \ + commonHandlerPlugin + +section.label.AuditService.Global.jsonStdoutConfig=JSONStdout Configuration + +section.label.AuditService.Organization.jsonStdoutConfig=JSONStdout Configuration ######################################################################################################################## # NOTE: @@ -427,3 +440,6 @@ audit.handlers.splunk.bufferingMaxEvents.help=Maximum number of audit logs in th events are dropped audit.handlers.splunk.bufferingWriteInterval=Write interval (in milliseconds) audit.handlers.splunk.bufferingWriteInterval.help=Interval at which buffered events are written to Splunk + +#JSON handler configuration +audit.handlers.json.elasticsearchCompatible.enabled=ElasticSearch JSON Format Compatible \ No newline at end of file diff --git a/openam-audit/openam-audit-configuration/src/main/resources/audit.xml b/openam-audit/openam-audit-configuration/src/main/resources/audit.xml index 4335b445fb..bfd9e9455d 100755 --- a/openam-audit/openam-audit-configuration/src/main/resources/audit.xml +++ b/openam-audit/openam-audit-configuration/src/main/resources/audit.xml @@ -13,6 +13,7 @@ * information: "Portions copyright [year] [name of copyright owner]". * * Copyright 2015-2016 ForgeRock AS. + * Portions copyright 2023 3A Systems LLC --> @@ -21,7 +22,7 @@ @@ -762,6 +763,44 @@ + + + + + true + false + + + true + + + + + + + + + + + + + true + false + + + true + + + + + org.forgerock.openam.audit.events.handlers.JsonStdoutAuditEventHandlerFactory + + + + @@ -807,7 +846,7 @@ - %BASE_DIR%/%SERVER_URI%/@LOG_DIR@/ + %BASE_DIR%/%SERVER_URI%/log/ + + + + + true + false + + + true + + + + + + + + + + + + + true + false + + + true + + + + + org.forgerock.openam.audit.events.handlers.JsonStdoutAuditEventHandlerFactory + + + diff --git a/openam-audit/openam-audit-configuration/src/test/java/org/forgerock/openam/audit/events/handlers/JsonStdoutAuditEventHandlerFactoryTest.java b/openam-audit/openam-audit-configuration/src/test/java/org/forgerock/openam/audit/events/handlers/JsonStdoutAuditEventHandlerFactoryTest.java new file mode 100644 index 0000000000..60dc0d66bc --- /dev/null +++ b/openam-audit/openam-audit-configuration/src/test/java/org/forgerock/openam/audit/events/handlers/JsonStdoutAuditEventHandlerFactoryTest.java @@ -0,0 +1,71 @@ +/* +* The contents of this file are subject to the terms of the Common Development and +* Distribution License (the License). You may not use this file except in compliance with the +* License. +* +* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the +* specific language governing permission and limitations under the License. +* +* When distributing Covered Software, include this CDDL Header Notice in each file and include +* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL +* Header, with the fields enclosed by brackets [] replaced by your own identifying +* information: "Portions copyright [year] [name of copyright owner]". +* +* Copyright 2023 3A Systems LLC +*/ +package org.forgerock.openam.audit.events.handlers; + +import org.forgerock.audit.AuditException; +import org.forgerock.audit.events.EventTopicsMetaData; +import org.forgerock.audit.events.EventTopicsMetaDataBuilder; +import org.forgerock.audit.events.handlers.AuditEventHandler; +import org.forgerock.audit.handlers.json.JsonStdoutAuditEventHandler; +import org.forgerock.openam.audit.AuditEventHandlerFactory; +import org.forgerock.openam.audit.configuration.AuditEventHandlerConfiguration; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import static java.util.Collections.singleton; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test the JsonStdoutAuditEventHandlerFactoryTest class. + * + * @since 14.8.3 + */ +public class JsonStdoutAuditEventHandlerFactoryTest { + + private AuditEventHandlerFactory factory; + private EventTopicsMetaData eventTopicsMetaData; + private Map> configAttributes; + + @BeforeMethod + public void setUp() { + factory = new JsonStdoutAuditEventHandlerFactory(); + eventTopicsMetaData = EventTopicsMetaDataBuilder.coreTopicSchemas().build(); + + configAttributes = new HashMap<>(); + configAttributes.put("enabled", singleton("true")); + configAttributes.put("topics", singleton("access")); + configAttributes.put("elasticsearchCompatible", singleton("true")); + } + + @Test + void shouldCreateJsonStdoutEventHandler() throws AuditException { + AuditEventHandlerConfiguration configuration = AuditEventHandlerConfiguration.builder() + .withName("JSONStdout") + .withAttributes(configAttributes) + .withEventTopicsMetaData(eventTopicsMetaData).build(); + + AuditEventHandler handler = factory.create(configuration); + + assertThat(handler).isInstanceOf(JsonStdoutAuditEventHandler.class); + assertThat(handler.getName()).isEqualTo("JSONStdout"); + assertThat(handler.getHandledTopics()).containsExactly("access"); + assertThat(handler.isEnabled()).isTrue(); + } +} diff --git a/pom.xml b/pom.xml index 89e7bdeb92..f82434eb4f 100644 --- a/pom.xml +++ b/pom.xml @@ -79,7 +79,7 @@ 1.8 checkstyle/suppressions.xml - 4.6.1 + 4.6.2-SNAPSHOT 1.0.0 1.0b3 3.0