-
Notifications
You must be signed in to change notification settings - Fork 1k
/
StreamServiceImpl.java
406 lines (346 loc) · 16.2 KB
/
StreamServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.streams;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.bson.types.ObjectId;
import org.graylog2.alarmcallbacks.AlarmCallbackConfiguration;
import org.graylog2.alarmcallbacks.AlarmCallbackConfigurationAVImpl;
import org.graylog2.alarmcallbacks.AlarmCallbackConfigurationService;
import org.graylog2.alarmcallbacks.EmailAlarmCallback;
import org.graylog2.alerts.AlertService;
import org.graylog2.database.MongoConnection;
import org.graylog2.database.NotFoundException;
import org.graylog2.database.PersistedServiceImpl;
import org.graylog2.indexer.IndexSet;
import org.graylog2.indexer.MongoIndexSet;
import org.graylog2.indexer.indexset.IndexSetConfig;
import org.graylog2.indexer.indexset.IndexSetService;
import org.graylog2.notifications.Notification;
import org.graylog2.notifications.NotificationService;
import org.graylog2.plugin.Tools;
import org.graylog2.plugin.alarms.AlertCondition;
import org.graylog2.plugin.database.EmbeddedPersistable;
import org.graylog2.plugin.database.ValidationException;
import org.graylog2.plugin.streams.Output;
import org.graylog2.plugin.streams.Stream;
import org.graylog2.plugin.streams.StreamRule;
import org.graylog2.rest.resources.streams.requests.CreateStreamRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.ws.rs.BadRequestException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import static com.google.common.base.Strings.isNullOrEmpty;
public class StreamServiceImpl extends PersistedServiceImpl implements StreamService {
private static final Logger LOG = LoggerFactory.getLogger(StreamServiceImpl.class);
private final StreamRuleService streamRuleService;
private final AlertService alertService;
private final OutputService outputService;
private final IndexSetService indexSetService;
private final MongoIndexSet.Factory indexSetFactory;
private final NotificationService notificationService;
private final AlarmCallbackConfigurationService alarmCallbackConfigurationService;
@Inject
public StreamServiceImpl(MongoConnection mongoConnection,
StreamRuleService streamRuleService,
AlertService alertService,
OutputService outputService,
IndexSetService indexSetService,
MongoIndexSet.Factory indexSetFactory,
NotificationService notificationService,
AlarmCallbackConfigurationService alarmCallbackConfigurationService) {
super(mongoConnection);
this.streamRuleService = streamRuleService;
this.alertService = alertService;
this.outputService = outputService;
this.indexSetService = indexSetService;
this.indexSetFactory = indexSetFactory;
this.notificationService = notificationService;
this.alarmCallbackConfigurationService = alarmCallbackConfigurationService;
}
@Nullable
private IndexSet getIndexSet(DBObject dbObject) {
return getIndexSet((String) dbObject.get(StreamImpl.FIELD_INDEX_SET_ID));
}
@Nullable
private IndexSet getIndexSet(String id) {
if (isNullOrEmpty(id)) {
return null;
}
final Optional<IndexSetConfig> indexSetConfig = indexSetService.get(id);
return indexSetConfig.flatMap(c -> Optional.of(indexSetFactory.create(c))).orElse(null);
}
public Stream load(ObjectId id) throws NotFoundException {
final DBObject o = get(StreamImpl.class, id);
if (o == null) {
throw new NotFoundException("Stream <" + id + "> not found!");
}
final List<StreamRule> streamRules = streamRuleService.loadForStreamId(id.toHexString());
final Set<Output> outputs = loadOutputsForRawStream(o);
@SuppressWarnings("unchecked")
final Map<String, Object> fields = o.toMap();
return new StreamImpl((ObjectId) o.get("_id"), fields, streamRules, outputs, getIndexSet(o));
}
@Override
public Stream create(Map<String, Object> fields) {
return new StreamImpl(fields, getIndexSet((String) fields.get(StreamImpl.FIELD_INDEX_SET_ID)));
}
@Override
public Stream create(CreateStreamRequest cr, String userId) {
Map<String, Object> streamData = Maps.newHashMap();
streamData.put(StreamImpl.FIELD_TITLE, cr.title());
streamData.put(StreamImpl.FIELD_DESCRIPTION, cr.description());
streamData.put(StreamImpl.FIELD_CREATOR_USER_ID, userId);
streamData.put(StreamImpl.FIELD_CREATED_AT, Tools.nowUTC());
streamData.put(StreamImpl.FIELD_CONTENT_PACK, cr.contentPack());
streamData.put(StreamImpl.FIELD_MATCHING_TYPE, cr.matchingType().toString());
streamData.put(StreamImpl.FIELD_REMOVE_MATCHES_FROM_DEFAULT_STREAM, cr.removeMatchesFromDefaultStream());
streamData.put(StreamImpl.FIELD_INDEX_SET_ID, cr.indexSetId());
return create(streamData);
}
@Override
public Stream load(String id) throws NotFoundException {
try {
return load(new ObjectId(id));
} catch (IllegalArgumentException e) {
throw new NotFoundException("Stream <" + id + "> not found!");
}
}
@Override
public List<Stream> loadAllEnabled() {
return loadAllEnabled(new HashMap<>());
}
public List<Stream> loadAllEnabled(Map<String, Object> additionalQueryOpts) {
additionalQueryOpts.put(StreamImpl.FIELD_DISABLED, false);
return loadAll(additionalQueryOpts);
}
@Override
public List<Stream> loadAll() {
return loadAll(Collections.emptyMap());
}
public List<Stream> loadAll(Map<String, Object> additionalQueryOpts) {
final DBObject query = new BasicDBObject(additionalQueryOpts);
final List<DBObject> results = query(StreamImpl.class, query);
final List<String> streamIds = results.stream()
.map(o -> o.get("_id").toString())
.collect(Collectors.toList());
final Map<String, List<StreamRule>> allStreamRules = streamRuleService.loadForStreamIds(streamIds);
final ImmutableList.Builder<Stream> streams = ImmutableList.builder();
for (DBObject o : results) {
final ObjectId objectId = (ObjectId) o.get("_id");
final String id = objectId.toHexString();
final List<StreamRule> streamRules = allStreamRules.getOrDefault(id, Collections.emptyList());
LOG.debug("Found {} rules for stream <{}>", streamRules.size(), id);
final Set<Output> outputs = loadOutputsForRawStream(o);
@SuppressWarnings("unchecked")
final Map<String, Object> fields = o.toMap();
streams.add(new StreamImpl(objectId, fields, streamRules, outputs, getIndexSet(o)));
}
return streams.build();
}
@Override
public List<Stream> loadAllWithConfiguredAlertConditions() {
// Explanation: alert_conditions.1 is the first Array element.
Map<String, Object> queryOpts = Collections.singletonMap(
StreamImpl.EMBEDDED_ALERT_CONDITIONS, new BasicDBObject("$ne", Collections.emptyList()));
return loadAll(queryOpts);
}
protected Set<Output> loadOutputsForRawStream(DBObject stream) {
@SuppressWarnings("unchecked")
List<ObjectId> outputIds = (List<ObjectId>) stream.get(StreamImpl.FIELD_OUTPUTS);
Set<Output> result = new HashSet<>();
if (outputIds != null)
for (ObjectId outputId : outputIds)
try {
result.add(outputService.load(outputId.toHexString()));
} catch (NotFoundException e) {
LOG.warn("Non-existing output <{}> referenced from stream <{}>!", outputId.toHexString(), stream.get("_id"));
}
return result;
}
@Override
public long count() {
return totalCount(StreamImpl.class);
}
@Override
public void destroy(Stream stream) throws NotFoundException {
for (StreamRule streamRule : streamRuleService.loadForStream(stream)) {
super.destroy(streamRule);
}
for (Notification notification : notificationService.all()) {
Object rawValue = notification.getDetail("stream_id");
if (rawValue != null && rawValue.toString().equals(stream.getId())) {
LOG.debug("Removing notification that references stream: {}", notification);
notificationService.destroy(notification);
}
}
super.destroy(stream);
}
public void update(Stream stream, String title, String description) throws ValidationException {
if (title != null) {
stream.getFields().put(StreamImpl.FIELD_TITLE, title);
}
if (description != null) {
stream.getFields().put(StreamImpl.FIELD_DESCRIPTION, description);
}
save(stream);
}
@Override
public void pause(Stream stream) throws ValidationException {
stream.setDisabled(true);
save(stream);
}
@Override
public void resume(Stream stream) throws ValidationException {
stream.setDisabled(false);
save(stream);
}
@Override
public List<StreamRule> getStreamRules(Stream stream) throws NotFoundException {
return streamRuleService.loadForStream(stream);
}
@Override
public List<AlertCondition> getAlertConditions(Stream stream) {
List<AlertCondition> conditions = Lists.newArrayList();
if (stream.getFields().containsKey(StreamImpl.EMBEDDED_ALERT_CONDITIONS)) {
@SuppressWarnings("unchecked")
final List<BasicDBObject> alertConditions = (List<BasicDBObject>) stream.getFields().get(StreamImpl.EMBEDDED_ALERT_CONDITIONS);
for (BasicDBObject conditionFields : alertConditions) {
try {
conditions.add(alertService.fromPersisted(conditionFields, stream));
} catch (Exception e) {
LOG.error("Skipping alert condition.", e);
}
}
}
return conditions;
}
@Override
public AlertCondition getAlertCondition(Stream stream, String conditionId) throws NotFoundException {
if (stream.getFields().containsKey(StreamImpl.EMBEDDED_ALERT_CONDITIONS)) {
@SuppressWarnings("unchecked")
final List<BasicDBObject> alertConditions = (List<BasicDBObject>) stream.getFields().get(StreamImpl.EMBEDDED_ALERT_CONDITIONS);
for (BasicDBObject conditionFields : alertConditions) {
try {
if (conditionFields.get("id").equals(conditionId)) {
return alertService.fromPersisted(conditionFields, stream);
}
} catch (Exception e) {
LOG.error("Skipping alert condition.", e);
}
}
}
throw new org.graylog2.database.NotFoundException();
}
@Override
public void addAlertCondition(Stream stream, AlertCondition condition) throws ValidationException {
embed(stream, StreamImpl.EMBEDDED_ALERT_CONDITIONS, (EmbeddedPersistable) condition);
}
@Override
public void updateAlertCondition(Stream stream, AlertCondition condition) throws ValidationException {
removeAlertCondition(stream, condition.getId());
addAlertCondition(stream, condition);
}
@Override
public void removeAlertCondition(Stream stream, String conditionId) {
removeEmbedded(stream, StreamImpl.EMBEDDED_ALERT_CONDITIONS, conditionId);
}
@Override
public void addAlertReceiver(Stream stream, String type, String name) {
final List<AlarmCallbackConfiguration> streamCallbacks = alarmCallbackConfigurationService.getForStream(stream);
updateCallbackConfiguration("add", type, name, streamCallbacks);
}
@Override
public void removeAlertReceiver(Stream stream, String type, String name) {
final List<AlarmCallbackConfiguration> streamCallbacks = alarmCallbackConfigurationService.getForStream(stream);
updateCallbackConfiguration("remove", type, name, streamCallbacks);
}
// I tried to be sorry, really. https://www.youtube.com/watch?v=3KVyRqloGmk
private void updateCallbackConfiguration(String action, String type, String entity, List<AlarmCallbackConfiguration> streamCallbacks) {
final AtomicBoolean ran = new AtomicBoolean(false);
streamCallbacks.stream()
.filter(callback -> callback.getType().equals(EmailAlarmCallback.class.getCanonicalName()))
.forEach(callback -> {
ran.set(true);
final Map<String, Object> configuration = callback.getConfiguration();
String key;
if (type.equals("users")) {
key = EmailAlarmCallback.CK_USER_RECEIVERS;
} else {
key = EmailAlarmCallback.CK_EMAIL_RECEIVERS;
}
final List<String> recipients = (List<String>) configuration.get(key);
if (action.equals("add")) {
if (!recipients.contains(entity)) {
recipients.add(entity);
}
} else {
if (recipients.contains(entity)) {
recipients.remove(entity);
}
}
configuration.put(key, recipients);
final AlarmCallbackConfiguration updatedConfig = ((AlarmCallbackConfigurationAVImpl) callback).toBuilder()
.setConfiguration(configuration)
.build();
try {
alarmCallbackConfigurationService.save(updatedConfig);
} catch (ValidationException e) {
throw new BadRequestException("Unable to save alarm callback configuration", e);
}
});
if (!ran.get()) {
throw new BadRequestException("Unable to " + action + " receiver: Stream has no email alarm callback.");
}
}
@Override
public void addOutput(Stream stream, Output output) {
collection(stream).update(
new BasicDBObject("_id", new ObjectId(stream.getId())),
new BasicDBObject("$addToSet", new BasicDBObject(StreamImpl.FIELD_OUTPUTS, new ObjectId(output.getId())))
);
}
@Override
public void removeOutput(Stream stream, Output output) {
collection(stream).update(
new BasicDBObject("_id", new ObjectId(stream.getId())),
new BasicDBObject("$pull", new BasicDBObject(StreamImpl.FIELD_OUTPUTS, new ObjectId(output.getId())))
);
}
@Override
public void removeOutputFromAllStreams(Output output) {
ObjectId outputId = new ObjectId(output.getId());
DBObject match = new BasicDBObject(StreamImpl.FIELD_OUTPUTS, outputId);
DBObject modify = new BasicDBObject("$pull", new BasicDBObject(StreamImpl.FIELD_OUTPUTS, outputId));
collection(StreamImpl.class).update(
match, modify, false, true
);
}
}