View Javadoc
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 2016 ForgeRock AS.
15   */
16  package org.forgerock.audit.handlers.elasticsearch;
17  
18  import org.forgerock.audit.events.handlers.EventHandlerConfiguration;
19  
20  import com.fasterxml.jackson.annotation.JsonPropertyDescription;
21  
22  /**
23   * A configuration for Elasticsearch audit event handler.
24   * <p/>
25   * This configuration object can be created from JSON. Example of valid JSON configuration:
26   * <pre>
27   *  {
28   *    "name" : "elasticsearch",
29   *    "topics": [ "access", "activity", "config", "authentication" ],
30   *    "connection" : {
31   *      "useSSL" : true,
32   *      "host" : "localhost",
33   *      "port" : 9200,
34   *      "username" : "myUsername",
35   *      "password" : "myPassword"
36   *    },
37   *    "indexMapping" : {
38   *      "indexName" : "audit"
39   *    },
40   *    "buffering" : {
41   *      "enabled" : true,
42   *      "maxSize" : 10000,
43   *      "writeInterval" : "250 millis",
44   *      "maxBatchedEvents" : 500
45   *    }
46   *  }
47   * </pre>
48   */
49  public class ElasticsearchAuditEventHandlerConfiguration extends EventHandlerConfiguration {
50  
51      @JsonPropertyDescription("audit.handlers.elasticsearch.connection")
52      private ConnectionConfiguration connection = new ConnectionConfiguration();
53  
54      @JsonPropertyDescription("audit.handlers.elasticsearch.indexMapping")
55      private IndexMappingConfiguration indexMapping = new IndexMappingConfiguration();
56  
57      @JsonPropertyDescription("audit.handlers.elasticsearch.buffering")
58      private EventBufferingConfiguration buffering = new EventBufferingConfiguration();
59  
60      /**
61       * Gets configuration of connection to Elasticsearch.
62       *
63       * @return configuration of connection to Elasticsearch
64       */
65      public ConnectionConfiguration getConnection() {
66          return connection;
67      }
68  
69      /**
70       * Sets configuration of connection to Elasticsearch.
71       *
72       * @param connection configuration of connection to Elasticsearch
73       */
74      public void setConnection(ConnectionConfiguration connection) {
75          this.connection = connection;
76      }
77  
78      /**
79       * Sets configuration of index mapping.
80       *
81       * @return configuration of index mapping
82       */
83      public IndexMappingConfiguration getIndexMapping() {
84          return indexMapping;
85      }
86  
87      /**
88       * Gets configuration of index mapping.
89       *
90       * @param indexMapping configuration of index mapping
91       */
92      public void setIndexMapping(IndexMappingConfiguration indexMapping) {
93          this.indexMapping = indexMapping;
94      }
95  
96      /**
97       * Gets configuration of event buffering.
98       *
99       * @return configuration of event buffering
100      */
101     public EventBufferingConfiguration getBuffering() {
102         return buffering;
103     }
104 
105     /**
106      * Sets configuration of event buffering.
107      *
108      * @param buffering configuration of event buffering
109      */
110     public void setBuffering(EventBufferingConfiguration buffering) {
111         this.buffering = buffering;
112     }
113 
114     @Override
115     public boolean isUsableForQueries() {
116         return true;
117     }
118 
119     /**
120      * Configuration of connection to Elasticsearch.
121      */
122     public static class ConnectionConfiguration {
123 
124         /**
125          * Elasticsearch default host ({@code localhost}) in a development environment.
126          */
127         private static final String DEFAULT_HOST = "localhost";
128 
129         /**
130          * Elasticsearch default port ({@code 9200}) in a development environment.
131          */
132         private static final int DEFAULT_PORT = 9200;
133 
134         @JsonPropertyDescription("audit.handlers.elasticsearch.connection.useSSL")
135         private boolean useSSL;
136 
137         @JsonPropertyDescription("audit.handlers.elasticsearch.connection.host")
138         private String host;
139 
140         @JsonPropertyDescription("audit.handlers.elasticsearch.connection.port")
141         private int port;
142 
143         @JsonPropertyDescription("audit.handlers.elasticsearch.connection.username")
144         private String username;
145 
146         @JsonPropertyDescription("audit.handlers.elasticsearch.connection.password")
147         private String password;
148 
149         /**
150          * Indicates if the connection uses SSL.
151          *
152          * @return {@code true} when the connection uses SSL.
153          */
154         public boolean isUseSSL() {
155             return useSSL;
156         }
157 
158         /**
159          * Sets the use of a SSL connection.
160          *
161          * @param useSSL {@code true} when the connection uses SSL.
162          */
163         public void setUseSSL(boolean useSSL) {
164             this.useSSL = useSSL;
165         }
166 
167         /**
168          * Gets the {@code host} for the connection (default {@code localhost}).
169          *
170          * @return The {@code host} for the connection.
171          */
172         public String getHost() {
173             return host != null && !host.isEmpty() ? host : DEFAULT_HOST;
174         }
175 
176         /**
177          * Sets the {@code host} for the connection.
178          *
179          * @param host The {@code host} for the connection.
180          */
181         public void setHost(String host) {
182             this.host = host;
183         }
184 
185         /**
186          * Gets the {@code port} for the connection (default {@code 9200}).
187          *
188          * @return The {@code port} for the connection.
189          */
190         public int getPort() {
191             return port > 0 ? port : DEFAULT_PORT;
192         }
193 
194         /**
195          * Sets the {@code port} for the connection.
196          *
197          * @param port The {@code port} for the connection.
198          */
199         public void setPort(int port) {
200             this.port = port;
201         }
202 
203         /**
204          * Gets Elasticsearch password for HTTP basic authentication.
205          *
206          * @return The password.
207          */
208         public String getPassword() {
209             return password;
210         }
211 
212         /**
213          * Sets Elasticsearch password for HTTP basic authentication.
214          *
215          * @param password The password.
216          */
217         public void setPassword(String password) {
218             this.password = password;
219         }
220 
221         /**
222          * Gets Elasticsearch username for HTTP basic authentication.
223          *
224          * @return The username.
225          */
226         public String getUsername() {
227             return username;
228         }
229 
230         /**
231          * Sets Elasticsearch username for HTTP basic authentication.
232          *
233          * @param username The username.
234          */
235         public void setUsername(String username) {
236             this.username = username;
237         }
238     }
239 
240     /**
241      * Configuration of index mapping.
242      */
243     public static class IndexMappingConfiguration {
244 
245         private static final String DEFAULT_INDEX_NAME = "audit";
246 
247         @JsonPropertyDescription("audit.handlers.elasticsearch.indexMapping.indexName")
248         private String indexName;
249 
250         /**
251          * Gets primary index name (default is {@code audit}).
252          *
253          * @return Index name
254          */
255         public String getIndexName() {
256             return indexName != null && !indexName.isEmpty() ? indexName : DEFAULT_INDEX_NAME;
257         }
258 
259         /**
260          * Sets primary index name.
261          *
262          * @param indexName Index name
263          */
264         public void setIndexName(String indexName) {
265             this.indexName = indexName;
266         }
267     }
268 
269     /**
270      * Configuration of event buffering.
271      */
272     public static class EventBufferingConfiguration {
273 
274         @JsonPropertyDescription("audit.handlers.elasticsearch.buffering.enabled")
275         private boolean enabled;
276 
277         @JsonPropertyDescription("audit.handlers.elasticsearch.buffering.maxSize")
278         private int maxSize;
279 
280         @JsonPropertyDescription("audit.handlers.elasticsearch.buffering.writeInterval")
281         private String writeInterval;
282 
283         @JsonPropertyDescription("audit.handlers.elasticsearch.buffering.maxBatchedEvents")
284         private int maxBatchedEvents;
285 
286         /**
287          * Indicates if event buffering is enabled.
288          *
289          * @return {@code true} if buffering is enabled.
290          */
291         public boolean isEnabled() {
292             return enabled;
293         }
294 
295         /**
296          * Sets the buffering status.
297          *
298          * @param enabled Indicates if buffering is enabled.
299          */
300         public void setEnabled(boolean enabled) {
301             this.enabled = enabled;
302         }
303 
304         /**
305          * Gets the buffer capacity, which are the maximum number of events that can be buffered.
306          *
307          * @return buffer capacity
308          */
309         public int getMaxSize() {
310             return maxSize;
311         }
312 
313         /**
314          * Sets the buffer capacity, which are the maximum number of events that can be buffered.
315          *
316          * @param maxSize buffer capacity
317          */
318         public void setMaxSize(int maxSize) {
319             this.maxSize = maxSize;
320         }
321 
322         /**
323          * Gets the interval for reading events from the buffer to transmit to Elasticsearch.
324          *
325          * @return Interval (e.g., "20 millis")
326          */
327         public String getWriteInterval() {
328             return writeInterval;
329         }
330 
331         /**
332          * Sets the interval for reading events from the buffer to transmit to Elasticsearch.
333          *
334          * @param writeInterval Interval (e.g., "20 millis")
335          */
336         public void setWriteInterval(String writeInterval) {
337             this.writeInterval = writeInterval;
338         }
339 
340         /**
341          * Gets the maximum number of events to read from the buffer on each {@link #getWriteInterval() interval}.
342          *
343          * @return Batch size
344          */
345         public int getMaxBatchedEvents() {
346             return maxBatchedEvents;
347         }
348 
349         /**
350          * Sets the maximum number of events to read from the buffer on each {@link #getWriteInterval() interval}.
351          *
352          * @param maxBatchedEvents Batch size
353          */
354         public void setMaxBatchedEvents(int maxBatchedEvents) {
355             this.maxBatchedEvents = maxBatchedEvents;
356         }
357     }
358 }