diff --git a/src/main/docker/Dockerfile.jvm b/src/main/docker/Dockerfile.jvm index 84d622370..7085d1300 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..f3817a61e --- /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.startsWith(\"quarkus\"))", + "enabled": false +} diff --git a/src/main/java/io/cryostat/ConfigProperties.java b/src/main/java/io/cryostat/ConfigProperties.java index 55c489849..81342e5d0 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..427188913 100644 --- a/src/main/java/io/cryostat/rules/Rules.java +++ b/src/main/java/io/cryostat/rules/Rules.java @@ -15,14 +15,21 @@ */ package io.cryostat.rules; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.nio.file.Files; import java.util.List; +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 +43,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 +54,58 @@ @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) + .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 declarative Automated Rule with name \"{}\" at {}", + declarativeRule.name, + path); + var exists = Rule.find("name", declarativeRule.name).count() != 0; + if (exists) { + logger.tracev( + "Rule with name \"{}\" already exists in database. Skipping declarative" + + " rule at {}", + declarativeRule.name, + path); + return; + } + 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 7d1d44e28..5a2910330 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -49,6 +49,7 @@ cryostat.target.uri-range=PUBLIC cryostat.agent.tls.required=true 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 diff --git a/src/test/java/itest/PresetRulesIT.java b/src/test/java/itest/PresetRulesIT.java new file mode 100644 index 000000000..86b84ac9f --- /dev/null +++ b/src/test/java/itest/PresetRulesIT.java @@ -0,0 +1,77 @@ +/* + * Copyright The Cryostat Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package itest; + +import java.io.File; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.quarkus.test.junit.QuarkusIntegrationTest; +import io.vertx.core.buffer.Buffer; +import io.vertx.core.json.JsonArray; +import io.vertx.ext.web.client.HttpRequest; +import itest.bases.StandardSelfTest; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +@QuarkusIntegrationTest +public class PresetRulesIT extends StandardSelfTest { + + @Test + public void shouldListPresetRules() throws Exception { + CompletableFuture future = new CompletableFuture<>(); + HttpRequest req = webClient.get("/api/v4/rules"); + req.send( + ar -> { + if (ar.failed()) { + future.completeExceptionally(ar.cause()); + return; + } + future.complete(ar.result().bodyAsJsonArray()); + }); + JsonArray response = future.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS); + MatcherAssert.assertThat(response.size(), Matchers.equalTo(1)); + } + + @Test + public void shouldHavePresetQuarkusRule() throws Exception { + String url = "/api/v4/rules/quarkus"; + File file = + downloadFile(url, "quarkus", ".json") + .get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS) + .toFile(); + + ObjectMapper mapper = new ObjectMapper(); + JsonNode json = mapper.readTree(file); + + MatcherAssert.assertThat(json.get("name").asText(), Matchers.equalTo("quarkus")); + MatcherAssert.assertThat( + json.get("description").asText(), + Matchers.equalTo( + "Preset Automated Rule for enabling Quarkus framework-specific events when" + + " available")); + MatcherAssert.assertThat( + json.get("eventSpecifier").asText(), + Matchers.equalTo("template=Quarkus,type=PRESET")); + MatcherAssert.assertThat( + json.get("matchExpression").asText(), + Matchers.equalTo("jfrEventTypeIds(target).exists(x, x.startsWith(\"quarkus\"))")); + MatcherAssert.assertThat(json.get("enabled").asBoolean(), Matchers.is(false)); + } +} diff --git a/src/test/java/itest/PresetTemplatesIT.java b/src/test/java/itest/PresetTemplatesIT.java index 12b69f65e..8e0a62bfe 100644 --- a/src/test/java/itest/PresetTemplatesIT.java +++ b/src/test/java/itest/PresetTemplatesIT.java @@ -55,8 +55,7 @@ public void shouldListPresetTemplates() throws Exception { @Test public void shouldHavePresetQuarkusTemplate() throws Exception { - String url = - String.format("/api/v4/event_templates/PRESET/Quarkus", getSelfReferenceTargetId()); + String url = "/api/v4/event_templates/PRESET/Quarkus"; File file = downloadFile(url, "quarkus", ".jfc") .get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS)