-
-
Notifications
You must be signed in to change notification settings - Fork 817
Support multiple configurable OSV data sources #7113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sahibamittal
wants to merge
8
commits into
DependencyTrack:main
Choose a base branch
from
sahibamittal:issue-6331-Support-Multiple-OSV-Sources
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6fabd43
Support multiple configurable OSV data sources
sahibamittal caf3816
Merge branch 'main' into issue-6331-Support-Multiple-OSV-Sources
sahibamittal 82f7098
Cleanup formatting
sahibamittal 6cd14e5
Rename OSV sources to feeds; add MDC logging
sahibamittal eb80dd5
Track origin osv data source for Bom
sahibamittal 046216c
Merge remote-tracking branch 'upstream/main' into pr/7113
sahibamittal ef4861f
Allow space in osv feed name
sahibamittal 501058f
Update OsvCompositeVulnDataSourceTest.java
sahibamittal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...c/main/resources/org/dependencytrack/migration/V202608241029__osv_multi_source_config.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| -- Convert legacy OSV config to multi-source config | ||
| -- example: {"enabled": true, ...} -> {"sources": [{"name":"default", "enabled": true, ...}]}. | ||
| UPDATE "EXTENSION_RUNTIME_CONFIG" | ||
| SET "CONFIG" = jsonb_build_object( | ||
| 'sources', | ||
| jsonb_build_array( "CONFIG" || jsonb_build_object('name', 'default') ) | ||
| ), | ||
| "UPDATED_AT" = now() | ||
| WHERE "EXTENSION_POINT" = 'vuln-data-source' | ||
| AND "EXTENSION" = 'osv' | ||
| AND NOT ("CONFIG" ? 'sources'); | ||
|
|
||
| -- Watermarks are namespaced by source name. Move existing watermarks to the default source. | ||
| UPDATE "EXTENSION_KV_STORE" | ||
| SET "KEY" = 'watermark/default/' || replace("KEY", 'watermark/', '') | ||
| WHERE "EXTENSION_POINT" = 'vuln-data-source' | ||
| AND "EXTENSION" = 'osv' | ||
| AND "KEY" LIKE 'watermark/%' | ||
| AND NOT "KEY" LIKE 'watermark/default/%'; |
106 changes: 106 additions & 0 deletions
106
.../osv/src/main/java/org/dependencytrack/vulndatasource/osv/OsvCompositeVulnDataSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * 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.vulndatasource.osv; | ||
|
|
||
| import org.cyclonedx.proto.v1_7.Bom; | ||
| import org.dependencytrack.vulndatasource.api.VulnDataSource; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.slf4j.MDC; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.IdentityHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * @since 5.0.0 | ||
| */ | ||
| final class OsvCompositeVulnDataSource implements VulnDataSource { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(OsvCompositeVulnDataSource.class); | ||
| private final List<OsvVulnDataSource> dataSources; | ||
| private @Nullable OsvVulnDataSource currentDataSource; | ||
| private int currentDataSourceIndex; | ||
|
|
||
| /** | ||
| * Tracks the originating data source for a Bom instance so markProcessed can be | ||
| * delegated to the producer even if currentDataSource has moved on. | ||
| */ | ||
| private final Map<Bom, OsvVulnDataSource> originMap = Collections.synchronizedMap(new IdentityHashMap<>()); | ||
|
|
||
| OsvCompositeVulnDataSource(final List<OsvVulnDataSource> dataSources) { | ||
| this.dataSources = requireNonNull(dataSources, "dataSources must not be null"); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| while (currentDataSourceIndex < dataSources.size()) { | ||
| if (dataSources.get(currentDataSourceIndex).hasNext()) { | ||
| return true; | ||
| } | ||
| currentDataSourceIndex++; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public Bom next() { | ||
| if (currentDataSourceIndex >= dataSources.size()) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
| currentDataSource = dataSources.get(currentDataSourceIndex); | ||
| try (final var _ = MDC.putCloseable("osvSource", currentDataSource.getDataSourceName())) { | ||
| final Bom bom = currentDataSource.next(); | ||
| originMap.put(bom, currentDataSource); | ||
| return bom; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void markProcessed(final Bom bom) { | ||
| final var origin = originMap.remove(bom); | ||
| final var target = origin != null ? origin : currentDataSource; | ||
| if (target == null) { | ||
| throw new IllegalStateException("No data source available to mark processed"); | ||
| } | ||
| try (final var _ = MDC.putCloseable("osvSource", target.getDataSourceName())) { | ||
| target.markProcessed(bom); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| for (final var dataSource : dataSources) { | ||
| try { | ||
| dataSource.close(); | ||
| } catch (final Exception e) { | ||
| LOGGER.warn("Failed to close data source: {}", dataSource.getDataSourceName(), e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| List<OsvVulnDataSource> getDataSources() { | ||
| return dataSources; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding the name of the current source to SLF4J's MDC so log statements show what source is currently being processed. Must be cautious though that the new MDC field doesn't leak, so probably best to only set it around calls to
currentDataSource.