From 4bc82ce2ba7a1a459510d71bf8aafcc2693ae561 Mon Sep 17 00:00:00 2001 From: Andre Henn Date: Tue, 12 Dec 2023 14:24:03 +0100 Subject: [PATCH] Apply some CIS Apache Tomcat benchmark recommendations --- Dockerfile | 49 +++++++----- config/context.xml | 2 +- config/server.xml | 187 +++++++++++++++++++++++++++++++++++++++++++++ startup.sh | 11 ++- 4 files changed, 230 insertions(+), 19 deletions(-) create mode 100644 config/server.xml diff --git a/Dockerfile b/Dockerfile index 4bd8921..31a6fed 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,25 +30,28 @@ ENV CATALINA_OPTS="\$EXTRA_JAVA_OPTS \ # init RUN apt update \ -&& apt -y upgrade \ -&& apt install -y --no-install-recommends openssl unzip gdal-bin wget curl openjdk-11-jdk gettext \ -&& apt clean \ -&& rm -rf /var/cache/apt/* \ -&& rm -rf /var/lib/apt/lists/* + && apt -y upgrade \ + && apt install -y --no-install-recommends openssl unzip gdal-bin wget curl openjdk-11-jdk gettext \ + && apt clean \ + && rm -rf /var/cache/apt/* \ + && rm -rf /var/lib/apt/lists/* WORKDIR /opt/ RUN wget -q https://archive.apache.org/dist/tomcat/tomcat-9/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz \ -&& tar xf apache-tomcat-${TOMCAT_VERSION}.tar.gz \ -&& rm apache-tomcat-${TOMCAT_VERSION}.tar.gz \ -&& rm -rf /opt/apache-tomcat-${TOMCAT_VERSION}/webapps/ROOT \ -&& rm -rf /opt/apache-tomcat-${TOMCAT_VERSION}/webapps/docs \ -&& rm -rf /opt/apache-tomcat-${TOMCAT_VERSION}/webapps/examples + && tar xf apache-tomcat-${TOMCAT_VERSION}.tar.gz \ + && rm apache-tomcat-${TOMCAT_VERSION}.tar.gz \ + && rm -rf /opt/apache-tomcat-${TOMCAT_VERSION}/webapps/ROOT \ + && rm -rf /opt/apache-tomcat-${TOMCAT_VERSION}/webapps/docs \ + && rm -rf /opt/apache-tomcat-${TOMCAT_VERSION}/webapps/examples \ + && rm -rf /opt/apache-tomcat-${TOMCAT_VERSION}/webapps/host-manager \ + && rm -rf /opt/apache-tomcat-${TOMCAT_VERSION}/webapps/manager # cleanup RUN apt purge -y \ -&& apt autoremove --purge -y \ -&& rm -rf /tmp/* + && apt autoremove --purge -y \ + && rm -rf /var/lib/apt/lists/* \ + && rm -rf /tmp/* FROM tomcat as download @@ -61,10 +64,10 @@ ENV GEOSERVER_BUILD=$GS_BUILD WORKDIR /tmp RUN echo "Downloading GeoServer ${GS_VERSION} ${GS_BUILD}" \ -&& wget -q -O /tmp/geoserver.zip $WAR_ZIP_URL \ -&& unzip geoserver.zip geoserver.war -d /tmp/ \ -&& unzip -q /tmp/geoserver.war -d /tmp/geoserver \ -&& rm /tmp/geoserver.war + && wget -q -O /tmp/geoserver.zip $WAR_ZIP_URL \ + && unzip geoserver.zip geoserver.war -d /tmp/ \ + && unzip -q /tmp/geoserver.war -d /tmp/geoserver \ + && rm /tmp/geoserver.war FROM tomcat as install @@ -117,7 +120,19 @@ COPY $ADDITIONAL_FONTS_PATH /usr/share/fonts/truetype/ RUN rm -rf /tmp/* # Add default configs -ADD config $CONFIG_DIR +COPY config $CONFIG_DIR + +COPY config/server.xml $CATALINA_HOME/conf/server.xml + +# Apply CIS Apache tomcat recommendations regarding server information +# * Alter the advertised server.info String (2.1 - 2.3) +RUN cd $CATALINA_HOME/lib \ + && jar xf catalina.jar org/apache/catalina/util/ServerInfo.properties \ + && sed -i 's/Apache Tomcat\/'"${TOMCAT_VERSION}"'/i_am_a_teapot/g' org/apache/catalina/util/ServerInfo.properties \ + && sed -i 's/'"${TOMCAT_VERSION}"'/x.y.z/g' org/apache/catalina/util/ServerInfo.properties \ + && sed -i 's/^server.built=.*/server.built=/g' org/apache/catalina/util/ServerInfo.properties \ + && jar uf catalina.jar org/apache/catalina/util/ServerInfo.properties \ + && rm -rf org/apache/catalina/util/ServerInfo.properties # copy scripts COPY *.sh /opt/ diff --git a/config/context.xml b/config/context.xml index c38276b..157fb73 100644 --- a/config/context.xml +++ b/config/context.xml @@ -17,4 +17,4 @@ validationQuery="SELECT 1" rollbackOnReturn="true" /> - \ No newline at end of file + diff --git a/config/server.xml b/config/server.xml new file mode 100644 index 0000000..5ade9f9 --- /dev/null +++ b/config/server.xml @@ -0,0 +1,187 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/startup.sh b/startup.sh index 9195d26..d148115 100755 --- a/startup.sh +++ b/startup.sh @@ -112,4 +112,13 @@ if [ "${POSTGRES_JNDI_ENABLED}" = "true" ]; then fi # start the tomcat -exec $CATALINA_HOME/bin/catalina.sh run +# CIS - Tomcat Benchmark recommendations: +# * Turn off session facade recycling +# * Set a nondeterministic Shutdown command value +if [ ! "${ENABLE_DEFAULT_SHUTDOWN}" = "true" ]; then + REPLACEMENT="$(echo $RANDOM | md5sum | head -c 10)" + sed -i 's/SHUTDOWN/'"$REPLACEMENT"'/g' "$CATALINA_HOME/conf/server.xml" + REPLACEMENT= +fi + +exec $CATALINA_HOME/bin/catalina.sh run -Dorg.apache.catalina.connector.RECYCLE_FACADES=true