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.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    @Override
147    public boolean isUsableForQueries() {
148        return true;
149    }
150
151    /**
152     * Contains the csv writer configuration parameters.
153     */
154    public static class CsvFormatting {
155        @JsonPropertyDescription("audit.handlers.csv.formatting.quoteChar")
156        private char quoteChar = '"';
157
158        @JsonPropertyDescription("audit.handlers.csv.formatting.delimiterChar")
159        private char delimiterChar = ',';
160
161        @JsonPropertyDescription("audit.handlers.csv.formatting.endOfLineSymbols")
162        private String endOfLineSymbols = System.getProperty("line.separator");
163
164        /**
165         * Gets the character to use to quote the csv entries.
166         * @return The quote character.
167         */
168        public char getQuoteChar() {
169            return quoteChar;
170        }
171
172        /**
173         * Sets the character to use to quote the csv entries.
174         * @param quoteChar The quote character.
175         */
176        public void setQuoteChar(char quoteChar) {
177            this.quoteChar = quoteChar;
178        }
179
180        /**
181         * Gets the character to use to delimit the csv entries.
182         * @return The character used to delimit the entries.
183         */
184        public char getDelimiterChar() {
185            return delimiterChar;
186        }
187
188        /**
189         * Sets the character to use to delimit the csv entries.
190         * @param delimiterChar The character used to delimit the entries.
191         */
192        public void setDelimiterChar(char delimiterChar) {
193            this.delimiterChar = delimiterChar;
194        }
195
196        /**
197         * Gets the end of line symbol.
198         * @return The end of line symbol.
199         */
200        public String getEndOfLineSymbols() {
201            return endOfLineSymbols;
202        }
203
204        /**
205         * Gets the end of line symbol.
206         * @param endOfLineSymbols The end of line symbol.
207         */
208        public void setEndOfLineSymbols(String endOfLineSymbols) {
209            this.endOfLineSymbols = endOfLineSymbols;
210        }
211    }
212
213    /**
214     * Contains the configuration parameters to configure tamper evident logging.
215     */
216    public static class CsvSecurity {
217
218        @JsonPropertyDescription("audit.handlers.csv.security.enabled")
219        private boolean enabled = false;
220
221        @JsonPropertyDescription("audit.handlers.csv.security.filename")
222        private String filename;
223
224        @JsonPropertyDescription("audit.handlers.csv.security.password")
225        private String password;
226
227        @JsonPropertyDescription("audit.handlers.csv.security.keyStoreHandlerName")
228        private String keyStoreHandlerName;
229
230        @JsonPropertyDescription("audit.handlers.csv.security.signatureInterval")
231        private String signatureInterval;
232
233        @JsonIgnore
234        private Duration signatureIntervalDuration;
235
236        /**
237         * Enables tamper evident logging. By default tamper evident logging is disabled.
238         * @param enabled True - To enable tamper evident logging.
239         *                False - To disable tamper evident logging.
240         */
241        public void setEnabled(boolean enabled) {
242            this.enabled = enabled;
243        }
244
245        /**
246         *
247         * Gets tamper evident logging enabled status. By default tamper evident logging is disabled.
248         * @return True - If tamper evident logging enabled.
249         *         False - If tamper evident logging disabled.
250         */
251        public boolean isEnabled() {
252            return enabled;
253        }
254
255        /**
256         * Sets the location of the keystore to be used.
257         * @param filename The location of the keystore.
258         */
259        public void setFilename(String filename) {
260            this.filename = filename;
261        }
262
263        /**
264         * Gets the location of the keystore to be used.
265         * @return The location of the keystore.
266         */
267        public String getFilename() {
268            return filename;
269        }
270
271        /**
272         * Sets the password of the keystore.
273         * @param password The password of the keystore.
274         */
275        public void setPassword(String password) {
276            this.password = password;
277        }
278
279        /**
280         * Gets the password of the keystore.
281         * @return The password of the keystore.
282         */
283        public String getPassword() {
284            return password;
285        }
286
287        /**
288         * Sets the signature's interval.
289         * @param signatureInterval The time's interval to insert periodically a signature.
290         */
291        public void setSignatureInterval(String signatureInterval) {
292            this.signatureInterval = signatureInterval;
293            this.signatureIntervalDuration = Duration.duration(signatureInterval);
294        }
295
296        /**
297         * Gets the signature's interval.
298         * @return The time's interval to insert periodically a signature.
299         */
300        public String getSignatureInterval() {
301            return signatureInterval;
302        }
303
304        /**
305         * Get's {@link #getSignatureInterval()} value as a {@link Duration}.
306         * @return The signature internval as a Duration object.
307         */
308        public Duration getSignatureIntervalDuration() {
309            return signatureIntervalDuration;
310        }
311
312        /**
313         * Set the key store handler name.
314         * @param keyStoreName The name.
315         */
316        public void setKeyStoreHandlerName(String keyStoreName) {
317            this.keyStoreHandlerName = keyStoreName;
318        }
319
320        /**
321         * Get the key store handler name.
322         * @return The name.
323         */
324        public String getKeyStoreHandlerName() {
325            return keyStoreHandlerName;
326        }
327
328    }
329
330    /**
331     * Configuration of event buffering.
332     */
333    public static class EventBufferingConfiguration {
334
335        @JsonPropertyDescription("audit.handlers.csv.buffering.enabled")
336        private boolean enabled;
337
338        @JsonPropertyDescription("audit.handlers.csv.buffering.autoFlush")
339        private boolean autoFlush = true;
340
341        /**
342         * Indicates if event buffering is enabled.
343         *
344         * @return {@code true} if buffering is enabled.
345         */
346        public boolean isEnabled() {
347            return enabled;
348        }
349
350        /**
351         * Sets the buffering status.
352         *
353         * @param enabled
354         *            Indicates if buffering is enabled.
355         */
356        public void setEnabled(boolean enabled) {
357            this.enabled = enabled;
358        }
359
360        /**
361         * Indicates if events are automatically flushed after being written.
362         *
363         * @return {@code true} if events must be flushed
364         */
365        public boolean isAutoFlush() {
366            return autoFlush;
367        }
368
369        /**
370         * Sets the auto flush indicator.
371         *
372         * @param auto
373         *            Indicates if events are automatically flushed after being written.
374         */
375        public void setAutoFlush(boolean auto) {
376            this.autoFlush = auto;
377        }
378
379    }
380}