-
-
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 (#339)
* added integrity analysis event on apiserver Signed-off-by: mehab <[email protected]> * fixed unit tests Signed-off-by: mehab <[email protected]> * fixed unit test for repometaanalysistask Signed-off-by: mehab <[email protected]> * fixed tests for bomupload Signed-off-by: mehab <[email protected]> * fixed tests for bomupload Signed-off-by: mehab <[email protected]> * PR review fixes Signed-off-by: mehab <[email protected]> * end to end test successful Signed-off-by: mehab <[email protected]> * fixed broken test Signed-off-by: mehab <[email protected]> --------- Signed-off-by: mehab <[email protected]>
- Loading branch information
Showing
26 changed files
with
607 additions
and
85 deletions.
There are no files selected for viewing
12 changes: 4 additions & 8 deletions
12
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,17 @@ | ||
package org.dependencytrack.event; | ||
|
||
import alpine.event.framework.Event; | ||
import com.github.packageurl.PackageURL; | ||
import org.dependencytrack.model.Component; | ||
|
||
import java.util.Optional; | ||
import org.hyades.proto.repometaanalysis.v1.FetchMeta; | ||
|
||
/** | ||
* 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 fetchMeta Whether component hash data or component meta data needs to be fetched from external api | ||
*/ | ||
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, | ||
FetchMeta fetchMeta) 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
28 changes: 28 additions & 0 deletions
28
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,28 @@ | ||
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 org.hyades.proto.repometaanalysis.v1.FetchMeta; | ||
|
||
import java.time.Instant; | ||
import java.util.Date; | ||
|
||
public abstract class AbstractMetaHandler implements Handler { | ||
|
||
ComponentProjection componentProjection; | ||
QueryManager queryManager; | ||
KafkaEventDispatcher kafkaEventDispatcher; | ||
FetchMeta fetchMeta; | ||
|
||
|
||
public static IntegrityMetaComponent createIntegrityMetaComponent(String purl) { | ||
IntegrityMetaComponent integrityMetaComponent = new IntegrityMetaComponent(); | ||
integrityMetaComponent.setStatus(FetchStatus.IN_PROGRESS); | ||
integrityMetaComponent.setPurl(purl); | ||
integrityMetaComponent.setLastFetch(Date.from(Instant.now())); | ||
return integrityMetaComponent; | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
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,6 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import com.github.packageurl.PackageURL; | ||
|
||
public record ComponentProjection(String purlCoordinates, Boolean internal, PackageURL purl) { | ||
} |
8 changes: 8 additions & 0 deletions
8
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,8 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import com.github.packageurl.MalformedPackageURLException; | ||
import org.dependencytrack.model.IntegrityMetaComponent; | ||
|
||
public interface Handler { | ||
IntegrityMetaComponent handle() throws MalformedPackageURLException; | ||
} |
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import com.github.packageurl.MalformedPackageURLException; | ||
import org.dependencytrack.event.kafka.KafkaEventDispatcher; | ||
import org.dependencytrack.persistence.QueryManager; | ||
import org.hyades.proto.repometaanalysis.v1.FetchMeta; | ||
|
||
public class HandlerFactory { | ||
|
||
public static Handler createHandler(ComponentProjection componentProjection, QueryManager queryManager, KafkaEventDispatcher kafkaEventDispatcher, FetchMeta fetchMeta) throws MalformedPackageURLException { | ||
boolean result = RepoMetaConstants.SUPPORTED_PACKAGE_URLS_FOR_INTEGRITY_CHECK.contains(componentProjection.purl().getType()); | ||
if (result) { | ||
return new SupportedMetaHandler(componentProjection, queryManager, kafkaEventDispatcher, fetchMeta); | ||
} else { | ||
return new UnSupportedMetaHandler(componentProjection, queryManager, kafkaEventDispatcher, FetchMeta.FETCH_META_LATEST_VERSION); | ||
} | ||
} | ||
} |
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"); | ||
} |
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import com.github.packageurl.MalformedPackageURLException; | ||
import com.github.packageurl.PackageURL; | ||
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 org.hyades.proto.repometaanalysis.v1.FetchMeta; | ||
|
||
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, FetchMeta fetchMeta) { | ||
this.componentProjection = componentProjection; | ||
this.kafkaEventDispatcher = kafkaEventDispatcher; | ||
this.queryManager = queryManager; | ||
this.fetchMeta = fetchMeta; | ||
} | ||
|
||
@Override | ||
public IntegrityMetaComponent handle() throws MalformedPackageURLException { | ||
IntegrityMetaComponent persistentIntegrityMetaComponent = queryManager.getIntegrityMetaComponent(componentProjection.purl().toString()); | ||
if (persistentIntegrityMetaComponent == null) { | ||
IntegrityMetaComponent integrityMetaComponent = queryManager.createIntegrityMetaComponent(createIntegrityMetaComponent(componentProjection.purl().toString())); | ||
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purl().canonicalize(), componentProjection.internal(), fetchMeta)); | ||
return integrityMetaComponent; | ||
} | ||
if (persistentIntegrityMetaComponent.getStatus() == null || (persistentIntegrityMetaComponent.getStatus() == FetchStatus.IN_PROGRESS && Date.from(Instant.now()).getTime() - persistentIntegrityMetaComponent.getLastFetch().getTime() > TIME_SPAN)) { | ||
persistentIntegrityMetaComponent.setLastFetch(Date.from(Instant.now())); | ||
IntegrityMetaComponent updateIntegrityMetaComponent = queryManager.updateIntegrityMetaComponent(persistentIntegrityMetaComponent); | ||
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purl().canonicalize(), componentProjection.internal(), fetchMeta)); | ||
return updateIntegrityMetaComponent; | ||
} else { | ||
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purl().canonicalize(), componentProjection.internal(), fetchMeta)); | ||
return persistentIntegrityMetaComponent; | ||
} | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import com.github.packageurl.MalformedPackageURLException; | ||
import com.github.packageurl.PackageURL; | ||
import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent; | ||
import org.dependencytrack.event.kafka.KafkaEventDispatcher; | ||
import org.dependencytrack.model.IntegrityMetaComponent; | ||
import org.dependencytrack.persistence.QueryManager; | ||
import org.hyades.proto.repometaanalysis.v1.FetchMeta; | ||
|
||
public class UnSupportedMetaHandler extends AbstractMetaHandler { | ||
|
||
public UnSupportedMetaHandler(ComponentProjection componentProjection, QueryManager queryManager, KafkaEventDispatcher kafkaEventDispatcher, FetchMeta fetchMeta) { | ||
this.componentProjection = componentProjection; | ||
this.kafkaEventDispatcher = kafkaEventDispatcher; | ||
this.queryManager = queryManager; | ||
this.fetchMeta = fetchMeta; | ||
} | ||
|
||
@Override | ||
public IntegrityMetaComponent handle() throws MalformedPackageURLException { | ||
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(new PackageURL(componentProjection.purlCoordinates()).canonicalize(), componentProjection.internal(), fetchMeta)); | ||
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
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,8 +1,9 @@ | ||
package org.dependencytrack.model; | ||
|
||
public enum FetchStatus { | ||
//request processed successfully | ||
PROCESSED, | ||
TIMED_OUT, | ||
//fetching information for this component is in progress | ||
IN_PROGRESS, | ||
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
Oops, something went wrong.