-
-
Notifications
You must be signed in to change notification settings - Fork 597
/
Copy pathNotificationRouter.java
367 lines (326 loc) · 18.5 KB
/
NotificationRouter.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
/*
* This file is part of Dependency-Track.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) OWASP Foundation. All Rights Reserved.
*/
package org.dependencytrack.notification;
import alpine.common.logging.Logger;
import alpine.notification.Notification;
import alpine.notification.NotificationLevel;
import alpine.notification.Subscriber;
import org.dependencytrack.exception.PublisherException;
import org.dependencytrack.model.NotificationPublisher;
import org.dependencytrack.model.NotificationRule;
import org.dependencytrack.model.Project;
import org.dependencytrack.model.Tag;
import org.dependencytrack.notification.publisher.PublishContext;
import org.dependencytrack.notification.publisher.Publisher;
import org.dependencytrack.notification.publisher.SendMailPublisher;
import org.dependencytrack.notification.vo.AnalysisDecisionChange;
import org.dependencytrack.notification.vo.BomConsumedOrProcessed;
import org.dependencytrack.notification.vo.BomProcessingFailed;
import org.dependencytrack.notification.vo.BomValidationFailed;
import org.dependencytrack.notification.vo.NewVulnerabilityIdentified;
import org.dependencytrack.notification.vo.NewVulnerableDependency;
import org.dependencytrack.notification.vo.PolicyViolationIdentified;
import org.dependencytrack.notification.vo.VexConsumedOrProcessed;
import org.dependencytrack.notification.vo.ViolationAnalysisDecisionChange;
import org.dependencytrack.persistence.QueryManager;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static java.util.Objects.requireNonNull;
import static org.dependencytrack.notification.publisher.Publisher.CONFIG_TEMPLATE_KEY;
import static org.dependencytrack.notification.publisher.Publisher.CONFIG_TEMPLATE_MIME_TYPE_KEY;
public class NotificationRouter implements Subscriber {
private static final Logger LOGGER = Logger.getLogger(NotificationRouter.class);
public void inform(final Notification notification) {
final PublishContext ctx = PublishContext.from(notification);
for (final NotificationRule rule : resolveRules(ctx, notification)) {
final PublishContext ruleCtx = ctx.withRule(rule);
// Not all publishers need configuration (i.e. ConsolePublisher)
JsonObject config = Json.createObjectBuilder().build();
if (rule.getPublisherConfig() != null) {
try (StringReader stringReader = new StringReader(rule.getPublisherConfig());
final JsonReader jsonReader = Json.createReader(stringReader)) {
config = jsonReader.readObject();
} catch (Exception e) {
LOGGER.error("An error occurred while preparing the configuration for the notification publisher (%s)".formatted(ruleCtx), e);
}
}
try {
NotificationPublisher notificationPublisher = rule.getPublisher();
final Class<?> publisherClass = Class.forName(notificationPublisher.getPublisherClass());
if (Publisher.class.isAssignableFrom(publisherClass)) {
final Publisher publisher = (Publisher) publisherClass.getDeclaredConstructor().newInstance();
JsonObject notificationPublisherConfig = Json.createObjectBuilder()
.add(CONFIG_TEMPLATE_MIME_TYPE_KEY, notificationPublisher.getTemplateMimeType())
.add(CONFIG_TEMPLATE_KEY, notificationPublisher.getTemplate())
.addAll(Json.createObjectBuilder(config))
.build();
if (publisherClass != SendMailPublisher.class || rule.getTeams().isEmpty() || rule.getTeams() == null) {
publisher.inform(ruleCtx, restrictNotificationToRuleProjects(notification, rule), notificationPublisherConfig);
} else {
((SendMailPublisher) publisher).inform(ruleCtx, restrictNotificationToRuleProjects(notification, rule), notificationPublisherConfig, rule.getTeams());
}
} else {
LOGGER.error("The defined notification publisher is not assignable from " + Publisher.class.getCanonicalName() + " (%s)".formatted(ruleCtx));
}
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
InvocationTargetException | IllegalAccessException e) {
LOGGER.error("An error occurred while instantiating a notification publisher (%s)".formatted(ruleCtx), e);
} catch (PublisherException publisherException) {
LOGGER.error("An error occurred during the publication of the notification (%s)".formatted(ruleCtx), publisherException);
}
}
}
private Notification restrictNotificationToRuleProjects(final Notification notification, final NotificationRule rule) {
if (!(notification.getSubject() instanceof final NewVulnerabilityIdentified subject)
|| subject.getAffectedProjects() == null || subject.getAffectedProjects().isEmpty()) {
return notification;
}
final boolean shouldFilterOnRuleProjects = rule.getProjects() != null && !rule.getProjects().isEmpty();
final boolean shouldFilterOnRuleTags = rule.getTags() != null && !rule.getTags().isEmpty();
if (!shouldFilterOnRuleProjects && !shouldFilterOnRuleTags) {
return notification;
}
final Predicate<Project> projectFilterPredicate;
if (shouldFilterOnRuleProjects && shouldFilterOnRuleTags) {
projectFilterPredicate = matchesAnyProjectOfRule(rule).or(hasAnyTagOfRule(rule));
} else if (shouldFilterOnRuleProjects) {
projectFilterPredicate = matchesAnyProjectOfRule(rule);
} else {
projectFilterPredicate = hasAnyTagOfRule(rule);
}
final Set<Project> filteredAffectedProjects = subject.getAffectedProjects().stream()
.filter(projectFilterPredicate)
.collect(Collectors.toSet());
if (filteredAffectedProjects.size() == subject.getAffectedProjects().size()) {
return notification;
}
final var filteredSubject = new NewVulnerabilityIdentified(
subject.getVulnerability(),
subject.getComponent(),
filteredAffectedProjects,
subject.getVulnerabilityAnalysisLevel()
);
return new Notification()
.group(notification.getGroup())
.scope(notification.getScope())
.level(notification.getLevel())
.title(notification.getTitle())
.content(notification.getContent())
.timestamp(notification.getTimestamp())
.subject(filteredSubject);
}
private Predicate<Project> matchesAnyProjectOfRule(final NotificationRule rule) {
requireNonNull(rule.getProjects());
return project -> rule.getProjects().stream()
.map(Project::getUuid)
.anyMatch(project.getUuid()::equals);
}
private Predicate<Project> hasAnyTagOfRule(final NotificationRule rule) {
requireNonNull(rule.getTags());
return project -> {
if (project.getTags() == null || project.getTags().isEmpty()) {
return false;
}
final Set<String> projectTagNames = project.getTags().stream()
.map(Tag::getName)
.collect(Collectors.toSet());
return rule.getTags().stream()
.map(Tag::getName)
.anyMatch(projectTagNames::contains);
};
}
List<NotificationRule> resolveRules(final PublishContext ctx, final Notification notification) {
final List<NotificationRule> rules = new ArrayList<>();
if (notification == null || notification.getScope() == null || notification.getGroup() == null || notification.getLevel() == null) {
LOGGER.debug("Mandatory fields of notification are missing; Unable to resolve rules (%s)".formatted(ctx));
return rules;
}
try (QueryManager qm = new QueryManager()) {
final PersistenceManager pm = qm.getPersistenceManager();
final Query<NotificationRule> query = pm.newQuery(NotificationRule.class);
pm.getFetchPlan().addGroup(NotificationPublisher.FetchGroup.ALL.name());
final StringBuilder sb = new StringBuilder();
final NotificationLevel level = notification.getLevel();
if (NotificationLevel.INFORMATIONAL == level) {
sb.append("notificationLevel == 'INFORMATIONAL' && ");
} else if (NotificationLevel.WARNING == level) {
sb.append("(notificationLevel == 'WARNING' || notificationLevel == 'INFORMATIONAL') && ");
} else if (NotificationLevel.ERROR == level) {
sb.append("(notificationLevel == 'INFORMATIONAL' || notificationLevel == 'WARNING' || notificationLevel == 'ERROR') && ");
}
sb.append("enabled == true && scope == :scope"); //todo: improve this - this only works for testing
query.setFilter(sb.toString());
query.setParameters(NotificationScope.valueOf(notification.getScope()));
final List<NotificationRule> result = query.executeList();
pm.detachCopyAll(result);
LOGGER.debug("Matched %d notification rules (%s)".formatted(result.size(), ctx));
if (NotificationScope.PORTFOLIO.name().equals(notification.getScope())
&& notification.getSubject() instanceof final NewVulnerabilityIdentified subject) {
limitToProject(ctx, rules, result, notification, subject.getComponent().getProject());
} else if (NotificationScope.PORTFOLIO.name().equals(notification.getScope())
&& notification.getSubject() instanceof final NewVulnerableDependency subject) {
limitToProject(ctx, rules, result, notification, subject.getComponent().getProject());
} else if (NotificationScope.PORTFOLIO.name().equals(notification.getScope())
&& notification.getSubject() instanceof final BomConsumedOrProcessed subject) {
limitToProject(ctx, rules, result, notification, subject.getProject());
} else if (NotificationScope.PORTFOLIO.name().equals(notification.getScope())
&& notification.getSubject() instanceof final BomProcessingFailed subject) {
limitToProject(ctx, rules, result, notification, subject.getProject());
} else if (NotificationScope.PORTFOLIO.name().equals(notification.getScope())
&& notification.getSubject() instanceof final BomValidationFailed subject) {
limitToProject(ctx, rules, result, notification, subject.getProject());
} else if (NotificationScope.PORTFOLIO.name().equals(notification.getScope())
&& notification.getSubject() instanceof final VexConsumedOrProcessed subject) {
limitToProject(ctx, rules, result, notification, subject.getProject());
} else if (NotificationScope.PORTFOLIO.name().equals(notification.getScope())
&& notification.getSubject() instanceof final PolicyViolationIdentified subject) {
limitToProject(ctx, rules, result, notification, subject.getProject());
} else if (NotificationScope.PORTFOLIO.name().equals(notification.getScope())
&& notification.getSubject() instanceof final AnalysisDecisionChange subject) {
limitToProject(ctx, rules, result, notification, subject.getProject());
} else if (NotificationScope.PORTFOLIO.name().equals(notification.getScope())
&& notification.getSubject() instanceof final ViolationAnalysisDecisionChange subject) {
limitToProject(ctx, rules, result, notification, subject.getComponent().getProject());
} else {
for (final NotificationRule rule : result) {
if (rule.getNotifyOn().contains(NotificationGroup.valueOf(notification.getGroup()))) {
rules.add(rule);
}
}
}
}
return rules;
}
/**
* if the rule specified one or more projects as targets, reduce the execution
* of the notification down to those projects that the rule matches and which
* also match projects affected by the vulnerability.
*/
private void limitToProject(
final PublishContext ctx,
final List<NotificationRule> applicableRules,
final List<NotificationRule> rules,
final Notification notification,
final Project limitToProject
) {
requireNonNull(limitToProject, "limitToProject must not be null");
for (final NotificationRule rule : rules) {
final PublishContext ruleCtx = ctx.withRule(rule);
if (!rule.getNotifyOn().contains(NotificationGroup.valueOf(notification.getGroup()))) {
continue;
}
final boolean isLimitedToProjects = rule.getProjects() != null && !rule.getProjects().isEmpty();
final boolean isLimitedToTags = rule.getTags() != null && !rule.getTags().isEmpty();
if (!isLimitedToProjects && !isLimitedToTags) {
LOGGER.debug("Rule is not limited to projects or tags; Rule is applicable (%s)".formatted(ruleCtx));
applicableRules.add(rule);
continue;
}
if (isLimitedToTags) {
final Predicate<Project> tagMatchPredicate = project -> project.isActive()
&& project.getTags() != null
&& project.getTags().stream().anyMatch(rule.getTags()::contains);
if (tagMatchPredicate.test(limitToProject)) {
LOGGER.debug("""
Project %s is tagged with any of the "limit to" tags; \
Rule is applicable (%s)""".formatted(limitToProject.getUuid(), ruleCtx));
applicableRules.add(rule);
continue;
} else if (rule.isNotifyChildren() && isChildOfProjectMatching(limitToProject, tagMatchPredicate)) {
LOGGER.debug("""
Project %s is child of a project tagged with any of the "limit to" tags; \
Rule is applicable (%s)""".formatted(limitToProject.getUuid(), ruleCtx));
applicableRules.add(rule);
continue;
}
} else {
LOGGER.debug("Rule is not limited to tags (%s)".formatted(ruleCtx));
}
if (isLimitedToProjects) {
var matched = false;
for (final Project project : rule.getProjects()) {
if (project.getUuid().equals(limitToProject.getUuid())) {
LOGGER.debug("Project %s is part of the \"limit to\" list of the rule; Rule is applicable (%s)"
.formatted(limitToProject.getUuid(), ruleCtx));
matched = true;
break;
} else if (rule.isNotifyChildren()) {
final boolean isChildOfLimitToProject = checkIfChildrenAreAffected(project, limitToProject.getUuid());
if (isChildOfLimitToProject) {
LOGGER.debug("Project %s is child of \"limit to\" project %s; Rule is applicable (%s)"
.formatted(limitToProject.getUuid(), project.getUuid(), ruleCtx));
matched = true;
break;
} else {
LOGGER.debug("Project %s is not a child of \"limit to\" project %s (%s)"
.formatted(limitToProject.getUuid(), project.getUuid(), ruleCtx));
}
}
}
if (matched) {
applicableRules.add(rule);
} else {
LOGGER.debug("Project %s is not part of the \"limit to\" list of the rule; Rule is not applicable (%s)"
.formatted(limitToProject.getUuid(), ruleCtx));
}
} else {
LOGGER.debug("Rule is not limited to projects (%s)".formatted(ruleCtx));
}
}
LOGGER.debug("Applicable rules: %s (%s)"
.formatted(applicableRules.stream().map(NotificationRule::getName).collect(Collectors.joining(", ")), ctx));
}
private boolean checkIfChildrenAreAffected(Project parent, UUID uuid) {
// TODO: Making this a recursive SQL query would be a lot more efficient.
boolean isChild = false;
if (parent.getChildren() == null || parent.getChildren().isEmpty()) {
return false;
}
for (Project child : parent.getChildren()) {
final boolean isChildActive = child.isActive();
if ((child.getUuid().equals(uuid) && isChildActive) || isChild) {
return true;
}
isChild = checkIfChildrenAreAffected(child, uuid);
}
return isChild;
}
private boolean isChildOfProjectMatching(final Project childProject, final Predicate<Project> matchFunction) {
// TODO: Making this a recursive SQL query would be a lot more efficient.
Project parent = childProject.getParent();
while (parent != null) {
if (matchFunction.test(parent)) {
return true;
}
parent = parent.getParent();
}
return false;
}
}