Skip to content

Commit

Permalink
flag to disable integrity initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
sahibamittal committed Sep 29, 2023
1 parent 1f312df commit 85c10db
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/dependencytrack/common/ConfigKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public enum ConfigKey implements Config.Key {
TMP_DELAY_BOM_PROCESSED_NOTIFICATION("tmp.delay.bom.processed.notification", "false"),
CEL_POLICY_ENGINE_ENABLED("cel.policy.engine.enabled", "false"),
TASK_INTEGRITY_META_INITIALIZER_LOCK_AT_MOST_FOR("task.integrity.integrityMetaInitializer.lockAtMostForInMillis", String.valueOf(Duration.ofMinutes(15).toMillis())),
TASK_INTEGRITY_META_INITIALIZER_LOCK_AT_LEAST_FOR("task.integrity.integrityMetaInitializer.lockAtLeastForInMillis", String.valueOf(Duration.ofMinutes(5).toMillis()));
TASK_INTEGRITY_META_INITIALIZER_LOCK_AT_LEAST_FOR("task.integrity.integrityMetaInitializer.lockAtLeastForInMillis", String.valueOf(Duration.ofMinutes(5).toMillis())),
INTEGRITY_INITIALIZER_ENABLED("integrity.initializer.enabled", "false");

private final String propertyName;
private final Object defaultValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.dependencytrack.event;

import alpine.Config;
import alpine.common.logging.Logger;
import net.javacrumbs.shedlock.core.LockConfiguration;
import net.javacrumbs.shedlock.core.LockExtender;
import net.javacrumbs.shedlock.core.LockingTaskExecutor;
import org.dependencytrack.common.ConfigKey;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.model.IntegrityMetaComponent;
import org.dependencytrack.persistence.QueryManager;
Expand All @@ -22,13 +24,27 @@ public class IntegrityMetaInitializer implements ServletContextListener {

private static final Logger LOGGER = Logger.getLogger(IntegrityMetaInitializer.class);
private final KafkaEventDispatcher kafkaEventDispatcher = new KafkaEventDispatcher();
private final boolean integrityInitializerEnabled;

public IntegrityMetaInitializer() {
this(Config.getInstance().getPropertyAsBoolean(ConfigKey.INTEGRITY_INITIALIZER_ENABLED));
}

IntegrityMetaInitializer(final boolean integrityInitializerEnabled) {
this.integrityInitializerEnabled = integrityInitializerEnabled;
}


@Override
public void contextInitialized(final ServletContextEvent event) {
try {
LockProvider.executeWithLock(INTEGRITY_META_INITIALIZER_TASK_LOCK, (LockingTaskExecutor.Task) () -> process());
} catch (Throwable e) {
throw new RuntimeException("An unexpected error occurred while running Initializer for integrity meta", e);
if (integrityInitializerEnabled) {
try {
LockProvider.executeWithLock(INTEGRITY_META_INITIALIZER_TASK_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.");
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,8 @@ tmp.delay.bom.processed.notification=false

# Optional
# Specifies whether the Common Expression Language (CEL) based policy engine shall be enabled.
cel.policy.engine.enabled=false
cel.policy.engine.enabled=false

# Optional
# Specifies whether the Integrity Initializer shall be enabled.
integrity.initializer.enabled=false
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ public void persistComponentData() {
}

@Test
public void testIntegrityMetaInitializer() {
public void testIntegrityMetaInitializerWhenDisabledByDefault() {
IntegrityMetaInitializer initializer = new IntegrityMetaInitializer();
initializer.contextInitialized(null);
assertThat(qm.getIntegrityMetaComponentCount()).isEqualTo(0);
assertThat(kafkaMockProducer.history().size()).isEqualTo(0);
}

@Test
public void testIntegrityMetaInitializer() {
IntegrityMetaInitializer initializer = new IntegrityMetaInitializer(true);
initializer.contextInitialized(null);
assertThat(qm.getIntegrityMetaComponentCount()).isEqualTo(1);
assertThat(kafkaMockProducer.history()).satisfiesExactly(
record -> assertThat(record.topic()).isEqualTo(KafkaTopics.REPO_META_ANALYSIS_COMMAND.name())
Expand All @@ -60,7 +68,7 @@ public void testIntegrityMetaInitializerWithExistingDataProcessed() {
integrityMetaExisting.setStatus(PROCESSED);
qm.persist(integrityMetaExisting);
// data exists in IntegrityMetaComponent so sync will be skipped
IntegrityMetaInitializer initializer = new IntegrityMetaInitializer();
IntegrityMetaInitializer initializer = new IntegrityMetaInitializer(true);
initializer.contextInitialized(null);
// kafka event is not dispatched
assertThat(kafkaMockProducer.history().size()).isEqualTo(0);
Expand All @@ -73,7 +81,7 @@ public void testIntegrityMetaInitializerWithExistingDataNotProcessed() {
var integrityMetaExisting = new IntegrityMetaComponent();
integrityMetaExisting.setPurl(componentPersisted.getPurl().toString());
qm.persist(integrityMetaExisting);
IntegrityMetaInitializer initializer = new IntegrityMetaInitializer();
IntegrityMetaInitializer initializer = new IntegrityMetaInitializer(true);
initializer.contextInitialized(null);
// kafka event is dispatched
assertThat(kafkaMockProducer.history().size()).isEqualTo(1);
Expand All @@ -88,7 +96,7 @@ public void testIntegrityMetaInitializerWithExistingDataFetchedNotRecently() {
integrityMetaExisting.setStatus(IN_PROGRESS);
integrityMetaExisting.setLastFetch(Date.from(Instant.now().minus(3, ChronoUnit.HOURS)));
qm.persist(integrityMetaExisting);
IntegrityMetaInitializer initializer = new IntegrityMetaInitializer();
IntegrityMetaInitializer initializer = new IntegrityMetaInitializer(true);
initializer.contextInitialized(null);
// kafka event is dispatched
assertThat(kafkaMockProducer.history().size()).isEqualTo(1);
Expand All @@ -104,7 +112,7 @@ public void testIntegrityMetaInitializerWithExistingDataFetchedRecently() {
integrityMetaExisting.setLastFetch(Date.from(Instant.now().minus(30, ChronoUnit.MINUTES)));
qm.persist(integrityMetaExisting);

IntegrityMetaInitializer initializer = new IntegrityMetaInitializer();
IntegrityMetaInitializer initializer = new IntegrityMetaInitializer(true);
initializer.contextInitialized(null);
// kafka event is dispatched
assertThat(kafkaMockProducer.history().size()).isEqualTo(0);
Expand Down

0 comments on commit 85c10db

Please sign in to comment.