-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added integrity analysis event on apiserver
Signed-off-by: mehab <[email protected]>
- Loading branch information
Showing
21 changed files
with
474 additions
and
38 deletions.
There are no files selected for viewing
17 changes: 7 additions & 10 deletions
17
src/main/java/org/dependencytrack/event/ComponentRepositoryMetaAnalysisEvent.java
This file contains 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 |
---|---|---|
@@ -1,21 +1,18 @@ | ||
package org.dependencytrack.event; | ||
|
||
import alpine.event.framework.Event; | ||
import com.github.packageurl.PackageURL; | ||
import org.dependencytrack.model.Component; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Defines an {@link Event} triggered when requesting a component to be analyzed for meta information. | ||
* | ||
* @param purlCoordinates The package URL coordinates of the {@link Component} to analyze | ||
* @param internal Whether the {@link Component} is internal | ||
* @param purlCoordinates The package URL coordinates of the {@link Component} to analyze | ||
* @param internal Whether the {@link Component} is internal | ||
* @param fetchIntegrityData Whether component hash information needs to be fetched from external api | ||
* @param fetchLatestVersion Whether to fetch latest version meta information for a component. | ||
*/ | ||
public record ComponentRepositoryMetaAnalysisEvent(String purlCoordinates, Boolean internal) implements Event { | ||
|
||
public ComponentRepositoryMetaAnalysisEvent(final Component component) { | ||
this(Optional.ofNullable(component.getPurlCoordinates()).map(PackageURL::canonicalize).orElse(null), component.isInternal()); | ||
} | ||
public record ComponentRepositoryMetaAnalysisEvent(String purlCoordinates, Boolean internal, | ||
boolean fetchIntegrityData, | ||
boolean fetchLatestVersion) implements Event { | ||
|
||
} |
This file contains 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 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
27 changes: 27 additions & 0 deletions
27
src/main/java/org/dependencytrack/event/kafka/componentmeta/AbstractMetaHandler.java
This file contains 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,27 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import org.dependencytrack.event.kafka.KafkaEventDispatcher; | ||
import org.dependencytrack.model.FetchStatus; | ||
import org.dependencytrack.model.IntegrityMetaComponent; | ||
import org.dependencytrack.persistence.QueryManager; | ||
|
||
import java.time.Instant; | ||
import java.util.Date; | ||
|
||
public abstract class AbstractMetaHandler implements Handler { | ||
|
||
ComponentProjection componentProjection; | ||
QueryManager queryManager; | ||
KafkaEventDispatcher kafkaEventDispatcher; | ||
boolean fetchLatestVersion; | ||
|
||
|
||
public static IntegrityMetaComponent createIntegrityMetaComponent(String purl) { | ||
IntegrityMetaComponent integrityMetaComponent1 = new IntegrityMetaComponent(); | ||
integrityMetaComponent1.setStatus(FetchStatus.IN_PROGRESS); | ||
integrityMetaComponent1.setPurl(purl); | ||
integrityMetaComponent1.setLastFetch(Date.from(Instant.now())); | ||
return integrityMetaComponent1; | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/org/dependencytrack/event/kafka/componentmeta/ComponentProjection.java
This file contains 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,4 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
public record ComponentProjection(String purlCoordinates, Boolean internal, String purl) { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/org/dependencytrack/event/kafka/componentmeta/Handler.java
This file contains 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,7 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import org.dependencytrack.model.IntegrityMetaComponent; | ||
|
||
public interface Handler { | ||
IntegrityMetaComponent handle(); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/dependencytrack/event/kafka/componentmeta/HandlerFactory.java
This file contains 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 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import com.github.packageurl.MalformedPackageURLException; | ||
import com.github.packageurl.PackageURL; | ||
import org.dependencytrack.event.kafka.KafkaEventDispatcher; | ||
import org.dependencytrack.persistence.QueryManager; | ||
|
||
public class HandlerFactory { | ||
|
||
public static Handler createHandler(ComponentProjection componentProjection, QueryManager queryManager, KafkaEventDispatcher kafkaEventDispatcher, boolean fetchLatestVersion) throws MalformedPackageURLException { | ||
PackageURL packageURL = new PackageURL(componentProjection.purl()); | ||
boolean result = RepoMetaConstants.SUPPORTED_PACKAGE_URLS_FOR_INTEGRITY_CHECK.contains(packageURL.getType()); | ||
if (result) { | ||
return new SupportedMetaHandler(componentProjection, queryManager, kafkaEventDispatcher, fetchLatestVersion); | ||
} else { | ||
return new UnSupportedMetaHandler(componentProjection, queryManager, kafkaEventDispatcher, fetchLatestVersion); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/org/dependencytrack/event/kafka/componentmeta/RepoMetaConstants.java
This file contains 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,9 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import java.util.List; | ||
|
||
public class RepoMetaConstants { | ||
|
||
public static final long TIME_SPAN = 60 * 60 * 1000L; | ||
public static final List<String> SUPPORTED_PACKAGE_URLS_FOR_INTEGRITY_CHECK =List.of("maven", "npm", "pypi"); | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/dependencytrack/event/kafka/componentmeta/SupportedMetaHandler.java
This file contains 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,46 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent; | ||
import org.dependencytrack.event.kafka.KafkaEventDispatcher; | ||
import org.dependencytrack.model.FetchStatus; | ||
import org.dependencytrack.model.IntegrityMetaComponent; | ||
import org.dependencytrack.persistence.QueryManager; | ||
|
||
import java.time.Instant; | ||
import java.util.Date; | ||
|
||
import static org.dependencytrack.event.kafka.componentmeta.RepoMetaConstants.TIME_SPAN; | ||
|
||
public class SupportedMetaHandler extends AbstractMetaHandler { | ||
|
||
public SupportedMetaHandler(ComponentProjection componentProjection, QueryManager queryManager, KafkaEventDispatcher kafkaEventDispatcher, boolean fetchLatestVersion) { | ||
this.componentProjection = componentProjection; | ||
this.kafkaEventDispatcher = kafkaEventDispatcher; | ||
this.queryManager = queryManager; | ||
this.fetchLatestVersion = fetchLatestVersion; | ||
} | ||
|
||
@Override | ||
public IntegrityMetaComponent handle() { | ||
KafkaEventDispatcher kafkaEventDispatcher = new KafkaEventDispatcher(); | ||
try (QueryManager queryManager = new QueryManager()) { | ||
IntegrityMetaComponent integrityMetaComponent = queryManager.getIntegrityMetaComponent(componentProjection.purl()); | ||
if (integrityMetaComponent != null) { | ||
if (integrityMetaComponent.getStatus() == null || (integrityMetaComponent.getStatus() == FetchStatus.IN_PROGRESS && Date.from(Instant.now()).getTime() - integrityMetaComponent.getLastFetch().getTime() > TIME_SPAN)) { | ||
integrityMetaComponent.setLastFetch(Date.from(Instant.now())); | ||
IntegrityMetaComponent integrityMetaComponent1 = queryManager.updateIntegrityMetaComponent(integrityMetaComponent); | ||
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), true, fetchLatestVersion)); | ||
return integrityMetaComponent1; | ||
} else { | ||
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false, fetchLatestVersion)); | ||
return integrityMetaComponent; | ||
} | ||
} else { | ||
IntegrityMetaComponent integrityMetaComponent1 = queryManager.createIntegrityMetaComponent(createIntegrityMetaComponent(componentProjection.purl())); | ||
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), true, fetchLatestVersion)); | ||
return integrityMetaComponent1; | ||
} | ||
} | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/dependencytrack/event/kafka/componentmeta/UnSupportedMetaHandler.java
This file contains 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,23 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent; | ||
import org.dependencytrack.event.kafka.KafkaEventDispatcher; | ||
import org.dependencytrack.model.IntegrityMetaComponent; | ||
import org.dependencytrack.persistence.QueryManager; | ||
|
||
public class UnSupportedMetaHandler extends AbstractMetaHandler { | ||
|
||
public UnSupportedMetaHandler(ComponentProjection componentProjection, QueryManager queryManager, KafkaEventDispatcher kafkaEventDispatcher,boolean fetchLatestVersion) { | ||
this.componentProjection = componentProjection; | ||
this.kafkaEventDispatcher = kafkaEventDispatcher; | ||
this.queryManager = queryManager; | ||
this.fetchLatestVersion = fetchLatestVersion; | ||
} | ||
|
||
@Override | ||
public IntegrityMetaComponent handle() { | ||
KafkaEventDispatcher kafkaEventDispatcher = new KafkaEventDispatcher(); | ||
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false, fetchLatestVersion)); | ||
return null; | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package org.dependencytrack.model; | ||
|
||
public enum FetchStatus { | ||
//request processed successfully | ||
PROCESSED, | ||
TIMED_OUT, | ||
IN_PROGRESS | ||
//fetching information for this component is in progress | ||
IN_PROGRESS, | ||
|
||
//to be used when information is not available in source of truth so we don't go fetching this repo information again | ||
//after first attempt | ||
NOT_AVAILABLE | ||
} |
This file contains 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 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.