From 2affb1a6d24bd762d9ab01e324f86a2b9003860a Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Tue, 6 Jun 2023 14:44:30 -0400 Subject: [PATCH] feat(plugin): send additional platform env extras to plugins on registration --- smoketest.sh | 6 ++--- .../api/v2/DiscoveryRegistrationHandler.java | 21 ++++++++++++--- .../internal/OpenShiftPlatformStrategy.java | 26 +++++++++++++++++++ .../internal/PlatformDetectionStrategy.java | 6 +++++ .../internal/PodmanPlatformStrategy.java | 8 ++++++ 5 files changed, 60 insertions(+), 7 deletions(-) diff --git a/smoketest.sh b/smoketest.sh index 472d2e8b94..385465e0c7 100755 --- a/smoketest.sh +++ b/smoketest.sh @@ -125,7 +125,7 @@ runDemoApps() { --label io.cryostat.discovery="true" \ --label io.cryostat.jmxHost="localhost" \ --label io.cryostat.jmxPort="9093" \ - --rm -d quay.io/andrewazores/vertx-fib-demo:0.12.2 + --rm -d quay.io/andrewazores/vertx-fib-demo:0.12.4 podman run \ --name vertx-fib-demo-2 \ @@ -146,7 +146,7 @@ runDemoApps() { --label io.cryostat.jmxHost="localhost" \ --label io.cryostat.jmxPort="9094" \ --label io.cryostat.jmxUrl="service:jmx:rmi:///jndi/rmi://localhost:9094/jmxrmi" \ - --rm -d quay.io/andrewazores/vertx-fib-demo:0.12.2 + --rm -d quay.io/andrewazores/vertx-fib-demo:0.12.4 podman run \ --name vertx-fib-demo-3 \ @@ -166,7 +166,7 @@ runDemoApps() { --pod cryostat-pod \ --label io.cryostat.discovery="true" \ --label io.cryostat.jmxUrl="service:jmx:rmi:///jndi/rmi://localhost:9095/jmxrmi" \ - --rm -d quay.io/andrewazores/vertx-fib-demo:0.12.2 + --rm -d quay.io/andrewazores/vertx-fib-demo:0.12.4 # this config is broken on purpose (missing required env vars) to test the agent's behaviour # when not properly set up diff --git a/src/main/java/io/cryostat/net/web/http/api/v2/DiscoveryRegistrationHandler.java b/src/main/java/io/cryostat/net/web/http/api/v2/DiscoveryRegistrationHandler.java index f530de6904..be261b3676 100644 --- a/src/main/java/io/cryostat/net/web/http/api/v2/DiscoveryRegistrationHandler.java +++ b/src/main/java/io/cryostat/net/web/http/api/v2/DiscoveryRegistrationHandler.java @@ -42,6 +42,7 @@ import java.net.URISyntaxException; import java.net.URL; import java.util.EnumSet; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -65,6 +66,8 @@ import io.cryostat.net.web.WebServer; import io.cryostat.net.web.http.HttpMimeType; import io.cryostat.net.web.http.api.ApiVersion; +import io.cryostat.platform.PlatformModule; +import io.cryostat.platform.internal.PlatformDetectionStrategy; import io.cryostat.util.StringUtil; import com.google.gson.Gson; @@ -77,11 +80,12 @@ import io.vertx.core.json.JsonObject; import org.apache.commons.lang3.StringUtils; -class DiscoveryRegistrationHandler extends AbstractV2RequestHandler> { +class DiscoveryRegistrationHandler extends AbstractV2RequestHandler> { static final String PATH = "discovery"; private final DiscoveryStorage storage; private final Lazy webServer; + private final Set> selectedStrategies; private final DiscoveryJwtHelper jwtFactory; private final Function uuidFromString; private final Logger logger; @@ -92,6 +96,8 @@ class DiscoveryRegistrationHandler extends AbstractV2RequestHandler webServer, + @Named(PlatformModule.SELECTED_PLATFORMS) + Set> selectedStrategies, DiscoveryJwtHelper jwt, @Named(MainModule.UUID_FROM_STRING) Function uuidFromString, Gson gson, @@ -99,6 +105,7 @@ class DiscoveryRegistrationHandler extends AbstractV2RequestHandler> handle(RequestParameters params) + public IntermediateResponse> handle(RequestParameters params) throws Exception { String pluginId, realm, priorToken; URI callbackUri; @@ -197,9 +204,15 @@ public IntermediateResponse> handle(RequestParameters params realm, address, AbstractDiscoveryJwtConsumingHandler.getResourceUri(hostUrl, pluginId)); - return new IntermediateResponse>() + Map mergedEnv = new HashMap<>(); + // FIXME currently only the OpenShiftPlatformStrategy provides any entries for the env map, + // but in the future if any more strategies also provide entries then the order here may be + // undefined and the map entries may collide and be overwritten. There should be some + // prefixing scheme to prevent collisions. + selectedStrategies.forEach(s -> mergedEnv.putAll(s.environment())); + return new IntermediateResponse>() .statusCode(201) .addHeader(HttpHeaders.LOCATION, String.format("%s/%s", path(), pluginId)) - .body(Map.of("id", pluginId, "token", token)); + .body(Map.of("id", pluginId, "token", token, "env", mergedEnv)); } } diff --git a/src/main/java/io/cryostat/platform/internal/OpenShiftPlatformStrategy.java b/src/main/java/io/cryostat/platform/internal/OpenShiftPlatformStrategy.java index b6d53c3e17..c23580b0dd 100644 --- a/src/main/java/io/cryostat/platform/internal/OpenShiftPlatformStrategy.java +++ b/src/main/java/io/cryostat/platform/internal/OpenShiftPlatformStrategy.java @@ -37,6 +37,11 @@ */ package io.cryostat.platform.internal; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + import io.cryostat.core.log.Logger; import io.cryostat.core.net.JFRConnectionToolkit; import io.cryostat.core.sys.Environment; @@ -49,6 +54,8 @@ class OpenShiftPlatformStrategy extends KubeApiPlatformStrategy { + static final String INSIGHTS_TOKEN_PATH = "/var/run/TODO"; + OpenShiftPlatformStrategy( Logger logger, Lazy authMgr, @@ -67,4 +74,23 @@ protected boolean testAvailability(KubernetesClient client) { protected OpenShiftClient createClient() { return super.createClient().adapt(OpenShiftClient.class); } + + @Override + public Map environment() { + Map env = new HashMap<>(super.environment()); + String token = getInsightsToken(); + if (token != null) { + env.put("INSIGHTS_TOKEN", token); + } + return env; + } + + private String getInsightsToken() { + try { + return fs.readString(Paths.get(INSIGHTS_TOKEN_PATH)); + } catch (IOException e) { + logger.trace(e); + return null; + } + } } diff --git a/src/main/java/io/cryostat/platform/internal/PlatformDetectionStrategy.java b/src/main/java/io/cryostat/platform/internal/PlatformDetectionStrategy.java index b78d022ea6..91ab3dc67d 100644 --- a/src/main/java/io/cryostat/platform/internal/PlatformDetectionStrategy.java +++ b/src/main/java/io/cryostat/platform/internal/PlatformDetectionStrategy.java @@ -37,6 +37,8 @@ */ package io.cryostat.platform.internal; +import java.util.Map; + import io.cryostat.net.AuthManager; import io.cryostat.platform.PlatformClient; @@ -46,4 +48,8 @@ public interface PlatformDetectionStrategy { T getPlatformClient(); AuthManager getAuthManager(); + + default Map environment() { + return Map.of(); + } } diff --git a/src/main/java/io/cryostat/platform/internal/PodmanPlatformStrategy.java b/src/main/java/io/cryostat/platform/internal/PodmanPlatformStrategy.java index dd529ba2d1..5232665288 100644 --- a/src/main/java/io/cryostat/platform/internal/PodmanPlatformStrategy.java +++ b/src/main/java/io/cryostat/platform/internal/PodmanPlatformStrategy.java @@ -38,6 +38,8 @@ package io.cryostat.platform.internal; import java.net.URI; +import java.util.Map; +import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; @@ -161,6 +163,12 @@ public AuthManager getAuthManager() { return authMgr.get(); } + // FIXME remove this, just for testing purposes + @Override + public Map environment() { + return Map.of("TEST", UUID.randomUUID().toString()); + } + private static String getSocketPath() { long uid = new UnixSystem().getUid(); String socketPath = String.format("/run/user/%d/podman/podman.sock", uid);