Skip to content

Commit 7278252

Browse files
authored
[grid] Dynamic Grid standalone support passing basic auth credential (#17072)
Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>
1 parent 2d5fab8 commit 7278252

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.openqa.selenium.RetrySessionRequestException;
5252
import org.openqa.selenium.SessionNotCreatedException;
5353
import org.openqa.selenium.TimeoutException;
54+
import org.openqa.selenium.UsernameAndPassword;
5455
import org.openqa.selenium.WebDriverException;
5556
import org.openqa.selenium.docker.Container;
5657
import org.openqa.selenium.docker.ContainerConfig;
@@ -186,6 +187,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
186187
URL remoteAddress = getUrl(port, containerIp);
187188
ClientConfig clientConfig =
188189
ClientConfig.defaultConfig().baseUrl(remoteAddress).readTimeout(sessionTimeout);
190+
clientConfig = applyBasicAuth(clientConfig);
189191
HttpClient client = clientFactory.createClient(clientConfig);
190192

191193
attributeMap.put("docker.browser.image", browserImage.toString());
@@ -520,10 +522,28 @@ private void waitForServerToStart(HttpClient client, Duration duration) {
520522
obj -> {
521523
HttpResponse response = client.execute(new HttpRequest(GET, "/status"));
522524
LOG.fine(string(response));
525+
if (401 == response.getStatus()) {
526+
LOG.warning(
527+
"Server requires basic authentication. "
528+
+ "Set SE_ROUTER_USERNAME and SE_ROUTER_PASSWORD environment variables "
529+
+ "to provide credentials.");
530+
}
523531
return 200 == response.getStatus();
524532
});
525533
}
526534

535+
private ClientConfig applyBasicAuth(ClientConfig clientConfig) {
536+
String routerUsername = System.getenv("SE_ROUTER_USERNAME");
537+
String routerPassword = System.getenv("SE_ROUTER_PASSWORD");
538+
if (routerUsername != null
539+
&& !routerUsername.isEmpty()
540+
&& routerPassword != null
541+
&& !routerPassword.isEmpty()) {
542+
return clientConfig.authenticateAs(new UsernameAndPassword(routerUsername, routerPassword));
543+
}
544+
return clientConfig;
545+
}
546+
527547
private URL getUrl(int port, String containerIp) {
528548
try {
529549
String host = "localhost";

0 commit comments

Comments
 (0)