Skip to content

Commit

Permalink
Issue #12714 verify workername for Mongo usage (#12715)
Browse files Browse the repository at this point in the history
* Issue #12714 verify workername for Mongo usage
  • Loading branch information
janbartel authored Jan 22, 2025
1 parent 74a8bc8 commit dc685b6
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.session.AbstractSessionDataStoreFactory;
import org.eclipse.jetty.session.AbstractSessionDataStoreTest;
import org.eclipse.jetty.session.DefaultSessionCacheFactory;
import org.eclipse.jetty.session.DefaultSessionIdManager;
import org.eclipse.jetty.session.SessionContext;
import org.eclipse.jetty.session.SessionData;
Expand All @@ -35,8 +36,10 @@
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* MongoSessionDataStoreTest
Expand Down Expand Up @@ -128,6 +131,85 @@ public boolean checkSessionPersisted(SessionData data) throws Exception
}
}

@Test
public void testBadWorkerName() throws Exception
{
Server server = new Server();
DefaultSessionIdManager idMgr = new DefaultSessionIdManager(server);
idMgr.setWorkerName("b-a-d");
server.addBean(idMgr);

//create the SessionDataStore
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/ctx");

server.setHandler(context);
context.getSessionHandler().setSessionIdManager(idMgr);

DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setSaveOnCreate(true);
server.addBean(cacheFactory);

SessionDataStoreFactory factory = createSessionDataStoreFactory();
server.addBean(factory);

assertThrows(IllegalStateException.class, () ->
{
server.start();
});
}

@Test
public void testGoodWorkerName() throws Exception
{
Server server = new Server();
DefaultSessionIdManager idMgr = new DefaultSessionIdManager(server);
idMgr.setWorkerName("NODE_99");
server.addBean(idMgr);

//create the SessionDataStore
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/ctx");

server.setHandler(context);
context.getSessionHandler().setSessionIdManager(idMgr);

DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setSaveOnCreate(true);
server.addBean(cacheFactory);

SessionDataStoreFactory factory = createSessionDataStoreFactory();
server.addBean(factory);

assertDoesNotThrow(() ->
server.start());
}

@Test
public void testDefaultWorkerName() throws Exception
{
Server server = new Server();
DefaultSessionIdManager idMgr = new DefaultSessionIdManager(server);
server.addBean(idMgr);

//create the SessionDataStore
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/ctx");

server.setHandler(context);
context.getSessionHandler().setSessionIdManager(idMgr);

DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setSaveOnCreate(true);
server.addBean(cacheFactory);

SessionDataStoreFactory factory = createSessionDataStoreFactory();
server.addBean(factory);

assertDoesNotThrow(() ->
server.start());
}

/**
* Test that a session stored in the legacy attribute
* format can be read.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.session.AbstractSessionDataStoreFactory;
import org.eclipse.jetty.session.AbstractSessionDataStoreTest;
import org.eclipse.jetty.session.DefaultSessionCacheFactory;
import org.eclipse.jetty.session.DefaultSessionIdManager;
import org.eclipse.jetty.session.SessionContext;
import org.eclipse.jetty.session.SessionData;
Expand All @@ -34,8 +35,10 @@
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* MongoSessionDataStoreTest
Expand Down Expand Up @@ -114,6 +117,85 @@ public boolean checkSessionPersisted(SessionData data) throws Exception
}
}

@Test
public void testBadWorkerName() throws Exception
{
Server server = new Server();
DefaultSessionIdManager idMgr = new DefaultSessionIdManager(server);
idMgr.setWorkerName("b-a-d");
server.addBean(idMgr);

//create the SessionDataStore
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/ctx");

server.setHandler(context);
context.getSessionHandler().setSessionIdManager(idMgr);

DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setSaveOnCreate(true);
server.addBean(cacheFactory);

SessionDataStoreFactory factory = createSessionDataStoreFactory();
server.addBean(factory);

assertThrows(IllegalStateException.class, () ->
{
server.start();
});
}

@Test
public void testGoodWorkerName() throws Exception
{
Server server = new Server();
DefaultSessionIdManager idMgr = new DefaultSessionIdManager(server);
idMgr.setWorkerName("NODE_99");
server.addBean(idMgr);

//create the SessionDataStore
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/ctx");

server.setHandler(context);
context.getSessionHandler().setSessionIdManager(idMgr);

DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setSaveOnCreate(true);
server.addBean(cacheFactory);

SessionDataStoreFactory factory = createSessionDataStoreFactory();
server.addBean(factory);

assertDoesNotThrow(() ->
server.start());
}

@Test
public void testDefaultWorkerName() throws Exception
{
Server server = new Server();
DefaultSessionIdManager idMgr = new DefaultSessionIdManager(server);
server.addBean(idMgr);

//create the SessionDataStore
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/ctx");

server.setHandler(context);
context.getSessionHandler().setSessionIdManager(idMgr);

DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setSaveOnCreate(true);
server.addBean(cacheFactory);

SessionDataStoreFactory factory = createSessionDataStoreFactory();
server.addBean(factory);

assertDoesNotThrow(() ->
server.start());
}

/**
* Test that a session stored in the legacy attribute
* format can be read.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import com.mongodb.MongoException;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.IndexModel;
import com.mongodb.client.model.IndexOptions;
import com.mongodb.client.model.Indexes;
import com.mongodb.client.model.Projections;
Expand Down Expand Up @@ -159,6 +157,8 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore
*/
private DBObject _version1;

private static final Pattern _workerNamePattern = Pattern.compile("[_0-9a-zA-Z]*");

/**
* Access to MongoDB
*/
Expand All @@ -175,6 +175,22 @@ public MongoCollection<Document> getDBCollection()
return _dbSessions;
}

@Override
protected void doStart() throws Exception
{
checkWorkerName();
super.doStart();
}

private void checkWorkerName() throws IllegalStateException
{
if (_context == null || StringUtil.isEmpty(_context.getWorkerName()))
return;

if (!_workerNamePattern.matcher(_context.getWorkerName()).matches())
throw new IllegalStateException("Worker name " + _context.getWorkerName() + " does not match pattern " + _workerNamePattern.pattern());
}

@Override
public SessionData doLoad(String id) throws Exception
{
Expand Down

0 comments on commit dc685b6

Please sign in to comment.