Skip to content

Commit

Permalink
[grid] Make registration secret mandatory in the LocalNode
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Oct 20, 2020
1 parent f5aa572 commit b845a3c
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private LocalNode(
this.gridUri = Require.nonNull("Grid URI", gridUri);
this.maxSessionCount = Math.min(Require.positive("Max session count", maxSessionCount), factories.size());
this.factories = ImmutableList.copyOf(factories);
this.registrationSecret = registrationSecret;
this.registrationSecret = Require.nonNull("Registration secret", registrationSecret);

this.healthCheck = healthCheck == null ?
() -> new HealthCheck.Result(
Expand Down Expand Up @@ -491,7 +491,7 @@ public static class Builder {
private Duration sessionTimeout = Duration.ofMinutes(5);
private HealthCheck healthCheck;

public Builder(
private Builder(
Tracer tracer,
EventBus bus,
URI uri,
Expand All @@ -501,7 +501,7 @@ public Builder(
this.bus = Require.nonNull("Event bus", bus);
this.uri = Require.nonNull("Remote node URI", uri);
this.gridUri = Require.nonNull("Grid URI", gridUri);
this.registrationSecret = registrationSecret;
this.registrationSecret = Require.nonNull("Registration secret", registrationSecret);
this.factories = ImmutableList.builder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void stop(SessionId id) throws NoSuchSessionException {
HttpRequest req = new HttpRequest(DELETE, "/se/grid/node/session/" + id);
HttpTracing.inject(tracer, tracer.getCurrentContext(), req);

HttpResponse res = client.execute(req);
HttpResponse res = client.with(addSecret).execute(req);

Values.get(res, Void.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ public void attemptingToStartASessionWhichFailsMarksAsTheSlotAsAvailable() {
handler.addHandler(sessions);

URI uri = createUri();
Node node = LocalNode.builder(tracer, bus, uri, uri, null)
Node node = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(caps, new TestSessionFactory((id, caps) -> {
throw new SessionNotCreatedException("OMG");
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void setUp() throws URISyntaxException {

Capabilities caps = new ImmutableCapabilities("browserName", "cheese");
uri = new URI("http://localhost:1234");
localNode = LocalNode.builder(tracer, bus, uri, uri, null)
localNode = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(caps, new TestSessionFactory((id, c) -> new Handler(c)))
.maximumConcurrentSessions(2)
.build();
Expand Down Expand Up @@ -185,7 +185,7 @@ public HttpResponse execute(HttpRequest req) {
}

// Only use one node.
Node node = LocalNode.builder(tracer, bus, uri, uri, null)
Node node = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(caps, new TestSessionFactory(VerifyingHandler::new))
.add(caps, new TestSessionFactory(VerifyingHandler::new))
.add(caps, new TestSessionFactory(VerifyingHandler::new))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ java_test_suite(
"//java/server/src/org/openqa/selenium/grid/distributor/selector",
"//java/server/src/org/openqa/selenium/grid/node",
"//java/server/src/org/openqa/selenium/grid/node/local",
"//java/server/src/org/openqa/selenium/grid/security",
"//java/server/test/org/openqa/selenium/grid/testing",
artifact("com.google.guava:guava"),
artifact("junit:junit"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openqa.selenium.grid.data.SlotId;
import org.openqa.selenium.grid.node.Node;
import org.openqa.selenium.grid.node.local.LocalNode;
import org.openqa.selenium.grid.security.Secret;
import org.openqa.selenium.grid.testing.TestSessionFactory;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.HttpHandler;
Expand Down Expand Up @@ -195,7 +196,7 @@ private NodeStatus createNode(Capabilities stereotype, int count, int currentLoa
//Create a single node with the given browserName
private NodeStatus createNode(String...browsers) {
URI uri = createUri();
LocalNode.Builder nodeBuilder = LocalNode.builder(tracer, bus, uri, uri, null);
LocalNode.Builder nodeBuilder = LocalNode.builder(tracer, bus, uri, uri, new Secret("cornish yarg"));
nodeBuilder.maximumConcurrentSessions(browsers.length);

Arrays.stream(browsers).forEach(browser -> {
Expand Down
17 changes: 9 additions & 8 deletions java/server/test/org/openqa/selenium/grid/node/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,14 @@ public void setUp() throws URISyntaxException {
tracer = DefaultTestTracer.createTracer();
bus = new GuavaEventBus();

registrationSecret = new Secret("sussex charmer");

stereotype = new ImmutableCapabilities("browserName", "cheese");
caps = new ImmutableCapabilities("browserName", "cheese");

uri = new URI("http://localhost:1234");

class Handler extends Session implements HttpHandler {

private Handler(Capabilities capabilities) {
super(new SessionId(UUID.randomUUID()), uri, stereotype, capabilities, Instant.now());
}
Expand All @@ -118,7 +119,7 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
}
}

local = LocalNode.builder(tracer, bus, uri, uri, null)
local = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(caps, new TestSessionFactory((id, c) -> new Handler(c)))
.add(caps, new TestSessionFactory((id, c) -> new Handler(c)))
.add(caps, new TestSessionFactory((id, c) -> new Handler(c)))
Expand All @@ -136,7 +137,7 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {

@Test
public void shouldRefuseToCreateASessionIfNoFactoriesAttached() {
Node local = LocalNode.builder(tracer, bus, uri, uri, null).build();
Node local = LocalNode.builder(tracer, bus, uri, uri, registrationSecret).build();
HttpClient.Factory clientFactory = new PassthroughHttpClient.Factory(local);
Node node = new RemoteNode(
tracer,
Expand All @@ -162,7 +163,7 @@ public void shouldCreateASessionIfTheCorrectCapabilitiesArePassedToIt() {

@Test
public void shouldOnlyCreateAsManySessionsAsFactories() {
Node node = LocalNode.builder(tracer, bus, uri, uri, null)
Node node = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(caps, new TestSessionFactory((id, c) -> new Session(id, uri, stereotype, c, Instant.now())))
.build();

Expand Down Expand Up @@ -255,7 +256,7 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
}
}

Node local = LocalNode.builder(tracer, bus, uri, uri, null)
Node local = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(caps, new TestSessionFactory((id, c) -> new Recording()))
.build();
Node remote = new RemoteNode(
Expand Down Expand Up @@ -296,7 +297,7 @@ public void aSessionThatTimesOutWillBeStoppedAndRemovedFromTheSessionMap() {
AtomicReference<Instant> now = new AtomicReference<>(Instant.now());

Clock clock = new MyClock(now);
Node node = LocalNode.builder(tracer, bus, uri, uri, null)
Node node = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(caps, new TestSessionFactory((id, c) -> new Session(id, uri, stereotype, c, Instant.now())))
.sessionTimeout(Duration.ofMinutes(3))
.advanced()
Expand All @@ -314,7 +315,7 @@ public void aSessionThatTimesOutWillBeStoppedAndRemovedFromTheSessionMap() {

@Test
public void shouldNotPropagateExceptionsWhenSessionCreationFails() {
Node local = LocalNode.builder(tracer, bus, uri, uri, null)
Node local = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(caps, new TestSessionFactory((id, c) -> {
throw new SessionNotCreatedException("eeek");
}))
Expand All @@ -329,7 +330,7 @@ public void shouldNotPropagateExceptionsWhenSessionCreationFails() {
@Test
public void eachSessionShouldReportTheNodesUrl() throws URISyntaxException {
URI sessionUri = new URI("http://cheese:42/peas");
Node node = LocalNode.builder(tracer, bus, uri, uri, null)
Node node = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(caps, new TestSessionFactory((id, c) -> new Session(id, sessionUri, stereotype, c, Instant.now())))
.build();
Optional<Session> session = node.newSession(createSessionRequest(caps))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ java_test_suite(
"//java/server/src/org/openqa/selenium/grid/node",
"//java/server/src/org/openqa/selenium/grid/node/config",
"//java/server/src/org/openqa/selenium/grid/node/local",
"//java/server/src/org/openqa/selenium/grid/security",
"//java/server/test/org/openqa/selenium/grid/testing",
artifact("com.google.guava:guava"),
artifact("io.opentelemetry:opentelemetry-api"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openqa.selenium.grid.node.ActiveSession;
import org.openqa.selenium.grid.node.SessionFactory;
import org.openqa.selenium.grid.node.local.LocalNode;
import org.openqa.selenium.grid.security.Secret;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.tracing.DefaultTestTracer;
Expand Down Expand Up @@ -73,7 +74,7 @@ public void setUp() throws URISyntaxException {
EventBus bus = new GuavaEventBus();
clientFactory = HttpClient.Factory.createDefault();
URI uri = new URI("http://localhost:1234");
builder = LocalNode.builder(tracer, bus, uri, uri, null);
builder = LocalNode.builder(tracer, bus, uri, uri, new Secret("wensleydale"));
builderSpy = spy(builder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ java_test_suite(
"//java/server/src/org/openqa/selenium/grid/data",
"//java/server/src/org/openqa/selenium/grid/node",
"//java/server/src/org/openqa/selenium/grid/node/local",
"//java/server/src/org/openqa/selenium/grid/security",
"//java/server/test/org/openqa/selenium/grid/testing",
artifact("com.google.guava:guava"),
artifact("io.opentelemetry:opentelemetry-api"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openqa.selenium.grid.data.CreateSessionResponse;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.node.Node;
import org.openqa.selenium.grid.security.Secret;
import org.openqa.selenium.grid.testing.TestSessionFactory;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.ErrorCodes;
Expand All @@ -50,6 +51,7 @@ public class CreateSessionTest {

private final Json json = new Json();
private final Capabilities stereotype = new ImmutableCapabilities("cheese", "brie");
private final Secret registrationSecret = new Secret("tunworth");

@Test
public void shouldAcceptAW3CPayload() throws URISyntaxException {
Expand All @@ -67,7 +69,7 @@ public void shouldAcceptAW3CPayload() throws URISyntaxException {
new GuavaEventBus(),
uri,
uri,
null)
registrationSecret)
.add(stereotype, new TestSessionFactory((id, caps) -> new Session(id, uri, new ImmutableCapabilities(), caps, Instant.now())))
.build();

Expand Down Expand Up @@ -118,7 +120,7 @@ public void ifOnlyJWPPayloadSentResponseShouldBeJWPOnlyIfJWPConfigured()
new GuavaEventBus(),
uri,
uri,
null)
registrationSecret)
.add(stereotype, new TestSessionFactory((id, caps) -> new Session(id, uri, new ImmutableCapabilities(), caps, Instant.now())))
.build();

Expand Down Expand Up @@ -161,7 +163,7 @@ public void shouldPreferUsingTheW3CProtocol() throws URISyntaxException {
new GuavaEventBus(),
uri,
uri,
null)
registrationSecret)
.add(stereotype, new TestSessionFactory((id, caps) -> new Session(id, uri, new ImmutableCapabilities(), caps, Instant.now())))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openqa.selenium.grid.data.NodeStatus;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.node.Node;
import org.openqa.selenium.grid.security.Secret;
import org.openqa.selenium.grid.testing.TestSessionFactory;
import org.openqa.selenium.remote.HttpSessionId;
import org.openqa.selenium.remote.SessionId;
Expand Down Expand Up @@ -60,14 +61,16 @@ public class LocalNodeTest {

private LocalNode node;
private Session session;
private Secret registrationSecret;

@Before
public void setUp() throws URISyntaxException {
Tracer tracer = DefaultTestTracer.createTracer();
EventBus bus = new GuavaEventBus();
URI uri = new URI("http://localhost:1234");
Capabilities stereotype = new ImmutableCapabilities("cheese", "brie");
node = LocalNode.builder(tracer, bus, uri, uri, null)
registrationSecret = new Secret("red leicester");
node = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(stereotype, new TestSessionFactory((id, caps) -> new Session(id, uri, stereotype, caps, Instant.now())))
.build();

Expand Down Expand Up @@ -175,7 +178,7 @@ public HttpResponse execute(HttpRequest req) {
}
}

Node node = LocalNode.builder(tracer, bus, uri, uri, null)
Node node = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(caps, new TestSessionFactory(VerifyingHandler::new))
.add(caps, new TestSessionFactory(VerifyingHandler::new))
.add(caps, new TestSessionFactory(VerifyingHandler::new))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.common.collect.ImmutableSet;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.WebDriverInfo;
import org.openqa.selenium.chrome.ChromeDriverInfo;
import org.openqa.selenium.events.EventBus;
Expand All @@ -35,6 +34,7 @@
import org.openqa.selenium.grid.node.Node;
import org.openqa.selenium.grid.node.config.DriverServiceSessionFactory;
import org.openqa.selenium.grid.node.local.LocalNode;
import org.openqa.selenium.grid.security.Secret;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.Server;
import org.openqa.selenium.grid.sessionmap.SessionMap;
Expand Down Expand Up @@ -67,12 +67,14 @@ public class NewSessionCreationTest {
private Tracer tracer;
private EventBus events;
private HttpClient.Factory clientFactory;
private Secret registrationSecret;

@Before
public void setup() {
tracer = DefaultTestTracer.createTracer();
events = new GuavaEventBus();
clientFactory = HttpClient.Factory.createDefault();
registrationSecret = new Secret("hereford hop");
}

@Test
Expand All @@ -83,7 +85,7 @@ public void ensureJsCannotCreateANewSession() throws URISyntaxException {
assumeThat(geckoDriverInfo.isAvailable()).isTrue();

SessionMap sessions = new LocalSessionMap(tracer, events);
Distributor distributor = new LocalDistributor(tracer, events, clientFactory, sessions, null);
Distributor distributor = new LocalDistributor(tracer, events, clientFactory, sessions, registrationSecret);
Routable router = new Router(tracer, clientFactory, sessions, distributor)
.with(new EnsureSpecCompliantHeaders(ImmutableList.of(), ImmutableSet.of()));

Expand All @@ -99,7 +101,7 @@ public void ensureJsCannotCreateANewSession() throws URISyntaxException {
events,
uri,
uri,
null)
registrationSecret)
.add(Browser.detect().getCapabilities(), new TestSessionFactory((id, caps) -> new Session(id, uri, Browser.detect().getCapabilities(), caps, Instant.now())))
.build();
distributor.add(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class RouterTest {
private SessionMap sessions;
private Distributor distributor;
private Router router;
private Secret registrationSecret;

@Before
public void setUp() {
Expand All @@ -77,7 +78,8 @@ public void setUp() {
sessions = new LocalSessionMap(tracer, bus);
handler.addHandler(sessions);

distributor = new LocalDistributor(tracer, bus, clientFactory, sessions, new Secret("stinking bishop"));
registrationSecret = new Secret("stinking bishop");
distributor = new LocalDistributor(tracer, bus, clientFactory, sessions, registrationSecret);
handler.addHandler(distributor);

router = new Router(tracer, clientFactory, sessions, distributor);
Expand All @@ -96,7 +98,7 @@ public void addingANodeThatIsDownMeansTheGridIsNotReady() throws URISyntaxExcept

AtomicReference<Availability> isUp = new AtomicReference<>(DOWN);

Node node = LocalNode.builder(tracer, bus, uri, uri, null)
Node node = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(capabilities, new TestSessionFactory((id, caps) -> new Session(id, uri, new ImmutableCapabilities(), caps, Instant.now())))
.advanced()
.healthCheck(() -> new HealthCheck.Result(isUp.get(), "TL;DR"))
Expand All @@ -114,7 +116,7 @@ public void aNodeThatIsUpAndHasSpareSessionsMeansTheGridIsReady() throws URISynt

AtomicReference<Availability> isUp = new AtomicReference<>(UP);

Node node = LocalNode.builder(tracer, bus, uri, uri, null)
Node node = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
.add(capabilities, new TestSessionFactory((id, caps) -> new Session(id, uri, new ImmutableCapabilities(), caps, Instant.now())))
.advanced()
.healthCheck(() -> new HealthCheck.Result(isUp.get(), "TL;DR"))
Expand Down

0 comments on commit b845a3c

Please sign in to comment.