Skip to content

Commit

Permalink
[java] add ability to disabled UI on grid (#13212)
Browse files Browse the repository at this point in the history
* add stuff to disabled UI

* [java] Picking up disabling UI work

---------

Co-authored-by: Puja Jagani <[email protected]>
Co-authored-by: Diego Molina <[email protected]>
Co-authored-by: Diego Molina <[email protected]>
  • Loading branch information
4 people authored Dec 11, 2023
1 parent 602d015 commit 0720bbd
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 43 deletions.
15 changes: 12 additions & 3 deletions java/src/org/openqa/selenium/grid/commands/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ protected Handlers createHandlers(Config config) {

Routable routerWithSpecChecks = router.with(networkOptions.getSpecComplianceChecks());

String subPath = new RouterOptions(config).subPath();
Routable ui = new GridUiRoute(subPath);
RouterOptions routerOptions = new RouterOptions(config);
String subPath = routerOptions.subPath();

Routable appendRoute =
Stream.of(
Expand All @@ -192,10 +192,19 @@ protected Handlers createHandlers(Config config) {
graphqlRoute(subPath, () -> graphqlHandler))
.reduce(Route::combine)
.get();

if (!subPath.isEmpty()) {
appendRoute = Route.combine(appendRoute, baseRoute(subPath, combine(routerWithSpecChecks)));
}
Routable httpHandler = combine(ui, appendRoute);

Routable httpHandler;
if (routerOptions.disableUi()) {
LOG.info("Grid UI has been disabled.");
httpHandler = appendRoute;
} else {
Routable ui = new GridUiRoute(subPath);
httpHandler = combine(ui, appendRoute);
}

UsernameAndPassword uap = secretOptions.getServerAuthentication();
if (uap != null) {
Expand Down
72 changes: 42 additions & 30 deletions java/src/org/openqa/selenium/grid/commands/Standalone.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ protected Handlers createHandlers(Config config) {
new GraphqlHandler(
tracer, distributor, queue, serverOptions.getExternalUri(), getFormattedVersion());

String subPath = new RouterOptions(config).subPath();
Routable ui = new GridUiRoute(subPath);
RouterOptions routerOptions = new RouterOptions(config);
String subPath = routerOptions.subPath();

Routable appendRoute =
Stream.of(
Expand All @@ -202,7 +202,14 @@ protected Handlers createHandlers(Config config) {
appendRoute = Route.combine(appendRoute, baseRoute(subPath, combine(router)));
}

Routable httpHandler = combine(ui, appendRoute);
Routable httpHandler;
if (routerOptions.disableUi()) {
LOG.info("Grid UI has been disabled.");
httpHandler = appendRoute;
} else {
Routable ui = new GridUiRoute(subPath);
httpHandler = combine(ui, appendRoute);
}

UsernameAndPassword uap = secretOptions.getServerAuthentication();
if (uap != null) {
Expand All @@ -213,33 +220,7 @@ protected Handlers createHandlers(Config config) {
// Allow the liveness endpoint to be reached, since k8s doesn't make it easy to authenticate
// these checks
httpHandler = combine(httpHandler, Route.get("/readyz").to(() -> readinessCheck));

Node node = new NodeOptions(config).getNode();
combinedHandler.addHandler(node);
distributor.add(node);

bus.addListener(
NodeDrainComplete.listener(
nodeId -> {
if (!node.getId().equals(nodeId)) {
return;
}

// Wait a beat before shutting down so the final response from the
// node can escape.
new Thread(
() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Swallow, the next thing we're doing is shutting down
}
LOG.info("Shutting down");
System.exit(0);
},
"Standalone shutdown: " + nodeId)
.start();
}));
Node node = createNode(config, bus, distributor, combinedHandler);

return new Handlers(httpHandler, new ProxyNodeWebsockets(clientFactory, node));
}
Expand Down Expand Up @@ -270,4 +251,35 @@ private String getFormattedVersion() {
BuildInfo info = new BuildInfo();
return String.format("%s (revision %s)", info.getReleaseLabel(), info.getBuildRevision());
}

private Node createNode(
Config config, EventBus bus, Distributor distributor, CombinedHandler combinedHandler) {
Node node = new NodeOptions(config).getNode();
combinedHandler.addHandler(node);
distributor.add(node);

bus.addListener(
NodeDrainComplete.listener(
nodeId -> {
if (!node.getId().equals(nodeId)) {
return;
}

// Wait a beat before shutting down so the final response from the
// node can escape.
new Thread(
() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Swallow, the next thing we're doing is shutting down
}
LOG.info("Shutting down");
System.exit(0);
},
"Standalone shutdown: " + nodeId)
.start();
}));
return node;
}
}
18 changes: 13 additions & 5 deletions java/src/org/openqa/selenium/grid/router/httpd/RouterFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
package org.openqa.selenium.grid.router.httpd;

import static org.openqa.selenium.grid.config.StandardGridRoles.ROUTER_ROLE;
import static org.openqa.selenium.grid.router.httpd.RouterOptions.NETWORK;
import static org.openqa.selenium.grid.router.httpd.RouterOptions.NETWORK_SECTION;
import static org.openqa.selenium.grid.router.httpd.RouterOptions.ROUTER_SECTION;

import com.beust.jcommander.Parameter;
import com.google.auto.service.AutoService;
Expand All @@ -38,23 +39,23 @@ public class RouterFlags implements HasRoles {
"Relax checks on origin header and content type of incoming requests,"
+ " in contravention of strict W3C spec compliance.",
arity = 1)
@ConfigValue(section = "network", name = "relax-checks", example = "true")
@ConfigValue(section = NETWORK_SECTION, name = "relax-checks", example = "true")
private Boolean relaxChecks = false;

@Parameter(
names = "--username",
description =
"User name clients must use to connect to the server. "
+ "Both this and password need to be set in order to be used.")
@ConfigValue(section = "router", name = "username", example = "admin")
@ConfigValue(section = ROUTER_SECTION, name = "username", example = "admin")
private String username;

@Parameter(
names = "--password",
description =
"Password clients must use to connect to the server. "
+ "Both this and the username need to be set in order to be used.")
@ConfigValue(section = "router", name = "password", example = "hunter2")
@ConfigValue(section = ROUTER_SECTION, name = "password", example = "hunter2")
private String password;

@Parameter(
Expand All @@ -63,9 +64,16 @@ public class RouterFlags implements HasRoles {
description =
"A sub-path that should be considered for all user facing routes on the"
+ " Hub/Router/Standalone")
@ConfigValue(section = NETWORK, name = "sub-path", example = "my_company/selenium_grid")
@ConfigValue(section = NETWORK_SECTION, name = "sub-path", example = "my_company/selenium_grid")
public String subPath;

@Parameter(
names = {"--disable-ui"},
arity = 1,
description = "Disable the Grid UI")
@ConfigValue(section = ROUTER_SECTION, name = "disable-ui", example = "true")
public boolean disableUi = false;

@Override
public Set<Role> getRoles() {
return Collections.singleton(ROUTER_ROLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

public class RouterOptions {

static final String NETWORK = "network";
static final String NETWORK_SECTION = "network";
static final String ROUTER_SECTION = "router";

private final Config config;

Expand All @@ -31,7 +32,7 @@ public RouterOptions(Config config) {

public String subPath() {
return config
.get(NETWORK, "sub-path")
.get(NETWORK_SECTION, "sub-path")
.map(
prefix -> {
prefix = prefix.trim();
Expand All @@ -46,4 +47,8 @@ public String subPath() {
})
.orElse("");
}

public boolean disableUi() {
return config.get(ROUTER_SECTION, "disable-ui").map(Boolean::parseBoolean).orElse(false);
}
}
16 changes: 13 additions & 3 deletions java/src/org/openqa/selenium/grid/router/httpd/RouterServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ protected Handlers createHandlers(Config config) {
new GraphqlHandler(
tracer, distributor, queue, serverOptions.getExternalUri(), getServerVersion());

String subPath = new RouterOptions(config).subPath();
Routable ui = new GridUiRoute(subPath);
RouterOptions routerOptions = new RouterOptions(config);
String subPath = routerOptions.subPath();

Router router = new Router(tracer, clientFactory, sessions, queue, distributor);
Routable routerWithSpecChecks = router.with(networkOptions.getSpecComplianceChecks());

Expand All @@ -153,10 +154,19 @@ protected Handlers createHandlers(Config config) {
graphqlRoute(subPath, () -> graphqlHandler))
.reduce(Route::combine)
.get();

if (!subPath.isEmpty()) {
appendRoute = Route.combine(appendRoute, baseRoute(subPath, combine(routerWithSpecChecks)));
}
Routable route = Route.combine(ui, appendRoute);

Routable route;
if (routerOptions.disableUi()) {
LOG.info("Grid UI has been disabled.");
route = appendRoute;
} else {
Routable ui = new GridUiRoute(subPath);
route = combine(ui, appendRoute);
}

UsernameAndPassword uap = secretOptions.getServerAuthentication();
if (uap != null) {
Expand Down

0 comments on commit 0720bbd

Please sign in to comment.