Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #12714 verify workername for Mongo usage #12715

Open
wants to merge 7 commits into
base: jetty-12.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading