100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Add flag to disable groovy rules compilation/execution (#3001)
Eric Bariaux654f9b55d0c6
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.javaindex 7fe90e3fb..5a3c4fee1 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java@@ -262,7 +262,7 @@ public class RulesEngine<T extends Ruleset> {removeRuleset(deployment.ruleset);}- deployment = new RulesetDeployment(ruleset, this, timerService, assetStorageService, executorService, scheduledExecutorService, assetsFacade, usersFacade, notificationFacade, webhooksFacade, alarmsFacade, historicFacade, predictedFacade);+ deployment = new RulesetDeployment(ruleset, this, timerService, assetStorageService, executorService, scheduledExecutorService, assetsFacade, usersFacade, notificationFacade, webhooksFacade, alarmsFacade, historicFacade, predictedFacade, rulesService.isGroovyRulesExecutionEnabled());deployment.init();deployments.put(ruleset.getId(), deployment);publishRulesetStatus(deployment);diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesService.java b/manager/src/main/java/org/openremote/manager/rules/RulesService.javaindex fba1c03d2..9b3bced71 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesService.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesService.java@@ -72,6 +72,7 @@ import static java.util.logging.Level.FINEST;import static java.util.logging.Level.SEVERE;import static org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC;import static org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType;+import static org.openremote.model.util.MapAccess.getBoolean;import static org.openremote.model.util.MapAccess.getInteger;import static org.openremote.manager.gateway.GatewayService.isNotForGateway;@@ -106,6 +107,8 @@ public class RulesService extends RouteBuilder implements ContainerService {public static final int OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS_DEFAULT = 50000; // Just under a minute to catch 1 min timer rulespublic static final String OR_RULES_QUICK_FIRE_MILLIS = "OR_RULES_QUICK_FIRE_MILLIS";public static final int OR_RULES_QUICK_FIRE_MILLIS_DEFAULT = 3000;+ public static final String OR_RULES_GROOVY_EXECUTION_ENABLED = "OR_RULES_GROOVY_EXECUTION_ENABLED";+ public static final boolean OR_RULES_GROOVY_EXECUTION_ENABLED_DEFAULT = true;private static final Logger LOG = Logger.getLogger(RulesService.class.getName());protected final AtomicReference<RulesEngine<GlobalRuleset>> globalEngine = new AtomicReference<>();protected final Map<String, RulesEngine<RealmRuleset>> realmEngines = new ConcurrentHashMap<>();@@ -138,6 +141,7 @@ public class RulesService extends RouteBuilder implements ContainerService {protected final Map<AttributeRef, AttributeEvent> preInitAttributeEvents = new LinkedHashMap<>();protected long tempFactExpirationMillis;protected long quickFireMillis;+ protected boolean groovyRulesExecutionEnabled;protected boolean initDone;protected boolean startDone;protected io.micrometer.core.instrument.Timer rulesFiringTimer;@@ -167,6 +171,7 @@ public class RulesService extends RouteBuilder implements ContainerService {tempFactExpirationMillis = getInteger(container.getConfig(), OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS, OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS_DEFAULT);quickFireMillis = getInteger(container.getConfig(), OR_RULES_QUICK_FIRE_MILLIS, OR_RULES_QUICK_FIRE_MILLIS_DEFAULT);+ groovyRulesExecutionEnabled = getBoolean(container.getConfig(), OR_RULES_GROOVY_EXECUTION_ENABLED, OR_RULES_GROOVY_EXECUTION_ENABLED_DEFAULT);if (initDone) {return;@@ -385,6 +390,10 @@ public class RulesService extends RouteBuilder implements ContainerService {.orElse(metaHolder.getMeta().has(MetaItemType.AGENT_LINK));}+ public boolean isGroovyRulesExecutionEnabled() {+ return groovyRulesExecutionEnabled;+ }+/*** React to events that have been committed to the DB and inject them into the appropriate {@link RulesEngine}s.*/diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java b/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.javaindex df306aec6..92d0c3381 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java@@ -110,6 +110,7 @@ public class RulesetDeployment {final protected Alarms alarmsFacade;final protected HistoricDatapoints historicDatapointsFacade;final protected PredictedDatapoints predictedDatapointsFacade;+ final protected boolean groovyRulesExecutionEnabled;final protected List<ScheduledFuture<?>> scheduledRuleActions = Collections.synchronizedList(new ArrayList<>());final protected RulesEngine<?> rulesEngine;protected final Logger LOG;@@ -124,7 +125,8 @@ public class RulesetDeployment {public RulesetDeployment(Ruleset ruleset, RulesEngine<?> rulesEngine, TimerService timerService,AssetStorageService assetStorageService, ExecutorService executorService, ScheduledExecutorService scheduledExecutorService,Assets assetsFacade, Users usersFacade, Notifications notificationsFacade, Webhooks webhooksFacade,- Alarms alarmsFacade, HistoricDatapoints historicDatapointsFacade, PredictedDatapoints predictedDatapointsFacade) {+ Alarms alarmsFacade, HistoricDatapoints historicDatapointsFacade, PredictedDatapoints predictedDatapointsFacade,+ boolean groovyRulesExecutionEnabled) {this.ruleset = ruleset;this.rulesEngine = rulesEngine;this.timerService = timerService;@@ -138,6 +140,7 @@ public class RulesetDeployment {this.alarmsFacade = alarmsFacade;this.historicDatapointsFacade = historicDatapointsFacade;this.predictedDatapointsFacade = predictedDatapointsFacade;+ this.groovyRulesExecutionEnabled = groovyRulesExecutionEnabled;String ruleCategory = ruleset.getClass().getSimpleName() + "-" + ruleset.getId();LOG = SyslogCategory.getLogger(SyslogCategory.RULES, RulesEngine.class.getName() + "." + ruleCategory);@@ -165,6 +168,12 @@ public class RulesetDeployment {status = DISABLED;}+ if (ruleset.getLang() == Ruleset.Lang.GROOVY && !groovyRulesExecutionEnabled) {+ LOG.info("Groovy ruleset execution is disabled; skipping compilation for ruleset: " + ruleset.getName());+ status = DISABLED;+ return;+ }+if (!compile()) {LOG.log(Level.SEVERE, "Ruleset compilation error: " + ruleset.getName(), getError());status = COMPILATION_ERROR;diff --git a/profile/deploy.yml b/profile/deploy.ymlindex a32be0380..9c1559ee1 100644--- a/profile/deploy.yml+++ b/profile/deploy.yml@@ -303,6 +303,10 @@ services:OR_EXECUTOR_THREADS_MIN:OR_EXECUTOR_THREADS_MAX:+ # Enable/disable execution of Groovy rulesets; when disabled they can still be created, edited and deleted+ # but will not be compiled or executed by the manager.+ OR_RULES_GROOVY_EXECUTION_ENABLED: ${OR_RULES_GROOVY_EXECUTION_ENABLED:-true}+# The minimum number of threads that will be used to process incoming auto provisioning messagesOR_AUTO_PROVISIONING_THREADS_MIN:# The maximum number of threads that will be used to process incoming auto provisioning messagesdiff --git a/test/src/test/groovy/org/openremote/test/rules/GroovyRulesRuntimeTest.groovy b/test/src/test/groovy/org/openremote/test/rules/GroovyRulesRuntimeTest.groovyindex f8dc24830..3ac2f66ac 100644--- a/test/src/test/groovy/org/openremote/test/rules/GroovyRulesRuntimeTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/GroovyRulesRuntimeTest.groovy@@ -12,9 +12,12 @@ import spock.util.concurrent.PollingConditionsimport static org.openremote.manager.rules.RulesService.OR_RULES_QUICK_FIRE_MILLISimport static org.openremote.model.rules.Ruleset.Lang.GROOVYimport static org.openremote.model.rules.RulesetStatus.DEPLOYED+import static org.openremote.model.rules.RulesetStatus.DISABLEDclass GroovyRulesRuntimeTest extends Specification implements ManagerContainerTrait {+ private static final String OR_RULES_GROOVY_EXECUTION_ENABLED = "OR_RULES_GROOVY_EXECUTION_ENABLED"+private static final String MODULO_RULE = '''package demo.rules@@ -66,4 +69,41 @@ class GroovyRulesRuntimeTest extends Specification implements ManagerContainerTrengine.facts.get("Modulo operator") == "fired"engine.isRunning()}++ @SuppressWarnings("GroovyAccessibility")+ def "Groovy rulesets are not compiled or executed when Groovy execution is disabled"() {+ given: "the container is started with Groovy ruleset execution disabled"+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)+ def config = defaultConfig()+ config[OR_RULES_GROOVY_EXECUTION_ENABLED] = "false"+ def container = startContainer(config, defaultServices())+ def rulesService = container.getService(RulesService.class)+ def rulesetStorageService = container.getService(RulesetStorageService.class)+ RulesEngine engine = null++ and: "a Groovy ruleset which would fail if the manager attempted to compile it"+ def ruleset = rulesetStorageService.merge(new RealmRuleset(+ Constants.MASTER_REALM,+ "Disabled Groovy ruleset",+ GROOVY,+ "this is not valid Groovy"))++ expect: "the ruleset is tracked but marked disabled instead of compiled"+ conditions.eventually {+ engine = rulesService.realmEngines.get(Constants.MASTER_REALM)+ assert engine != null+ assert engine.isRunning()+ assert engine.deployments[ruleset.id].status == DISABLED+ assert engine.deployments[ruleset.id].getError() == null+ }++ when: "the rules engine is evaluated"+ engine.fireAllDeployments()++ then: "the Groovy ruleset is still not executed and the engine remains healthy"+ def deployment = engine.deployments[ruleset.id]+ deployment.status == DISABLED+ deployment.getError() == null+ engine.isRunning()+ }}diff --git a/ui/app/shared/locales/en/or.json b/ui/app/shared/locales/en/or.jsonindex 62ae5493f..26bd0ce1f 100644--- a/ui/app/shared/locales/en/or.json+++ b/ui/app/shared/locales/en/or.json@@ -831,6 +831,7 @@"rulesetStatus": {"DEPLOYED": "Successfully deployed","NOSTATUS": "Disabled",+ "DISABLED": "Disabled","LOOP_ERROR": "Loop error","COMPILATION_ERROR": "Compilation error","EXECUTION_ERROR": "Execution error",diff --git a/ui/app/shared/locales/nl/or.json b/ui/app/shared/locales/nl/or.jsonindex b8d9d7bca..18538d677 100644--- a/ui/app/shared/locales/nl/or.json+++ b/ui/app/shared/locales/nl/or.json@@ -823,6 +823,7 @@"rulesetStatus": {"DEPLOYED": "Succesvol aangemaakt","NOSTATUS": "Uitgeschakeld",+ "DISABLED": "Uitgeschakeld","LOOP_ERROR": "Loop fout","COMPILATION_ERROR": "Compilatiefout","EXECUTION_ERROR": "Uitvoeringsfout",diff --git a/ui/app/shared/locales/pl/or.json b/ui/app/shared/locales/pl/or.jsonindex f239b2df8..2708cdbad 100644--- a/ui/app/shared/locales/pl/or.json+++ b/ui/app/shared/locales/pl/or.json@@ -1108,6 +1108,7 @@"rulesetStatus": {"DEPLOYED": "Wdrożono pomyślnie","NOSTATUS": "Wyłączony",+ "DISABLED": "Wyłączony","LOOP_ERROR": "Błąd pętli","COMPILATION_ERROR": "Błąd kompilacji","EXECUTION_ERROR": "Błąd wykonania",diff --git a/ui/app/shared/locales/ro/or.json b/ui/app/shared/locales/ro/or.jsonindex 8df4e8125..68e31e4ec 100644--- a/ui/app/shared/locales/ro/or.json+++ b/ui/app/shared/locales/ro/or.json@@ -579,6 +579,7 @@"rulesetStatus": {"DEPLOYED": "Instalat cu succes","NOSTATUS": "Dezactivat",+ "DISABLED": "Dezactivat","LOOP_ERROR": "Eroare de buclă","COMPILATION_ERROR": "Eroare de compilare","EXECUTION_ERROR": "Eroare de execuție",diff --git a/ui/app/shared/locales/uk/or.json b/ui/app/shared/locales/uk/or.jsonindex b3c5098b8..17c36ccd2 100644--- a/ui/app/shared/locales/uk/or.json+++ b/ui/app/shared/locales/uk/or.json@@ -594,6 +594,7 @@"rulesetStatus": {"DEPLOYED": "Успішно розгорнуто","NOSTATUS": "Вимкнено",+ "DISABLED": "Вимкнено","LOOP_ERROR": "Помилка в циклі","COMPILATION_ERROR": "Помилка компіляції","EXECUTION_ERROR": "Помилка виконання",