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 2016 ForgeRock AS.
015 */
016package org.forgerock.audit.handlers.elasticsearch;
017
018import org.forgerock.audit.events.handlers.EventHandlerConfiguration;
019
020import com.fasterxml.jackson.annotation.JsonPropertyDescription;
021
022/**
023 * A configuration for Elasticsearch audit event handler.
024 * <p/>
025 * This configuration object can be created from JSON. Example of valid JSON configuration:
026 * <pre>
027 *  {
028 *    "name" : "elasticsearch",
029 *    "topics": [ "access", "activity", "config", "authentication" ],
030 *    "connection" : {
031 *      "useSSL" : true,
032 *      "host" : "localhost",
033 *      "port" : 9200,
034 *      "username" : "myUsername",
035 *      "password" : "myPassword"
036 *    },
037 *    "indexMapping" : {
038 *      "indexName" : "audit"
039 *    },
040 *    "buffering" : {
041 *      "enabled" : true,
042 *      "maxSize" : 10000,
043 *      "writeInterval" : "250 millis",
044 *      "maxBatchedEvents" : 500
045 *    }
046 *  }
047 * </pre>
048 */
049public class ElasticsearchAuditEventHandlerConfiguration extends EventHandlerConfiguration {
050
051    @JsonPropertyDescription("audit.handlers.elasticsearch.connection")
052    private ConnectionConfiguration connection = new ConnectionConfiguration();
053
054    @JsonPropertyDescription("audit.handlers.elasticsearch.indexMapping")
055    private IndexMappingConfiguration indexMapping = new IndexMappingConfiguration();
056
057    @JsonPropertyDescription("audit.handlers.elasticsearch.buffering")
058    private EventBufferingConfiguration buffering = new EventBufferingConfiguration();
059
060    /**
061     * Gets configuration of connection to Elasticsearch.
062     *
063     * @return configuration of connection to Elasticsearch
064     */
065    public ConnectionConfiguration getConnection() {
066        return connection;
067    }
068
069    /**
070     * Sets configuration of connection to Elasticsearch.
071     *
072     * @param connection configuration of connection to Elasticsearch
073     */
074    public void setConnection(ConnectionConfiguration connection) {
075        this.connection = connection;
076    }
077
078    /**
079     * Sets configuration of index mapping.
080     *
081     * @return configuration of index mapping
082     */
083    public IndexMappingConfiguration getIndexMapping() {
084        return indexMapping;
085    }
086
087    /**
088     * Gets configuration of index mapping.
089     *
090     * @param indexMapping configuration of index mapping
091     */
092    public void setIndexMapping(IndexMappingConfiguration indexMapping) {
093        this.indexMapping = indexMapping;
094    }
095
096    /**
097     * Gets configuration of event buffering.
098     *
099     * @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}