Skip to content

Commit

Permalink
[java] Schema HTTPS in Distributor, SessionQueue, SessionMap
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Nguyen Duc <[email protected]>
  • Loading branch information
VietND96 committed Jan 9, 2024
1 parent fffd05c commit f985825
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class DistributorOptions {

public static final int DEFAULT_HEALTHCHECK_INTERVAL = 120;
public static final String DISTRIBUTOR_SECTION = "distributor";
private static final String SERVER_SECTION = "server";
static final String DEFAULT_DISTRIBUTOR_IMPLEMENTATION =
"org.openqa.selenium.grid.distributor.local.LocalDistributor";
static final String DEFAULT_SLOT_MATCHER = "org.openqa.selenium.grid.data.DefaultSlotMatcher";
Expand Down Expand Up @@ -67,6 +68,7 @@ public URI getDistributorUri() {
return host.get();
}

String schema = (isSecure() || isSelfSigned()) ? "https" : "http";
Optional<Integer> port = config.getInt(DISTRIBUTOR_SECTION, "port");
Optional<String> hostname = config.get(DISTRIBUTOR_SECTION, "hostname");

Expand All @@ -75,14 +77,23 @@ public URI getDistributorUri() {
}

try {
return new URI("http", null, hostname.get(), port.get(), null, null, null);
return new URI(schema, null, hostname.get(), port.get(), null, null, null);
} catch (URISyntaxException e) {
throw new ConfigException(
"Distributor uri configured through host (%s) and port (%d) is not a valid URI",
hostname.get(), port.get());
}
}

public boolean isSecure() {
return config.get(SERVER_SECTION, "https-private-key").isPresent()
&& config.get(SERVER_SECTION, "https-certificate").isPresent();
}

public boolean isSelfSigned() {
return config.getBool(SERVER_SECTION, "https-self-signed").orElse(false);
}

public Duration getHealthCheckInterval() {
// If the user sets 0s or less, we default to 10s.
int seconds =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
public class SessionMapOptions {

private static final String SESSIONS_SECTION = "sessions";
private static final String SERVER_SECTION = "server";

private static final String DEFAULT_SESSION_MAP =
"org.openqa.selenium.grid.sessionmap.remote.RemoteSessionMap";
private static final String DEFAULT_SESSION_MAP_SCHEME = "http";
private final Config config;

public SessionMapOptions(Config config) {
Expand All @@ -39,7 +39,10 @@ public SessionMapOptions(Config config) {

public URI getSessionMapUri() {

String scheme = config.get(SESSIONS_SECTION, "scheme").orElse(DEFAULT_SESSION_MAP_SCHEME);
String scheme =
config
.get(SESSIONS_SECTION, "scheme")
.orElse((isSecure() || isSelfSigned()) ? "https" : "http");

Optional<URI> host =
config
Expand Down Expand Up @@ -78,6 +81,15 @@ public URI getSessionMapUri() {
}
}

public boolean isSecure() {
return config.get(SERVER_SECTION, "https-private-key").isPresent()
&& config.get(SERVER_SECTION, "https-certificate").isPresent();
}

public boolean isSelfSigned() {
return config.getBool(SERVER_SECTION, "https-self-signed").orElse(false);
}

public SessionMap getSessionMap() {
return config.getClass(
SESSIONS_SECTION, "implementation", SessionMap.class, DEFAULT_SESSION_MAP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
public class NewSessionQueueOptions {

static final String SESSION_QUEUE_SECTION = "sessionqueue";
private static final String SERVER_SECTION = "server";
static final int DEFAULT_REQUEST_TIMEOUT = 300;
static final int DEFAULT_REQUEST_TIMEOUT_PERIOD = 10;
static final int DEFAULT_RETRY_INTERVAL = 15;
Expand Down Expand Up @@ -70,6 +71,7 @@ public URI getSessionQueueUri() {
return host.get();
}

String schema = (isSecure() || isSelfSigned()) ? "https" : "http";
Optional<Integer> port = config.getInt(SESSION_QUEUE_SECTION, "port");
Optional<String> hostname = config.get(SESSION_QUEUE_SECTION, "hostname");

Expand All @@ -78,14 +80,23 @@ public URI getSessionQueueUri() {
}

try {
return new URI("http", null, hostname.get(), port.get(), "", null, null);
return new URI(schema, null, hostname.get(), port.get(), "", null, null);
} catch (URISyntaxException e) {
throw new ConfigException(
"Session queue server uri configured through host (%s) and port (%d) is not a valid URI",
hostname.get(), port.get());
}
}

public boolean isSecure() {
return config.get(SERVER_SECTION, "https-private-key").isPresent()
&& config.get(SERVER_SECTION, "https-certificate").isPresent();
}

public boolean isSelfSigned() {
return config.getBool(SERVER_SECTION, "https-self-signed").orElse(false);
}

public Duration getSessionRequestTimeout() {
// If the user sets 0 or less, we default to 1s.
int timeout =
Expand Down

0 comments on commit f985825

Please sign in to comment.