Skip to content

Commit

Permalink
Northbound websocket improvements
Browse files Browse the repository at this point in the history
This commit makes several improvements to the northbound websocket interface:

* Authentication using the sensinact Authenticator services
* Improved ordering guarantees. Websocket clients will always receive a subscription response before receiving their first notification for that subscription
* Thread safety - the web socket session was not updated in a thread safe way so notifications would sometimes fail with NPE

Signed-off-by: Tim Ward <timothyjward@apache.org>
  • Loading branch information
timothyjward committed Jun 26, 2023
1 parent 0a6f310 commit 3001cde
Show file tree
Hide file tree
Showing 12 changed files with 564 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public class SensiNactSessionImpl implements SensiNactSession {

private final GatewayThread thread;

public SensiNactSessionImpl(final GatewayThread thread) {
private final UserInfo user;

public SensiNactSessionImpl(UserInfo user, final GatewayThread thread) {
this.user = user;
expiry = Instant.now().plusSeconds(600);
this.thread = thread;
}
Expand Down Expand Up @@ -595,7 +598,6 @@ public void notify(String topic, AbstractResourceNotification notification) {

@Override
public UserInfo getUserInfo() {
// TODO Auto-generated method stub
return null;
return user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public List<String> getSessionIds(UserInfo user) {
@Override
public SensiNactSession createNewSession(UserInfo user) {
Objects.requireNonNull(user);
SensiNactSessionImpl session = new SensiNactSessionImpl(thread);
SensiNactSessionImpl session = new SensiNactSessionImpl(user, thread);
String sessionId = session.getSessionId();

synchronized (lock) {
Expand Down
2 changes: 1 addition & 1 deletion prototype/northbound/rest/integration-test.bndrun
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
junit-platform-launcher;version='[1.9.0,1.9.1)',\
org.antlr.antlr4-runtime;version='[4.12.0,4.12.1)',\
org.apache.aries.component-dsl.component-dsl;version='[1.2.2,1.2.3)',\
org.apache.aries.spifly.dynamic.framework.extension;version='[1.3.5,1.3.6)',\
org.apache.aries.spifly.dynamic.framework.extension;version='[1.3.6,1.3.7)',\
org.apache.aries.typedevent.bus;version='[0.0.2,0.0.3)',\
org.apache.felix.cm.json;version='[2.0.0,2.0.1)',\
org.apache.felix.configadmin;version='[1.9.24,1.9.25)',\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
net.bytebuddy.byte-buddy-agent;version='[1.12.13,1.12.14)',\
org.antlr.antlr4-runtime;version='[4.12.0,4.12.1)',\
org.apache.aries.component-dsl.component-dsl;version='[1.2.2,1.2.3)',\
org.apache.aries.spifly.dynamic.framework.extension;version='[1.3.5,1.3.6)',\
org.apache.aries.spifly.dynamic.framework.extension;version='[1.3.6,1.3.7)',\
org.apache.aries.typedevent.bus;version='[0.0.2,0.0.3)',\
org.apache.commons.commons-compress;version='[1.22.0,1.22.1)',\
org.apache.felix.cm.json;version='[2.0.0,2.0.1)',\
Expand Down
7 changes: 6 additions & 1 deletion prototype/northbound/websocket/integration-test.bndrun
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
junit-platform-launcher;version='[1.9.0,1.9.1)',\
org.antlr.antlr4-runtime;version='[4.12.0,4.12.1)',\
org.apache.aries.component-dsl.component-dsl;version='[1.2.2,1.2.3)',\
org.apache.aries.spifly.dynamic.framework.extension;version='[1.3.5,1.3.6)',\
org.apache.aries.spifly.dynamic.framework.extension;version='[1.3.6,1.3.7)',\
org.apache.aries.typedevent.bus;version='[0.0.2,0.0.3)',\
org.apache.felix.cm.json;version='[2.0.0,2.0.1)',\
org.apache.felix.configadmin;version='[1.9.24,1.9.25)',\
Expand All @@ -47,11 +47,14 @@
org.eclipse.emf.ecore;version='[2.33.0,2.33.1)',\
org.eclipse.emf.ecore.xmi;version='[2.18.0,2.18.1)',\
org.eclipse.jetty.alpn.client;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.annotations;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.client;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.http;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.jndi;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.plus;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.security;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.server;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.servlet;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.util;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.webapp;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.websocket.api;version='[11.0.13,11.0.14)',\
Expand All @@ -74,6 +77,7 @@
org.eclipse.sensinact.gateway.northbound.filters.filters.core;version='[0.0.2,0.0.3)',\
org.eclipse.sensinact.gateway.northbound.filters.ldap;version='[0.0.2,0.0.3)',\
org.eclipse.sensinact.gateway.northbound.query-handler;version='[0.0.2,0.0.3)',\
org.eclipse.sensinact.gateway.northbound.security.authentication-api;version='[0.0.2,0.0.3)',\
org.eclipse.sensinact.gateway.northbound.websocket;version='[0.0.2,0.0.3)',\
org.eclipse.sensinact.gateway.northbound.websocket-tests;version='[0.0.2,0.0.3)',\
org.gecko.emf.osgi.api;version='[4.6.2,4.6.3)',\
Expand All @@ -84,6 +88,7 @@
org.osgi.service.typedevent;version='[1.0.0,1.0.1)',\
org.osgi.test.common;version='[1.2.1,1.2.2)',\
org.osgi.test.junit5;version='[1.2.1,1.2.2)',\
org.osgi.test.junit5.cm;version='[1.2.1,1.2.2)',\
org.osgi.util.converter;version='[1.0.9,1.0.10)',\
org.osgi.util.function;version='[1.1.0,1.1.1)',\
org.osgi.util.promise;version='[1.3.0,1.3.1)',\
Expand Down
12 changes: 10 additions & 2 deletions prototype/northbound/websocket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<artifactId>query-handler</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.sensinact.gateway.northbound.security</groupId>
<artifactId>authentication-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.sensinact.gateway.northbound.filters</groupId>
<artifactId>ldap</artifactId>
Expand Down Expand Up @@ -82,6 +87,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.test.junit5.cm</artifactId>
<scope>test</scope>
</dependency>

<!-- OSGi -->
<dependency>
Expand Down Expand Up @@ -161,7 +171,6 @@
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.http.servlet-api</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -195,7 +204,6 @@
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-resolver-maven-plugin</artifactId>
<version>6.4.0</version>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.jetty.websocket.server.JettyServerUpgradeRequest;
import org.eclipse.jetty.websocket.server.JettyServerUpgradeResponse;
import org.eclipse.jetty.websocket.server.JettyWebSocketCreator;
import org.eclipse.sensinact.core.security.UserInfo;
import org.eclipse.sensinact.core.session.SensiNactSessionManager;
import org.eclipse.sensinact.northbound.query.api.IQueryHandler;
import org.slf4j.Logger;
Expand Down Expand Up @@ -80,7 +81,9 @@ public void close() {

@Override
public Object createWebSocket(final JettyServerUpgradeRequest req, final JettyServerUpgradeResponse resp) {
final WebSocketEndpoint wsConnection = new WebSocketEndpoint(this, sessionManager, queryHandler);
UserInfo userInfo = (UserInfo) req.getServletAttribute(WebSocketJettyRegistrar.SENSINACT_USER_INFO);
final WebSocketEndpoint wsConnection = new WebSocketEndpoint(this, sessionManager.createNewSession(userInfo),
queryHandler);
sessions.add(wsConnection);
return wsConnection;
}
Expand Down

0 comments on commit 3001cde

Please sign in to comment.