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.handlers.csv;
017
018import org.forgerock.audit.events.handlers.FileBasedEventHandlerConfiguration;
019import org.forgerock.util.Reject;
020import org.forgerock.util.time.Duration;
021
022import com.fasterxml.jackson.annotation.JsonIgnore;
023import com.fasterxml.jackson.annotation.JsonProperty;
024import com.fasterxml.jackson.annotation.JsonPropertyDescription;
025
026/**
027 * A configuration for CSV audit event handler.
028 * <p>
029 * This configuration object can be created from JSON. Example of valid JSON configuration:
030 *
031 * <pre>
032 *  {
033 *    "name" : "csv",
034 *    "topics": [ "access", "activity", "config", "authentication" ],
035 *    "logDirectory" : "/path/to/audit/files/",
036 *    "formatting" : {
037 *      "quoteChar" : "\"",
038 *      "delimiterChar" : ",",
039 *      "endOfLineSymbols" : "\n"
040 *    },
041 *    "security" : {
042 *      "enabled" : "true",
043 *      "filename" : "/path/to/keystore.jks",
044 *      "password" : "correcthorsebatterystaple",
045 *      "signatureInterval" : "3 seconds"
046 *    },
047 *    "buffering" : {
048 *      "enabled" : "true",
049 *      "autoFlush" : "true"
050 *    }
051 *  }
052 * </pre>
053 */
054public class CsvAuditEventHandlerConfiguration extends FileBasedEventHandlerConfiguration {
055
056    @JsonProperty(required=true)
057    @JsonPropertyDescription("audit.handlers.csv.logDirectory")
058    private String logDirectory;
059
060    @JsonPropertyDescription("audit.handlers.csv.formatting")
061    private CsvFormatting formatting = new CsvFormatting();
062
063    @JsonPropertyDescription("audit.handlers.csv.security")
064    private CsvSecurity security = new CsvSecurity();
065
066    /** Event buffering is disabled by default. */
067    @JsonPropertyDescription("audit.handlers.csv.buffering")
068    protected EventBufferingConfiguration buffering = new EventBufferingConfiguration();
069
070    /**
071     * Returns the directory where CSV file is located.
072     *
073     * @return the location of the CSV file.
074     */
075    public String getLogDirectory() {
076        return logDirectory;
077    }
078
079    /**
080     * Sets the directory where CSV file is located.
081     *
082     * @param directory
083     *            the directory.
084     */
085    public void setLogDirectory(String directory) {
086        logDirectory = directory;
087    }
088
089    /**
090     * Returns the CSV formatting options.
091     *
092     * @return the CSV formatting options.
093     */
094    public CsvFormatting getFormatting() {
095        return formatting;
096    }
097
098    /**
099     * Sets the CSV formatting options.
100     *
101     * @param formatting
102     *            the CSV formatting options to set.
103     */
104    public void setFormatting(CsvFormatting formatting) {
105        this.formatting = Reject.checkNotNull(formatting);
106    }
107
108    /**
109     * Returns the CSV tamper evident options.
110     *
111     * @return the CSV tamper evident options.
112     */
113    public CsvSecurity getSecurity() {
114        return security;
115    }
116
117    /**
118     * Sets the CSV tamper evident options.
119     *
120     * @param security
121     *            the CSV tamper evident options to set.
122     */
123    public void setSecurity(CsvSecurity security) {
124        this.security = Reject.checkNotNull(security);
125    }
126
127    /**
128     * Returns the configuration for events buffering.
129     *
130     * @return the configuration
131     */
132    public EventBufferingConfiguration getBuffering() {
133        return buffering;
134    }
135
136    /**
137     * Sets the configuration for events buffering.
138     *
139     * @param bufferingConfiguration
140     *            The configuration
141     */
142    public void setBufferingConfiguration(EventBufferingConfiguration bufferingConfiguration) {
143        this.buffering = bufferingConfiguration;
144    }
145
146    /**
147     * Contains the csv writer configuration parameters
148     */
149    public static class CsvFormatting {
150        @JsonPropertyDescription("audit.handlers.csv.formatting.quoteChar")
151        private char quoteChar = '"';
152
153        @JsonPropertyDescription("audit.handlers.csv.formatting.delimiterChar")
154        private char delimiterChar = ',';
155
156        @JsonPropertyDescription("audit.handlers.csv.formatting.endOfLineSymbols")
157        private String endOfLineSymbols = System.getProperty("line.separator");
158
159        /**
160         * Gets the character to use to quote the csv entries.
161         * @return The quote character.
162         */
163        public char getQuoteChar() {
164            return quoteChar;
165        }
166
167        /**
168         * Sets the character to use to quote the csv entries.
169         * @param quoteChar The quote character.
170         */
171        public void setQuoteChar(char quoteChar) {
172            this.quoteChar = quoteChar;
173        }
174
175        /**
176         * Gets the character to use to delimit the csv entries.
177         * @return The character used to delimit the entries.
178         */
179        public char getDelimiterChar() {
180            return delimiterChar;
181        }
182
183        /**
184         * Sets the character to use to delimit the csv entries.
185         * @param delimiterChar The character used to delimit the entries.
186         */
187        public void setDelimiterChar(char delimiterChar) {
188            this.delimiterChar = delimiterChar;
189        }
190
191        /**
192         * Gets the end of line symbol.
193         * @return The end of line symbol.
194         */
195        public String getEndOfLineSymbols() {
196            return endOfLineSymbols;
197        }
198
199        /**
200         * Gets the end of line symbol.
201         * @param endOfLineSymbols The end of line symbol.
202         */
203        public void setEndOfLineSymbols(String endOfLineSymbols) {
204            this.endOfLineSymbols = endOfLineSymbols;
205        }
206    }
207
208    /**
209     * Contains the configuration parameters to configure tamper evident logging.
210     */
211    public static class CsvSecurity {
212
213        @JsonPropertyDescription("audit.handlers.csv.security.enabled")
214        private boolean enabled = false;
215
216        @JsonPropertyDescription("audit.handlers.csv.security.filename")
217        private String filename;
218
219        @JsonPropertyDescription("audit.handlers.csv.security.password")
220        private String password;
221
222        @JsonPropertyDescription("audit.handlers.csv.security.keyStoreHandlerName")
223        private String keyStoreHandlerName;
224
225        @JsonPropertyDescription("audit.handlers.csv.security.signatureInterval")
226        private String signatureInterval;
227
228        @JsonIgnore
229        private Duration signatureIntervalDuration;
230
231        /**
232         * Enables tamper evident logging. By default tamper evident logging is disabled.
233         * @param enabled True - To enable tamper evident logging.
234         *                False - To disable tamper evident logging.
235         */
236        public void setEnabled(boolean enabled) {
237            this.enabled = enabled;
238        }
239
240        /**
241         *
242         * Gets tamper evident logging enabled status. By default tamper evident logging is disabled.
243         * @return True - If tamper evident logging enabled.
244         *         False - If tamper evident logging disabled.
245         */
246        public boolean isEnabled() {
247            return enabled;
248        }
249
250        /**
251         * Sets the location of the keystore to be used.
252         * @param filename The location of the keystore.
253         */
254        public void setFilename(String filename) {
255            this.filename = filename;
256        }
257
258        /**
259         * Gets the location of the keystore to be used.
260         * @return The location of the keystore.
261         */
262        public String getFilename() {
263            return filename;
264        }
265
266        /**
267         * Sets the password of the keystore.
268         * @param password The password of the keystore.
269         */
270        public void setPassword(String password) {
271            this.password = password;
272        }
273
274        /**
275         * Gets the password of the keystore.
276         * @return The password of the keystore.
277         */
278        public String getPassword() {
279            return password;
280        }
281
282        /**
283         * Sets the signature's interval.
284         * @param signatureInterval The time's interval to insert periodically a signature.
285         */
286        public void setSignatureInterval(String signatureInterval) {
287            this.signatureInterval = signatureInterval;
288            this.signatureIntervalDuration = Duration.duration(signatureInterval);
289        }
290
291        /**
292         * Gets the signature's interval.
293         * @return The time's interval to insert periodically a signature.
294         */
295        public String getSignatureInterval() {
296            return signatureInterval;
297        }
298
299        /**
300         * Get's {@link #getSignatureInterval()} value as a {@link Duration}.
301         * @return The signature internval as a Duration object.
302         */
303        public Duration getSignatureIntervalDuration() {
304            return signatureIntervalDuration;
305        }
306
307        public void setKeyStoreHandlerName(String keyStoreName) {
308            this.keyStoreHandlerName = keyStoreName;
309        }
310
311        public String getKeyStoreHandlerName() {
312            return keyStoreHandlerName;
313        }
314
315    }
316
317    /**
318     * Configuration of event buffering.
319     */
320    public static class EventBufferingConfiguration {
321
322        @JsonPropertyDescription("audit.handlers.csv.buffering.enabled")
323        private boolean enabled;
324
325        @JsonPropertyDescription("audit.handlers.csv.buffering.autoFlush")
326        private boolean autoFlush = true;
327
328        /**
329         * Indicates if event buffering is enabled.
330         *
331         * @return {@code true} if buffering is enabled.
332         */
333        public boolean isEnabled() {
334            return enabled;
335        }
336
337        /**
338         * Sets the buffering status.
339         *
340         * @param enabled
341         *            Indicates if buffering is enabled.
342         */
343        public void setEnabled(boolean enabled) {
344            this.enabled = enabled;
345        }
346
347        /**
348         * Indicates if events are automatically flushed after being written.
349         *
350         * @return {@code true} if events must be flushed
351         */
352        public boolean isAutoFlush() {
353            return autoFlush;
354        }
355
356        /**
357         * Sets the auto flush indicator.
358         *
359         * @param auto
360         *            Indicates if events are automatically flushed after being written.
361         */
362        public void setAutoFlush(boolean auto) {
363            this.autoFlush = auto;
364        }
365
366    }
367}