From 013871c6dc04df3c9165ab2056866d3a32140333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Malka=20Lacombe?= Date: Tue, 10 Nov 2026 16:16:37 +0100 Subject: [PATCH 1/3] feat(W77): feature flags --- doc/distributed.md | 24 +++++- pom.xml | 6 ++ src/main/docker/Dockerfile.jvm | 2 +- .../java/tech/illuin/wombat/WombatMain.java | 39 +++++++++ .../commons/NodeModeConfigSourceFactory.java | 3 - .../illuin/wombat/endpoint/AppController.java | 3 + .../endpoint/EnvironmentController.java | 3 + .../wombat/endpoint/ImpactController.java | 3 + .../wombat/endpoint/MetricController.java | 10 +-- .../wombat/endpoint/ModuleController.java | 3 + .../wombat/endpoint/SimulationController.java | 3 + .../wombat/endpoint/WorkerController.java | 9 +- .../endpoint/access/ApiKeyController.java | 3 + .../endpoint/access/UserController.java | 3 + .../endpoint/access/UserRoleController.java | 3 + .../engine/secret/SecretController.java | 3 + .../database/DatabaseSecretController.java | 3 + .../tech/illuin/wombat/feature/Feature.java | 33 +++++++ .../illuin/wombat/feature/FeatureFlags.java | 43 ++++++++++ .../wombat/feature/FeatureRequestFilter.java | 43 ++++++++++ .../wombat/feature/NodeModeFlagProvider.java | 41 +++++++++ .../wombat/feature/RequiresFeature.java | 13 +++ .../wombat/feature/UiFeatureFilter.java | 49 +++++++++++ .../tech/illuin/wombat/WombatMainTest.java | 50 +++++++++++ .../illuin/wombat/feature/FeatureTest.java | 38 ++++++++ .../wombat/feature/WorkerFeaturesTest.java | 86 +++++++++++++++++++ 26 files changed, 503 insertions(+), 16 deletions(-) create mode 100644 src/main/java/tech/illuin/wombat/WombatMain.java create mode 100644 src/main/java/tech/illuin/wombat/feature/Feature.java create mode 100644 src/main/java/tech/illuin/wombat/feature/FeatureFlags.java create mode 100644 src/main/java/tech/illuin/wombat/feature/FeatureRequestFilter.java create mode 100644 src/main/java/tech/illuin/wombat/feature/NodeModeFlagProvider.java create mode 100644 src/main/java/tech/illuin/wombat/feature/RequiresFeature.java create mode 100644 src/main/java/tech/illuin/wombat/feature/UiFeatureFilter.java create mode 100644 src/test/java/tech/illuin/wombat/WombatMainTest.java create mode 100644 src/test/java/tech/illuin/wombat/feature/FeatureTest.java create mode 100644 src/test/java/tech/illuin/wombat/feature/WorkerFeaturesTest.java diff --git a/doc/distributed.md b/doc/distributed.md index c8a1522..2891d5e 100644 --- a/doc/distributed.md +++ b/doc/distributed.md @@ -20,7 +20,9 @@ flowchart LR AGGREGATOR ==> DB ``` -Every instance runs the same image, in one of three modes set by `wombat.mode` (`WOMBAT_MODE`): +Every instance runs the same image, in one of three modes set by `wombat.mode` (`WOMBAT_MODE`), or by the `--mode` +argument, which takes precedence (e.g. `java -jar quarkus-run.jar --mode worker`, or `command: ["--mode", "worker"]` +in a compose file): | Mode | Samples its environments | Writes metrics to | Accepts `POST /api/workers/{worker}/metrics` | |------------------------|--------------------------|---------------------|-----------------------------| @@ -28,7 +30,23 @@ Every instance runs the same image, in one of three modes set by `wombat.mode` ( | `aggregator` | | its database | ✓ | | `worker` | ✓ | the aggregator | | -Outside of the aggregator mode, `POST /api/workers/{worker}/metrics` and `PUT /api/workers/{worker}/environments` answer `404`. +### Features + +The mode also sets which functionalities an instance serves: + +| Feature | Serves | `standalone` | `aggregator` | `worker` | +|-------------|--------------------------------------------------------------------------------|--------------|--------------|----------| +| `ui` | the web UI and its dashboards, `GET /api/app` | ✓ | ✓ | | +| `api-read` | `/api/impact`, `/api/simulation`, `/api/environments`, `/api/modules` | ✓ | ✓ | | +| `api-write` | `POST /api/workers/{worker}/metrics`, `PUT /api/workers/{worker}/environments` | | ✓ | | +| `admin` | `/api/users`, `/api/keys`, `/api/secrets` | ✓ | ✓ | | + +`api-write` only covers what workers push, so only an aggregator serves it. The endpoints managing an instance (users, +API keys, secrets) write too, but fall under `admin`, so that a standalone instance can still be administered. + +A disabled feature answers `404`, and `/q/*` is never gated: by default, a worker serves nothing else, so it needs no +authentication. The features are served as [quarkus-flags](https://docs.quarkiverse.io/quarkus-flags/dev) flags +named after them, resolved once at startup and logged. ## Aggregator @@ -153,7 +171,7 @@ holding `api.metric.write` and `api.environment.write` on its **API keys** page, For development purposes, you can use this sample distributed configuration to your advantage by running the aggregator as a dev instance, and launching workers as configured in the docker compose demo: ```sh -./mvnw quarkus:dev -Dwombat.mode=aggregator -Dmonitor.environments-file=compose/demo-distributed/aggregator/monitored-environments.yaml ; +./mvnw quarkus:dev -Dquarkus.args="--mode aggregator" -Dmonitor.environments-file=compose/demo-distributed/aggregator/monitored-environments.yaml ; WOMBAT_AGGREGATOR_URL=http://host.docker.internal:8080 docker compose -f compose/demo-distributed/compose.yaml up --no-deps worker-east worker-west ; ``` diff --git a/pom.xml b/pom.xml index 5cbaebf..ee03edd 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,7 @@ 14.4.0 checkstyle.xml 1.21.4 + 1.0.1 @@ -123,6 +124,11 @@ io.quarkus quarkus-kubernetes-client + + io.quarkiverse.flags + quarkus-flags + ${quarkus-flags.version} + io.quarkus quarkus-scheduler diff --git a/src/main/docker/Dockerfile.jvm b/src/main/docker/Dockerfile.jvm index 988b89b..b0b8694 100644 --- a/src/main/docker/Dockerfile.jvm +++ b/src/main/docker/Dockerfile.jvm @@ -15,4 +15,4 @@ USER 185 ENV JAVA_OPTS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" -ENTRYPOINT [ "sh", "-c", "exec java $JAVA_OPTS -jar /deployments/quarkus-run.jar" ] +ENTRYPOINT [ "sh", "-c", "exec java $JAVA_OPTS -jar /deployments/quarkus-run.jar \"$@\"", "--" ] diff --git a/src/main/java/tech/illuin/wombat/WombatMain.java b/src/main/java/tech/illuin/wombat/WombatMain.java new file mode 100644 index 0000000..b2d654a --- /dev/null +++ b/src/main/java/tech/illuin/wombat/WombatMain.java @@ -0,0 +1,39 @@ +package tech.illuin.wombat; + +import io.quarkus.runtime.Quarkus; +import io.quarkus.runtime.annotations.QuarkusMain; + +import java.util.ArrayList; +import java.util.List; + +@QuarkusMain +public class WombatMain +{ + static final String MODE_OPTION = "--mode"; + static final String MODE_PROPERTY = "wombat.mode"; + + static void main(String... args) + { + Quarkus.run(applyOptions(args)); + } + + static String[] applyOptions(String... args) + { + List remaining = new ArrayList<>(args.length); + for (int i = 0; i < args.length; i++) + { + String arg = args[i]; + if (arg.equals(MODE_OPTION)) + { + if (i + 1 >= args.length) + throw new IllegalArgumentException(MODE_OPTION + " requires a value: standalone, aggregator or worker"); + System.setProperty(MODE_PROPERTY, args[++i]); + } + else if (arg.startsWith(MODE_OPTION + "=")) + System.setProperty(MODE_PROPERTY, arg.substring(MODE_OPTION.length() + 1)); + else + remaining.add(arg); + } + return remaining.toArray(String[]::new); + } +} diff --git a/src/main/java/tech/illuin/wombat/commons/NodeModeConfigSourceFactory.java b/src/main/java/tech/illuin/wombat/commons/NodeModeConfigSourceFactory.java index 0882df1..43efda5 100644 --- a/src/main/java/tech/illuin/wombat/commons/NodeModeConfigSourceFactory.java +++ b/src/main/java/tech/illuin/wombat/commons/NodeModeConfigSourceFactory.java @@ -18,7 +18,6 @@ @StaticInitSafe public class NodeModeConfigSourceFactory implements ConfigSourceFactory { - public static final String AGGREGATOR_CONFIG_KEY = "_internal.aggregator"; private static final String WORKER_DB_KIND = "sqlite"; private static final String WORKER_JDBC_URL = "jdbc:sqlite::memory:"; @@ -31,8 +30,6 @@ public Iterable getConfigSources(ConfigSourceContext context) NodeMode mode = value.getValue() == null ? null : parse(value.getValue()); Map properties = new HashMap<>(); - properties.put(AGGREGATOR_CONFIG_KEY, String.valueOf(mode == NodeMode.aggregator)); - if (mode == NodeMode.worker) { String backend = context.getValue(ACTIVE_BACKEND_CONFIG_KEY).getValue(); diff --git a/src/main/java/tech/illuin/wombat/endpoint/AppController.java b/src/main/java/tech/illuin/wombat/endpoint/AppController.java index 034bbff..116f4cd 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/AppController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/AppController.java @@ -12,12 +12,15 @@ import tech.illuin.wombat.commons.WombatProperties; import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; +import tech.illuin.wombat.feature.Feature; +import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.AuthProperties; import tech.illuin.wombat.endpoint.UIProperties; import java.util.Locale; @Path("api/app") +@RequiresFeature(Feature.UI) public class AppController { private final SecurityIdentity identity; diff --git a/src/main/java/tech/illuin/wombat/endpoint/EnvironmentController.java b/src/main/java/tech/illuin/wombat/endpoint/EnvironmentController.java index 6878606..905726b 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/EnvironmentController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/EnvironmentController.java @@ -16,6 +16,8 @@ import tech.illuin.wombat.engine.context.persistence.AssetRepository; import tech.illuin.wombat.engine.context.persistence.EnvironmentEntity; import tech.illuin.wombat.engine.context.persistence.EnvironmentRepository; +import tech.illuin.wombat.feature.Feature; +import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.Permission; import java.util.List; @@ -26,6 +28,7 @@ * this controller exposes the current state and the per-asset change history but never mutates config. */ @Path("api/environments") +@RequiresFeature(Feature.API_READ) @PermissionsAllowed(Permission.Names.API_ENVIRONMENT_READ) public class EnvironmentController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/ImpactController.java b/src/main/java/tech/illuin/wombat/endpoint/ImpactController.java index 8fe0e54..d672a6a 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/ImpactController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/ImpactController.java @@ -21,6 +21,8 @@ import tech.illuin.wombat.engine.impact.timeline.TimelineStep; import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; +import tech.illuin.wombat.feature.Feature; +import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.Permission; import java.time.Duration; @@ -33,6 +35,7 @@ import java.util.stream.Collectors; @Path("api/impact") +@RequiresFeature(Feature.API_READ) @PermissionsAllowed(Permission.Names.API_IMPACT_READ) public class ImpactController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/MetricController.java b/src/main/java/tech/illuin/wombat/endpoint/MetricController.java index 2d3def3..5b1f2cd 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/MetricController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/MetricController.java @@ -1,6 +1,5 @@ package tech.illuin.wombat.endpoint; -import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.PermissionsAllowed; import jakarta.transaction.Transactional; import jakarta.validation.Valid; @@ -14,13 +13,14 @@ import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import tech.illuin.wombat.commons.NodeModeConfigSourceFactory; import tech.illuin.wombat.core.asset.AssetIdentity; import tech.illuin.wombat.core.source.data.MetricData; import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; import tech.illuin.wombat.engine.module.registry.ModuleMetadata; import tech.illuin.wombat.engine.module.registry.ModuleRegistry; +import tech.illuin.wombat.feature.Feature; +import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.persistence.backend.push.PushBackendConfig; import tech.illuin.wombat.persistence.backend.api.store.MetricSample; import tech.illuin.wombat.persistence.backend.api.store.MetricStoreContainer; @@ -33,15 +33,15 @@ import java.util.Map; /** - * Receives metrics sampled by workers, see {@link tech.illuin.wombat.persistence.backend.push.PushBackendConfig}. Only - * an {@link NodeMode#AGGREGATOR} serves it. Each worker already aggregated its metrics over its sampling window, so they + * Receives metrics sampled by workers, see {@link tech.illuin.wombat.persistence.backend.push.PushBackendConfig}. Served + * with the {@link Feature#API_WRITE}, by an aggregator by default. Each worker already aggregated its metrics over its sampling window, so they * are saved to the database as they are, at the start of the worker's window, for the asset {@code workerId.assetId}, see * {@link WorkerController}. A sample for an asset type without an active module, or of a service family without a store, * is skipped and reported in the response status, under {@code errors}, with the asset id the worker knows; the others * are saved. */ @Path("api/workers/{worker}/metrics") -@EndpointDisabled(name = NodeModeConfigSourceFactory.AGGREGATOR_CONFIG_KEY, stringValue = "false") +@RequiresFeature(Feature.API_WRITE) @PermissionsAllowed(Permission.Names.API_METRIC_WRITE) public class MetricController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java b/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java index 8281af4..2ed41ef 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java @@ -13,12 +13,15 @@ import tech.illuin.wombat.engine.module.registry.ModuleMetadata; import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import tech.illuin.wombat.engine.module.registry.schema.AssetSchemaRegistry; +import tech.illuin.wombat.feature.Feature; +import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.Permission; import com.fasterxml.jackson.databind.JsonNode; import java.util.List; @Path("api/modules") +@RequiresFeature(Feature.API_READ) @PermissionsAllowed(Permission.Names.API_MODULE_READ) public class ModuleController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/SimulationController.java b/src/main/java/tech/illuin/wombat/endpoint/SimulationController.java index d61c53f..2a7504b 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/SimulationController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/SimulationController.java @@ -16,6 +16,8 @@ import tech.illuin.wombat.core.evaluation.AssetEvaluator; import tech.illuin.wombat.core.evaluation.WombatEvaluationException; import tech.illuin.wombat.endpoint.response.Status; +import tech.illuin.wombat.feature.Feature; +import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.Permission; import java.util.ArrayList; @@ -25,6 +27,7 @@ import static tech.illuin.wombat.endpoint.SimulationRequest.Scope.IMPACT; @Path("api/simulation") +@RequiresFeature(Feature.API_READ) @PermissionsAllowed(Permission.Names.API_SIMULATION_READ) public class SimulationController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/WorkerController.java b/src/main/java/tech/illuin/wombat/endpoint/WorkerController.java index e920e88..c3ec3a9 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/WorkerController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/WorkerController.java @@ -3,7 +3,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.node.ObjectNode; -import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.PermissionsAllowed; import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; @@ -16,7 +15,6 @@ import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import tech.illuin.wombat.commons.NodeModeConfigSourceFactory; import tech.illuin.wombat.core.asset.Asset; import tech.illuin.wombat.core.asset.Environment; import tech.illuin.wombat.core.source.Monitorable; @@ -28,6 +26,8 @@ import tech.illuin.wombat.engine.module.registry.ModuleMetadata; import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import tech.illuin.wombat.engine.monitor.MonitoredEnvironments; +import tech.illuin.wombat.feature.Feature; +import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.persistence.backend.push.PushBackendConfig; import tech.illuin.wombat.security.Permission; @@ -37,10 +37,11 @@ /** * Receives the environments declared by workers, so an aggregator evaluates their assets without declaring them itself. * Each worker owns the assets it declares, see {@link EnvironmentReconciler}, identified as {@code workerId.assetId}, so - * two workers declaring the same asset id declare two assets. Only an {@link NodeMode#AGGREGATOR} serves it. + * two workers declaring the same asset id declare two assets. + * Served with the {@link Feature#API_WRITE}, by an aggregator by default. */ @Path("api/workers") -@EndpointDisabled(name = NodeModeConfigSourceFactory.AGGREGATOR_CONFIG_KEY, stringValue = "false") +@RequiresFeature(Feature.API_WRITE) public class WorkerController { private final EnvironmentReconciler reconciler; diff --git a/src/main/java/tech/illuin/wombat/endpoint/access/ApiKeyController.java b/src/main/java/tech/illuin/wombat/endpoint/access/ApiKeyController.java index 7e4b00e..d78cbb8 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/access/ApiKeyController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/access/ApiKeyController.java @@ -11,6 +11,8 @@ import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; +import tech.illuin.wombat.feature.Feature; +import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.AuthProperties; import tech.illuin.wombat.security.Caller; import tech.illuin.wombat.security.Permission; @@ -20,6 +22,7 @@ /** Users manage their own API keys; admins also list ({@code ?all=true}) and revoke everyone's. */ @Path("api/keys") +@RequiresFeature(Feature.ADMIN) @PermissionsAllowed(Permission.Names.API_APIKEY_MANAGE) public class ApiKeyController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/access/UserController.java b/src/main/java/tech/illuin/wombat/endpoint/access/UserController.java index d8b8ae0..eb26a35 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/access/UserController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/access/UserController.java @@ -14,6 +14,8 @@ import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; +import tech.illuin.wombat.feature.Feature; +import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.AuthProperties; import tech.illuin.wombat.security.Caller; import tech.illuin.wombat.security.Permission; @@ -29,6 +31,7 @@ import java.util.stream.Collectors; @Path("api/users") +@RequiresFeature(Feature.ADMIN) @PermissionsAllowed(Permission.Names.API_USER_READ) public class UserController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/access/UserRoleController.java b/src/main/java/tech/illuin/wombat/endpoint/access/UserRoleController.java index 0b4de92..0923c19 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/access/UserRoleController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/access/UserRoleController.java @@ -10,6 +10,8 @@ import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; +import tech.illuin.wombat.feature.Feature; +import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.AuthProperties; import tech.illuin.wombat.security.Caller; import tech.illuin.wombat.security.Permission; @@ -17,6 +19,7 @@ /** Admin-only: grants and removes Wombat roles held on top of the identity provider's (see the users page). */ @Path("api/users/{userId}/roles/{role}") +@RequiresFeature(Feature.ADMIN) @PermissionsAllowed(Permission.Names.API_USER_MANAGE) public class UserRoleController { diff --git a/src/main/java/tech/illuin/wombat/engine/secret/SecretController.java b/src/main/java/tech/illuin/wombat/engine/secret/SecretController.java index c388654..505672a 100644 --- a/src/main/java/tech/illuin/wombat/engine/secret/SecretController.java +++ b/src/main/java/tech/illuin/wombat/engine/secret/SecretController.java @@ -17,12 +17,15 @@ import tech.illuin.wombat.core.secret.SecretWriter; import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; +import tech.illuin.wombat.feature.Feature; +import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.Permission; import java.util.List; @ApplicationScoped @Path("api/secrets") +@RequiresFeature(Feature.ADMIN) @EndpointDisabled(name = "wombat.secret.write-mode", stringValue = "DISABLED", disableIfMissing = true) @PermissionsAllowed(Permission.Names.API_SECRET_MANAGE) @Blocking diff --git a/src/main/java/tech/illuin/wombat/engine/secret/database/DatabaseSecretController.java b/src/main/java/tech/illuin/wombat/engine/secret/database/DatabaseSecretController.java index d610cf1..86e5c0c 100644 --- a/src/main/java/tech/illuin/wombat/engine/secret/database/DatabaseSecretController.java +++ b/src/main/java/tech/illuin/wombat/engine/secret/database/DatabaseSecretController.java @@ -16,10 +16,13 @@ import tech.illuin.wombat.endpoint.response.Status; import tech.illuin.wombat.engine.secret.database.crypto.SecretRewrap; import tech.illuin.wombat.engine.secret.database.crypto.SecretRewrapService; +import tech.illuin.wombat.feature.Feature; +import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.Permission; @ApplicationScoped @Path("api/secrets/database") +@RequiresFeature(Feature.ADMIN) @EndpointDisabled(name = "wombat.secret.write-mode", stringValue = "DISABLED", disableIfMissing = true) @PermissionsAllowed(Permission.Names.API_SECRET_MANAGE) @Blocking diff --git a/src/main/java/tech/illuin/wombat/feature/Feature.java b/src/main/java/tech/illuin/wombat/feature/Feature.java new file mode 100644 index 0000000..1dc008b --- /dev/null +++ b/src/main/java/tech/illuin/wombat/feature/Feature.java @@ -0,0 +1,33 @@ +package tech.illuin.wombat.feature; + +import tech.illuin.wombat.commons.NodeMode; + +import java.util.function.Predicate; + +public enum Feature +{ + UI("ui", mode -> mode != NodeMode.worker), + API_READ("api-read", mode -> mode != NodeMode.worker), + API_WRITE("api-write", mode -> mode == NodeMode.aggregator), + ADMIN("admin", mode -> mode != NodeMode.worker), + ; + + private final String flag; + private final Predicate enabled; + + Feature(String flag, Predicate enabled) + { + this.flag = flag; + this.enabled = enabled; + } + + public String flag() + { + return this.flag; + } + + public boolean enabledIn(NodeMode mode) + { + return this.enabled.test(mode); + } +} diff --git a/src/main/java/tech/illuin/wombat/feature/FeatureFlags.java b/src/main/java/tech/illuin/wombat/feature/FeatureFlags.java new file mode 100644 index 0000000..c4ee8cf --- /dev/null +++ b/src/main/java/tech/illuin/wombat/feature/FeatureFlags.java @@ -0,0 +1,43 @@ +package tech.illuin.wombat.feature; + +import io.quarkiverse.flags.Flag; +import io.quarkiverse.flags.Flags; +import io.quarkus.runtime.StartupEvent; +import io.smallrye.mutiny.Uni; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.stream.Collectors; + +@ApplicationScoped +public class FeatureFlags +{ + private static final Logger logger = LoggerFactory.getLogger(FeatureFlags.class); + + private final Flags flags; + + public FeatureFlags(Flags flags) + { + this.flags = flags; + } + + public Uni isEnabled(Feature feature) + { + return this.flags.find(feature.flag()).chain(flag -> flag + .map(Flag::compute) + .map(value -> value.map(Flag.Value::asBoolean)) + .orElseGet(() -> Uni.createFrom().item(false)) + ); + } + + void logFeatures(@Observes StartupEvent event) + { + String features = Arrays.stream(Feature.values()) + .map(feature -> feature.flag() + "=" + this.isEnabled(feature).await().indefinitely()) + .collect(Collectors.joining(", ")); + logger.info("Features: {}", features); + } +} diff --git a/src/main/java/tech/illuin/wombat/feature/FeatureRequestFilter.java b/src/main/java/tech/illuin/wombat/feature/FeatureRequestFilter.java new file mode 100644 index 0000000..6e97006 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/feature/FeatureRequestFilter.java @@ -0,0 +1,43 @@ +package tech.illuin.wombat.feature; + +import io.smallrye.mutiny.Uni; +import jakarta.ws.rs.container.ResourceInfo; +import org.jboss.resteasy.reactive.RestResponse; +import org.jboss.resteasy.reactive.server.ServerRequestFilter; + +import java.lang.reflect.Method; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; + +public class FeatureRequestFilter +{ + private final FeatureFlags features; + private final Map> required = new ConcurrentHashMap<>(); + + public FeatureRequestFilter(FeatureFlags features) + { + this.features = features; + } + + @ServerRequestFilter + public Uni> filter(ResourceInfo resource) + { + Optional feature = this.required.computeIfAbsent( + resource.getResourceMethod(), + method -> requiredFor(method, resource.getResourceClass()) + ); + if (feature.isEmpty()) + return Uni.createFrom().nullItem(); + + return this.features.isEnabled(feature.get()).map(enabled -> enabled ? null : RestResponse.notFound()); + } + + static Optional requiredFor(Method method, Class type) + { + RequiresFeature annotation = method.getAnnotation(RequiresFeature.class); + if (annotation == null) + annotation = type.getAnnotation(RequiresFeature.class); + return Optional.ofNullable(annotation).map(RequiresFeature::value); + } +} diff --git a/src/main/java/tech/illuin/wombat/feature/NodeModeFlagProvider.java b/src/main/java/tech/illuin/wombat/feature/NodeModeFlagProvider.java new file mode 100644 index 0000000..9f1b0e8 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/feature/NodeModeFlagProvider.java @@ -0,0 +1,41 @@ +package tech.illuin.wombat.feature; + +import io.quarkiverse.flags.Flag; +import io.quarkiverse.flags.spi.ComponentOrder; +import io.quarkiverse.flags.spi.FlagProvider; +import io.smallrye.common.annotation.Identifier; +import io.smallrye.mutiny.Uni; +import jakarta.inject.Singleton; +import tech.illuin.wombat.commons.WombatProperties; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +@Singleton +@Identifier(NodeModeFlagProvider.ID) +@ComponentOrder(before = NodeModeFlagProvider.QUARKUS_CONFIG_ID) +public class NodeModeFlagProvider implements FlagProvider +{ + static final String ID = "wombat.mode"; + static final String QUARKUS_CONFIG_ID = "quarkus.config"; + + private final List flags; + + public NodeModeFlagProvider(WombatProperties wombat) + { + this.flags = Arrays.stream(Feature.values()) + .map(feature -> Flag.builder(feature.flag()) + .setEnabled(feature.enabledIn(wombat.mode())) + .setOrigin(ID) + .build() + ) + .toList(); + } + + @Override + public Uni> getFlags() + { + return Uni.createFrom().item(this.flags); + } +} diff --git a/src/main/java/tech/illuin/wombat/feature/RequiresFeature.java b/src/main/java/tech/illuin/wombat/feature/RequiresFeature.java new file mode 100644 index 0000000..c01584d --- /dev/null +++ b/src/main/java/tech/illuin/wombat/feature/RequiresFeature.java @@ -0,0 +1,13 @@ +package tech.illuin.wombat.feature; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.METHOD }) +public @interface RequiresFeature +{ + Feature value(); +} diff --git a/src/main/java/tech/illuin/wombat/feature/UiFeatureFilter.java b/src/main/java/tech/illuin/wombat/feature/UiFeatureFilter.java new file mode 100644 index 0000000..55917f5 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/feature/UiFeatureFilter.java @@ -0,0 +1,49 @@ +package tech.illuin.wombat.feature; + +import io.quarkus.vertx.http.runtime.filters.Filters; +import io.quarkus.vertx.http.runtime.security.SecurityHandlerPriorities; +import io.vertx.ext.web.RoutingContext; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; + +@ApplicationScoped +public class UiFeatureFilter +{ + static final int PRIORITY = (SecurityHandlerPriorities.CORS + SecurityHandlerPriorities.AUTHENTICATION) / 2; + + private final FeatureFlags features; + + public UiFeatureFilter(FeatureFlags features) + { + this.features = features; + } + + void register(@Observes Filters filters) + { + filters.register(this::filter, PRIORITY); + } + + private void filter(RoutingContext context) + { + if (!isUiPath(context.normalizedPath())) + { + context.next(); + return; + } + + this.features.isEnabled(Feature.UI).subscribe().with( + enabled -> { + if (enabled) + context.next(); + else + context.response().setStatusCode(404).end(); + }, + context::fail + ); + } + + static boolean isUiPath(String path) + { + return !path.startsWith("/api/") && !path.startsWith("/q/"); + } +} diff --git a/src/test/java/tech/illuin/wombat/WombatMainTest.java b/src/test/java/tech/illuin/wombat/WombatMainTest.java new file mode 100644 index 0000000..d6e59f9 --- /dev/null +++ b/src/test/java/tech/illuin/wombat/WombatMainTest.java @@ -0,0 +1,50 @@ +package tech.illuin.wombat; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class WombatMainTest +{ + @AfterEach + void clearMode() + { + System.clearProperty(WombatMain.MODE_PROPERTY); + } + + @Test + void modeOptionSetsTheModeProperty() + { + String[] remaining = WombatMain.applyOptions("--mode", "worker", "other"); + + assertEquals("worker", System.getProperty(WombatMain.MODE_PROPERTY)); + assertArrayEquals(new String[] { "other" }, remaining); + } + + @Test + void modeOptionAcceptsAnInlineValue() + { + String[] remaining = WombatMain.applyOptions("--mode=aggregator"); + + assertEquals("aggregator", System.getProperty(WombatMain.MODE_PROPERTY)); + assertArrayEquals(new String[0], remaining); + } + + @Test + void withoutModeOptionThePropertyIsLeftUnset() + { + WombatMain.applyOptions("other"); + + assertNull(System.getProperty(WombatMain.MODE_PROPERTY)); + } + + @Test + void modeOptionWithoutValueIsRejected() + { + assertThrows(IllegalArgumentException.class, () -> WombatMain.applyOptions("--mode")); + } +} diff --git a/src/test/java/tech/illuin/wombat/feature/FeatureTest.java b/src/test/java/tech/illuin/wombat/feature/FeatureTest.java new file mode 100644 index 0000000..b4212c5 --- /dev/null +++ b/src/test/java/tech/illuin/wombat/feature/FeatureTest.java @@ -0,0 +1,38 @@ +package tech.illuin.wombat.feature; + +import org.junit.jupiter.api.Test; +import tech.illuin.wombat.commons.NodeMode; + +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class FeatureTest +{ + @Test + void standaloneServesTheUiTheReadApiAndTheAdministration() + { + assertEquals(Set.of(Feature.UI, Feature.API_READ, Feature.ADMIN), enabled(NodeMode.standalone)); + } + + @Test + void aggregatorServesEveryFeature() + { + assertEquals(Set.of(Feature.values()), enabled(NodeMode.aggregator)); + } + + @Test + void workerServesNoFeature() + { + assertEquals(Set.of(), enabled(NodeMode.worker)); + } + + private static Set enabled(NodeMode mode) + { + return Arrays.stream(Feature.values()) + .filter(feature -> feature.enabledIn(mode)) + .collect(Collectors.toSet()); + } +} diff --git a/src/test/java/tech/illuin/wombat/feature/WorkerFeaturesTest.java b/src/test/java/tech/illuin/wombat/feature/WorkerFeaturesTest.java new file mode 100644 index 0000000..492789f --- /dev/null +++ b/src/test/java/tech/illuin/wombat/feature/WorkerFeaturesTest.java @@ -0,0 +1,86 @@ +package tech.illuin.wombat.feature; + +import io.quarkiverse.flags.Flags; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.QuarkusTestProfile; +import io.quarkus.test.junit.TestProfile; +import io.restassured.http.ContentType; +import jakarta.inject.Inject; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Base64; +import java.util.Map; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.assertFalse; + +/** A worker serves none of the features, only {@code /q/*}, whatever the flag configuration. */ +@QuarkusTest +@TestProfile(WorkerFeaturesTest.WorkerProfile.class) +class WorkerFeaturesTest +{ + @Inject + Flags flags; + + @Test + void flagsAreAllDisabled() + { + Arrays.stream(Feature.values()).forEach(feature -> assertFalse(this.flags.isEnabled(feature.flag()), feature.flag())); + } + + @Test + void uiIsNotServed() + { + given().when().get("/api/app").then().statusCode(404); + given().redirects().follow(false).when().get("/logout").then().statusCode(404); + } + + @Test + void readApiIsNotServed() + { + given().when().get("/api/environments").then().statusCode(404); + given().when().get("/api/impact/environments").then().statusCode(404); + given().when().get("/api/modules").then().statusCode(404); + } + + @Test + void writeApiIsNotServed() + { + given().contentType(ContentType.JSON).body("[]").when().post("/api/workers/east/metrics").then().statusCode(404); + } + + @Test + void administrationIsNotServed() + { + // Served to anonymous callers without authentication, when the feature is enabled + given().when().get("/api/secrets").then().statusCode(404); + given().when().post("/api/secrets/database/rewrap").then().statusCode(404); + } + + @Test + void healthIsServed() + { + given().when().get("/q/health/live").then().statusCode(200); + } + + public static class WorkerProfile implements QuarkusTestProfile + { + @Override + public Map getConfigOverrides() + { + String kek = Base64.getEncoder().encodeToString("01234567890123456789012345678901".getBytes(StandardCharsets.UTF_8)); + return Map.of( + "wombat.mode", "worker", + "quarkus.flags.runtime.ui.value", "true", // ignored: the mode alone sets the features + "wombat.worker.id", "test-worker", + "wombat.worker.aggregator.url", "http://localhost:0", + "wombat.worker.aggregator.api-key", "unused", + "wombat.secret.write-mode", "DATABASE", + "wombat.secret.database.active-kek-id", "test", + "wombat.secret.database.kek.test", kek + ); + } + } +} From 107a8ab4caa36c23bb29cdf1f00983edfbac1283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Malka=20Lacombe?= Date: Mon, 16 Nov 2026 12:38:59 +0100 Subject: [PATCH 2/3] feat(W77): use source config factory --- doc/distributed.md | 13 +++-- src/main/docker/Dockerfile.jvm | 2 +- .../java/tech/illuin/wombat/WombatMain.java | 39 --------------- .../tech/illuin/wombat/commons/NodeMode.java | 6 +++ .../commons/NodeModeConfigSourceFactory.java | 7 +-- .../illuin/wombat/endpoint/AppController.java | 6 +-- .../endpoint/EnvironmentController.java | 4 +- .../wombat/endpoint/ImpactController.java | 4 +- .../wombat/endpoint/MetricController.java | 4 +- .../wombat/endpoint/ModuleController.java | 4 +- .../wombat/endpoint/SimulationController.java | 4 +- .../wombat/endpoint/WorkerController.java | 4 +- .../endpoint/access/ApiKeyController.java | 4 +- .../endpoint/access/UserController.java | 4 +- .../endpoint/access/UserRoleController.java | 4 +- .../engine/secret/SecretController.java | 4 +- .../database/DatabaseSecretController.java | 4 +- .../tech/illuin/wombat/feature/Feature.java | 24 +++++++++ .../feature/FeatureConfigSourceFactory.java | 41 +++++++++++++++ .../wombat/feature/FeatureRequestFilter.java | 43 ---------------- .../wombat/feature/RequiresFeature.java | 13 ----- .../illuin/wombat/ui/SessionController.java | 3 ++ .../io.smallrye.config.ConfigSourceFactory | 1 + .../tech/illuin/wombat/WombatMainTest.java | 50 ------------------- .../MetricControllerStandaloneTest.java | 13 +++++ .../wombat/endpoint/MetricControllerTest.java | 11 ++++ .../illuin/wombat/feature/FeatureTest.java | 9 ++++ .../wombat/feature/WorkerFeaturesTest.java | 7 +++ 28 files changed, 145 insertions(+), 187 deletions(-) delete mode 100644 src/main/java/tech/illuin/wombat/WombatMain.java create mode 100644 src/main/java/tech/illuin/wombat/feature/FeatureConfigSourceFactory.java delete mode 100644 src/main/java/tech/illuin/wombat/feature/FeatureRequestFilter.java delete mode 100644 src/main/java/tech/illuin/wombat/feature/RequiresFeature.java delete mode 100644 src/test/java/tech/illuin/wombat/WombatMainTest.java diff --git a/doc/distributed.md b/doc/distributed.md index 2891d5e..7722fa4 100644 --- a/doc/distributed.md +++ b/doc/distributed.md @@ -20,9 +20,7 @@ flowchart LR AGGREGATOR ==> DB ``` -Every instance runs the same image, in one of three modes set by `wombat.mode` (`WOMBAT_MODE`), or by the `--mode` -argument, which takes precedence (e.g. `java -jar quarkus-run.jar --mode worker`, or `command: ["--mode", "worker"]` -in a compose file): +Every instance runs the same image, in one of three modes set by `wombat.mode` (`WOMBAT_MODE`): | Mode | Samples its environments | Writes metrics to | Accepts `POST /api/workers/{worker}/metrics` | |------------------------|--------------------------|---------------------|-----------------------------| @@ -44,9 +42,10 @@ The mode also sets which functionalities an instance serves: `api-write` only covers what workers push, so only an aggregator serves it. The endpoints managing an instance (users, API keys, secrets) write too, but fall under `admin`, so that a standalone instance can still be administered. -A disabled feature answers `404`, and `/q/*` is never gated: by default, a worker serves nothing else, so it needs no -authentication. The features are served as [quarkus-flags](https://docs.quarkiverse.io/quarkus-flags/dev) flags -named after them, resolved once at startup and logged. +The endpoints of a disabled feature are not registered: they answer `404` and are left out of the OpenAPI document +(`/api/openapi`). `/q/*` is never gated: a worker serves nothing else, so it needs no authentication. The features are +also served as [quarkus-flags](https://docs.quarkiverse.io/quarkus-flags/dev) flags named after them, resolved once at +startup and logged. ## Aggregator @@ -171,7 +170,7 @@ holding `api.metric.write` and `api.environment.write` on its **API keys** page, For development purposes, you can use this sample distributed configuration to your advantage by running the aggregator as a dev instance, and launching workers as configured in the docker compose demo: ```sh -./mvnw quarkus:dev -Dquarkus.args="--mode aggregator" -Dmonitor.environments-file=compose/demo-distributed/aggregator/monitored-environments.yaml ; +./mvnw quarkus:dev -Dwombat.mode=aggregator -Dmonitor.environments-file=compose/demo-distributed/aggregator/monitored-environments.yaml ; WOMBAT_AGGREGATOR_URL=http://host.docker.internal:8080 docker compose -f compose/demo-distributed/compose.yaml up --no-deps worker-east worker-west ; ``` diff --git a/src/main/docker/Dockerfile.jvm b/src/main/docker/Dockerfile.jvm index b0b8694..988b89b 100644 --- a/src/main/docker/Dockerfile.jvm +++ b/src/main/docker/Dockerfile.jvm @@ -15,4 +15,4 @@ USER 185 ENV JAVA_OPTS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" -ENTRYPOINT [ "sh", "-c", "exec java $JAVA_OPTS -jar /deployments/quarkus-run.jar \"$@\"", "--" ] +ENTRYPOINT [ "sh", "-c", "exec java $JAVA_OPTS -jar /deployments/quarkus-run.jar" ] diff --git a/src/main/java/tech/illuin/wombat/WombatMain.java b/src/main/java/tech/illuin/wombat/WombatMain.java deleted file mode 100644 index b2d654a..0000000 --- a/src/main/java/tech/illuin/wombat/WombatMain.java +++ /dev/null @@ -1,39 +0,0 @@ -package tech.illuin.wombat; - -import io.quarkus.runtime.Quarkus; -import io.quarkus.runtime.annotations.QuarkusMain; - -import java.util.ArrayList; -import java.util.List; - -@QuarkusMain -public class WombatMain -{ - static final String MODE_OPTION = "--mode"; - static final String MODE_PROPERTY = "wombat.mode"; - - static void main(String... args) - { - Quarkus.run(applyOptions(args)); - } - - static String[] applyOptions(String... args) - { - List remaining = new ArrayList<>(args.length); - for (int i = 0; i < args.length; i++) - { - String arg = args[i]; - if (arg.equals(MODE_OPTION)) - { - if (i + 1 >= args.length) - throw new IllegalArgumentException(MODE_OPTION + " requires a value: standalone, aggregator or worker"); - System.setProperty(MODE_PROPERTY, args[++i]); - } - else if (arg.startsWith(MODE_OPTION + "=")) - System.setProperty(MODE_PROPERTY, arg.substring(MODE_OPTION.length() + 1)); - else - remaining.add(arg); - } - return remaining.toArray(String[]::new); - } -} diff --git a/src/main/java/tech/illuin/wombat/commons/NodeMode.java b/src/main/java/tech/illuin/wombat/commons/NodeMode.java index 210ad72..f24c2e1 100644 --- a/src/main/java/tech/illuin/wombat/commons/NodeMode.java +++ b/src/main/java/tech/illuin/wombat/commons/NodeMode.java @@ -8,4 +8,10 @@ public enum NodeMode aggregator, /** Samples its environments and pushes the metrics to an aggregator instead of its own database. */ worker, + ; + + public static NodeMode parse(String mode) + { + return NodeMode.valueOf(mode.trim().replace('-', '_').toLowerCase()); + } } diff --git a/src/main/java/tech/illuin/wombat/commons/NodeModeConfigSourceFactory.java b/src/main/java/tech/illuin/wombat/commons/NodeModeConfigSourceFactory.java index 43efda5..ccc2cb1 100644 --- a/src/main/java/tech/illuin/wombat/commons/NodeModeConfigSourceFactory.java +++ b/src/main/java/tech/illuin/wombat/commons/NodeModeConfigSourceFactory.java @@ -27,7 +27,7 @@ public class NodeModeConfigSourceFactory implements ConfigSourceFactory public Iterable getConfigSources(ConfigSourceContext context) { ConfigValue value = context.getValue("wombat.mode"); - NodeMode mode = value.getValue() == null ? null : parse(value.getValue()); + NodeMode mode = value.getValue() == null ? null : NodeMode.parse(value.getValue()); Map properties = new HashMap<>(); if (mode == NodeMode.worker) @@ -49,9 +49,4 @@ public Iterable getConfigSources(ConfigSourceContext context) Integer.MAX_VALUE )); } - - private static NodeMode parse(String mode) - { - return NodeMode.valueOf(mode.trim().replace('-', '_').toLowerCase()); - } } diff --git a/src/main/java/tech/illuin/wombat/endpoint/AppController.java b/src/main/java/tech/illuin/wombat/endpoint/AppController.java index 116f4cd..dcc2eaa 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/AppController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/AppController.java @@ -1,5 +1,6 @@ package tech.illuin.wombat.endpoint; +import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.identity.SecurityIdentity; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; @@ -9,18 +10,15 @@ import org.eclipse.microprofile.openapi.annotations.media.Schema; import org.eclipse.microprofile.openapi.annotations.media.SchemaProperty; import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; -import tech.illuin.wombat.commons.WombatProperties; import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; import tech.illuin.wombat.feature.Feature; -import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.AuthProperties; -import tech.illuin.wombat.endpoint.UIProperties; import java.util.Locale; @Path("api/app") -@RequiresFeature(Feature.UI) +@EndpointDisabled(name = Feature.Keys.UI, stringValue = "false") public class AppController { private final SecurityIdentity identity; diff --git a/src/main/java/tech/illuin/wombat/endpoint/EnvironmentController.java b/src/main/java/tech/illuin/wombat/endpoint/EnvironmentController.java index 905726b..cddb1d9 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/EnvironmentController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/EnvironmentController.java @@ -1,5 +1,6 @@ package tech.illuin.wombat.endpoint; +import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.PermissionsAllowed; import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; @@ -17,7 +18,6 @@ import tech.illuin.wombat.engine.context.persistence.EnvironmentEntity; import tech.illuin.wombat.engine.context.persistence.EnvironmentRepository; import tech.illuin.wombat.feature.Feature; -import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.Permission; import java.util.List; @@ -28,7 +28,7 @@ * this controller exposes the current state and the per-asset change history but never mutates config. */ @Path("api/environments") -@RequiresFeature(Feature.API_READ) +@EndpointDisabled(name = Feature.Keys.API_READ, stringValue = "false") @PermissionsAllowed(Permission.Names.API_ENVIRONMENT_READ) public class EnvironmentController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/ImpactController.java b/src/main/java/tech/illuin/wombat/endpoint/ImpactController.java index d672a6a..bf3e6c5 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/ImpactController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/ImpactController.java @@ -1,5 +1,6 @@ package tech.illuin.wombat.endpoint; +import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.PermissionsAllowed; import jakarta.validation.Valid; import jakarta.ws.rs.*; @@ -22,7 +23,6 @@ import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; import tech.illuin.wombat.feature.Feature; -import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.Permission; import java.time.Duration; @@ -35,7 +35,7 @@ import java.util.stream.Collectors; @Path("api/impact") -@RequiresFeature(Feature.API_READ) +@EndpointDisabled(name = Feature.Keys.API_READ, stringValue = "false") @PermissionsAllowed(Permission.Names.API_IMPACT_READ) public class ImpactController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/MetricController.java b/src/main/java/tech/illuin/wombat/endpoint/MetricController.java index 5b1f2cd..36f9729 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/MetricController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/MetricController.java @@ -1,5 +1,6 @@ package tech.illuin.wombat.endpoint; +import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.PermissionsAllowed; import jakarta.transaction.Transactional; import jakarta.validation.Valid; @@ -20,7 +21,6 @@ import tech.illuin.wombat.engine.module.registry.ModuleMetadata; import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import tech.illuin.wombat.feature.Feature; -import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.persistence.backend.push.PushBackendConfig; import tech.illuin.wombat.persistence.backend.api.store.MetricSample; import tech.illuin.wombat.persistence.backend.api.store.MetricStoreContainer; @@ -41,7 +41,7 @@ * are saved. */ @Path("api/workers/{worker}/metrics") -@RequiresFeature(Feature.API_WRITE) +@EndpointDisabled(name = Feature.Keys.API_WRITE, stringValue = "false") @PermissionsAllowed(Permission.Names.API_METRIC_WRITE) public class MetricController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java b/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java index 2ed41ef..5badea9 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java @@ -1,5 +1,6 @@ package tech.illuin.wombat.endpoint; +import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.PermissionsAllowed; import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; @@ -14,14 +15,13 @@ import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import tech.illuin.wombat.engine.module.registry.schema.AssetSchemaRegistry; import tech.illuin.wombat.feature.Feature; -import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.Permission; import com.fasterxml.jackson.databind.JsonNode; import java.util.List; @Path("api/modules") -@RequiresFeature(Feature.API_READ) +@EndpointDisabled(name = Feature.Keys.API_READ, stringValue = "false") @PermissionsAllowed(Permission.Names.API_MODULE_READ) public class ModuleController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/SimulationController.java b/src/main/java/tech/illuin/wombat/endpoint/SimulationController.java index 2a7504b..b2eac53 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/SimulationController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/SimulationController.java @@ -1,5 +1,6 @@ package tech.illuin.wombat.endpoint; +import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.PermissionsAllowed; import jakarta.validation.Valid; import jakarta.ws.rs.*; @@ -17,7 +18,6 @@ import tech.illuin.wombat.core.evaluation.WombatEvaluationException; import tech.illuin.wombat.endpoint.response.Status; import tech.illuin.wombat.feature.Feature; -import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.Permission; import java.util.ArrayList; @@ -27,7 +27,7 @@ import static tech.illuin.wombat.endpoint.SimulationRequest.Scope.IMPACT; @Path("api/simulation") -@RequiresFeature(Feature.API_READ) +@EndpointDisabled(name = Feature.Keys.API_READ, stringValue = "false") @PermissionsAllowed(Permission.Names.API_SIMULATION_READ) public class SimulationController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/WorkerController.java b/src/main/java/tech/illuin/wombat/endpoint/WorkerController.java index c3ec3a9..3858c86 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/WorkerController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/WorkerController.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.node.ObjectNode; +import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.PermissionsAllowed; import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; @@ -27,7 +28,6 @@ import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import tech.illuin.wombat.engine.monitor.MonitoredEnvironments; import tech.illuin.wombat.feature.Feature; -import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.persistence.backend.push.PushBackendConfig; import tech.illuin.wombat.security.Permission; @@ -41,7 +41,7 @@ * Served with the {@link Feature#API_WRITE}, by an aggregator by default. */ @Path("api/workers") -@RequiresFeature(Feature.API_WRITE) +@EndpointDisabled(name = Feature.Keys.API_WRITE, stringValue = "false") public class WorkerController { private final EnvironmentReconciler reconciler; diff --git a/src/main/java/tech/illuin/wombat/endpoint/access/ApiKeyController.java b/src/main/java/tech/illuin/wombat/endpoint/access/ApiKeyController.java index d78cbb8..fa7d9dd 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/access/ApiKeyController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/access/ApiKeyController.java @@ -1,5 +1,6 @@ package tech.illuin.wombat.endpoint.access; +import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.PermissionsAllowed; import io.quarkus.security.identity.SecurityIdentity; import jakarta.ws.rs.*; @@ -12,7 +13,6 @@ import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; import tech.illuin.wombat.feature.Feature; -import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.AuthProperties; import tech.illuin.wombat.security.Caller; import tech.illuin.wombat.security.Permission; @@ -22,7 +22,7 @@ /** Users manage their own API keys; admins also list ({@code ?all=true}) and revoke everyone's. */ @Path("api/keys") -@RequiresFeature(Feature.ADMIN) +@EndpointDisabled(name = Feature.Keys.ADMIN, stringValue = "false") @PermissionsAllowed(Permission.Names.API_APIKEY_MANAGE) public class ApiKeyController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/access/UserController.java b/src/main/java/tech/illuin/wombat/endpoint/access/UserController.java index eb26a35..b18eca3 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/access/UserController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/access/UserController.java @@ -1,5 +1,6 @@ package tech.illuin.wombat.endpoint.access; +import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.PermissionsAllowed; import io.quarkus.security.identity.SecurityIdentity; import jakarta.ws.rs.GET; @@ -15,7 +16,6 @@ import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; import tech.illuin.wombat.feature.Feature; -import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.AuthProperties; import tech.illuin.wombat.security.Caller; import tech.illuin.wombat.security.Permission; @@ -31,7 +31,7 @@ import java.util.stream.Collectors; @Path("api/users") -@RequiresFeature(Feature.ADMIN) +@EndpointDisabled(name = Feature.Keys.ADMIN, stringValue = "false") @PermissionsAllowed(Permission.Names.API_USER_READ) public class UserController { diff --git a/src/main/java/tech/illuin/wombat/endpoint/access/UserRoleController.java b/src/main/java/tech/illuin/wombat/endpoint/access/UserRoleController.java index 0923c19..f3395c5 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/access/UserRoleController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/access/UserRoleController.java @@ -1,5 +1,6 @@ package tech.illuin.wombat.endpoint.access; +import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.PermissionsAllowed; import io.quarkus.security.identity.SecurityIdentity; import jakarta.ws.rs.*; @@ -11,7 +12,6 @@ import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; import tech.illuin.wombat.feature.Feature; -import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.AuthProperties; import tech.illuin.wombat.security.Caller; import tech.illuin.wombat.security.Permission; @@ -19,7 +19,7 @@ /** Admin-only: grants and removes Wombat roles held on top of the identity provider's (see the users page). */ @Path("api/users/{userId}/roles/{role}") -@RequiresFeature(Feature.ADMIN) +@EndpointDisabled(name = Feature.Keys.ADMIN, stringValue = "false") @PermissionsAllowed(Permission.Names.API_USER_MANAGE) public class UserRoleController { diff --git a/src/main/java/tech/illuin/wombat/engine/secret/SecretController.java b/src/main/java/tech/illuin/wombat/engine/secret/SecretController.java index 505672a..e841dc4 100644 --- a/src/main/java/tech/illuin/wombat/engine/secret/SecretController.java +++ b/src/main/java/tech/illuin/wombat/engine/secret/SecretController.java @@ -18,15 +18,13 @@ import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; import tech.illuin.wombat.feature.Feature; -import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.Permission; import java.util.List; @ApplicationScoped @Path("api/secrets") -@RequiresFeature(Feature.ADMIN) -@EndpointDisabled(name = "wombat.secret.write-mode", stringValue = "DISABLED", disableIfMissing = true) +@EndpointDisabled(name = Feature.Keys.ADMIN_SECRETS, stringValue = "false") @PermissionsAllowed(Permission.Names.API_SECRET_MANAGE) @Blocking public class SecretController diff --git a/src/main/java/tech/illuin/wombat/engine/secret/database/DatabaseSecretController.java b/src/main/java/tech/illuin/wombat/engine/secret/database/DatabaseSecretController.java index 86e5c0c..e551693 100644 --- a/src/main/java/tech/illuin/wombat/engine/secret/database/DatabaseSecretController.java +++ b/src/main/java/tech/illuin/wombat/engine/secret/database/DatabaseSecretController.java @@ -17,13 +17,11 @@ import tech.illuin.wombat.engine.secret.database.crypto.SecretRewrap; import tech.illuin.wombat.engine.secret.database.crypto.SecretRewrapService; import tech.illuin.wombat.feature.Feature; -import tech.illuin.wombat.feature.RequiresFeature; import tech.illuin.wombat.security.Permission; @ApplicationScoped @Path("api/secrets/database") -@RequiresFeature(Feature.ADMIN) -@EndpointDisabled(name = "wombat.secret.write-mode", stringValue = "DISABLED", disableIfMissing = true) +@EndpointDisabled(name = Feature.Keys.ADMIN_SECRETS, stringValue = "false") @PermissionsAllowed(Permission.Names.API_SECRET_MANAGE) @Blocking public class DatabaseSecretController diff --git a/src/main/java/tech/illuin/wombat/feature/Feature.java b/src/main/java/tech/illuin/wombat/feature/Feature.java index 1dc008b..d3f4a64 100644 --- a/src/main/java/tech/illuin/wombat/feature/Feature.java +++ b/src/main/java/tech/illuin/wombat/feature/Feature.java @@ -30,4 +30,28 @@ public boolean enabledIn(NodeMode mode) { return this.enabled.test(mode); } + + public String configKey() + { + return Keys.PREFIX + this.flag; + } + + /** + * Config keys set to whether a feature is enabled, see {@link FeatureConfigSourceFactory}. Endpoints are gated with + * {@link io.quarkus.resteasy.reactive.server.EndpointDisabled} on them, so a disabled endpoint is neither served nor + * listed in the OpenAPI document. + */ + public static final class Keys + { + static final String PREFIX = "_internal.feature."; + + public static final String UI = PREFIX + "ui"; + public static final String API_READ = PREFIX + "api-read"; + public static final String API_WRITE = PREFIX + "api-write"; + public static final String ADMIN = PREFIX + "admin"; + /** The {@link #ADMIN} feature with a secret write mode: {@code @EndpointDisabled} only takes a single key. */ + public static final String ADMIN_SECRETS = PREFIX + "admin-secrets"; + + private Keys() {} + } } diff --git a/src/main/java/tech/illuin/wombat/feature/FeatureConfigSourceFactory.java b/src/main/java/tech/illuin/wombat/feature/FeatureConfigSourceFactory.java new file mode 100644 index 0000000..8fe6b37 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/feature/FeatureConfigSourceFactory.java @@ -0,0 +1,41 @@ +package tech.illuin.wombat.feature; + +import io.quarkus.runtime.annotations.StaticInitSafe; +import io.smallrye.config.ConfigSourceContext; +import io.smallrye.config.ConfigSourceFactory; +import io.smallrye.config.PropertiesConfigSource; +import org.eclipse.microprofile.config.spi.ConfigSource; +import tech.illuin.wombat.commons.NodeMode; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** Sets the {@link Feature.Keys} from the node mode, so endpoints are gated before the application starts. */ +@StaticInitSafe +public class FeatureConfigSourceFactory implements ConfigSourceFactory +{ + static final String SECRET_WRITE_MODE = "wombat.secret.write-mode"; + private static final String SECRET_WRITE_DISABLED = "DISABLED"; + + @Override + public Iterable getConfigSources(ConfigSourceContext context) + { + String value = context.getValue("wombat.mode").getValue(); + NodeMode mode = value == null ? NodeMode.standalone : NodeMode.parse(value); + + Map properties = new HashMap<>(); + for (Feature feature : Feature.values()) + properties.put(feature.configKey(), String.valueOf(feature.enabledIn(mode))); + + String writeMode = context.getValue(SECRET_WRITE_MODE).getValue(); + boolean secretsWritable = writeMode != null && !writeMode.equals(SECRET_WRITE_DISABLED); + properties.put(Feature.Keys.ADMIN_SECRETS, String.valueOf(Feature.ADMIN.enabledIn(mode) && secretsWritable)); + + return List.of(new PropertiesConfigSource( + properties, + FeatureConfigSourceFactory.class.getSimpleName(), + Integer.MAX_VALUE + )); + } +} diff --git a/src/main/java/tech/illuin/wombat/feature/FeatureRequestFilter.java b/src/main/java/tech/illuin/wombat/feature/FeatureRequestFilter.java deleted file mode 100644 index 6e97006..0000000 --- a/src/main/java/tech/illuin/wombat/feature/FeatureRequestFilter.java +++ /dev/null @@ -1,43 +0,0 @@ -package tech.illuin.wombat.feature; - -import io.smallrye.mutiny.Uni; -import jakarta.ws.rs.container.ResourceInfo; -import org.jboss.resteasy.reactive.RestResponse; -import org.jboss.resteasy.reactive.server.ServerRequestFilter; - -import java.lang.reflect.Method; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; - -public class FeatureRequestFilter -{ - private final FeatureFlags features; - private final Map> required = new ConcurrentHashMap<>(); - - public FeatureRequestFilter(FeatureFlags features) - { - this.features = features; - } - - @ServerRequestFilter - public Uni> filter(ResourceInfo resource) - { - Optional feature = this.required.computeIfAbsent( - resource.getResourceMethod(), - method -> requiredFor(method, resource.getResourceClass()) - ); - if (feature.isEmpty()) - return Uni.createFrom().nullItem(); - - return this.features.isEnabled(feature.get()).map(enabled -> enabled ? null : RestResponse.notFound()); - } - - static Optional requiredFor(Method method, Class type) - { - RequiresFeature annotation = method.getAnnotation(RequiresFeature.class); - if (annotation == null) - annotation = type.getAnnotation(RequiresFeature.class); - return Optional.ofNullable(annotation).map(RequiresFeature::value); - } -} diff --git a/src/main/java/tech/illuin/wombat/feature/RequiresFeature.java b/src/main/java/tech/illuin/wombat/feature/RequiresFeature.java deleted file mode 100644 index c01584d..0000000 --- a/src/main/java/tech/illuin/wombat/feature/RequiresFeature.java +++ /dev/null @@ -1,13 +0,0 @@ -package tech.illuin.wombat.feature; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target({ ElementType.TYPE, ElementType.METHOD }) -public @interface RequiresFeature -{ - Feature value(); -} diff --git a/src/main/java/tech/illuin/wombat/ui/SessionController.java b/src/main/java/tech/illuin/wombat/ui/SessionController.java index 1032010..3a59243 100644 --- a/src/main/java/tech/illuin/wombat/ui/SessionController.java +++ b/src/main/java/tech/illuin/wombat/ui/SessionController.java @@ -3,6 +3,7 @@ import io.quarkus.oidc.IdTokenCredential; import io.quarkus.oidc.OidcConfigurationMetadata; import io.quarkus.oidc.OidcSession; +import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.identity.SecurityIdentity; import io.smallrye.mutiny.Uni; import jakarta.ws.rs.GET; @@ -12,12 +13,14 @@ import jakarta.ws.rs.core.UriBuilder; import jakarta.ws.rs.core.UriInfo; import org.eclipse.microprofile.config.inject.ConfigProperty; +import tech.illuin.wombat.feature.Feature; import tech.illuin.wombat.security.AuthProperties; import java.net.URI; import java.util.Optional; @Path("logout") +@EndpointDisabled(name = Feature.Keys.UI, stringValue = "false") public class SessionController { /** {@code io.quarkus.oidc.runtime.OidcUtils#CONFIG_METADATA_ATTRIBUTE} */ diff --git a/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceFactory b/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceFactory index e4799b4..b4dbb6f 100644 --- a/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceFactory +++ b/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceFactory @@ -1 +1,2 @@ tech.illuin.wombat.commons.NodeModeConfigSourceFactory +tech.illuin.wombat.feature.FeatureConfigSourceFactory diff --git a/src/test/java/tech/illuin/wombat/WombatMainTest.java b/src/test/java/tech/illuin/wombat/WombatMainTest.java deleted file mode 100644 index d6e59f9..0000000 --- a/src/test/java/tech/illuin/wombat/WombatMainTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package tech.illuin.wombat; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; - -class WombatMainTest -{ - @AfterEach - void clearMode() - { - System.clearProperty(WombatMain.MODE_PROPERTY); - } - - @Test - void modeOptionSetsTheModeProperty() - { - String[] remaining = WombatMain.applyOptions("--mode", "worker", "other"); - - assertEquals("worker", System.getProperty(WombatMain.MODE_PROPERTY)); - assertArrayEquals(new String[] { "other" }, remaining); - } - - @Test - void modeOptionAcceptsAnInlineValue() - { - String[] remaining = WombatMain.applyOptions("--mode=aggregator"); - - assertEquals("aggregator", System.getProperty(WombatMain.MODE_PROPERTY)); - assertArrayEquals(new String[0], remaining); - } - - @Test - void withoutModeOptionThePropertyIsLeftUnset() - { - WombatMain.applyOptions("other"); - - assertNull(System.getProperty(WombatMain.MODE_PROPERTY)); - } - - @Test - void modeOptionWithoutValueIsRejected() - { - assertThrows(IllegalArgumentException.class, () -> WombatMain.applyOptions("--mode")); - } -} diff --git a/src/test/java/tech/illuin/wombat/endpoint/MetricControllerStandaloneTest.java b/src/test/java/tech/illuin/wombat/endpoint/MetricControllerStandaloneTest.java index d66501e..9e53439 100644 --- a/src/test/java/tech/illuin/wombat/endpoint/MetricControllerStandaloneTest.java +++ b/src/test/java/tech/illuin/wombat/endpoint/MetricControllerStandaloneTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.not; /** Runs in the default standalone mode, which accepts no pushed metrics. */ @QuarkusTest @@ -25,4 +27,15 @@ void declare_isNotServedOutsideOfTheAggregatorMode() .when().put("/api/workers/east/environments") .then().statusCode(404); } + + @Test + void openApi_listsNoWorkerEndpoint_butTheReadApi() + { + given().queryParam("format", "json") + .when().get("/api/openapi") + .then().statusCode(200) + .body("paths", not(hasKey("/api/workers/{worker}/metrics"))) + .body("paths", not(hasKey("/api/workers/{worker}/environments"))) + .body("paths", hasKey("/api/environments")); + } } diff --git a/src/test/java/tech/illuin/wombat/endpoint/MetricControllerTest.java b/src/test/java/tech/illuin/wombat/endpoint/MetricControllerTest.java index 03c5d5d..ca70f25 100644 --- a/src/test/java/tech/illuin/wombat/endpoint/MetricControllerTest.java +++ b/src/test/java/tech/illuin/wombat/endpoint/MetricControllerTest.java @@ -13,6 +13,7 @@ import java.util.List; import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; @@ -35,6 +36,16 @@ void setUp() QuarkusTransaction.requiringNew().run(() -> this.llm.delete("assetId in ?1", List.of("east.pushed-llm", "east.a"))); } + @Test + void openApi_listsTheWorkerEndpoints() + { + given().queryParam("format", "json") + .when().get("/api/openapi") + .then().statusCode(200) + .body("paths", hasKey("/api/workers/{worker}/metrics")) + .body("paths", hasKey("/api/workers/{worker}/environments")); + } + @Test void push_savesEverySample_atTheStartOfTheWorkersWindow() { diff --git a/src/test/java/tech/illuin/wombat/feature/FeatureTest.java b/src/test/java/tech/illuin/wombat/feature/FeatureTest.java index b4212c5..8d9aa79 100644 --- a/src/test/java/tech/illuin/wombat/feature/FeatureTest.java +++ b/src/test/java/tech/illuin/wombat/feature/FeatureTest.java @@ -29,6 +29,15 @@ void workerServesNoFeature() assertEquals(Set.of(), enabled(NodeMode.worker)); } + @Test + void configKeysMatchTheConstants() + { + assertEquals(Feature.Keys.UI, Feature.UI.configKey()); + assertEquals(Feature.Keys.API_READ, Feature.API_READ.configKey()); + assertEquals(Feature.Keys.API_WRITE, Feature.API_WRITE.configKey()); + assertEquals(Feature.Keys.ADMIN, Feature.ADMIN.configKey()); + } + private static Set enabled(NodeMode mode) { return Arrays.stream(Feature.values()) diff --git a/src/test/java/tech/illuin/wombat/feature/WorkerFeaturesTest.java b/src/test/java/tech/illuin/wombat/feature/WorkerFeaturesTest.java index 492789f..72cb9a8 100644 --- a/src/test/java/tech/illuin/wombat/feature/WorkerFeaturesTest.java +++ b/src/test/java/tech/illuin/wombat/feature/WorkerFeaturesTest.java @@ -14,6 +14,7 @@ import java.util.Map; import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.anEmptyMap; import static org.junit.jupiter.api.Assertions.assertFalse; /** A worker serves none of the features, only {@code /q/*}, whatever the flag configuration. */ @@ -59,6 +60,12 @@ void administrationIsNotServed() given().when().post("/api/secrets/database/rewrap").then().statusCode(404); } + @Test + void openApiListsNoEndpoint() + { + given().queryParam("format", "json").when().get("/api/openapi").then().statusCode(200).body("paths", anEmptyMap()); + } + @Test void healthIsServed() { From 022ec40eb71f5a36e509e2a193c74f19f6e6ef7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Malka=20Lacombe?= Date: Mon, 16 Nov 2026 14:01:23 +0100 Subject: [PATCH 3/3] feat(W77): rm useless flag lib --- doc/distributed.md | 3 +- pom.xml | 6 --- .../illuin/wombat/feature/FeatureFlags.java | 22 ++++------ .../wombat/feature/NodeModeFlagProvider.java | 41 ------------------- .../wombat/feature/UiFeatureFilter.java | 13 ++---- .../wombat/feature/WorkerFeaturesTest.java | 14 ++++--- 6 files changed, 22 insertions(+), 77 deletions(-) delete mode 100644 src/main/java/tech/illuin/wombat/feature/NodeModeFlagProvider.java diff --git a/doc/distributed.md b/doc/distributed.md index 7722fa4..0d6392b 100644 --- a/doc/distributed.md +++ b/doc/distributed.md @@ -44,8 +44,7 @@ API keys, secrets) write too, but fall under `admin`, so that a standalone insta The endpoints of a disabled feature are not registered: they answer `404` and are left out of the OpenAPI document (`/api/openapi`). `/q/*` is never gated: a worker serves nothing else, so it needs no authentication. The features are -also served as [quarkus-flags](https://docs.quarkiverse.io/quarkus-flags/dev) flags named after them, resolved once at -startup and logged. +resolved once at startup and logged. ## Aggregator diff --git a/pom.xml b/pom.xml index ee03edd..5cbaebf 100644 --- a/pom.xml +++ b/pom.xml @@ -81,7 +81,6 @@ 14.4.0 checkstyle.xml 1.21.4 - 1.0.1 @@ -124,11 +123,6 @@ io.quarkus quarkus-kubernetes-client - - io.quarkiverse.flags - quarkus-flags - ${quarkus-flags.version} - io.quarkus quarkus-scheduler diff --git a/src/main/java/tech/illuin/wombat/feature/FeatureFlags.java b/src/main/java/tech/illuin/wombat/feature/FeatureFlags.java index c4ee8cf..5d9e73c 100644 --- a/src/main/java/tech/illuin/wombat/feature/FeatureFlags.java +++ b/src/main/java/tech/illuin/wombat/feature/FeatureFlags.java @@ -1,42 +1,38 @@ package tech.illuin.wombat.feature; -import io.quarkiverse.flags.Flag; -import io.quarkiverse.flags.Flags; import io.quarkus.runtime.StartupEvent; -import io.smallrye.mutiny.Uni; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.event.Observes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import tech.illuin.wombat.commons.NodeMode; +import tech.illuin.wombat.commons.WombatProperties; import java.util.Arrays; import java.util.stream.Collectors; +/** The features served by this instance, set by its mode. REST endpoints are gated on {@link Feature.Keys} instead. */ @ApplicationScoped public class FeatureFlags { private static final Logger logger = LoggerFactory.getLogger(FeatureFlags.class); - private final Flags flags; + private final NodeMode mode; - public FeatureFlags(Flags flags) + public FeatureFlags(WombatProperties wombat) { - this.flags = flags; + this.mode = wombat.mode(); } - public Uni isEnabled(Feature feature) + public boolean isEnabled(Feature feature) { - return this.flags.find(feature.flag()).chain(flag -> flag - .map(Flag::compute) - .map(value -> value.map(Flag.Value::asBoolean)) - .orElseGet(() -> Uni.createFrom().item(false)) - ); + return feature.enabledIn(this.mode); } void logFeatures(@Observes StartupEvent event) { String features = Arrays.stream(Feature.values()) - .map(feature -> feature.flag() + "=" + this.isEnabled(feature).await().indefinitely()) + .map(feature -> feature.flag() + "=" + this.isEnabled(feature)) .collect(Collectors.joining(", ")); logger.info("Features: {}", features); } diff --git a/src/main/java/tech/illuin/wombat/feature/NodeModeFlagProvider.java b/src/main/java/tech/illuin/wombat/feature/NodeModeFlagProvider.java deleted file mode 100644 index 9f1b0e8..0000000 --- a/src/main/java/tech/illuin/wombat/feature/NodeModeFlagProvider.java +++ /dev/null @@ -1,41 +0,0 @@ -package tech.illuin.wombat.feature; - -import io.quarkiverse.flags.Flag; -import io.quarkiverse.flags.spi.ComponentOrder; -import io.quarkiverse.flags.spi.FlagProvider; -import io.smallrye.common.annotation.Identifier; -import io.smallrye.mutiny.Uni; -import jakarta.inject.Singleton; -import tech.illuin.wombat.commons.WombatProperties; - -import java.util.Arrays; -import java.util.Collection; -import java.util.List; - -@Singleton -@Identifier(NodeModeFlagProvider.ID) -@ComponentOrder(before = NodeModeFlagProvider.QUARKUS_CONFIG_ID) -public class NodeModeFlagProvider implements FlagProvider -{ - static final String ID = "wombat.mode"; - static final String QUARKUS_CONFIG_ID = "quarkus.config"; - - private final List flags; - - public NodeModeFlagProvider(WombatProperties wombat) - { - this.flags = Arrays.stream(Feature.values()) - .map(feature -> Flag.builder(feature.flag()) - .setEnabled(feature.enabledIn(wombat.mode())) - .setOrigin(ID) - .build() - ) - .toList(); - } - - @Override - public Uni> getFlags() - { - return Uni.createFrom().item(this.flags); - } -} diff --git a/src/main/java/tech/illuin/wombat/feature/UiFeatureFilter.java b/src/main/java/tech/illuin/wombat/feature/UiFeatureFilter.java index 55917f5..f2c0cfb 100644 --- a/src/main/java/tech/illuin/wombat/feature/UiFeatureFilter.java +++ b/src/main/java/tech/illuin/wombat/feature/UiFeatureFilter.java @@ -31,15 +31,10 @@ private void filter(RoutingContext context) return; } - this.features.isEnabled(Feature.UI).subscribe().with( - enabled -> { - if (enabled) - context.next(); - else - context.response().setStatusCode(404).end(); - }, - context::fail - ); + if (this.features.isEnabled(Feature.UI)) + context.next(); + else + context.response().setStatusCode(404).end(); } static boolean isUiPath(String path) diff --git a/src/test/java/tech/illuin/wombat/feature/WorkerFeaturesTest.java b/src/test/java/tech/illuin/wombat/feature/WorkerFeaturesTest.java index 72cb9a8..25eed51 100644 --- a/src/test/java/tech/illuin/wombat/feature/WorkerFeaturesTest.java +++ b/src/test/java/tech/illuin/wombat/feature/WorkerFeaturesTest.java @@ -1,11 +1,11 @@ package tech.illuin.wombat.feature; -import io.quarkiverse.flags.Flags; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTestProfile; import io.quarkus.test.junit.TestProfile; import io.restassured.http.ContentType; import jakarta.inject.Inject; +import org.eclipse.microprofile.config.ConfigProvider; import org.junit.jupiter.api.Test; import java.nio.charset.StandardCharsets; @@ -17,18 +17,21 @@ import static org.hamcrest.Matchers.anEmptyMap; import static org.junit.jupiter.api.Assertions.assertFalse; -/** A worker serves none of the features, only {@code /q/*}, whatever the flag configuration. */ +/** A worker serves none of the features, only {@code /q/*}. */ @QuarkusTest @TestProfile(WorkerFeaturesTest.WorkerProfile.class) class WorkerFeaturesTest { @Inject - Flags flags; + FeatureFlags features; @Test - void flagsAreAllDisabled() + void featuresAreAllDisabled() { - Arrays.stream(Feature.values()).forEach(feature -> assertFalse(this.flags.isEnabled(feature.flag()), feature.flag())); + Arrays.stream(Feature.values()).forEach(feature -> { + assertFalse(this.features.isEnabled(feature), feature.flag()); + assertFalse(ConfigProvider.getConfig().getValue(feature.configKey(), Boolean.class), feature.configKey()); + }); } @Test @@ -80,7 +83,6 @@ public Map getConfigOverrides() String kek = Base64.getEncoder().encodeToString("01234567890123456789012345678901".getBytes(StandardCharsets.UTF_8)); return Map.of( "wombat.mode", "worker", - "quarkus.flags.runtime.ui.value", "true", // ignored: the mode alone sets the features "wombat.worker.id", "test-worker", "wombat.worker.aggregator.url", "http://localhost:0", "wombat.worker.aggregator.api-key", "unused",