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 2015-2016 ForgeRock AS.
15 */
16 package org.forgerock.audit.handlers.csv;
17
18 import org.forgerock.audit.events.handlers.FileBasedEventHandlerConfiguration;
19 import org.forgerock.util.Reject;
20 import org.forgerock.util.time.Duration;
21
22 import com.fasterxml.jackson.annotation.JsonIgnore;
23 import com.fasterxml.jackson.annotation.JsonProperty;
24 import com.fasterxml.jackson.annotation.JsonPropertyDescription;
25
26 /**
27 * A configuration for CSV audit event handler.
28 * <p>
29 * This configuration object can be created from JSON. Example of valid JSON configuration:
30 *
31 * <pre>
32 * {
33 * "name" : "csv",
34 * "topics": [ "access", "activity", "config", "authentication" ],
35 * "logDirectory" : "/path/to/audit/files/",
36 * "formatting" : {
37 * "quoteChar" : "\"",
38 * "delimiterChar" : ",",
39 * "endOfLineSymbols" : "\n"
40 * },
41 * "security" : {
42 * "enabled" : "true",
43 * "filename" : "/path/to/keystore.jks",
44 * "password" : "correcthorsebatterystaple",
45 * "signatureInterval" : "3 seconds"
46 * },
47 * "buffering" : {
48 * "enabled" : "true",
49 * "autoFlush" : "true"
50 * }
51 * }
52 * </pre>
53 */
54 public class CsvAuditEventHandlerConfiguration extends FileBasedEventHandlerConfiguration {
55
56 @JsonProperty(required = true)
57 @JsonPropertyDescription("audit.handlers.csv.logDirectory")
58 private String logDirectory;
59
60 @JsonPropertyDescription("audit.handlers.csv.formatting")
61 private CsvFormatting formatting = new CsvFormatting();
62
63 @JsonPropertyDescription("audit.handlers.csv.security")
64 private CsvSecurity security = new CsvSecurity();
65
66 /** Event buffering is disabled by default. */
67 @JsonPropertyDescription("audit.handlers.csv.buffering")
68 protected EventBufferingConfiguration buffering = new EventBufferingConfiguration();
69
70 /**
71 * Returns the directory where CSV file is located.
72 *
73 * @return the location of the CSV file.
74 */
75 public String getLogDirectory() {
76 return logDirectory;
77 }
78
79 /**
80 * Sets the directory where CSV file is located.
81 *
82 * @param directory
83 * the directory.
84 */
85 public void setLogDirectory(String directory) {
86 logDirectory = directory;
87 }
88
89 /**
90 * Returns the CSV formatting options.
91 *
92 * @return the CSV formatting options.
93 */
94 public CsvFormatting getFormatting() {
95 return formatting;
96 }
97
98 /**
99 * 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 }