|
15 | 15 | */ |
16 | 16 | package io.cryostat.rules; |
17 | 17 |
|
| 18 | +import java.io.BufferedInputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.file.Files; |
18 | 21 | import java.util.List; |
| 22 | +import java.util.Objects; |
19 | 23 |
|
| 24 | +import io.cryostat.ConfigProperties; |
20 | 25 | import io.cryostat.expressions.MatchExpression; |
21 | 26 | import io.cryostat.util.EntityExistsException; |
22 | 27 |
|
| 28 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | +import io.quarkus.runtime.StartupEvent; |
23 | 30 | import io.vertx.core.json.JsonObject; |
24 | 31 | import io.vertx.mutiny.core.eventbus.EventBus; |
25 | 32 | import jakarta.annotation.security.RolesAllowed; |
| 33 | +import jakarta.enterprise.event.Observes; |
26 | 34 | import jakarta.inject.Inject; |
27 | 35 | import jakarta.transaction.Transactional; |
28 | 36 | import jakarta.ws.rs.BadRequestException; |
|
36 | 44 | import jakarta.ws.rs.core.Context; |
37 | 45 | import jakarta.ws.rs.core.MediaType; |
38 | 46 | import jakarta.ws.rs.core.UriInfo; |
| 47 | +import org.eclipse.microprofile.config.inject.ConfigProperty; |
| 48 | +import org.jboss.logging.Logger; |
39 | 49 | import org.jboss.resteasy.reactive.RestForm; |
40 | 50 | import org.jboss.resteasy.reactive.RestPath; |
41 | 51 | import org.jboss.resteasy.reactive.RestQuery; |
|
45 | 55 | @Path("/api/v4/rules") |
46 | 56 | public class Rules { |
47 | 57 |
|
| 58 | + @ConfigProperty(name = ConfigProperties.RULES_DIR) |
| 59 | + java.nio.file.Path dir; |
| 60 | + |
| 61 | + @Inject Logger logger; |
48 | 62 | @Inject EventBus bus; |
| 63 | + @Inject ObjectMapper mapper; |
| 64 | + |
| 65 | + @Transactional |
| 66 | + void onStart(@Observes StartupEvent evt) { |
| 67 | + if (!checkDir()) { |
| 68 | + return; |
| 69 | + } |
| 70 | + try { |
| 71 | + Files.walk(dir) |
| 72 | + .filter(Files::isRegularFile) |
| 73 | + .filter(Files::isReadable) |
| 74 | + .peek( |
| 75 | + p -> |
| 76 | + logger.tracev( |
| 77 | + "Processing declarative Automated Rule definition at" |
| 78 | + + " {}", |
| 79 | + p)) |
| 80 | + .forEach(this::processDeclarativeRule); |
| 81 | + } catch (IOException e) { |
| 82 | + logger.error(e); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void processDeclarativeRule(java.nio.file.Path path) { |
| 87 | + try (var is = new BufferedInputStream(Files.newInputStream(path))) { |
| 88 | + var declarativeRule = mapper.readValue(is, Rule.class); |
| 89 | + logger.tracev( |
| 90 | + "Processing eclarative Automated" + " Rule with name \"{}\"", |
| 91 | + declarativeRule.name); |
| 92 | + var exists = Rule.find("name", declarativeRule.name).count() != 0; |
| 93 | + if (exists) { |
| 94 | + var existingRule = Rule.<Rule>find("name", declarativeRule.name).singleResult(); |
| 95 | + // remove for equality check. The declarative rule is not expected to have a |
| 96 | + // database ID yet existingRule.id = null; |
| 97 | + if (Objects.equals(declarativeRule, existingRule)) { |
| 98 | + return; |
| 99 | + } |
| 100 | + logger.debugv( |
| 101 | + "Rule with name \"{}\" already exists in database. Replacing with" |
| 102 | + + " declarative rule at {}. Previous definition:\n" |
| 103 | + + "{}", |
| 104 | + declarativeRule.name, |
| 105 | + path, |
| 106 | + mapper.writeValueAsString(existingRule)); |
| 107 | + existingRule.delete(); |
| 108 | + } |
| 109 | + declarativeRule.persist(); |
| 110 | + } catch (IOException ioe) { |
| 111 | + logger.warn(ioe); |
| 112 | + } catch (Exception e) { |
| 113 | + logger.error(e); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private boolean checkDir() { |
| 118 | + return Files.exists(dir) |
| 119 | + && Files.isReadable(dir) |
| 120 | + && Files.isExecutable(dir) |
| 121 | + && Files.isDirectory(dir); |
| 122 | + } |
49 | 123 |
|
50 | 124 | @GET |
51 | 125 | @RolesAllowed("read") |
|
0 commit comments