Secure selenium gird with basic authentication and ssl verification
Posted in:
Uncategorized
Step 1: Create docker-compose with caddy basic auth
# Repo: https://github.com/SeleniumHQ/docker-selenium
# To execute this docker-compose yml file use `docker-compose -f docker-compose-v3.yml up`
# Add the `-d` flag at the end for detached execution
# To stop the execution, hit Ctrl+C, and then `docker-compose -f docker-compose-v3.yml down`
version: "3"
services:
chrome:
image: selenium/node-chrome:4.6.0-20221104
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
restart: unless-stopped
edge:
image: selenium/node-edge:4.6.0-20221104
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
restart: unless-stopped
firefox:
image: selenium/node-firefox:4.6.0-20221104
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
restart: unless-stopped
selenium-hub:
image: selenium/hub:4.6.0-20221104
container_name: selenium-hub
expose:
- 4442
- 4443
- 4444
restart: unless-stopped
caddy:
image: caddy:2.6.2
ports:
- "4444:4444"
volumes:
- ./caddy:/etc/caddy
environment:
- ADMIN_USER=${ADMIN_USER:-admin}
- ADMIN_PASSWORD=${ADMIN_PASSWORD:-admin}
- ADMIN_PASSWORD_HASH=${ADMIN_PASSWORD_HASH:-$2a$14$1l.IozJx7xQRVmlkEQ32OeEEfP5mRxTpbDTCTcXRqn19gXD8YK1pO}
restart: unless-stopped
Step 2: Config caddy
Add caddy forder to root folder. Create file caddy/Caddyfile with below content
:4444 {
# Uncomment below if you want to use ssl
# tls /etc/caddy/sel.devculi.crt /etc/caddy/sel.devculi.key
basicauth /* {
{$ADMIN_USER} {$ADMIN_PASSWORD_HASH}
}
reverse_proxy selenium-hub:4444
}
Create .env file to customize ADMIN_USER, ADMIN_PASSWORD, ADMIN_PASSWORD_HASH
ADMIN_USER=admin ADMIN_PASSWORD=admin # CHANGE THIS ADMIN_PASSWORD_HASH=$2a$14$1l.IozJx7xQRVmlkEQ32OeEEfP5mRxTpbDTCTcXRqn19gXD8YK1pO # CHANGE THIS
Note: To hash your own password, use this commandline
docker run --rm caddy caddy hash-password --plaintext 'pwd'
Step 3: Test your grid
Java code
public static void main(String[] args) throws MalformedURLException {
String baseURL = "https://google.com/";
String nodeURL = "https://admin:[email protected]:4444"; // TODO: change to your domain (with http or https)
ChromeOptions options = new ChromeOptions();
WebDriver browser = new RemoteWebDriver(new URL(nodeURL), options);
try {
browser.get(baseURL);
System.out.println(browser.getPageSource());
} catch (Exception e) {
e.printStackTrace();
} finally {
browser.quit();
}
}
Now we have a secure selenium grid
Note: For development, you can generate ssl certificate with mkcert