Login and HTTP sessions in the cluster (Tomcat Cluster)
The page describes how the login to the web pages of Mercury DB (HgDB) 3.0 works in a cluster behind a load balancer that spreads the requests of one client over all the nodes (round-robin), and how to configure the replication of HTTP sessions (Tomcat Cluster). The support is in the packages of release 3.0.4.24; the first published Docker image with it and the fix of the start of the cluster are in 3.0.4.25.
What needs a session and what does not
| Access | State of the login | Needs the replication of sessions |
|---|---|---|
REST services (/rest/service/**) with the OAuth 2.0 token | the token is kept in the database and in the cache shared in the cluster | no |
Pages with the login form (FORM): mercury-gui-registry (/registry/*, /system/*) and mercury-ws-app (/swagger-ui/types.html, /files/*, /lobs/*, /login/*, /static/admin/*) | the HTTP session of the node that served the login | yes |
A token issued by one node is accepted by every node (a check on a three-node cluster: the token from node 1 gave 200 on all the nodes, a wrong token or no token gave 401 on all of them), so the REST clients work behind the round-robin without any additional configuration.
The login form is different. Without the replication of sessions a request for a protected page and the following POST of the form (j_security_check) land on different nodes, and Tomcat rejects the login (HTTP 408). The user cannot log in to the pages through such a load balancer.
ip_hash on the load balancerThe technical users (REST clients, integrations) usually connect from one address. With ip_hash all of their requests would land on one node, so only its pools of connections to the database would be loaded. That is why the load balancer of the product stays on round-robin and the sessions are replicated between the nodes.
How it works
Since release 3.0.4.24 the nodes of the cluster replicate the HTTP sessions with Apache Tomcat Cluster (DeltaManager, the static list of the members, the port 4000 between the nodes). The standard FormAuthenticator of Tomcat 9 does not work in such a cluster, so the applications use org.mercury.gui.auth.ClusterFormAuthenticator (module mercury-gui-core). It keeps the protection against the session fixation attack: the identifier of the session is changed during the login, and the old identifier stops being valid (checked: an attacker who knew the identifier issued before the login of the victim gets the login form on every node, and the victim with the new identifier is logged in).
What the user can notice:
- After the login the user is redirected to the address that was requested before the login (the path and the parameters of a
GETrequest are copied to replicated session attributes, so it does not matter which node shows the login form and which node handles the login). The start page of the application is used only when there is no saved address (for example a direct entry to the login page) or the saved request was not aGET:/registry/mercury-registryinmercury-gui-registryand the main page/inmercury-ws-app. - The session survives a failure of a node. Checked: after logging in, two of three nodes were stopped one after another and the user stayed logged in on the remaining node; after the nodes came back the session was still valid.
Docker compositions
The compositions with several nodes (mercury-two-node-cluster, mercury-three-node-cluster) enable the replication by default. The server image (ibpm/mercury, release 3.0.4.25 and later) reads two environment variables:
| Variable | Default | Description |
|---|---|---|
TOMCAT_CLUSTER_MEMBERS | (empty) | Host names of all the nodes separated by commas, including the node itself, for example mercury-node1,mercury-node2,mercury-node3. HOST_NAME of the node must be on the list. Empty means that the replication is switched off (a single node). |
TOMCAT_CLUSTER_PORT | 4000 | The port of the replication of sessions between the nodes. Do not publish it on the host. |
The compositions set TOMCAT_CLUSTER_MEMBERS in the environment of every node and the value can be overridden in the file .env of the composition. At every start of the container the script of the image:
- checks the list of the members and the port,
- checks that the class
ClusterFormAuthenticatoris in the library of the server, - inserts the element
Clusterintoserver.xml(the list of the other members and the local member differ on every node), - creates the context descriptors of the two applications in
conf/Catalina/localhost/.
When any of the checks fails (a wrong host name, the node is not on the list, the class is missing), the node does not start and writes ERROR to the log. This is intended: see the warning below.
The class of the authenticator is loaded by Tomcat, not by the application. When the descriptor of the context points to a class that Tomcat cannot find, Tomcat does not report an error and starts the application without the authentication. In the image the jar of mercury-gui-core is in the directory lib of the product, which Tomcat reads, and the script above does not allow a start without it.
Installation without Docker
Requirements: Apache Tomcat 9.0.x (the solution was checked on 9.0.123) and the packages of the release 3.0.4.24 or later (the files web.xml of mercury-gui-registry and mercury-ws-app contain <distributable/>; the jar mercury-gui-core has to be on the classpath of Tomcat itself, for example in the directory read by common.loader).
-
In
server.xml, inside<Engine>, add the elementCluster. The example is for the nodemercury-node2of a three-node cluster (the local member and the other members are different on every node,uniqueIdhas 16 bytes):<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.StaticMembershipService">
<LocalMember className="org.apache.catalina.tribes.membership.StaticMember" uniqueId="{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2}"/>
<Member className="org.apache.catalina.tribes.membership.StaticMember" host="mercury-node1" port="4000" uniqueId="{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}"/>
<Member className="org.apache.catalina.tribes.membership.StaticMember" host="mercury-node3" port="4000" uniqueId="{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3}"/>
</Membership>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4000" autoBind="0" selectorTimeout="5000" maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpPingInterceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif|.*\.js|.*\.css|.*\.png|.*\.jpeg|.*\.jpg|.*\.txt"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>The filter of
ReplicationValvemust not contain.html: the protected pages, for example/swagger-ui/types.html, also change the session. -
For both applications create the context descriptor, for example
conf/Catalina/localhost/mercury-gui-registry.xml(andmercury-ws-app.xmlwithlandingPage="/"):<Context>
<Valve className="org.mercury.gui.auth.ClusterFormAuthenticator" landingPage="/registry/mercury-registry"/>
</Context> -
Open the port of the replication (
4000) between the nodes and make sure that the names of the members resolve on every node.
Single sign-on between the applications
With the Tomcat Cluster switched on the compositions and charts also insert the valve ClusterSingleSignOn into server.xml, so a login in one application (mercury-ws-app, mercury-gui-registry, mercury-gui-auth) is valid in the others, also on another node. Details and the configuration without Docker: Administration of users, roles and OAuth.
Start of the cluster
Before release 3.0.4.25 starting several nodes at the same time on a new, empty database could hang: each node creates the root folder of the CMIS repository, and the node that was first held a transaction of the database while it called another node over HTTP, which in turn waited for the lock of that transaction. The calls to the secretary of the index are now made after the transaction is committed, and a node that loses the race repeats the operation. Use release 3.0.4.25 or later for a cluster that starts from an empty database. A workaround for the earlier releases (not checked as a fix): start the first node, wait until it is healthy, and only then start the others.
Not checked
The solution was checked on a cluster of three nodes with the load balancer on round-robin. Not checked: the logout, the expiry of sessions in the cluster, the work under a long load, the application license-registry, and the membership of the nodes in Kubernetes (Tomcat 9.0.123 has the providers KubernetesMembershipProvider and DNSMembershipProvider). The authenticator depends on the behavior of FormAuthenticator of Tomcat 9.0.123, so repeat the test of the login through the load balancer after every change of the version of Tomcat.