Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config): config classloader strategy for different attach scenarios #523

Merged
merged 6 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 70 additions & 18 deletions src/main/java/io/cryostat/agent/ConfigModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.security.AccessController;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -36,6 +35,8 @@
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Matcher;
Expand All @@ -46,10 +47,12 @@
import javax.inject.Named;
import javax.inject.Singleton;

import io.cryostat.agent.util.ResourcesUtil;
import io.cryostat.agent.util.StringUtils;

import dagger.Module;
import dagger.Provides;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.client.utils.URIBuilder;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
Expand Down Expand Up @@ -177,6 +180,7 @@ public abstract class ConfigModule {
public static final String CRYOSTAT_AGENT_WEBSERVER_CREDENTIALS_PASS_LENGTH =
"cryostat.agent.webserver.credentials.pass.length";

public static final String CRYOSTAT_AGENT_CONFIG_LOADABLE = "cryostat.agent.config.loadable";
public static final String CRYOSTAT_AGENT_APP_NAME = "cryostat.agent.app.name";
public static final String CRYOSTAT_AGENT_HOSTNAME = "cryostat.agent.hostname";
public static final String CRYOSTAT_AGENT_APP_JMX_PORT = "cryostat.agent.app.jmx.port";
Expand Down Expand Up @@ -231,24 +235,72 @@ public abstract class ConfigModule {
@Provides
@Singleton
public static Config provideConfig() {
// if we don't do this then the SmallRye Config loader may end up with a null classloader in
// the case that the Agent starts separately and is dynamically attached to a running VM,
// which results in an NPE. Here we try to detect and preempt that case and ensure that
// there is a reasonable classloader for the SmallRye config loader to use.
PrivilegedExceptionAction<ClassLoader> pea =
() -> {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl != null) {
return cl;
}
return new URLClassLoader(new URL[] {Agent.selfJarLocation().toURL()});
};
try {
ClassLoader cl = AccessController.doPrivileged(pea);
return ConfigProvider.getConfig(cl);
} catch (PrivilegedActionException pae) {
throw new RuntimeException(pae);
List<Pair<String, Callable<Config>>> fns = new ArrayList<>();
fns.add(Pair.of("Simple", ConfigProvider::getConfig));
Function<Callable<ClassLoader>, Callable<Config>> clConfigLoader =
cl ->
() -> {
ClassLoader loader;
try {
loader =
AccessController.doPrivileged(
(PrivilegedExceptionAction<ClassLoader>)
() -> cl.call());
} catch (Exception e) {
log.warn(
"ClassLoader AccessController failure - is this JVM too new"
+ " to have the AccessController and SecurityManager?",
e);
loader = cl.call();
}
return ConfigProvider.getConfig(loader);
};
fns.add(
Pair.of(
"ResourcesUtil ClassLoader",
clConfigLoader.apply(ResourcesUtil::getClassLoader)));
fns.add(
Pair.of(
"Config Class ClassLoader",
clConfigLoader.apply(Config.class::getClassLoader)));
fns.add(
Pair.of(
"Agent JAR URL ClassLoader",
clConfigLoader.apply(
() ->
new URLClassLoader(
new URL[] {Agent.selfJarLocation().toURL()}))));

Config config = null;
for (Pair<String, Callable<Config>> fn : fns) {
try {
log.trace(
"Testing classloader \"{}\" for {} property",
fn.getLeft(),
CRYOSTAT_AGENT_CONFIG_LOADABLE);
Config candidate = fn.getRight().call();
if (!candidate.getValue(CRYOSTAT_AGENT_CONFIG_LOADABLE, boolean.class)) {
log.warn(
"{} was false. Assuming that this means the {} classloader"
+ " cannot be used to load properties. Do not override this"
+ " configuration property with a blank or false value!",
CRYOSTAT_AGENT_CONFIG_LOADABLE,
fn.getLeft());
continue;
}
config = candidate;
break;
} catch (Exception e) {
config = null;
log.debug(
String.format("Failed to load config from \"%s\" supplier", fn.getLeft()),
e);
}
}
if (config == null || !config.getValue(CRYOSTAT_AGENT_CONFIG_LOADABLE, boolean.class)) {
log.error("Unable to load configuration from any classloader source!");
}
return config;
}

@Provides
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/io/cryostat/agent/VersionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import io.cryostat.agent.util.ResourcesUtil;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -36,6 +37,8 @@ public class VersionInfo {
static final String MIN_VERSION_KEY = "cryostat.server.version.min";
static final String MAX_VERSION_KEY = "cryostat.server.version.max";

private static Logger log = LoggerFactory.getLogger(VersionInfo.class);

private final Semver agentVersion;
private final Semver serverMin;
private final Semver serverMax;
Expand All @@ -50,7 +53,15 @@ public class VersionInfo {
public static VersionInfo load() throws IOException {
Properties prop = new Properties();
try (InputStream is = ResourcesUtil.getResourceAsStream(RESOURCE_LOCATION)) {
prop.load(is);
if (is == null) {
log.warn("Could not locate resource {}", RESOURCE_LOCATION);
} else {
try {
prop.load(is);
} catch (Exception e) {
log.warn(String.format("Failed to load resource %s", RESOURCE_LOCATION), e);
}
}
}
Semver agentVersion = Semver.fromString(prop.getProperty(AGENT_VERSION_KEY));
Semver serverMin = Semver.fromString(prop.getProperty(MIN_VERSION_KEY));
Expand Down Expand Up @@ -111,6 +122,9 @@ public Semver(int major, int minor, int patch) {
}

public static Semver fromString(String in) {
if (StringUtils.isBlank(in)) {
return UNKNOWN;
}
try {
Matcher m = VERSION_PATTERN.matcher(in);
if (!m.matches()) {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/io/cryostat/agent/util/ResourcesUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@

import java.io.InputStream;

import io.cryostat.agent.Agent;

public class ResourcesUtil {

private ResourcesUtil() {}

public static ClassLoader getClassLoader() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
return getClassLoader(Agent.class);
}

public static ClassLoader getClassLoader(Class<?> klazz) {
ClassLoader cl = klazz.getClassLoader();
if (cl == null) {
cl = Thread.currentThread().getContextClassLoader();
}
if (cl == null) {
cl = ClassLoader.getSystemClassLoader();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/microprofile-config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cryostat.agent.app.name=cryostat-agent
cryostat.agent.baseuri-range=dns_local
cryostat.agent.baseuri=

cryostat.agent.config.loadable=true

cryostat.agent.api.writes-enabled=false

cryostat.agent.webserver.host=0.0.0.0
Expand Down
Loading