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 ForgeRock AS.
015 */
016package org.forgerock.audit.events.handlers;
017
018import java.text.SimpleDateFormat;
019import java.util.LinkedList;
020import java.util.List;
021
022import com.fasterxml.jackson.annotation.JsonPropertyDescription;
023
024import org.forgerock.util.Reject;
025import org.forgerock.util.time.Duration;
026
027/**
028 * Configures time based or size based log file rotation.
029 */
030public 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        public static final long NO_MAX_FILE_SIZE = -1;
117        public static final String DEFAULT_ROTATION_FILE_SUFFIX = "-yyyy.MM.dd-HH.mm.ss";
118
119        @JsonPropertyDescription("audit.handlers.file.rotationEnabled")
120        private boolean rotationEnabled = false;
121
122        // size based rotation config parameters
123        @JsonPropertyDescription("audit.handlers.file.maxFileSize")
124        private long maxFileSize = NO_MAX_FILE_SIZE;
125
126        // time Based Rotation config parameters
127        @JsonPropertyDescription("audit.handlers.file.rotationFilePrefix")
128        private String rotationFilePrefix = null;
129
130        // fixed time based rotation config parameters
131        @JsonPropertyDescription("audit.handlers.file.rotationTimes")
132        private final List<String> rotationTimes = new LinkedList<>();
133
134        @JsonPropertyDescription("audit.handlers.file.rotationFileSuffix")
135        private String rotationFileSuffix = DEFAULT_ROTATION_FILE_SUFFIX;
136
137        @JsonPropertyDescription("audit.handlers.file.rotationInterval")
138        private String rotationInterval = "disabled";
139
140        /**
141         * Gets log rotation enabled state. By default log rotation is disabled.
142         * @return True - If log rotation is enabled.
143         *         False - If log rotation is disabled.
144         */
145        public boolean isRotationEnabled() {
146            return rotationEnabled;
147        }
148
149        /**
150         * Sets log rotation enabled state. By default log rotation is disabled.
151         * @param rotationEnabled True - Enabled log rotation.
152         *                        False - Disables log rotation.
153         */
154        public void setRotationEnabled(boolean rotationEnabled) {
155            this.rotationEnabled = rotationEnabled;
156        }
157
158        /**
159         * Gets the maximum file size of an audit log file in bytes.
160         * @return The maximum file size in bytes.
161         */
162        public long getMaxFileSize() {
163            return maxFileSize;
164        }
165
166        /**
167         * Sets the maximum file size of an audit log file in bytes.
168         * @param maxFileSize The maximum file size in bytes.
169         */
170        public void setMaxFileSize(long maxFileSize) {
171            this.maxFileSize = maxFileSize;
172        }
173
174        /**
175         * Gets the prefix to add to a log file on rotation. This is only used when time based rotation is enabled.
176         * @return The prefix to add to the file.
177         */
178        public String getRotationFilePrefix() {
179            return rotationFilePrefix;
180        }
181
182        /**
183         * Sets the prefix to add to a log file on rotation. This is only used when time based rotation is enabled.
184         * @param rotationFilePrefix The prefix to add to the file.
185         */
186        public void setRotationFilePrefix(String rotationFilePrefix) {
187            this.rotationFilePrefix = rotationFilePrefix;
188        }
189
190        /**
191         * Gets the suffix to add to a log file on rotation. This is only used when time based rotation is enabled.
192         * The suffix allows use of Date and Time patterns defined in {@link SimpleDateFormat}. The default suffix is
193         * "-yyyy.MM.dd-HH.mm.ss".
194         * @return The suffix to add to the file.
195         */
196        public String getRotationFileSuffix() {
197            return rotationFileSuffix;
198        }
199
200        /**
201         * Sets the suffix to add to a log file on rotation. This is only used when time based rotation is enabled.
202         * The suffix allows use of Date and Time patterns defined in {@link SimpleDateFormat}. The default suffix is
203         * "-yyyy.MM.dd-HH.mm.ss".
204         * @param rotationFileSuffix The suffix to add to the file.
205         */
206        public void setRotationFileSuffix(String rotationFileSuffix) {
207            this.rotationFileSuffix = rotationFileSuffix;
208        }
209
210        /**
211         * Gets the interval to trigger a file rotation. The interval should be set as a {@link Duration}.
212         * <p/>
213         * Examples of valid durations are:
214         * <pre>
215         *      5 seconds
216         *      5 minutes
217         *      5 hours
218         *      disabled
219         * </pre>
220         * <p/>
221         * A value of "zero" or "disabled" means that time based file rotation is disabled.
222         *
223         * @return The interval duration.
224         */
225        public String getRotationInterval() {
226            return rotationInterval;
227        }
228
229        /**
230         * Sets the interval to trigger a file rotation. The interval should be set as a {@link Duration}.
231         * <p/>
232         * Examples of valid durations are:
233         * <pre>
234         *      5 seconds
235         *      5 minutes
236         *      5 hours
237         *      disabled
238         * </pre>
239         * <p/>
240         * A value of "zero" or "disabled" disables time based file rotation.
241         *
242         * @param rotationInterval A String that can be parsed as a {@link Duration}, specifying rotation interval.
243         */
244        public void setRotationInterval(String rotationInterval) {
245            this.rotationInterval = rotationInterval;
246        }
247
248        /**
249         * Gets a list of times at which file rotation should be triggered; times should be provided as Strings that can
250         * be parsed by {@link Duration} that each specify an offset from midnight.
251         * <p/>
252         * For example the list of [10 milliseconds, 20 milliseconds, 30 milliseconds] will
253         * cause a file rotation to happen 10 milliseconds, 20 milliseconds and 30 milliseconds after midnight.
254         *
255         * @return The list of durations after midnight that rotation should happen.
256         */
257        public List<String> getRotationTimes() {
258            return rotationTimes;
259        }
260
261        /**
262         * Sets a list of times at which file rotation should be triggered; times should be provided as Strings that can
263         * be parsed by {@link Duration} that each specify an offset from midnight.
264         * <p/>
265         * For example the list of [10 milliseconds, 20 milliseconds, 30 milliseconds] will
266         * cause a file rotation to happen 10 milliseconds, 20 milliseconds and 30 milliseconds after midnight.
267         *
268         * @param rotationTimes The list of durations after midnight that rotation should happen.
269         */
270        public void setRotationTimes(List<String> rotationTimes) {
271            this.rotationTimes.addAll(rotationTimes);
272        }
273    }
274
275    /**
276     * Groups the file retention config parameters.
277     */
278    public static class FileRetention {
279
280        public static final int UNLIMITED_HISTORY_FILES = -1;
281        public static final long ANY_DISK_SPACE = -1;
282
283        @JsonPropertyDescription("audit.handlers.file.maxNumberOfHistoryFiles")
284        private int maxNumberOfHistoryFiles = UNLIMITED_HISTORY_FILES;
285
286        @JsonPropertyDescription("audit.handlers.file.maxDiskSpaceToUse")
287        private long maxDiskSpaceToUse = ANY_DISK_SPACE;
288
289        @JsonPropertyDescription("audit.handlers.file.minFreeSpaceRequired")
290        private long minFreeSpaceRequired = ANY_DISK_SPACE;
291
292        /**
293         * Gets the maximum number of historical log files to retain. -1 disables pruning of old history files.
294         * @return The maximum number of log files. -1 disables pruning of old history files.
295         */
296        public int getMaxNumberOfHistoryFiles() {
297            return maxNumberOfHistoryFiles;
298        }
299
300        /**
301         * Sets the maximum number of historical log files to retain. -1 disables pruning of old history files.
302         * @return The maximum number of log files. -1 disables pruning of old history files.
303         */
304        public void setMaxNumberOfHistoryFiles(int maxNumberOfHistoryFiles) {
305            this.maxNumberOfHistoryFiles = maxNumberOfHistoryFiles;
306        }
307
308        /**
309         * Gets the maximum disk space the audit logs can occupy. A negative or zero value indicates this
310         * policy is disabled.
311         * @return The maximum disk space the audit logs can occupy.
312         */
313        public long getMaxDiskSpaceToUse() {
314            return maxDiskSpaceToUse;
315        }
316
317        /**
318         * Sets the maximum disk space the audit logs can occupy. A negative or zero value indicates this
319         * policy is disabled.
320         * @param maxDiskSpaceToUse The maximum disk space the audit logs can occupy.
321         */
322        public void setMaxDiskSpaceToUse(final long maxDiskSpaceToUse) {
323            this.maxDiskSpaceToUse = maxDiskSpaceToUse;
324        }
325
326        /**
327         * Gets the minimum free space the system must contain. A negative or zero value indicates this
328         * policy is disabled.
329         * @return The minimum free space the system must contain.
330         */
331        public long getMinFreeSpaceRequired() {
332            return minFreeSpaceRequired;
333        }
334
335        /**
336         * Sets the minimum free space the system must contain. A negative or zero value indicates this
337         * policy is disabled.
338         * @param minFreeSpaceRequired The minimum free space the system must contain.
339         */
340        public void setMinFreeSpaceRequired(final long minFreeSpaceRequired) {
341            this.minFreeSpaceRequired = minFreeSpaceRequired;
342        }
343    }
344}