Skip to content

Commit

Permalink
feat(plugin): send additional platform env extras to plugins on regis…
Browse files Browse the repository at this point in the history
…tration
  • Loading branch information
andrewazores committed Jun 6, 2023
1 parent 3ac890a commit 2affb1a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
6 changes: 3 additions & 3 deletions smoketest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -77,11 +80,12 @@
import io.vertx.core.json.JsonObject;
import org.apache.commons.lang3.StringUtils;

class DiscoveryRegistrationHandler extends AbstractV2RequestHandler<Map<String, String>> {
class DiscoveryRegistrationHandler extends AbstractV2RequestHandler<Map<String, Object>> {

static final String PATH = "discovery";
private final DiscoveryStorage storage;
private final Lazy<WebServer> webServer;
private final Set<PlatformDetectionStrategy<?>> selectedStrategies;
private final DiscoveryJwtHelper jwtFactory;
private final Function<String, UUID> uuidFromString;
private final Logger logger;
Expand All @@ -92,13 +96,16 @@ class DiscoveryRegistrationHandler extends AbstractV2RequestHandler<Map<String,
CredentialsManager credentialsManager,
DiscoveryStorage storage,
Lazy<WebServer> webServer,
@Named(PlatformModule.SELECTED_PLATFORMS)
Set<PlatformDetectionStrategy<?>> selectedStrategies,
DiscoveryJwtHelper jwt,
@Named(MainModule.UUID_FROM_STRING) Function<String, UUID> uuidFromString,
Gson gson,
Logger logger) {
super(auth, credentialsManager, gson);
this.storage = storage;
this.webServer = webServer;
this.selectedStrategies = selectedStrategies;
this.jwtFactory = jwt;
this.uuidFromString = uuidFromString;
this.logger = logger;
Expand Down Expand Up @@ -140,7 +147,7 @@ public boolean isAsync() {
}

@Override
public IntermediateResponse<Map<String, String>> handle(RequestParameters params)
public IntermediateResponse<Map<String, Object>> handle(RequestParameters params)
throws Exception {
String pluginId, realm, priorToken;
URI callbackUri;
Expand Down Expand Up @@ -197,9 +204,15 @@ public IntermediateResponse<Map<String, String>> handle(RequestParameters params
realm,
address,
AbstractDiscoveryJwtConsumingHandler.getResourceUri(hostUrl, pluginId));
return new IntermediateResponse<Map<String, String>>()
Map<String, String> 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<Map<String, Object>>()
.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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,6 +54,8 @@

class OpenShiftPlatformStrategy extends KubeApiPlatformStrategy {

static final String INSIGHTS_TOKEN_PATH = "/var/run/TODO";

OpenShiftPlatformStrategy(
Logger logger,
Lazy<? extends AuthManager> authMgr,
Expand All @@ -67,4 +74,23 @@ protected boolean testAvailability(KubernetesClient client) {
protected OpenShiftClient createClient() {
return super.createClient().adapt(OpenShiftClient.class);
}

@Override
public Map<String, String> environment() {
Map<String, String> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
*/
package io.cryostat.platform.internal;

import java.util.Map;

import io.cryostat.net.AuthManager;
import io.cryostat.platform.PlatformClient;

Expand All @@ -46,4 +48,8 @@ public interface PlatformDetectionStrategy<T extends PlatformClient> {
T getPlatformClient();

AuthManager getAuthManager();

default Map<String, String> environment() {
return Map.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -161,6 +163,12 @@ public AuthManager getAuthManager() {
return authMgr.get();
}

// FIXME remove this, just for testing purposes
@Override
public Map<String, String> 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);
Expand Down

0 comments on commit 2affb1a

Please sign in to comment.