From ee96cc9d2906b11f4f6c5690193a2da0cb88553a Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Wed, 4 Sep 2024 17:02:54 -0400 Subject: [PATCH] remove handling of V2 response format --- .../io/cryostat/agent/CryostatClient.java | 26 +------- .../agent/insights/InsightsAgentHelper.java | 4 +- .../io/cryostat/agent/model/KeyValue.java | 61 +++++++++++++++++++ .../io/cryostat/agent/model/MBeanInfo.java | 2 + .../io/cryostat/agent/model/PluginInfo.java | 38 +++++++++--- .../insights/InsightsAgentHelperTest.java | 4 +- 6 files changed, 99 insertions(+), 36 deletions(-) create mode 100644 src/main/java/io/cryostat/agent/model/KeyValue.java diff --git a/src/main/java/io/cryostat/agent/CryostatClient.java b/src/main/java/io/cryostat/agent/CryostatClient.java index ab9e1673..3f1577c9 100644 --- a/src/main/java/io/cryostat/agent/CryostatClient.java +++ b/src/main/java/io/cryostat/agent/CryostatClient.java @@ -48,7 +48,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import jdk.jfr.Recording; import org.apache.commons.io.FileUtils; @@ -160,18 +159,7 @@ public CompletableFuture register( .thenApply( res -> { try (InputStream is = res.getEntity().getContent()) { - return mapper.readValue(is, ObjectNode.class); - } catch (IOException e) { - log.error("Unable to parse response as JSON", e); - throw new RegistrationException(e); - } - }) - .thenApply( - node -> { - try { - return mapper.readValue( - node.get("data").get("result").toString(), - PluginInfo.class); + return mapper.readValue(is, PluginInfo.class); } catch (IOException e) { log.error("Unable to parse response as JSON", e); throw new RegistrationException(e); @@ -231,18 +219,8 @@ private CompletableFuture queryExistingCredentials(URI callback) { .thenApply( res -> { try (InputStream is = res.getEntity().getContent()) { - return mapper.readValue(is, ObjectNode.class); - } catch (IOException e) { - log.error("Unable to parse response as JSON", e); - throw new RegistrationException(e); - } - }) - .thenApply( - node -> { - try { return mapper.readValue( - node.get("data").get("result").toString(), - new TypeReference>() {}); + is, new TypeReference>() {}); } catch (IOException e) { log.error("Unable to parse response as JSON", e); throw new RegistrationException(e); diff --git a/src/main/java/io/cryostat/agent/insights/InsightsAgentHelper.java b/src/main/java/io/cryostat/agent/insights/InsightsAgentHelper.java index 06d47713..79d548f2 100644 --- a/src/main/java/io/cryostat/agent/insights/InsightsAgentHelper.java +++ b/src/main/java/io/cryostat/agent/insights/InsightsAgentHelper.java @@ -63,12 +63,12 @@ public boolean isInsightsEnabled(PluginInfo pluginInfo) { // Check if the user has opted out boolean optingOut = config.getOptionalValue(RHT_INSIGHTS_JAVA_OPT_OUT, boolean.class).orElse(false); - return pluginInfo.getEnv().containsKey(INSIGHTS_SVC) && !optingOut; + return pluginInfo.getEnvAsMap().containsKey(INSIGHTS_SVC) && !optingOut; } public void runInsightsAgent(PluginInfo pluginInfo) { log.info("Starting Red Hat Insights client"); - String server = pluginInfo.getEnv().get(INSIGHTS_SVC); + String server = pluginInfo.getEnvAsMap().get(INSIGHTS_SVC); Objects.requireNonNull(server, "Insights server is missing"); String appName = config.getValue(ConfigModule.CRYOSTAT_AGENT_APP_NAME, String.class); diff --git a/src/main/java/io/cryostat/agent/model/KeyValue.java b/src/main/java/io/cryostat/agent/model/KeyValue.java new file mode 100644 index 00000000..178d13d7 --- /dev/null +++ b/src/main/java/io/cryostat/agent/model/KeyValue.java @@ -0,0 +1,61 @@ +/* + * Copyright The Cryostat Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.cryostat.agent.model; + +import java.util.Objects; + +public class KeyValue { + private final String key; + private final String value; + + public KeyValue(String key, String value) { + this.key = Objects.requireNonNull(key); + this.value = value; + } + + public String getKey() { + return key; + } + + public String getValue() { + return value; + } + + @Override + public int hashCode() { + return Objects.hash(key, value); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + KeyValue other = (KeyValue) obj; + return Objects.equals(key, other.key) && Objects.equals(value, other.value); + } + + @Override + public String toString() { + return "KeyValue [key=" + key + ", value=" + value + "]"; + } +} diff --git a/src/main/java/io/cryostat/agent/model/MBeanInfo.java b/src/main/java/io/cryostat/agent/model/MBeanInfo.java index e7f163aa..9d353672 100644 --- a/src/main/java/io/cryostat/agent/model/MBeanInfo.java +++ b/src/main/java/io/cryostat/agent/model/MBeanInfo.java @@ -59,6 +59,8 @@ public MBeanInfo() { getJvmID(getRuntimeMetrics())); } + protected final void finalize() {} + public MBeanMetrics getMBeanMetrics() { return mBeanMetrics; } diff --git a/src/main/java/io/cryostat/agent/model/PluginInfo.java b/src/main/java/io/cryostat/agent/model/PluginInfo.java index 2453244d..66a2cb37 100644 --- a/src/main/java/io/cryostat/agent/model/PluginInfo.java +++ b/src/main/java/io/cryostat/agent/model/PluginInfo.java @@ -15,22 +15,33 @@ */ package io.cryostat.agent.model; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; public class PluginInfo { private String id; private String token; - private Map env = new HashMap<>(); + private List env = new ArrayList<>(); public PluginInfo() {} - public PluginInfo(String id, String token, Map env) { + public PluginInfo(String id, String token, List env) { this.id = id; this.token = token; - this.env.putAll(env); + this.env.addAll(env); + } + + public PluginInfo(String id, String token, Map env) { + this( + id, + token, + env.entrySet().stream() + .map(e -> new KeyValue(e.getKey(), e.getValue())) + .collect(Collectors.toList())); } public void copyFrom(PluginInfo o) { @@ -55,8 +66,12 @@ public String getToken() { return token; } - public Map getEnv() { - return new HashMap<>(env); + public List getEnv() { + return new ArrayList<>(env); + } + + public Map getEnvAsMap() { + return env.stream().collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue)); } public void setId(String id) { @@ -67,9 +82,16 @@ public void setToken(String token) { this.token = token; } - public void setEnv(Map env) { + public void setEnv(List env) { this.env.clear(); - this.env.putAll(env); + this.env.addAll(env); + } + + public void setEnvFromMap(Map env) { + this.setEnv( + env.entrySet().stream() + .map(e -> new KeyValue(e.getKey(), e.getValue())) + .collect(Collectors.toList())); } @Override diff --git a/src/test/java/io/cryostat/agent/insights/InsightsAgentHelperTest.java b/src/test/java/io/cryostat/agent/insights/InsightsAgentHelperTest.java index d0ed5d2c..c5b5b648 100644 --- a/src/test/java/io/cryostat/agent/insights/InsightsAgentHelperTest.java +++ b/src/test/java/io/cryostat/agent/insights/InsightsAgentHelperTest.java @@ -82,7 +82,7 @@ void setupEach() { Map env = Collections.singletonMap("INSIGHTS_SVC", "http://insights-proxy.example.com:8080"); - when(pluginInfo.getEnv()).thenReturn(env); + when(pluginInfo.getEnvAsMap()).thenReturn(env); this.helper = new InsightsAgentHelper(config, instrumentation); } @@ -101,7 +101,7 @@ void testInsightsEnabled() { @Test void testInsightsDisabled() { - when(pluginInfo.getEnv()).thenReturn(Collections.emptyMap()); + when(pluginInfo.getEnvAsMap()).thenReturn(Collections.emptyMap()); Assertions.assertFalse(helper.isInsightsEnabled(pluginInfo)); }