Skip to content

Commit f5a1220

Browse files
feat(plugin): send additional env payload to plugins on registration (#1520) (#1773)
(cherry picked from commit 5cfd2fc) Co-authored-by: Andrew Azores <aazores@redhat.com>
1 parent 793db52 commit f5a1220

File tree

4 files changed

+108
-7
lines changed

4 files changed

+108
-7
lines changed

src/main/java/io/cryostat/net/web/http/api/v2/DiscoveryRegistrationHandler.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.URISyntaxException;
2121
import java.net.URL;
2222
import java.util.EnumSet;
23+
import java.util.HashMap;
2324
import java.util.List;
2425
import java.util.Map;
2526
import java.util.Objects;
@@ -34,6 +35,7 @@
3435
import io.cryostat.MainModule;
3536
import io.cryostat.configuration.CredentialsManager;
3637
import io.cryostat.core.log.Logger;
38+
import io.cryostat.core.sys.Environment;
3739
import io.cryostat.discovery.DiscoveryStorage;
3840
import io.cryostat.discovery.PluginInfo;
3941
import io.cryostat.discovery.RegistrationException;
@@ -43,6 +45,8 @@
4345
import io.cryostat.net.web.WebServer;
4446
import io.cryostat.net.web.http.HttpMimeType;
4547
import io.cryostat.net.web.http.api.ApiVersion;
48+
import io.cryostat.platform.PlatformModule;
49+
import io.cryostat.platform.internal.PlatformDetectionStrategy;
4650
import io.cryostat.util.StringUtil;
4751

4852
import com.google.gson.Gson;
@@ -55,11 +59,13 @@
5559
import io.vertx.core.json.JsonObject;
5660
import org.apache.commons.lang3.StringUtils;
5761

58-
class DiscoveryRegistrationHandler extends AbstractV2RequestHandler<Map<String, String>> {
62+
class DiscoveryRegistrationHandler extends AbstractV2RequestHandler<Map<String, Object>> {
5963

6064
static final String PATH = "discovery";
65+
private final Environment env;
6166
private final DiscoveryStorage storage;
6267
private final Lazy<WebServer> webServer;
68+
private final Set<PlatformDetectionStrategy<?>> selectedStrategies;
6369
private final DiscoveryJwtHelper jwtFactory;
6470
private final Function<String, UUID> uuidFromString;
6571
private final Logger logger;
@@ -68,15 +74,20 @@ class DiscoveryRegistrationHandler extends AbstractV2RequestHandler<Map<String,
6874
DiscoveryRegistrationHandler(
6975
AuthManager auth,
7076
CredentialsManager credentialsManager,
77+
Environment env,
7178
DiscoveryStorage storage,
7279
Lazy<WebServer> webServer,
80+
@Named(PlatformModule.SELECTED_PLATFORMS)
81+
Set<PlatformDetectionStrategy<?>> selectedStrategies,
7382
DiscoveryJwtHelper jwt,
7483
@Named(MainModule.UUID_FROM_STRING) Function<String, UUID> uuidFromString,
7584
Gson gson,
7685
Logger logger) {
7786
super(auth, credentialsManager, gson);
87+
this.env = env;
7888
this.storage = storage;
7989
this.webServer = webServer;
90+
this.selectedStrategies = selectedStrategies;
8091
this.jwtFactory = jwt;
8192
this.uuidFromString = uuidFromString;
8293
this.logger = logger;
@@ -118,7 +129,7 @@ public boolean isAsync() {
118129
}
119130

120131
@Override
121-
public IntermediateResponse<Map<String, String>> handle(RequestParameters params)
132+
public IntermediateResponse<Map<String, Object>> handle(RequestParameters params)
122133
throws Exception {
123134
String pluginId, realm, priorToken;
124135
URI callbackUri;
@@ -175,9 +186,15 @@ public IntermediateResponse<Map<String, String>> handle(RequestParameters params
175186
realm,
176187
address,
177188
AbstractDiscoveryJwtConsumingHandler.getResourceUri(hostUrl, pluginId));
178-
return new IntermediateResponse<Map<String, String>>()
189+
Map<String, String> mergedEnv = new HashMap<>();
190+
// FIXME currently only the OpenShiftPlatformStrategy provides any entries for the env map,
191+
// but in the future if any more strategies also provide entries then the order here may be
192+
// undefined and the map entries may collide and be overwritten. There should be some
193+
// prefixing scheme to prevent collisions.
194+
selectedStrategies.forEach(s -> mergedEnv.putAll(s.environment(env)));
195+
return new IntermediateResponse<Map<String, Object>>()
179196
.statusCode(201)
180197
.addHeader(HttpHeaders.LOCATION, String.format("%s/%s", path(), pluginId))
181-
.body(Map.of("id", pluginId, "token", token));
198+
.body(Map.of("id", pluginId, "token", token, "env", mergedEnv));
182199
}
183200
}

src/main/java/io/cryostat/platform/internal/PlatformDetectionStrategy.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package io.cryostat.platform.internal;
1717

18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
import io.cryostat.core.sys.Environment;
1822
import io.cryostat.net.AuthManager;
1923
import io.cryostat.platform.PlatformClient;
2024

@@ -24,4 +28,12 @@ public interface PlatformDetectionStrategy<T extends PlatformClient> {
2428
T getPlatformClient();
2529

2630
AuthManager getAuthManager();
31+
32+
default Map<String, String> environment(Environment env) {
33+
Map<String, String> map = new HashMap<>();
34+
if (env.hasEnv("INSIGHTS_PROXY")) {
35+
map.put("INSIGHTS_SVC", env.getEnv("INSIGHTS_PROXY"));
36+
}
37+
return map;
38+
}
2739
}

src/test/java/io/cryostat/net/web/http/api/v2/DiscoveryRegistrationHandlerTest.java

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@
2727
import io.cryostat.MainModule;
2828
import io.cryostat.configuration.CredentialsManager;
2929
import io.cryostat.core.log.Logger;
30+
import io.cryostat.core.sys.Environment;
3031
import io.cryostat.discovery.DiscoveryStorage;
3132
import io.cryostat.net.AuthManager;
3233
import io.cryostat.net.security.ResourceAction;
3334
import io.cryostat.net.security.jwt.DiscoveryJwtHelper;
3435
import io.cryostat.net.web.WebServer;
3536
import io.cryostat.net.web.http.HttpMimeType;
3637
import io.cryostat.net.web.http.api.ApiVersion;
38+
import io.cryostat.platform.PlatformClient;
39+
import io.cryostat.platform.internal.PlatformDetectionStrategy;
3740

3841
import com.google.gson.Gson;
3942
import io.vertx.core.MultiMap;
@@ -55,9 +58,10 @@
5558

5659
@ExtendWith(MockitoExtension.class)
5760
class DiscoveryRegistrationHandlerTest {
58-
AbstractV2RequestHandler<Map<String, String>> handler;
61+
AbstractV2RequestHandler<Map<String, Object>> handler;
5962
@Mock AuthManager auth;
6063
@Mock CredentialsManager credentialsManager;
64+
@Mock Environment env;
6165
@Mock DiscoveryStorage storage;
6266
@Mock WebServer webServer;
6367
@Mock DiscoveryJwtHelper jwt;
@@ -70,8 +74,10 @@ void setup() {
7074
new DiscoveryRegistrationHandler(
7175
auth,
7276
credentialsManager,
77+
env,
7378
storage,
7479
() -> webServer,
80+
Set.of(new AllEnvPlatformStrategy(), new FakePlatformStrategy()),
7581
jwt,
7682
UUID::fromString,
7783
gson,
@@ -161,6 +167,8 @@ void shouldRegisterWithStorageAndSendResponse() throws Exception {
161167
Mockito.when(storage.register(Mockito.anyString(), Mockito.any(URI.class)))
162168
.thenReturn(id);
163169

170+
Mockito.when(env.getEnv()).thenReturn(Map.of("CRYOSTAT", "hello", "TEST", "true"));
171+
164172
Mockito.when(requestParams.getBody())
165173
.thenReturn(
166174
gson.toJson(
@@ -185,19 +193,77 @@ void shouldRegisterWithStorageAndSendResponse() throws Exception {
185193
Mockito.any(URI.class)))
186194
.thenReturn(token);
187195

188-
IntermediateResponse<Map<String, String>> resp = handler.handle(requestParams);
196+
IntermediateResponse<Map<String, Object>> resp = handler.handle(requestParams);
189197

190198
MatcherAssert.assertThat(resp.getStatusCode(), Matchers.equalTo(201));
191199
MatcherAssert.assertThat(
192200
resp.getHeaders(),
193201
Matchers.equalTo(Map.of(HttpHeaders.LOCATION, "/api/v2.2/discovery/" + id)));
194202
MatcherAssert.assertThat(
195-
resp.getBody(), Matchers.equalTo(Map.of("token", token, "id", id.toString())));
203+
resp.getBody(),
204+
Matchers.equalTo(
205+
Map.of(
206+
"token",
207+
token,
208+
"id",
209+
id.toString(),
210+
"env",
211+
Map.of(
212+
"CRYOSTAT",
213+
"hello",
214+
"TEST",
215+
"true",
216+
"HELLO",
217+
"WORLD"))));
196218

197219
Mockito.verify(storage)
198220
.register(
199221
Mockito.eq("test-realm"),
200222
Mockito.eq(URI.create("http://example.com/callback")));
201223
}
202224
}
225+
226+
static class AllEnvPlatformStrategy implements PlatformDetectionStrategy {
227+
@Override
228+
public Map<String, String> environment(Environment env) {
229+
return env.getEnv();
230+
}
231+
232+
@Override
233+
public boolean isAvailable() {
234+
return true;
235+
}
236+
237+
@Override
238+
public PlatformClient getPlatformClient() {
239+
throw new UnsupportedOperationException("Unimplemented method 'getPlatformClient'");
240+
}
241+
242+
@Override
243+
public AuthManager getAuthManager() {
244+
throw new UnsupportedOperationException("Unimplemented method 'getAuthManager'");
245+
}
246+
}
247+
248+
static class FakePlatformStrategy implements PlatformDetectionStrategy {
249+
@Override
250+
public Map<String, String> environment(Environment env) {
251+
return Map.of("HELLO", "WORLD");
252+
}
253+
254+
@Override
255+
public boolean isAvailable() {
256+
return true;
257+
}
258+
259+
@Override
260+
public PlatformClient getPlatformClient() {
261+
throw new UnsupportedOperationException("Unimplemented method 'getPlatformClient'");
262+
}
263+
264+
@Override
265+
public AuthManager getAuthManager() {
266+
throw new UnsupportedOperationException("Unimplemented method 'getAuthManager'");
267+
}
268+
}
203269
}

src/test/java/itest/DiscoveryPluginIT.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.UUID;
2222
import java.util.concurrent.CompletableFuture;
2323
import java.util.concurrent.ExecutionException;
24+
import java.util.stream.Collectors;
2425

2526
import io.vertx.core.buffer.Buffer;
2627
import io.vertx.core.json.JsonArray;
@@ -40,6 +41,7 @@ class DiscoveryPluginIT extends StandardSelfTest {
4041
final URI callback = URI.create("http://localhost:8181/");
4142
private static volatile String id;
4243
private static volatile String token;
44+
private static volatile Map<String, String> env;
4345

4446
@Test
4547
@Order(1)
@@ -60,8 +62,12 @@ void shouldBeAbleToRegister() throws InterruptedException, ExecutionException {
6062
JsonObject info = resp.getJsonObject("data").getJsonObject("result");
6163
DiscoveryPluginIT.id = info.getString("id");
6264
DiscoveryPluginIT.token = info.getString("token");
65+
DiscoveryPluginIT.env =
66+
info.getJsonObject("env").getMap().entrySet().stream()
67+
.collect(Collectors.toMap(k -> k.toString(), v -> v.toString()));
6368
MatcherAssert.assertThat(id, Matchers.not(Matchers.emptyOrNullString()));
6469
MatcherAssert.assertThat(token, Matchers.not(Matchers.emptyOrNullString()));
70+
MatcherAssert.assertThat(env, Matchers.is(Matchers.equalTo(Map.of())));
6571
}
6672

6773
@Test

0 commit comments

Comments
 (0)