Skip to content

Commit

Permalink
extract SslConfiguration variables
Browse files Browse the repository at this point in the history
extract subprocess report generation variable

extract platform configuration variables

extract max WS connections variable

extract CORS origin variable

extract DISABLE_SSL variable

extract webserver configuration variables

use Variables

extract JMX connection config variables

finish extracting variables

fix missed import

fix extracted variables in tests
  • Loading branch information
andrewazores committed Dec 8, 2021
1 parent add2644 commit aabfdcd
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 59 deletions.
3 changes: 2 additions & 1 deletion src/main/java/io/cryostat/MainModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import javax.management.remote.JMXServiceURL;

import io.cryostat.configuration.ConfigurationModule;
import io.cryostat.configuration.Variables;
import io.cryostat.core.log.Logger;
import io.cryostat.core.sys.Environment;
import io.cryostat.core.tui.ClientWriter;
Expand Down Expand Up @@ -144,7 +145,7 @@ JMXServiceURL.class, new GsonJmxServiceUrlAdapter(logger))
@Singleton
@Named(RECORDINGS_PATH)
static Path provideSavedRecordingsPath(Logger logger, Environment env) {
String archivePath = env.getEnv("CRYOSTAT_ARCHIVE_PATH", "/flightrecordings");
String archivePath = env.getEnv(Variables.ARCHIVE_PATH, "/flightrecordings");
logger.info("Local save path for flight recordings set as {}", archivePath);
return Paths.get(archivePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public abstract class ConfigurationModule {
@Singleton
@Named(CONFIGURATION_PATH)
static Path provideConfigurationPath(Logger logger, Environment env) {
String path = env.getEnv("CRYOSTAT_CONFIG_PATH", "/opt/cryostat.d/conf.d");
String path = env.getEnv(Variables.CONFIG_PATH, "/opt/cryostat.d/conf.d");
logger.info(String.format("Local config path set as %s", path));
return Paths.get(path);
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/io/cryostat/configuration/Variables.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,39 @@
public final class Variables {
private Variables() {}

// jfr-datasource, cryostat-grafana-dashboard
public static final String GRAFANA_DATASOURCE_ENV = "GRAFANA_DATASOURCE_URL";
public static final String GRAFANA_DASHBOARD_ENV = "GRAFANA_DASHBOARD_URL";

// report generation
public static final String REPORT_GENERATOR_ENV = "CRYOSTAT_REPORT_GENERATOR";
public static final String SUBPROCESS_MAX_HEAP_ENV = "CRYOSTAT_REPORT_GENERATION_MAX_HEAP";

// SSL configuration
public static final String DISABLE_SSL = "CRYOSTAT_DISABLE_SSL";
public static final String KEYSTORE_PATH_ENV = "KEYSTORE_PATH";
public static final String KEYSTORE_PASS_ENV = "KEYSTORE_PASS";
public static final String KEY_PATH_ENV = "KEY_PATH";
public static final String CERT_PATH_ENV = "CERT_PATH";

// platform configuration
public static final String PLATFORM_STRATEGY_ENV_VAR = "CRYOSTAT_PLATFORM";
public static final String AUTH_MANAGER_ENV_VAR = "CRYOSTAT_AUTH_MANAGER";

// webserver configuration
public static final String WEBSERVER_HOST = "CRYOSTAT_WEB_HOST";
public static final String WEBSERVER_PORT = "CRYOSTAT_WEB_PORT";
public static final String WEBSERVER_PORT_EXT = "CRYOSTAT_EXT_WEB_PORT";
public static final String WEBSERVER_SSL_PROXIED = "CRYOSTAT_SSL_PROXIED";
public static final String WEBSERVER_ALLOW_UNTRUSTED_SSL = "CRYOSTAT_ALLOW_UNTRUSTED_SSL";
public static final String MAX_CONNECTIONS_ENV_VAR = "CRYOSTAT_MAX_WS_CONNECTIONS";
public static final String ENABLE_CORS_ENV = "CRYOSTAT_CORS_ORIGIN";

// JMX connections configuration
public static final String TARGET_CACHE_SIZE = "CRYOSTAT_TARGET_CACHE_SIZE";
public static final String TARGET_CACHE_TTL = "CRYOSTAT_TARGET_CACHE_TTL";

// paths configuration
public static final String ARCHIVE_PATH = "CRYOSTAT_ARCHIVE_PATH";
public static final String CONFIG_PATH = "CRYOSTAT_CONFIG_PATH";
}
5 changes: 2 additions & 3 deletions src/main/java/io/cryostat/messaging/MessagingModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import javax.inject.Named;
import javax.inject.Singleton;

import io.cryostat.configuration.Variables;
import io.cryostat.core.log.Logger;
import io.cryostat.core.sys.Clock;
import io.cryostat.core.sys.Environment;
Expand All @@ -63,8 +64,6 @@ public abstract class MessagingModule {

static final String WS_MAX_CONNECTIONS = "WS_MAX_CONNECTIONS";
static final String LIMBO_PRUNER = "LIMBO_PRUNER";

static final String MAX_CONNECTIONS_ENV_VAR = "CRYOSTAT_MAX_WS_CONNECTIONS";
static final int MIN_CONNECTIONS = 1;
static final int MAX_CONNECTIONS = 64;
static final int DEFAULT_MAX_CONNECTIONS = 2;
Expand Down Expand Up @@ -100,7 +99,7 @@ static int provideWebSocketMaxConnections(Environment env, Logger logger) {
int maxConn =
Integer.parseInt(
env.getEnv(
MAX_CONNECTIONS_ENV_VAR,
Variables.MAX_CONNECTIONS_ENV_VAR,
String.valueOf(DEFAULT_MAX_CONNECTIONS)));
if (maxConn > MAX_CONNECTIONS) {
logger.info("Requested maximum WebSocket connections {} is too large.", maxConn);
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/io/cryostat/net/NetworkConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.net.SocketException;
import java.net.UnknownHostException;

import io.cryostat.configuration.Variables;
import io.cryostat.core.sys.Environment;

public class NetworkConfiguration {
Expand All @@ -53,7 +54,7 @@ public class NetworkConfiguration {
}

public String getWebServerHost() throws SocketException, UnknownHostException {
return env.getEnv("CRYOSTAT_WEB_HOST", resolver.getHostAddress());
return env.getEnv(Variables.WEBSERVER_HOST, resolver.getHostAddress());
}

public int getDefaultWebServerPort() {
Expand All @@ -62,19 +63,20 @@ public int getDefaultWebServerPort() {

public int getInternalWebServerPort() {
return Integer.parseInt(
env.getEnv("CRYOSTAT_WEB_PORT", String.valueOf(getDefaultWebServerPort())));
env.getEnv(Variables.WEBSERVER_PORT, String.valueOf(getDefaultWebServerPort())));
}

public int getExternalWebServerPort() {
return Integer.parseInt(
env.getEnv("CRYOSTAT_EXT_WEB_PORT", String.valueOf(getInternalWebServerPort())));
env.getEnv(
Variables.WEBSERVER_PORT_EXT, String.valueOf(getInternalWebServerPort())));
}

public boolean isSslProxied() {
return env.hasEnv("CRYOSTAT_SSL_PROXIED");
return env.hasEnv(Variables.WEBSERVER_SSL_PROXIED);
}

public boolean isUntrustedSslAllowed() {
return env.hasEnv("CRYOSTAT_ALLOW_UNTRUSTED_SSL");
return env.hasEnv(Variables.WEBSERVER_ALLOW_UNTRUSTED_SSL);
}
}
16 changes: 7 additions & 9 deletions src/main/java/io/cryostat/net/NetworkModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import javax.inject.Singleton;

import io.cryostat.configuration.ConfigurationModule;
import io.cryostat.configuration.Variables;
import io.cryostat.core.log.Logger;
import io.cryostat.core.net.JFRConnectionToolkit;
import io.cryostat.core.sys.Environment;
Expand Down Expand Up @@ -77,9 +78,6 @@
})
public abstract class NetworkModule {

static final String TARGET_CACHE_SIZE = "CRYOSTAT_TARGET_CACHE_SIZE";
static final String TARGET_CACHE_TTL = "CRYOSTAT_TARGET_CACHE_TTL";

@Provides
@Singleton
static HttpServer provideHttpServer(
Expand All @@ -101,24 +99,24 @@ static NetworkResolver provideNetworkResolver() {
}

@Provides
@Named(TARGET_CACHE_SIZE)
@Named(Variables.TARGET_CACHE_SIZE)
static int provideMaxTargetConnections(Environment env) {
return Integer.parseInt(env.getEnv(TARGET_CACHE_SIZE, "-1"));
return Integer.parseInt(env.getEnv(Variables.TARGET_CACHE_SIZE, "-1"));
}

@Provides
@Named(TARGET_CACHE_TTL)
@Named(Variables.TARGET_CACHE_TTL)
static Duration provideMaxTargetTTL(Environment env) {
return Duration.ofSeconds(Integer.parseInt(env.getEnv(TARGET_CACHE_TTL, "10")));
return Duration.ofSeconds(Integer.parseInt(env.getEnv(Variables.TARGET_CACHE_TTL, "10")));
}

@Provides
@Singleton
static TargetConnectionManager provideTargetConnectionManager(
Lazy<JFRConnectionToolkit> connectionToolkit,
PlatformClient platformClient,
@Named(TARGET_CACHE_TTL) Duration maxTargetTtl,
@Named(TARGET_CACHE_SIZE) int maxTargetConnections,
@Named(Variables.TARGET_CACHE_TTL) Duration maxTargetTtl,
@Named(Variables.TARGET_CACHE_SIZE) int maxTargetConnections,
Logger logger) {
return new TargetConnectionManager(
connectionToolkit,
Expand Down
24 changes: 10 additions & 14 deletions src/main/java/io/cryostat/net/SslConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import java.nio.file.Path;

import io.cryostat.configuration.Variables;
import io.cryostat.core.log.Logger;
import io.cryostat.core.sys.Environment;
import io.cryostat.core.sys.FileSystem;
Expand All @@ -56,18 +57,13 @@ public class SslConfiguration {

private final SslConfigurationStrategy strategy;

private static final String KEYSTORE_PATH_ENV = "KEYSTORE_PATH";
private static final String KEYSTORE_PASS_ENV = "KEYSTORE_PASS";
private static final String KEY_PATH_ENV = "KEY_PATH";
private static final String CERT_PATH_ENV = "CERT_PATH";

SslConfiguration(Environment env, FileSystem fs, Logger logger)
throws SslConfigurationException {
this.env = env;
this.fs = fs;
this.logger = logger;

if (env.hasEnv("CRYOSTAT_DISABLE_SSL")) {
if (env.hasEnv(Variables.DISABLE_SSL)) {
strategy = new NoSslStrategy();
logger.info("Selected NoSSL strategy");
return;
Expand All @@ -76,7 +72,7 @@ public class SslConfiguration {
{
Path path = obtainKeyStorePathIfSpecified();
if (path != null) {
strategy = new KeyStoreStrategy(path, env.getEnv(KEYSTORE_PASS_ENV, ""));
strategy = new KeyStoreStrategy(path, env.getEnv(Variables.KEYSTORE_PASS_ENV, ""));
logger.info("Selected SSL KeyStore strategy with keystore {}", path.toString());
return;
}
Expand All @@ -99,7 +95,7 @@ public class SslConfiguration {
{
Path path = discoverKeyStorePathInDefaultLocations();
if (path != null) {
strategy = new KeyStoreStrategy(path, env.getEnv(KEYSTORE_PASS_ENV, ""));
strategy = new KeyStoreStrategy(path, env.getEnv(Variables.KEYSTORE_PASS_ENV, ""));
logger.info("Selected SSL KeyStore strategy in default location");
return;
}
Expand Down Expand Up @@ -128,11 +124,11 @@ public class SslConfiguration {
}

Path obtainKeyStorePathIfSpecified() throws SslConfigurationException {
if (!env.hasEnv(KEYSTORE_PATH_ENV)) {
if (!env.hasEnv(Variables.KEYSTORE_PATH_ENV)) {
return null;
}

Path path = fs.pathOf(env.getEnv(KEYSTORE_PATH_ENV)).normalize();
Path path = fs.pathOf(env.getEnv(Variables.KEYSTORE_PATH_ENV)).normalize();
if (!fs.exists(path)) {
throw new SslConfigurationException(
String.format(
Expand All @@ -144,16 +140,16 @@ Path obtainKeyStorePathIfSpecified() throws SslConfigurationException {
}

Pair<Path, Path> obtainKeyCertPathPairIfSpecified() throws SslConfigurationException {
if (!env.hasEnv(KEY_PATH_ENV) && !env.hasEnv(CERT_PATH_ENV)) {
if (!env.hasEnv(Variables.KEY_PATH_ENV) && !env.hasEnv(Variables.CERT_PATH_ENV)) {
return null;
}

if (env.hasEnv(KEY_PATH_ENV) ^ env.hasEnv(CERT_PATH_ENV)) {
if (env.hasEnv(Variables.KEY_PATH_ENV) ^ env.hasEnv(Variables.CERT_PATH_ENV)) {
throw new SslConfigurationException("both KEY_PATH and CERT_PATH must be specified");
}

Path key = fs.pathOf(env.getEnv(KEY_PATH_ENV)).normalize();
Path cert = fs.pathOf(env.getEnv(CERT_PATH_ENV)).normalize();
Path key = fs.pathOf(env.getEnv(Variables.KEY_PATH_ENV)).normalize();
Path cert = fs.pathOf(env.getEnv(Variables.CERT_PATH_ENV)).normalize();
if (!fs.exists(key)) {
throw new SslConfigurationException(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import javax.inject.Provider;

import io.cryostat.configuration.Variables;
import io.cryostat.core.log.Logger;
import io.cryostat.core.sys.Environment;
import io.cryostat.core.sys.FileSystem;
Expand Down Expand Up @@ -78,7 +79,7 @@ class RemoteReportGenerator extends AbstractReportGeneratorService {
@Override
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
public CompletableFuture<Path> exec(Path recording, Path destination) {
String reportGenerator = env.getEnv("CRYOSTAT_REPORT_GENERATOR");
String reportGenerator = env.getEnv(Variables.REPORT_GENERATOR_ENV);
logger.info("POSTing {} to {}", recording, reportGenerator);
var form =
MultipartForm.create()
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/cryostat/net/reports/ReportsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import javax.inject.Provider;
import javax.inject.Singleton;

import io.cryostat.configuration.Variables;
import io.cryostat.core.log.Logger;
import io.cryostat.core.reports.ReportTransformer;
import io.cryostat.core.sys.Environment;
Expand Down Expand Up @@ -96,7 +97,7 @@ static ReportGeneratorService provideReportGeneratorService(
Environment env,
RemoteReportGenerator remoteGenerator,
SubprocessReportGenerator subprocessGenerator) {
if (env.hasEnv("CRYOSTAT_REPORT_GENERATOR")) {
if (env.hasEnv(Variables.REPORT_GENERATOR_ENV)) {
return remoteGenerator;
}
return subprocessGenerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

import org.openjdk.jmc.rjmx.ConnectionException;

import io.cryostat.configuration.Variables;
import io.cryostat.core.CryostatCore;
import io.cryostat.core.log.Logger;
import io.cryostat.core.reports.ReportGenerator;
Expand All @@ -70,8 +71,6 @@

public class SubprocessReportGenerator extends AbstractReportGeneratorService {

static final String SUBPROCESS_MAX_HEAP_ENV = "CRYOSTAT_REPORT_GENERATION_MAX_HEAP";

private final Environment env;
private final Set<ReportTransformer> reportTransformers;
private final Provider<JavaProcess.Builder> javaProcessBuilderProvider;
Expand Down Expand Up @@ -117,7 +116,9 @@ public synchronized CompletableFuture<Path> exec(Path recording, Path saveFile)
// See https://github.com/cryostatio/cryostat/issues/287
.jvmArgs(
createJvmArgs(
Integer.parseInt(env.getEnv(SUBPROCESS_MAX_HEAP_ENV, "0"))))
Integer.parseInt(
env.getEnv(
Variables.SUBPROCESS_MAX_HEAP_ENV, "0"))))
.processArgs(createProcessArgs(recording, saveFile));
return CompletableFuture.supplyAsync(
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import javax.inject.Inject;

import io.cryostat.configuration.Variables;
import io.cryostat.core.sys.Environment;
import io.cryostat.net.security.ResourceAction;
import io.cryostat.net.web.WebServer;
Expand All @@ -54,7 +55,6 @@

class CorsEnablingHandler implements RequestHandler {
protected static final String DEV_ORIGIN = "http://localhost:9000";
protected static final String ENABLE_CORS_ENV = "CRYOSTAT_CORS_ORIGIN";
protected final CorsHandler corsHandler;
protected final Environment env;

Expand Down Expand Up @@ -88,7 +88,7 @@ public int getPriority() {

@Override
public boolean isAvailable() {
return this.env.hasEnv(ENABLE_CORS_ENV);
return this.env.hasEnv(Variables.ENABLE_CORS_ENV);
}

@Override
Expand All @@ -112,6 +112,6 @@ public void handle(RoutingContext ctx) {
}

String getOrigin() {
return this.env.getEnv(ENABLE_CORS_ENV, DEV_ORIGIN);
return this.env.getEnv(Variables.ENABLE_CORS_ENV, DEV_ORIGIN);
}
}
12 changes: 5 additions & 7 deletions src/main/java/io/cryostat/platform/PlatformModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import javax.inject.Singleton;

import io.cryostat.configuration.ConfigurationModule;
import io.cryostat.configuration.Variables;
import io.cryostat.core.log.Logger;
import io.cryostat.core.net.discovery.JvmDiscoveryClient;
import io.cryostat.core.sys.Environment;
Expand All @@ -64,9 +65,6 @@
@Module(includes = {PlatformStrategyModule.class, PlatformDiscoveryModule.class})
public abstract class PlatformModule {

static final String PLATFORM_STRATEGY_ENV_VAR = "CRYOSTAT_PLATFORM";
static final String AUTH_MANAGER_ENV_VAR = "CRYOSTAT_AUTH_MANAGER";

@Provides
@Singleton
static PlatformClient providePlatformClient(
Expand Down Expand Up @@ -96,8 +94,8 @@ static AuthManager provideAuthManager(
Set<AuthManager> authManagers,
Logger logger) {
final String authManagerClass;
if (env.hasEnv(AUTH_MANAGER_ENV_VAR)) {
authManagerClass = env.getEnv(AUTH_MANAGER_ENV_VAR);
if (env.hasEnv(Variables.AUTH_MANAGER_ENV_VAR)) {
authManagerClass = env.getEnv(Variables.AUTH_MANAGER_ENV_VAR);
logger.info("Selecting configured AuthManager \"{}\"", authManagerClass);
} else {
authManagerClass = platformStrategy.getAuthManager().getClass().getCanonicalName();
Expand All @@ -119,8 +117,8 @@ static AuthManager provideAuthManager(
static PlatformDetectionStrategy<?> providePlatformStrategy(
Logger logger, Set<PlatformDetectionStrategy<?>> strategies, Environment env) {
PlatformDetectionStrategy<?> strat = null;
if (env.hasEnv(PLATFORM_STRATEGY_ENV_VAR)) {
String platform = env.getEnv(PLATFORM_STRATEGY_ENV_VAR);
if (env.hasEnv(Variables.PLATFORM_STRATEGY_ENV_VAR)) {
String platform = env.getEnv(Variables.PLATFORM_STRATEGY_ENV_VAR);
logger.info("Selecting configured PlatformDetectionStrategy \"{}\"", platform);
for (PlatformDetectionStrategy<?> s : strategies) {
if (Objects.equals(platform, s.getClass().getCanonicalName())) {
Expand Down
Loading

0 comments on commit aabfdcd

Please sign in to comment.