From 0720bbdfb677b3923d7ec34f52c5a2eafaf7b637 Mon Sep 17 00:00:00 2001 From: Oscar Devora <100381276+RevealOscar@users.noreply.github.com> Date: Mon, 11 Dec 2023 07:16:52 -0600 Subject: [PATCH] [java] add ability to disabled UI on grid (#13212) * add stuff to disabled UI * [java] Picking up disabling UI work --------- Co-authored-by: Puja Jagani Co-authored-by: Diego Molina Co-authored-by: Diego Molina --- .../openqa/selenium/grid/commands/Hub.java | 15 +++- .../selenium/grid/commands/Standalone.java | 72 +++++++++++-------- .../grid/router/httpd/RouterFlags.java | 18 +++-- .../grid/router/httpd/RouterOptions.java | 9 ++- .../grid/router/httpd/RouterServer.java | 16 ++++- 5 files changed, 87 insertions(+), 43 deletions(-) diff --git a/java/src/org/openqa/selenium/grid/commands/Hub.java b/java/src/org/openqa/selenium/grid/commands/Hub.java index 4dc04beb46d65..21a97f3c9ad79 100644 --- a/java/src/org/openqa/selenium/grid/commands/Hub.java +++ b/java/src/org/openqa/selenium/grid/commands/Hub.java @@ -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( @@ -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) { diff --git a/java/src/org/openqa/selenium/grid/commands/Standalone.java b/java/src/org/openqa/selenium/grid/commands/Standalone.java index 8ca8f667d21bc..ec3c0080d5262 100644 --- a/java/src/org/openqa/selenium/grid/commands/Standalone.java +++ b/java/src/org/openqa/selenium/grid/commands/Standalone.java @@ -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( @@ -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) { @@ -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)); } @@ -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; + } } diff --git a/java/src/org/openqa/selenium/grid/router/httpd/RouterFlags.java b/java/src/org/openqa/selenium/grid/router/httpd/RouterFlags.java index 77d85fe0e694e..3960c59d00d31 100644 --- a/java/src/org/openqa/selenium/grid/router/httpd/RouterFlags.java +++ b/java/src/org/openqa/selenium/grid/router/httpd/RouterFlags.java @@ -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; @@ -38,7 +39,7 @@ 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( @@ -46,7 +47,7 @@ public class RouterFlags implements HasRoles { 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( @@ -54,7 +55,7 @@ public class RouterFlags implements HasRoles { 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( @@ -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 getRoles() { return Collections.singleton(ROUTER_ROLE); diff --git a/java/src/org/openqa/selenium/grid/router/httpd/RouterOptions.java b/java/src/org/openqa/selenium/grid/router/httpd/RouterOptions.java index 381ce5124da56..47d2e902a107a 100644 --- a/java/src/org/openqa/selenium/grid/router/httpd/RouterOptions.java +++ b/java/src/org/openqa/selenium/grid/router/httpd/RouterOptions.java @@ -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; @@ -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(); @@ -46,4 +47,8 @@ public String subPath() { }) .orElse(""); } + + public boolean disableUi() { + return config.get(ROUTER_SECTION, "disable-ui").map(Boolean::parseBoolean).orElse(false); + } } diff --git a/java/src/org/openqa/selenium/grid/router/httpd/RouterServer.java b/java/src/org/openqa/selenium/grid/router/httpd/RouterServer.java index 908f630d0b5d8..e4d3c831da059 100644 --- a/java/src/org/openqa/selenium/grid/router/httpd/RouterServer.java +++ b/java/src/org/openqa/selenium/grid/router/httpd/RouterServer.java @@ -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()); @@ -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) {