Issue
One common service customization is to add the HttpOnly and Secure flags onto JSESSIONID cookie at the application level for requests. This can be done through customizing the Liferay service and Tomcat via scripts or through updating NGINX configurations on the webserver service.
Environment
- Liferay PaaS
Resolution
Setting Cookie Configurations on the Liferay Service
- Create a
cookie-config.shscript.
#!/bin/bash show-files /lcp-container/configs cp -r /lcp-container/config/$LC_PROJECT_ENVIRONMENT/tomcat/conf/* /opt/liferay/tomcat/conf
- Run
chmod +x cookie-config.shto make the script executable. - Place
cookie-config.shinto the environment where it needs to be run:/liferay/configs/{env}/scripts - The script uses a DXPC entrypoint,
show-files. The script is environment-friendly, meaning it will recognize the environment which it is run in.show-filesis used to print the directory structure of a given directory to the logs. In this case, it prints the/lcp-container/configsdirectory structure. Entrypoint scripts may be found by accessing the service shell and looking in the/usr/local/bindirectory. The script also takes advantage of the environment variableLCP_PROJECT_ENVIRONMENT, which is a string that tells which environment the service lives. - Once the script is in place, we will update Tomcat's
/tomcat/conf/web.xml. Find the defaultweb.xmlfile that matches with the Liferay Tomcat bundle of your Liferay Experience Cloud instance. (e.g., if using Liferay DXP 7.3, you need theweb.xmlof a Liferay DXP 7.3 Tomcat bundle).
NOTE: This can be found in 1 of 2 ways: -In the Liferay Cloud Console, go to Services > Liferay > Shell and copy theweb.xmlfrom the Tomcat directory. -Download a separate Liferay Tomcat bundle (which matches the version on Liferay Experience Cloud) and copy theweb.xmlfrom the bundle - In the
web.xmlreplace the ` tag with this snippet:
<session-config> <session-timeout>30</session-timeout> <cookie-config> <http-only>true</http-only> <secure>true</secure> </cookie-config> </session-config> - This file should then be placed in the
/liferay/configs/{env}/tomcat/confdirectory.
When the Liferay service is deployed, the file is copied to Tomcat's/confdirectory (due to thecookie-config.shscript).
NOTE: If you ever need to upgrade Tomcat and the Tomcat upgrade includes changes to theweb.xml, make sure to merge yourweb.xmlcustomizations with theweb.xmlchanges made by the Tomcat update. - We can confirm that these changes took place by accessing the webserver endpoint and inspecting the
JSESSIONIDcookie. Upon cookie inspection,HttpOnlyandsecureflags should be checked.
Setting Cookie Configurations on the Webserver Service
Through the webserver service, we will be adding a proxy_cookie_path directive within the ssl.conf file.
- Create an
ssl.conffile in the/webserver/configs/{env}directory.
# Adds HttpOnly and Secure flags to cookie proxy_cookie_path / "/; HTTPOnly; Secure";
- Once the service has been deployed, we can confirm this by inspecting the
SERVER_IDcookie and seeing thatHttpOnlyandSecureflags have been marked.
Additional Information
Some File locations have changed between Liferay Experience Cloud stack version 3.x and 4.x. Please review this article to check in the Liferay Service Changes section: