-
-
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.
fix integrity meta initializer (#391)
Signed-off-by: vithikashukla <[email protected]> Co-authored-by: vithikashukla <[email protected]>
- Loading branch information
Showing
20 changed files
with
329 additions
and
277 deletions.
There are no files selected for viewing
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
99 changes: 0 additions & 99 deletions
99
src/main/java/org/dependencytrack/event/IntegrityMetaInitializer.java
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
src/main/java/org/dependencytrack/event/IntegrityMetaInitializerEvent.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; | ||
|
||
import alpine.event.framework.Event; | ||
|
||
public class IntegrityMetaInitializerEvent 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.dependencytrack.event; | ||
|
||
import alpine.Config; | ||
import alpine.common.logging.Logger; | ||
import net.javacrumbs.shedlock.core.LockingTaskExecutor; | ||
import org.dependencytrack.common.ConfigKey; | ||
import org.dependencytrack.persistence.QueryManager; | ||
import org.dependencytrack.util.LockProvider; | ||
|
||
import javax.servlet.ServletContextEvent; | ||
import javax.servlet.ServletContextListener; | ||
|
||
import static org.dependencytrack.tasks.LockName.INTEGRITY_META_INITIALIZER_LOCK; | ||
|
||
public class PurlMigrator implements ServletContextListener { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(PurlMigrator.class); | ||
private final boolean integrityInitializerEnabled; | ||
|
||
public PurlMigrator() { | ||
this(Config.getInstance().getPropertyAsBoolean(ConfigKey.INTEGRITY_INITIALIZER_ENABLED)); | ||
} | ||
|
||
PurlMigrator(final boolean integrityInitializerEnabled) { | ||
this.integrityInitializerEnabled = integrityInitializerEnabled; | ||
} | ||
|
||
|
||
@Override | ||
public void contextInitialized(final ServletContextEvent event) { | ||
if (integrityInitializerEnabled) { | ||
try { | ||
LockProvider.executeWithLock(INTEGRITY_META_INITIALIZER_LOCK, (LockingTaskExecutor.Task) () -> process()); | ||
} catch (Throwable e) { | ||
throw new RuntimeException("An unexpected error occurred while running Initializer for integrity meta", e); | ||
} | ||
} else { | ||
LOGGER.info("Component integrity initializer is disabled."); | ||
} | ||
} | ||
|
||
private void process() { | ||
LOGGER.info("Initializing integrity meta component sync"); | ||
try (final var qm = new QueryManager()) { | ||
qm.synchronizeIntegrityMetaComponent(); | ||
} | ||
} | ||
} |
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
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
58 changes: 58 additions & 0 deletions
58
src/main/java/org/dependencytrack/tasks/IntegrityMetaInitializerTask.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,58 @@ | ||
package org.dependencytrack.tasks; | ||
|
||
import alpine.Config; | ||
import alpine.common.logging.Logger; | ||
import alpine.event.framework.Event; | ||
import alpine.event.framework.Subscriber; | ||
import org.dependencytrack.common.ConfigKey; | ||
import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent; | ||
import org.dependencytrack.event.IntegrityMetaInitializerEvent; | ||
import org.dependencytrack.event.kafka.KafkaEventDispatcher; | ||
import org.dependencytrack.model.IntegrityMetaComponent; | ||
import org.dependencytrack.persistence.QueryManager; | ||
|
||
import java.util.List; | ||
|
||
import static org.hyades.proto.repometaanalysis.v1.FetchMeta.FETCH_META_INTEGRITY_DATA; | ||
|
||
public class IntegrityMetaInitializerTask implements Subscriber { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(IntegrityMetaInitializerTask.class); | ||
|
||
private final KafkaEventDispatcher kafkaEventDispatcher = new KafkaEventDispatcher(); | ||
|
||
public void inform(final Event e) { | ||
if (e instanceof IntegrityMetaInitializerEvent) { | ||
if (!Config.getInstance().getPropertyAsBoolean(ConfigKey.INTEGRITY_INITIALIZER_ENABLED)) { | ||
LOGGER.debug("Integrity initializer is disabled"); | ||
return; | ||
} | ||
try (final var qm = new QueryManager()) { | ||
batchProcessPurls(qm); | ||
} | ||
} | ||
} | ||
|
||
private void batchProcessPurls(QueryManager qm) { | ||
long offset = 0; | ||
List<IntegrityMetaComponent> integrityMetaPurls = qm.fetchNextPurlsPage(offset); | ||
while (!integrityMetaPurls.isEmpty()) { | ||
dispatchPurls(qm, integrityMetaPurls); | ||
qm.batchUpdateIntegrityMetaComponent(integrityMetaPurls); | ||
offset += integrityMetaPurls.size(); | ||
integrityMetaPurls = qm.fetchNextPurlsPage(offset); | ||
} | ||
} | ||
|
||
private void dispatchPurls(QueryManager qm, List<IntegrityMetaComponent> integrityMetaPurls) { | ||
for (final var integrityMetaPurl : integrityMetaPurls) { | ||
IntegrityMetaInitializerTask.ComponentProjection componentProjection = qm.getComponentByPurl(integrityMetaPurl.getPurl()); | ||
LOGGER.debug("Dispatching purl for integrity metadata: " + integrityMetaPurl.getPurl()); | ||
//Initializer will not trigger Integrity Check on component so component uuid is not required | ||
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(null, integrityMetaPurl.getPurl(), componentProjection.internal(), FETCH_META_INTEGRITY_DATA)); | ||
} | ||
} | ||
|
||
public record ComponentProjection(String purlCoordinates, Boolean internal) { | ||
} | ||
} |
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.