|
| 1 | +/* |
| 2 | + * The contents of this file are subject to the terms of the Common Development and |
| 3 | + * Distribution License (the License). You may not use this file except in compliance with the |
| 4 | + * License. |
| 5 | + * |
| 6 | + * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the |
| 7 | + * specific language governing permission and limitations under the License. |
| 8 | + * |
| 9 | + * When distributing Covered Software, include this CDDL Header Notice in each file and include |
| 10 | + * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL |
| 11 | + * Header, with the fields enclosed by brackets [] replaced by your own identifying |
| 12 | + * information: "Portions copyright [year] [name of copyright owner]". |
| 13 | + * |
| 14 | + * Copyright 2025 Wren Security. All rights reserved. |
| 15 | + */ |
| 16 | +package org.forgerock.openam.audit.events.handlers; |
| 17 | + |
| 18 | +import static com.iplanet.am.util.SystemProperties.CONFIG_PATH; |
| 19 | +import static com.sun.identity.shared.Constants.AM_SERVICES_DEPLOYMENT_DESCRIPTOR; |
| 20 | +import static com.sun.identity.shared.datastruct.CollectionHelper.getBooleanMapAttr; |
| 21 | +import static com.sun.identity.shared.datastruct.CollectionHelper.getIntMapAttr; |
| 22 | +import static com.sun.identity.shared.datastruct.CollectionHelper.getLongMapAttr; |
| 23 | +import static com.sun.identity.shared.datastruct.CollectionHelper.getMapAttr; |
| 24 | + |
| 25 | +import com.iplanet.am.util.SystemProperties; |
| 26 | +import com.sun.identity.shared.debug.Debug; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Set; |
| 31 | +import javax.inject.Singleton; |
| 32 | +import org.forgerock.audit.AuditException; |
| 33 | +import org.forgerock.audit.events.handlers.AuditEventHandler; |
| 34 | +import org.forgerock.audit.handlers.json.JsonAuditEventHandler; |
| 35 | +import org.forgerock.audit.handlers.json.JsonAuditEventHandlerConfiguration; |
| 36 | +import org.forgerock.audit.handlers.json.JsonAuditEventHandlerConfiguration.EventBufferingConfiguration; |
| 37 | +import org.forgerock.openam.audit.AuditEventHandlerFactory; |
| 38 | +import org.forgerock.openam.audit.configuration.AuditEventHandlerConfiguration; |
| 39 | + |
| 40 | +/** |
| 41 | + * This factory is responsible for creating an instance of the {@link JsonAuditEventHandler}. |
| 42 | + */ |
| 43 | +@Singleton |
| 44 | +public final class JsonAuditEventHandlerFactory implements AuditEventHandlerFactory { |
| 45 | + |
| 46 | + private static final Debug DEBUG = Debug.getInstance("amAudit"); |
| 47 | + |
| 48 | + @Override |
| 49 | + public AuditEventHandler create(final AuditEventHandlerConfiguration configuration) throws AuditException { |
| 50 | + Map<String, Set<String>> attributes = configuration.getAttributes(); |
| 51 | + JsonAuditEventHandlerConfiguration jsonHandlerConfiguration = new JsonAuditEventHandlerConfiguration(); |
| 52 | + jsonHandlerConfiguration.setLogDirectory(getLogDirectory(attributes)); |
| 53 | + jsonHandlerConfiguration.setTopics(attributes.get("topics")); |
| 54 | + jsonHandlerConfiguration.setName(configuration.getHandlerName()); |
| 55 | + jsonHandlerConfiguration.setEnabled(getBooleanMapAttr(attributes, "enabled", true)); |
| 56 | + jsonHandlerConfiguration.setElasticsearchCompatible( |
| 57 | + getBooleanMapAttr(attributes, "elasticsearchCompatible", false)); |
| 58 | + jsonHandlerConfiguration.setRotationRetentionCheckInterval( |
| 59 | + getIntMapAttr(attributes, "rotationRetentionCheckInterval", 20, DEBUG) + " s"); |
| 60 | + setFileRotationPolicies(jsonHandlerConfiguration, attributes); |
| 61 | + setFileRetentionPolicies(jsonHandlerConfiguration, attributes); |
| 62 | + jsonHandlerConfiguration.setBuffering(getBufferingConfiguration(attributes)); |
| 63 | + return new JsonAuditEventHandler(jsonHandlerConfiguration, configuration.getEventTopicsMetaData()); |
| 64 | + } |
| 65 | + |
| 66 | + private String getLogDirectory(Map<String, Set<String>> attributes) { |
| 67 | + String location = getMapAttr(attributes, "location", ""); |
| 68 | + String baseDir = SystemProperties.get(CONFIG_PATH); |
| 69 | + if (baseDir != null) { |
| 70 | + location = location.replace("%BASE_DIR%", baseDir); |
| 71 | + } |
| 72 | + String serverUri = SystemProperties.get(AM_SERVICES_DEPLOYMENT_DESCRIPTOR); |
| 73 | + if (serverUri != null) { |
| 74 | + location = location.replace("%SERVER_URI%", serverUri); |
| 75 | + } |
| 76 | + return location; |
| 77 | + } |
| 78 | + |
| 79 | + private void setFileRotationPolicies(JsonAuditEventHandlerConfiguration jsonHandlerConfiguration, |
| 80 | + Map<String, Set<String>> attributes) throws AuditException { |
| 81 | + jsonHandlerConfiguration.getFileRotation() |
| 82 | + .setRotationEnabled(getBooleanMapAttr(attributes, "rotationEnabled", true)); |
| 83 | + jsonHandlerConfiguration.getFileRotation() |
| 84 | + .setMaxFileSize(getLongMapAttr(attributes, "rotationMaxFileSize", 100000000L, DEBUG)); |
| 85 | + jsonHandlerConfiguration.getFileRotation() |
| 86 | + .setRotationFilePrefix(getMapAttr(attributes, "rotationFilePrefix", "")); |
| 87 | + jsonHandlerConfiguration.getFileRotation() |
| 88 | + .setRotationFileSuffix(getMapAttr(attributes, "rotationFileSuffix", "-yyyy.MM.dd-HH.mm.ss")); |
| 89 | + jsonHandlerConfiguration.getFileRotation() |
| 90 | + .setRotationInterval(parseRotationInterval(getMapAttr(attributes, "rotationInterval", "-1"))); |
| 91 | + |
| 92 | + List<String> times = new ArrayList<>(); |
| 93 | + Set<String> rotationTimesAttribute = attributes.get("rotationTimes"); |
| 94 | + if (rotationTimesAttribute != null && !rotationTimesAttribute.isEmpty()) { |
| 95 | + for (String rotationTime : rotationTimesAttribute) { |
| 96 | + times.add(rotationTime + " seconds"); |
| 97 | + } |
| 98 | + jsonHandlerConfiguration.getFileRotation().setRotationTimes(times); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private String parseRotationInterval(String interval) throws AuditException { |
| 103 | + try { |
| 104 | + Long intervalAsLong = Long.valueOf(interval); |
| 105 | + |
| 106 | + if (intervalAsLong <= 0) { |
| 107 | + // If interval is 0 or a negative value, the feature is disabled |
| 108 | + return "disabled"; |
| 109 | + } else { |
| 110 | + // If interval is a positive number, add seconds as the time unit |
| 111 | + return interval + " seconds"; |
| 112 | + } |
| 113 | + } catch (NumberFormatException nfe) { |
| 114 | + throw new AuditException("Attribute 'rotationInterval' is invalid: " + interval); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private void setFileRetentionPolicies(JsonAuditEventHandlerConfiguration jsonHandlerConfiguration, |
| 119 | + Map<String, Set<String>> attributes) { |
| 120 | + jsonHandlerConfiguration.getFileRetention() |
| 121 | + .setMaxNumberOfHistoryFiles(getIntMapAttr(attributes, "retentionMaxNumberOfHistoryFiles", 1, DEBUG)); |
| 122 | + jsonHandlerConfiguration.getFileRetention() |
| 123 | + .setMaxDiskSpaceToUse(getLongMapAttr(attributes, "retentionMaxDiskSpaceToUse", -1L, DEBUG)); |
| 124 | + jsonHandlerConfiguration.getFileRetention() |
| 125 | + .setMinFreeSpaceRequired(getLongMapAttr(attributes, "retentionMinFreeSpaceRequired", -1L, DEBUG)); |
| 126 | + } |
| 127 | + |
| 128 | + private EventBufferingConfiguration getBufferingConfiguration(Map<String, Set<String>> attributes) { |
| 129 | + EventBufferingConfiguration bufferingConfiguration = new EventBufferingConfiguration(); |
| 130 | + bufferingConfiguration.setMaxSize(getIntMapAttr(attributes, "maxSize", 10000, DEBUG)); |
| 131 | + bufferingConfiguration.setWriteInterval(getIntMapAttr(attributes, "writeInterval", 1000, DEBUG) + " millis"); |
| 132 | + return bufferingConfiguration; |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments