001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2015-2016 ForgeRock AS. 015 */ 016package org.forgerock.audit.events.handlers; 017 018import java.text.SimpleDateFormat; 019import java.util.LinkedList; 020import java.util.List; 021 022import org.forgerock.util.Reject; 023import org.forgerock.util.time.Duration; 024 025import com.fasterxml.jackson.annotation.JsonPropertyDescription; 026 027/** 028 * Configures time based or size based log file rotation. 029 */ 030public abstract class FileBasedEventHandlerConfiguration extends EventHandlerConfiguration { 031 032 @JsonPropertyDescription("audit.handlers.file.fileRotation") 033 private FileRotation fileRotation = new FileRotation(); 034 @JsonPropertyDescription("audit.handlers.file.fileRetention") 035 private FileRetention fileRetention = new FileRetention(); 036 @JsonPropertyDescription("audit.handlers.file.rotationRetentionCheckInterval") 037 private String rotationRetentionCheckInterval = "5s"; 038 039 /** 040 * Gets the {@link FileRotation}. 041 * @return Not-null, The {@link FileRotation}. 042 */ 043 public FileRotation getFileRotation() { 044 return fileRotation; 045 } 046 047 /** 048 * Sets the {@link FileRotation}. 049 * 050 * @param fileRotation Not-null, The {@link FileRotation}. 051 */ 052 public void setFileRotation(final FileRotation fileRotation) { 053 Reject.ifNull(fileRotation); 054 this.fileRotation = fileRotation; 055 } 056 057 /** 058 * Gets the {@link FileRetention}. 059 * @return Not-null, The {@link FileRetention}. 060 */ 061 public FileRetention getFileRetention() { 062 return fileRetention; 063 } 064 065 /** 066 * Sets the {@link FileRetention}. 067 * 068 * @param fileRetention Not-null, The {@link FileRetention}. 069 */ 070 public void setFileRetention(final FileRetention fileRetention) { 071 Reject.ifNull(fileRetention); 072 this.fileRetention = fileRetention; 073 } 074 075 /** 076 * Gets the interval to check time-based file rotation policies. The interval should be set as a {@link Duration}. 077 * <p/> 078 * Examples of valid durations are: 079 * <pre> 080 * 5 seconds 081 * 5 minutes 082 * 5 hours 083 * </pre> 084 * <p/> 085 * Value of "zero" or "disabled" are not acceptable. 086 * 087 * @return The interval duration. 088 */ 089 public String getRotationRetentionCheckInterval() { 090 return rotationRetentionCheckInterval; 091 } 092 093 /** 094 * Sets the interval to check time-based file rotation policies. The interval should be set as a {@link Duration}. 095 * <p/> 096 * Examples of valid durations are: 097 * <pre> 098 * 5 seconds 099 * 5 minutes 100 * 5 hours 101 * </pre> 102 * <p/> 103 * Value of "zero" or "disabled" are not acceptable. 104 * 105 * @param rotationRetentionCheckInterval The interval duration. 106 */ 107 public void setRotationRetentionCheckInterval(String rotationRetentionCheckInterval) { 108 this.rotationRetentionCheckInterval = rotationRetentionCheckInterval; 109 } 110 111 /** 112 * Groups the file rotation config parameters. 113 */ 114 public static class FileRotation { 115 116 /** The file size value to use when no maximum is set. */ 117 public static final long NO_MAX_FILE_SIZE = -1; 118 /** The default file rotation suffix format. */ 119 public static final String DEFAULT_ROTATION_FILE_SUFFIX = "-yyyy.MM.dd-HH.mm.ss"; 120 121 @JsonPropertyDescription("audit.handlers.file.rotationEnabled") 122 private boolean rotationEnabled = false; 123 124 // size based rotation config parameters 125 @JsonPropertyDescription("audit.handlers.file.maxFileSize") 126 private long maxFileSize = NO_MAX_FILE_SIZE; 127 128 // time Based Rotation config parameters 129 @JsonPropertyDescription("audit.handlers.file.rotationFilePrefix") 130 private String rotationFilePrefix = null; 131 132 // fixed time based rotation config parameters 133 @JsonPropertyDescription("audit.handlers.file.rotationTimes") 134 private final List<String> rotationTimes = new LinkedList<>(); 135 136 @JsonPropertyDescription("audit.handlers.file.rotationFileSuffix") 137 private String rotationFileSuffix = DEFAULT_ROTATION_FILE_SUFFIX; 138 139 @JsonPropertyDescription("audit.handlers.file.rotationInterval") 140 private String rotationInterval = "disabled"; 141 142 /** 143 * Gets log rotation enabled state. By default log rotation is disabled. 144 * @return True - If log rotation is enabled. 145 * False - If log rotation is disabled. 146 */ 147 public boolean isRotationEnabled() { 148 return rotationEnabled; 149 } 150 151 /** 152 * Sets log rotation enabled state. By default log rotation is disabled. 153 * @param rotationEnabled True - Enabled log rotation. 154 * False - Disables log rotation. 155 */ 156 public void setRotationEnabled(boolean rotationEnabled) { 157 this.rotationEnabled = rotationEnabled; 158 } 159 160 /** 161 * Gets the maximum file size of an audit log file in bytes. 162 * @return The maximum file size in bytes. 163 */ 164 public long getMaxFileSize() { 165 return maxFileSize; 166 } 167 168 /** 169 * Sets the maximum file size of an audit log file in bytes. 170 * @param maxFileSize The maximum file size in bytes. 171 */ 172 public void setMaxFileSize(long maxFileSize) { 173 this.maxFileSize = maxFileSize; 174 } 175 176 /** 177 * Gets the prefix to add to a log file on rotation. This is only used when time based rotation is enabled. 178 * @return The prefix to add to the file. 179 */ 180 public String getRotationFilePrefix() { 181 return rotationFilePrefix; 182 } 183 184 /** 185 * Sets the prefix to add to a log file on rotation. This is only used when time based rotation is enabled. 186 * @param rotationFilePrefix The prefix to add to the file. 187 */ 188 public void setRotationFilePrefix(String rotationFilePrefix) { 189 this.rotationFilePrefix = rotationFilePrefix; 190 } 191 192 /** 193 * Gets the suffix to add to a log file on rotation. This is only used when time based rotation is enabled. 194 * The suffix allows use of Date and Time patterns defined in {@link SimpleDateFormat}. The default suffix is 195 * "-yyyy.MM.dd-HH.mm.ss". 196 * @return The suffix to add to the file. 197 */ 198 public String getRotationFileSuffix() { 199 return rotationFileSuffix; 200 } 201 202 /** 203 * Sets the suffix to add to a log file on rotation. This is only used when time based rotation is enabled. 204 * The suffix allows use of Date and Time patterns defined in {@link SimpleDateFormat}. The default suffix is 205 * "-yyyy.MM.dd-HH.mm.ss". 206 * @param rotationFileSuffix The suffix to add to the file. 207 */ 208 public void setRotationFileSuffix(String rotationFileSuffix) { 209 this.rotationFileSuffix = rotationFileSuffix; 210 } 211 212 /** 213 * Gets the interval to trigger a file rotation. The interval should be set as a {@link Duration}. 214 * <p/> 215 * Examples of valid durations are: 216 * <pre> 217 * 5 seconds 218 * 5 minutes 219 * 5 hours 220 * disabled 221 * </pre> 222 * <p/> 223 * A value of "zero" or "disabled" means that time based file rotation is disabled. 224 * 225 * @return The interval duration. 226 */ 227 public String getRotationInterval() { 228 return rotationInterval; 229 } 230 231 /** 232 * Sets the interval to trigger a file rotation. The interval should be set as a {@link Duration}. 233 * <p/> 234 * Examples of valid durations are: 235 * <pre> 236 * 5 seconds 237 * 5 minutes 238 * 5 hours 239 * disabled 240 * </pre> 241 * <p/> 242 * A value of "zero" or "disabled" disables time based file rotation. 243 * 244 * @param rotationInterval A String that can be parsed as a {@link Duration}, specifying rotation interval. 245 */ 246 public void setRotationInterval(String rotationInterval) { 247 this.rotationInterval = rotationInterval; 248 } 249 250 /** 251 * Gets a list of times at which file rotation should be triggered; times should be provided as Strings that can 252 * be parsed by {@link Duration} that each specify an offset from midnight. 253 * <p/> 254 * For example the list of [10 milliseconds, 20 milliseconds, 30 milliseconds] will 255 * cause a file rotation to happen 10 milliseconds, 20 milliseconds and 30 milliseconds after midnight. 256 * 257 * @return The list of durations after midnight that rotation should happen. 258 */ 259 public List<String> getRotationTimes() { 260 return rotationTimes; 261 } 262 263 /** 264 * Sets a list of times at which file rotation should be triggered; times should be provided as Strings that can 265 * be parsed by {@link Duration} that each specify an offset from midnight. 266 * <p/> 267 * For example the list of [10 milliseconds, 20 milliseconds, 30 milliseconds] will 268 * cause a file rotation to happen 10 milliseconds, 20 milliseconds and 30 milliseconds after midnight. 269 * 270 * @param rotationTimes The list of durations after midnight that rotation should happen. 271 */ 272 public void setRotationTimes(List<String> rotationTimes) { 273 this.rotationTimes.addAll(rotationTimes); 274 } 275 } 276 277 /** 278 * Groups the file retention config parameters. 279 */ 280 public static class FileRetention { 281 282 /** The value of number of history files to use when the value is unlimited. */ 283 public static final int UNLIMITED_HISTORY_FILES = -1; 284 /** The disk space value when disk space is unrestricted. */ 285 public static final long ANY_DISK_SPACE = -1; 286 287 @JsonPropertyDescription("audit.handlers.file.maxNumberOfHistoryFiles") 288 private int maxNumberOfHistoryFiles = UNLIMITED_HISTORY_FILES; 289 290 @JsonPropertyDescription("audit.handlers.file.maxDiskSpaceToUse") 291 private long maxDiskSpaceToUse = ANY_DISK_SPACE; 292 293 @JsonPropertyDescription("audit.handlers.file.minFreeSpaceRequired") 294 private long minFreeSpaceRequired = ANY_DISK_SPACE; 295 296 /** 297 * Gets the maximum number of historical log files to retain. -1 disables pruning of old history files. 298 * @return The maximum number of log files. -1 disables pruning of old history files. 299 */ 300 public int getMaxNumberOfHistoryFiles() { 301 return maxNumberOfHistoryFiles; 302 } 303 304 /** 305 * Sets the maximum number of historical log files to retain. -1 disables pruning of old history files. 306 * @param maxNumberOfHistoryFiles The maximum number of log files. -1 disables pruning of old history files. 307 */ 308 public void setMaxNumberOfHistoryFiles(int maxNumberOfHistoryFiles) { 309 this.maxNumberOfHistoryFiles = maxNumberOfHistoryFiles; 310 } 311 312 /** 313 * Gets the maximum disk space the audit logs can occupy. A negative or zero value indicates this 314 * policy is disabled. 315 * @return The maximum disk space the audit logs can occupy. 316 */ 317 public long getMaxDiskSpaceToUse() { 318 return maxDiskSpaceToUse; 319 } 320 321 /** 322 * Sets the maximum disk space the audit logs can occupy. A negative or zero value indicates this 323 * policy is disabled. 324 * @param maxDiskSpaceToUse The maximum disk space the audit logs can occupy. 325 */ 326 public void setMaxDiskSpaceToUse(final long maxDiskSpaceToUse) { 327 this.maxDiskSpaceToUse = maxDiskSpaceToUse; 328 } 329 330 /** 331 * Gets the minimum free space the system must contain. A negative or zero value indicates this 332 * policy is disabled. 333 * @return The minimum free space the system must contain. 334 */ 335 public long getMinFreeSpaceRequired() { 336 return minFreeSpaceRequired; 337 } 338 339 /** 340 * Sets the minimum free space the system must contain. A negative or zero value indicates this 341 * policy is disabled. 342 * @param minFreeSpaceRequired The minimum free space the system must contain. 343 */ 344 public void setMinFreeSpaceRequired(final long minFreeSpaceRequired) { 345 this.minFreeSpaceRequired = minFreeSpaceRequired; 346 } 347 } 348}