|
| 1 | +/* |
| 2 | + * This file is part of Dependency-Track. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * Copyright (c) OWASP Foundation. All Rights Reserved. |
| 18 | + */ |
| 19 | +package org.dependencytrack.tasks; |
| 20 | + |
| 21 | +import alpine.event.framework.Event; |
| 22 | +import alpine.event.framework.Subscriber; |
| 23 | +import com.google.protobuf.util.Timestamps; |
| 24 | +import net.javacrumbs.shedlock.core.LockConfiguration; |
| 25 | +import net.javacrumbs.shedlock.core.LockExtender; |
| 26 | +import net.javacrumbs.shedlock.core.LockingTaskExecutor; |
| 27 | +import org.cyclonedx.proto.v1_6.Bom; |
| 28 | +import org.dependencytrack.model.Vulnerability; |
| 29 | +import org.dependencytrack.model.VulnerableSoftware; |
| 30 | +import org.dependencytrack.parser.dependencytrack.BovModelConverter; |
| 31 | +import org.dependencytrack.persistence.QueryManager; |
| 32 | +import org.dependencytrack.plugin.PluginManager; |
| 33 | +import org.dependencytrack.plugin.api.datasource.vuln.VulnDataSource; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | + |
| 37 | +import java.time.Duration; |
| 38 | +import java.time.Instant; |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.Collection; |
| 41 | +import java.util.HashMap; |
| 42 | +import java.util.List; |
| 43 | + |
| 44 | +import static org.datanucleus.PropertyNames.PROPERTY_PERSISTENCE_BY_REACHABILITY_AT_COMMIT; |
| 45 | +import static org.dependencytrack.util.LockProvider.executeWithLock; |
| 46 | +import static org.dependencytrack.util.LockProvider.isTaskLockToBeExtended; |
| 47 | +import static org.dependencytrack.util.TaskUtil.getLockConfigForTask; |
| 48 | + |
| 49 | +/** |
| 50 | + * @since 5.7.0 |
| 51 | + */ |
| 52 | +abstract class AbstractVulnDataSourceMirrorTask implements Subscriber { |
| 53 | + |
| 54 | + private final PluginManager pluginManager; |
| 55 | + private final Class<? extends Event> eventClass; |
| 56 | + private final String vulnDataSourceExtensionName; |
| 57 | + private final Vulnerability.Source source; |
| 58 | + private final Logger logger; |
| 59 | + private LockConfiguration lockConfig; |
| 60 | + private Instant lockAcquiredAt; |
| 61 | + |
| 62 | + AbstractVulnDataSourceMirrorTask( |
| 63 | + final PluginManager pluginManager, |
| 64 | + final Class<? extends Event> eventClass, |
| 65 | + final String vulnDataSourceExtensionName, |
| 66 | + final Vulnerability.Source source) { |
| 67 | + this.pluginManager = pluginManager; |
| 68 | + this.eventClass = eventClass; |
| 69 | + this.vulnDataSourceExtensionName = vulnDataSourceExtensionName; |
| 70 | + this.source = source; |
| 71 | + this.logger = LoggerFactory.getLogger(this.getClass()); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void inform(final Event e) { |
| 76 | + if (!eventClass.isAssignableFrom(e.getClass())) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + lockConfig = getLockConfigForTask(getClass()); |
| 81 | + |
| 82 | + try { |
| 83 | + executeWithLock( |
| 84 | + this.lockConfig, |
| 85 | + (LockingTaskExecutor.Task) this::informLocked); |
| 86 | + } catch (Throwable ex) { |
| 87 | + logger.error("Failed to acquire lock or execute task", ex); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void informLocked() { |
| 92 | + lockAcquiredAt = Instant.now(); |
| 93 | + |
| 94 | + try (final var dataSource = pluginManager.getExtension(VulnDataSource.class, vulnDataSourceExtensionName)) { |
| 95 | + if (dataSource == null) { |
| 96 | + return; // Likely disabled. |
| 97 | + } |
| 98 | + |
| 99 | + final var bovBatch = new ArrayList<Bom>(25); |
| 100 | + while (dataSource.hasNext()) { |
| 101 | + if (Thread.currentThread().isInterrupted()) { |
| 102 | + logger.warn("Interrupted before all BOVs could be consumed"); |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + maybeExtendLock(); |
| 107 | + |
| 108 | + final Bom bov = dataSource.next(); |
| 109 | + if (!bov.getVulnerabilities(0).hasRejected()) { |
| 110 | + bovBatch.add(bov); |
| 111 | + if (bovBatch.size() == 25) { |
| 112 | + processBatch(dataSource, bovBatch); |
| 113 | + bovBatch.clear(); |
| 114 | + } |
| 115 | + } else { |
| 116 | + // TODO: Store rejection / withdrawal timestamp instead, |
| 117 | + // and let analyzers / users decide how to deal with them. |
| 118 | + // Ignoring withdrawn vulnerabilities is legacy behavior. |
| 119 | + logger.warn( |
| 120 | + "Skipping vulnerability {} rejected at {}", |
| 121 | + bov.getVulnerabilities(0).getId(), |
| 122 | + Timestamps.toString(bov.getVulnerabilities(0).getRejected())); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if (!bovBatch.isEmpty()) { |
| 127 | + maybeExtendLock(); |
| 128 | + processBatch(dataSource, bovBatch); |
| 129 | + bovBatch.clear(); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private void processBatch(final VulnDataSource dataSource, final Collection<Bom> bovs) { |
| 135 | + logger.debug("Processing batch of {} BOVs", bovs.size()); |
| 136 | + |
| 137 | + final var vulns = new ArrayList<Vulnerability>(bovs.size()); |
| 138 | + final var vsListByVulnId = new HashMap<String, List<VulnerableSoftware>>(bovs.size()); |
| 139 | + |
| 140 | + for (final Bom bov : bovs) { |
| 141 | + if (bov.getVulnerabilitiesCount() == 0) { |
| 142 | + logger.warn("BOV contains no vulnerabilities; Skipping"); |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + if (bov.getVulnerabilitiesCount() > 1) { |
| 147 | + logger.warn("BOV contains more than one vulnerability; Skipping"); |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + final Vulnerability vuln = BovModelConverter.convert(bov, bov.getVulnerabilities(0), true); |
| 152 | + final List<VulnerableSoftware> vsList = BovModelConverter.extractVulnerableSoftware(bov); |
| 153 | + |
| 154 | + vulns.add(vuln); |
| 155 | + vsListByVulnId.put(vuln.getVulnId(), vsList); |
| 156 | + } |
| 157 | + |
| 158 | + try (final var qm = new QueryManager()) { |
| 159 | + qm.getPersistenceManager().setProperty(PROPERTY_PERSISTENCE_BY_REACHABILITY_AT_COMMIT, "false"); |
| 160 | + |
| 161 | + qm.runInTransaction(() -> { |
| 162 | + for (final Vulnerability vuln : vulns) { |
| 163 | + logger.debug("Synchronizing vulnerability {}", vuln.getVulnId()); |
| 164 | + final Vulnerability persistentVuln = qm.synchronizeVulnerability(vuln, false); |
| 165 | + final List<VulnerableSoftware> vsList = vsListByVulnId.get(persistentVuln.getVulnId()); |
| 166 | + qm.synchronizeVulnerableSoftware(persistentVuln, vsList, this.source); |
| 167 | + } |
| 168 | + }); |
| 169 | + } |
| 170 | + |
| 171 | + for (final Bom bov : bovs) { |
| 172 | + dataSource.markProcessed(bov); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private void maybeExtendLock() { |
| 177 | + final var lockAge = Duration.between(lockAcquiredAt, Instant.now()); |
| 178 | + if (isTaskLockToBeExtended(lockAge.toMillis(), getClass())) { |
| 179 | + logger.warn("Extending lock by {}", lockConfig.getLockAtMostFor()); |
| 180 | + LockExtender.extendActiveLock(lockConfig.getLockAtMostFor(), this.lockConfig.getLockAtLeastFor()); |
| 181 | + lockAcquiredAt = Instant.now(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments