-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathSyncVulnPolicyBundleActivity.java
More file actions
446 lines (385 loc) · 18.3 KB
/
SyncVulnPolicyBundleActivity.java
File metadata and controls
446 lines (385 loc) · 18.3 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* 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.policy.vulnerability;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
import com.networknt.schema.serialization.DefaultJsonNodeReader;
import org.apache.commons.codec.digest.DigestUtils;
import org.dependencytrack.dex.api.Activity;
import org.dependencytrack.dex.api.ActivityContext;
import org.dependencytrack.dex.api.ActivitySpec;
import org.dependencytrack.dex.api.failure.TerminalApplicationFailureException;
import org.dependencytrack.persistence.jdbi.VulnerabilityPolicyDao;
import org.dependencytrack.persistence.jdbi.VulnerabilityPolicyDao.VulnPolicyBundleRow;
import org.dependencytrack.persistence.jdbi.VulnerabilityPolicyDao.VulnPolicyIdentityRow;
import org.dependencytrack.policy.cel.CelPolicyScriptHost;
import org.dependencytrack.policy.cel.CelPolicyType;
import org.dependencytrack.proto.internal.workflow.v1.SyncVulnPolicyBundleArg;
import org.dependencytrack.util.HttpUtil;
import org.eclipse.microprofile.config.Config;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import org.projectnessie.cel.tools.ScriptCreateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import static org.dependencytrack.persistence.jdbi.JdbiFactory.inJdbiTransaction;
import static org.dependencytrack.persistence.jdbi.JdbiFactory.useJdbiTransaction;
/**
* @since 5.7.0
*/
@NullMarked
@ActivitySpec(name = "sync-vuln-policy-bundle")
public final class SyncVulnPolicyBundleActivity implements Activity<SyncVulnPolicyBundleArg, Void> {
private static final Logger LOGGER = LoggerFactory.getLogger(SyncVulnPolicyBundleActivity.class);
private static final Pattern POLICY_FILE_NAME_PATTERN =
Pattern.compile("^(?![._])[^/\\\\]+\\.(yaml|yml)$", Pattern.CASE_INSENSITIVE);
private static final int MAX_BUNDLE_SIZE = 10 * 1024 * 1024;
private static final int MAX_ZIP_ENTRIES = 1000;
private static final int MAX_ZIP_ENTRY_SIZE = 1024 * 1024;
private final Config config;
private final HttpClient httpClient;
private final ObjectMapper objectMapper;
private final JsonSchema schema;
public SyncVulnPolicyBundleActivity(Config config, HttpClient httpClient) {
this.config = config;
this.httpClient = httpClient;
this.objectMapper = new ObjectMapper(new YAMLFactory())
.registerModule(new JavaTimeModule())
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
this.schema = JsonSchemaFactory
.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012))
.jsonNodeReader(DefaultJsonNodeReader.builder()
.yamlMapper(objectMapper)
.build())
.build()
.getSchema(getClass().getResourceAsStream("/schema/vulnerability-policy-v1.schema.json"));
}
@Override
public @Nullable Void execute(ActivityContext ctx, @Nullable SyncVulnPolicyBundleArg arg) throws Exception {
if (arg == null || arg.getBundleUuid().isBlank()) {
throw new TerminalApplicationFailureException("No bundle UUID provided");
}
final var bundleUuid = UUID.fromString(arg.getBundleUuid());
// TODO: Eventually each bundle record will have its own URL.
// For the moment we only support a single default bundle.
final URI bundleUri;
try {
bundleUri = config.getValue("dt.vulnerability.policy.bundle.url", URI.class);
} catch (NoSuchElementException e) {
throw new TerminalApplicationFailureException("No bundle URL configured", e);
}
final Credentials credentials = getCredentials();
final BundleFile bundleFile = downloadBundle(bundleUri, credentials);
if (bundleFile == null) {
throw new TerminalApplicationFailureException("Bundle file not found");
}
try {
final VulnPolicyBundleRow bundle = getOrCreateBundle(bundleUuid, bundleUri);
if (isBundleUnchanged(bundle, bundleFile.digest())) {
LOGGER.info("Bundle {} is unchanged", bundleUuid);
return null;
}
final List<VulnerabilityPolicy> policies = parseAndValidatePolicies(bundleFile);
useJdbiTransaction(handle -> {
final var dao = handle.attach(VulnerabilityPolicyDao.class);
reconcilePolicies(dao, bundle.id(), policies);
dao.updateBundleAfterSync(bundleUuid, bundleUri.toString(), bundleFile.digest());
});
} finally {
Files.deleteIfExists(bundleFile.path());
}
return null;
}
private VulnPolicyBundleRow getOrCreateBundle(UUID bundleUuid, URI bundleUri) {
return inJdbiTransaction(handle -> {
final var dao = handle.attach(VulnerabilityPolicyDao.class);
final VulnPolicyBundleRow bundle = dao.getBundleByUuid(bundleUuid);
if (bundle != null) {
return bundle;
}
return dao.createBundle(bundleUuid, bundleUri);
});
}
private static boolean isBundleUnchanged(VulnPolicyBundleRow bundle, String bundleDigest) {
return Objects.equals(bundle.hash(), bundleDigest);
}
private record BundleFile(URI uri, String digest, Path path) {
}
private @Nullable BundleFile downloadBundle(
URI bundleUri,
@Nullable Credentials credentials) throws InterruptedException {
final var requestBuilder = HttpRequest.newBuilder()
.uri(bundleUri)
.timeout(Duration.ofSeconds(10))
.GET();
if (credentials != null) {
requestBuilder.header("Authorization", credentials.authHeaderValue());
}
final HttpResponse<InputStream> response;
try {
response = httpClient.send(
requestBuilder.build(),
HttpResponse.BodyHandlers.ofInputStream());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
try (final InputStream bodyStream = response.body()) {
if (response.statusCode() == 200) {
final Path filePath = Files.createTempFile(null, null);
try {
final MessageDigest fileDigest = DigestUtils.getSha256Digest();
try (final var fos = Files.newOutputStream(filePath);
final var dos = new DigestOutputStream(fos, fileDigest)) {
long totalRead = 0;
final var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = bodyStream.read(buffer)) != -1) {
totalRead += bytesRead;
if (totalRead > MAX_BUNDLE_SIZE) {
throw new TerminalApplicationFailureException(
"Bundle exceeds maximum size of %d bytes".formatted(MAX_BUNDLE_SIZE));
}
dos.write(buffer, 0, bytesRead);
}
}
return new BundleFile(
bundleUri,
HexFormat.of().formatHex(fileDigest.digest()),
filePath);
} catch (Exception e) {
try {
Files.deleteIfExists(filePath);
} catch (IOException ex) {
e.addSuppressed(ex);
}
throw e;
}
}
bodyStream.transferTo(OutputStream.nullOutputStream());
if (response.statusCode() == 404) {
return null;
}
throw new IllegalStateException("Unexpected response code: " + response.statusCode());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private List<VulnerabilityPolicy> parseAndValidatePolicies(BundleFile bundleFile) {
final var parsedPolicies = new ArrayList<VulnerabilityPolicy>();
int numTotalPolicies = 0;
int numFailures = 0;
try (final var zipFile = new ZipFile(bundleFile.path().toFile())) {
if (zipFile.size() > MAX_ZIP_ENTRIES) {
throw new TerminalApplicationFailureException(
"Bundle contains more than %d entries".formatted(MAX_ZIP_ENTRIES));
}
final var entries = zipFile.entries();
while (entries.hasMoreElements()) {
final ZipEntry zipEntry = entries.nextElement();
if (zipEntry.isDirectory()) {
continue;
}
final String fileName = Path.of(zipEntry.getName()).getFileName().toString();
if (!POLICY_FILE_NAME_PATTERN.matcher(fileName).matches()) {
LOGGER.debug("Skipping file '{}': Unexpected file name pattern", zipEntry.getName());
continue;
}
if (zipEntry.getSize() > MAX_ZIP_ENTRY_SIZE) {
throw new TerminalApplicationFailureException(
"Entry '%s' exceeds maximum size of %d bytes".formatted(
zipEntry.getName(), MAX_ZIP_ENTRY_SIZE));
}
final byte[] entryBytes;
try (final InputStream entryStream = zipFile.getInputStream(zipEntry)) {
entryBytes = entryStream.readNBytes(MAX_ZIP_ENTRY_SIZE + 1);
if (entryBytes.length > MAX_ZIP_ENTRY_SIZE) {
throw new TerminalApplicationFailureException(
"Entry '%s' exceeds maximum size of %d bytes".formatted(
zipEntry.getName(), MAX_ZIP_ENTRY_SIZE));
}
}
final JsonNode policyNode = objectMapper.readTree(entryBytes);
if (!"Vulnerability Policy".equals(policyNode.path("type").asText(null))) {
LOGGER.debug("Skipping file '{}': Not a vulnerability policy", zipEntry.getName());
continue;
}
numTotalPolicies++;
final Set<ValidationMessage> validationMessages = schema.validate(policyNode);
if (!validationMessages.isEmpty()) {
LOGGER.warn(
"Schema validation of '{}' failed: {}",
zipEntry.getName(),
validationMessages.stream()
.map(ValidationMessage::getMessage)
.collect(Collectors.joining(", ")));
numFailures++;
continue;
}
final var vulnPolicy = objectMapper.treeToValue(policyNode, VulnerabilityPolicy.class);
boolean conditionValid = true;
try {
CelPolicyScriptHost
.getInstance(CelPolicyType.VULNERABILITY)
.compile(vulnPolicy.getCondition(), CelPolicyScriptHost.CacheMode.NO_CACHE);
} catch (ScriptCreateException e) {
LOGGER.warn("Failed to compile condition of policy '{}'", vulnPolicy.getName(), e);
conditionValid = false;
}
if (conditionValid) {
parsedPolicies.add(vulnPolicy);
} else {
numFailures++;
}
}
} catch (ZipException e) {
throw new TerminalApplicationFailureException("Bundle is not a valid ZIP archive", e);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
if (numFailures > 0) {
throw new TerminalApplicationFailureException("""
Will not update policies in datastore because %d/%d policy definitions failed to validate. \
Policy bundles can only be applied when all policy definitions within them are valid.\
""".formatted(numFailures, numTotalPolicies));
}
return parsedPolicies;
}
private void reconcilePolicies(VulnerabilityPolicyDao dao, long bundleId, List<VulnerabilityPolicy> policies) {
final Map<String, VulnPolicyIdentityRow> existingPolicyByName =
dao.getAllByBundleId(bundleId).stream()
.collect(Collectors.toMap(VulnPolicyIdentityRow::name, Function.identity()));
final var policiesToCreate = new ArrayList<VulnerabilityPolicy>();
final var policiesToUpdate = new ArrayList<VulnerabilityPolicy>();
final var policyNamesSeen = new HashSet<String>();
final var duplicateNames = new ArrayList<String>();
for (final VulnerabilityPolicy policy : policies) {
if (!policyNamesSeen.add(policy.getName())) {
duplicateNames.add(policy.getName());
continue;
}
if (existingPolicyByName.containsKey(policy.getName())) {
policiesToUpdate.add(policy);
} else {
policiesToCreate.add(policy);
}
}
if (!duplicateNames.isEmpty()) {
throw new TerminalApplicationFailureException(
"Bundle contains duplicate policy names: " + duplicateNames);
}
// Detect name conflicts between bundle policies and user-managed policies.
// Names are globally unique, so if a user-managed policy has the same name as
// a bundle policy, the sync cannot proceed.
if (!policiesToCreate.isEmpty()) {
final Set<String> newNames = policiesToCreate.stream()
.map(VulnerabilityPolicy::getName)
.collect(Collectors.toSet());
final List<String> conflicting = dao.getUserManagedPolicyNames().stream()
.filter(newNames::contains)
.toList();
if (!conflicting.isEmpty()) {
throw new TerminalApplicationFailureException("""
Bundle sync aborted: the following policy names conflict with \
existing user-managed policies and must be renamed or deleted \
before the bundle can be applied:\s""" + conflicting);
}
}
final var policyNamesToDelete = new HashSet<>(existingPolicyByName.keySet());
policyNamesToDelete.removeAll(policyNamesSeen);
for (final String policyName : policyNamesToDelete) {
LOGGER.info("Deleting policy '{}'", policyName);
dao.unassignAndDeleteByNameAndBundleId(policyName, bundleId);
}
if (!policiesToCreate.isEmpty()) {
LOGGER.info("Creating {} policies", policiesToCreate.size());
dao.createAll(bundleId, policiesToCreate);
}
if (!policiesToUpdate.isEmpty()) {
LOGGER.info("Updating {} policies", policiesToUpdate.size());
dao.updateAll(policiesToUpdate);
}
}
private sealed interface Credentials {
String authHeaderValue();
record BasicAuth(String username, String password) implements Credentials {
@Override
public String authHeaderValue() {
return HttpUtil.basicAuthHeaderValue(username, password);
}
}
record BearerToken(String token) implements Credentials {
@Override
public String authHeaderValue() {
return "Bearer " + token;
}
}
}
private @Nullable Credentials getCredentials() {
final var username = config
.getOptionalValue("dt.vulnerability.policy.bundle.auth.username", String.class)
.orElse(null);
final var password = config
.getOptionalValue("dt.vulnerability.policy.bundle.auth.password", String.class)
.orElse(null);
if (username != null && password != null) {
return new Credentials.BasicAuth(username, password);
}
return config
.getOptionalValue("dt.vulnerability.policy.bundle.auth.bearer.token", String.class)
.map(Credentials.BearerToken::new)
.orElse(null);
}
}