From 702a425d908dbe4c003f84e4f872f92a725ca352 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Thu, 19 Dec 2024 14:46:42 -0500 Subject: [PATCH] feat(rules): implement declarative Automated Rules --- src/main/docker/Dockerfile.jvm | 1 + .../docker/include/rule_presets/quarkus.json | 7 ++ .../java/io/cryostat/ConfigProperties.java | 1 + src/main/java/io/cryostat/rules/Rules.java | 74 +++++++++++++++++++ src/main/resources/application.properties | 1 + 5 files changed, 84 insertions(+) create mode 100644 src/main/docker/include/rule_presets/quarkus.json diff --git a/src/main/docker/Dockerfile.jvm b/src/main/docker/Dockerfile.jvm index 1e2763863..2d644355e 100644 --- a/src/main/docker/Dockerfile.jvm +++ b/src/main/docker/Dockerfile.jvm @@ -93,6 +93,7 @@ ENTRYPOINT [ "/deployments/app/entrypoint.bash", "/opt/jboss/container/java/run/ # We make distinct layers so if there are application changes the library layers can be re-used COPY --chown=185 src/main/docker/include/cryostat.jfc /usr/lib/jvm/jre/lib/jfr/ COPY --chown=185 src/main/docker/include/template_presets/* /opt/cryostat.d/presets.d/ +COPY --chown=185 src/main/docker/include/rule_presets/* /opt/cryostat.d/rules.d/ COPY --chown=185 src/main/docker/include/genpass.bash /deployments/app/ COPY --chown=185 src/main/docker/include/entrypoint.bash /deployments/app/ COPY --chown=185 src/main/docker/include/truststore-setup.bash /deployments/app/ diff --git a/src/main/docker/include/rule_presets/quarkus.json b/src/main/docker/include/rule_presets/quarkus.json new file mode 100644 index 000000000..7716a5545 --- /dev/null +++ b/src/main/docker/include/rule_presets/quarkus.json @@ -0,0 +1,7 @@ +{ + "name": "quarkus", + "description": "Preset Automated Rule for enabling Quarkus framework-specific events when available", + "eventSpecifier": "template=Quarkus,type=PRESET", + "matchExpression": "jfrEventTypeIds(target).exists(x, x.contains(\"quarkus\"))", + "enabled": false +} diff --git a/src/main/java/io/cryostat/ConfigProperties.java b/src/main/java/io/cryostat/ConfigProperties.java index af0cf153a..d1c89f778 100644 --- a/src/main/java/io/cryostat/ConfigProperties.java +++ b/src/main/java/io/cryostat/ConfigProperties.java @@ -57,6 +57,7 @@ public class ConfigProperties { public static final String CUSTOM_TEMPLATES_DIR = "templates-dir"; public static final String PRESET_TEMPLATES_DIR = "preset-templates-dir"; public static final String SSL_TRUSTSTORE_DIR = "ssl.truststore.dir"; + public static final String RULES_DIR = "rules-dir"; public static final String URI_RANGE = "cryostat.target.uri-range"; } diff --git a/src/main/java/io/cryostat/rules/Rules.java b/src/main/java/io/cryostat/rules/Rules.java index 89ffc5768..badae3318 100644 --- a/src/main/java/io/cryostat/rules/Rules.java +++ b/src/main/java/io/cryostat/rules/Rules.java @@ -15,14 +15,22 @@ */ package io.cryostat.rules; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.nio.file.Files; import java.util.List; +import java.util.Objects; +import io.cryostat.ConfigProperties; import io.cryostat.expressions.MatchExpression; import io.cryostat.util.EntityExistsException; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.quarkus.runtime.StartupEvent; import io.vertx.core.json.JsonObject; import io.vertx.mutiny.core.eventbus.EventBus; import jakarta.annotation.security.RolesAllowed; +import jakarta.enterprise.event.Observes; import jakarta.inject.Inject; import jakarta.transaction.Transactional; import jakarta.ws.rs.BadRequestException; @@ -36,6 +44,8 @@ import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.UriInfo; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.jboss.logging.Logger; import org.jboss.resteasy.reactive.RestForm; import org.jboss.resteasy.reactive.RestPath; import org.jboss.resteasy.reactive.RestQuery; @@ -45,7 +55,71 @@ @Path("/api/v4/rules") public class Rules { + @ConfigProperty(name = ConfigProperties.RULES_DIR) + java.nio.file.Path dir; + + @Inject Logger logger; @Inject EventBus bus; + @Inject ObjectMapper mapper; + + @Transactional + void onStart(@Observes StartupEvent evt) { + if (!checkDir()) { + return; + } + try { + Files.walk(dir) + .filter(Files::isRegularFile) + .filter(Files::isReadable) + .peek( + p -> + logger.tracev( + "Processing declarative Automated Rule definition at" + + " {}", + p)) + .forEach(this::processDeclarativeRule); + } catch (IOException e) { + logger.error(e); + } + } + + private void processDeclarativeRule(java.nio.file.Path path) { + try (var is = new BufferedInputStream(Files.newInputStream(path))) { + var declarativeRule = mapper.readValue(is, Rule.class); + logger.tracev( + "Processing eclarative Automated" + " Rule with name \"{}\"", + declarativeRule.name); + var exists = Rule.find("name", declarativeRule.name).count() != 0; + if (exists) { + var existingRule = Rule.find("name", declarativeRule.name).singleResult(); + // remove for equality check. The declarative rule is not expected to have a + // database ID yet existingRule.id = null; + if (Objects.equals(declarativeRule, existingRule)) { + return; + } + logger.debugv( + "Rule with name \"{}\" already exists in database. Replacing with" + + " declarative rule at {}. Previous definition:\n" + + "{}", + declarativeRule.name, + path, + mapper.writeValueAsString(existingRule)); + existingRule.delete(); + } + declarativeRule.persist(); + } catch (IOException ioe) { + logger.warn(ioe); + } catch (Exception e) { + logger.error(e); + } + } + + private boolean checkDir() { + return Files.exists(dir) + && Files.isReadable(dir) + && Files.isExecutable(dir) + && Files.isDirectory(dir); + } @GET @RolesAllowed("read") diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 0ae9da059..c55a34f33 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -47,6 +47,7 @@ cryostat.http.proxy.path=/ cryostat.target.uri-range=PUBLIC conf-dir=/opt/cryostat.d +rules-dir=${conf-dir}/rules.d templates-dir=${conf-dir}/templates.d preset-templates-dir=${conf-dir}/presets.d ssl.truststore=${conf-dir}/truststore.p12