-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathAnalysisQueryManager.java
More file actions
251 lines (220 loc) · 10.7 KB
/
AnalysisQueryManager.java
File metadata and controls
251 lines (220 loc) · 10.7 KB
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
/*
* 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.persistence;
import alpine.model.ApiKey;
import alpine.model.User;
import alpine.resources.AlpineRequest;
import org.dependencytrack.model.Analysis;
import org.dependencytrack.model.AnalysisComment;
import org.dependencytrack.model.AnalysisJustification;
import org.dependencytrack.model.AnalysisResponse;
import org.dependencytrack.model.AnalysisState;
import org.dependencytrack.model.Component;
import org.dependencytrack.model.Project;
import org.dependencytrack.model.RatingSource;
import org.dependencytrack.model.Vulnerability;
import org.dependencytrack.notification.JdoNotificationEmitter;
import org.dependencytrack.notification.NotificationModelConverter;
import org.dependencytrack.persistence.command.MakeAnalysisCommand;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.dependencytrack.notification.api.NotificationFactory.createVulnerabilityAnalysisDecisionChangeNotification;
import static org.dependencytrack.util.PersistenceUtil.assertPersistent;
public class AnalysisQueryManager extends QueryManager implements IQueryManager {
/**
* Constructs a new QueryManager.
*
* @param pm a PersistenceManager object
*/
AnalysisQueryManager(final PersistenceManager pm) {
super(pm);
}
/**
* Constructs a new QueryManager.
*
* @param pm a PersistenceManager object
* @param request an AlpineRequest object
*/
AnalysisQueryManager(final PersistenceManager pm, final AlpineRequest request) {
super(pm, request);
}
/**
* Returns a List Analysis for the specified Project.
*
* @param project the Project
* @return a List of Analysis objects, or null if not found
*/
@SuppressWarnings("unchecked")
List<Analysis> getAnalyses(Project project) {
final Query<Analysis> query = pm.newQuery(Analysis.class, "project == :project");
return (List<Analysis>) query.execute(project);
}
/**
* Returns a Analysis for the specified Project, Component, and Vulnerability.
*
* @param component the Component
* @param vulnerability the Vulnerability
* @return a Analysis object, or null if not found
*/
public Analysis getAnalysis(Component component, Vulnerability vulnerability) {
final Query<Analysis> query = pm.newQuery(Analysis.class, "component == :component && vulnerability == :vulnerability");
query.setRange(0, 1);
return singleResult(query.execute(component, vulnerability));
}
/**
* @since 5.7.0
*/
@Override
public long makeAnalysis(final MakeAnalysisCommand command) {
assertPersistent(command.component(), "component must be persistent");
assertPersistent(command.vulnerability(), "vulnerability must be persistent");
return callInTransaction(() -> {
final var auditTrailComments = new ArrayList<String>();
Analysis analysis = getAnalysis(command.component(), command.vulnerability());
if (analysis == null) {
analysis = new Analysis();
analysis.setComponent(command.component());
analysis.setVulnerability(command.vulnerability());
analysis.setAnalysisState(AnalysisState.NOT_SET);
analysis.setAnalysisJustification(AnalysisJustification.NOT_SET);
analysis.setAnalysisResponse(AnalysisResponse.NOT_SET);
analysis.setSuppressed(false);
persist(analysis);
}
boolean stateChanged = false;
boolean suppressionChanged = false;
if (command.state() != null && command.state() != analysis.getAnalysisState()) {
auditTrailComments.add("Analysis: %s → %s".formatted(analysis.getAnalysisState(), command.state()));
analysis.setAnalysisState(command.state());
stateChanged = true;
}
if (command.justification() != null && command.justification() != analysis.getAnalysisJustification()) {
auditTrailComments.add("Justification: %s → %s".formatted(analysis.getAnalysisJustification(), command.justification()));
analysis.setAnalysisJustification(command.justification());
}
if (command.response() != null && command.response() != analysis.getAnalysisResponse()) {
auditTrailComments.add("Vendor Response: %s → %s".formatted(analysis.getAnalysisResponse(), command.response()));
analysis.setAnalysisResponse(command.response());
}
if (command.details() != null && !command.details().equals(analysis.getAnalysisDetails())) {
auditTrailComments.add("Details: %s".formatted(command.details()));
analysis.setAnalysisDetails(command.details());
}
if (command.suppress() != null && command.suppress() != analysis.isSuppressed()) {
auditTrailComments.add(command.suppress() ? "Suppressed" : "Unsuppressed");
analysis.setSuppressed(command.suppress());
suppressionChanged = true;
}
final boolean canUpdate = canUpdateAnalysis(analysis.getSource(), command.source());
if (canUpdate && (command.owaspVector() != null || command.owaspScore() != null || command.owaspSeverity() != null)) {
if (command.owaspVector() != null && !command.owaspVector().equals(analysis.getOwaspVector())) {
auditTrailComments.add("OWASP RR Vector: %s → %s".formatted(
analysis.getOwaspVector(), command.owaspVector()));
analysis.setOwaspVector(command.owaspVector());
}
if (command.owaspScore() != null && !command.owaspScore().equals(analysis.getOwaspScore())) {
auditTrailComments.add("OWASP RR Score: %s → %s".formatted(
analysis.getOwaspScore(), command.owaspScore()));
analysis.setOwaspScore(command.owaspScore());
}
if (command.owaspSeverity() != null && command.owaspSeverity() != analysis.getOwaspSeverity()) {
auditTrailComments.add("OWASP RR Severity: %s → %s".formatted(
analysis.getOwaspSeverity(), command.owaspSeverity()));
analysis.setOwaspSeverity(command.owaspSeverity());
}
}
if (command.source() != null && canUpdate) {
if (analysis.getSource() != command.source()) {
if (analysis.getSource() != null) {
auditTrailComments.add("Source: %s → %s".formatted(analysis.getSource(), command.source()));
}
analysis.setSource(command.source());
}
}
final List<String> comments =
!command.options().contains(MakeAnalysisCommand.Option.OMIT_AUDIT_TRAIL)
? auditTrailComments
: new ArrayList<>();
if (command.comment() != null) {
comments.add(command.comment());
}
createAnalysisComments(analysis, command.commenter(), comments);
if (!command.options().contains(MakeAnalysisCommand.Option.OMIT_NOTIFICATION)
&& (stateChanged || suppressionChanged)) {
new JdoNotificationEmitter(this).emit(
createVulnerabilityAnalysisDecisionChangeNotification(
NotificationModelConverter.convert(analysis.getProject()),
NotificationModelConverter.convert(analysis.getComponent()),
NotificationModelConverter.convert(analysis.getVulnerability()),
NotificationModelConverter.convert(analysis),
stateChanged,
suppressionChanged));
}
return analysis.getId();
});
}
private void createAnalysisComments(
final Analysis analysis,
final String commenter,
final List<String> comments) {
assertPersistent(analysis, "analysis must be persistent");
if (comments == null || comments.isEmpty()) {
return;
}
final var now = new Date();
final String commenterToUse;
if (commenter == null) {
commenterToUse = switch (principal) {
case User user -> user.getUsername();
case ApiKey apiKey -> apiKey.getTeams().get(0).getName();
case null -> null;
default -> throw new IllegalStateException(
"Unexpected principal type: " + principal.getClass().getName());
};
} else {
commenterToUse = commenter;
}
runInTransaction(() -> {
final var analysisComments = new ArrayList<AnalysisComment>(comments.size());
for (final String comment : comments) {
final var analysisComment = new AnalysisComment();
analysisComment.setAnalysis(analysis);
analysisComment.setCommenter(commenterToUse);
analysisComment.setComment(comment);
analysisComment.setTimestamp(now);
analysisComments.add(analysisComment);
}
persist(analysisComments);
if (analysis.getAnalysisComments() != null) {
analysis.getAnalysisComments().addAll(analysisComments);
} else {
analysis.setAnalysisComments(analysisComments);
}
});
}
private boolean canUpdateAnalysis(final RatingSource existingSource, final RatingSource newSource) {
if (newSource == null || existingSource == null) {
return true;
}
return newSource.hasHigherOrEqualPrecedenceThan(existingSource);
}
}