Repository navigation
Expand file tree
/
Copy pathDockerfile.tomcat
More file actions
282 lines (258 loc) · 13.9 KB
/
Copy pathDockerfile.tomcat
File metadata and controls
282 lines (258 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# Based on docker-tomcat:debian12-py
#docker build . -t quay.io/semoss/tomcat-builder:ubuntu22
# Base image arguments
ARG BASE_REGISTRY=docker.io
ARG BASE_IMAGE=ubuntu
ARG BASE_TAG=22.04
# JAVA and JDK default version
ARG AZUL_ZULU_VERSION=25.38.201
ARG JAVA_HOME=/usr/lib/jvm/zulu25
ARG JDK_VERSION=25.0.5.1
# FIPS 140-3 build switch. This file produces two images from one definition:
#
# docker build -f Dockerfile.tomcat -t quay.io/semoss/tomcat-builder:11.0.29 .
# docker build -f Dockerfile.tomcat --build-arg FIPS_ENABLED=true \
# -t quay.io/semoss/tomcat-builder:11.0.29-fips .
#
# With FIPS_ENABLED=false nothing below is installed, so the non-FIPS image is
# byte-identical to what it was before this switch existed. Downstream images
# pick a base with --build-arg TOMCAT_TAG=11.0.29-fips.
#
# IMPORTANT: a -fips base must be paired with a war built using -P dev,fips.
# A default war carries BouncyCastle in WEB-INF/lib, which would sit alongside
# the validated bc-fips on the system classloader - two copies of the
# org.bouncycastle.* namespace across two classloaders. That combination boots
# cleanly and is quietly non-compliant.
ARG FIPS_ENABLED=false
# BouncyCastle FIPS provider jars, staged into ${JAVA_HOME}/lib/fips so they
# travel with the existing "COPY --from=tomcat_builder /usr/lib/jvm/zulu25"
# in every downstream image.
#
# Only bc-fips is the CMVP-validated module - bctls/bcpkix/bcutil are companion
# libraries outside the certificate boundary that call into it. Before bumping
# BC_FIPS_VERSION, confirm the new version is on the CMVP active list, not just
# published to Maven Central.
ARG BC_FIPS_VERSION=2.1.3
ARG BCTLS_FIPS_VERSION=2.1.24
ARG BCPKIX_FIPS_VERSION=2.1.13
ARG BCUTIL_FIPS_VERSION=2.1.8
# Maven default version
ARG MAVEN_HOME=/opt/apache-maven-3.10.0
# Tomcat default version
ARG TOMCAT_VERSION=11.0.29
ARG TOMCAT_HOME=/opt/apache-tomcat-${TOMCAT_VERSION}
FROM ${BASE_REGISTRY}/${BASE_IMAGE}:${BASE_TAG} AS builder
ARG TOMCAT_VERSION
ENV TOMCAT_VERSION=${TOMCAT_VERSION}
ARG TOMCAT_HOME=/opt/apache-tomcat-${TOMCAT_VERSION}
# Tomcat and Maven
ARG TOMCAT_HOME
ARG TOMCAT_VERSION
ARG MAVEN_HOME
# JAVA arguments
ARG AZUL_ZULU_VERSION
ARG JAVA_HOME
ARG JDK_VERSION
# BouncyCastle FIPS arguments
ARG FIPS_ENABLED
ARG BC_FIPS_VERSION
ARG BCTLS_FIPS_VERSION
ARG BCPKIX_FIPS_VERSION
ARG BCUTIL_FIPS_VERSION
#JAVA env values for install_java.sh
ENV AZUL_ZULU_VERSION=${AZUL_ZULU_VERSION}
ENV JDK_VERSION=${JDK_VERSION}
LABEL maintainer="semoss@semoss.org"
ENV TOMCAT_HOME=${TOMCAT_HOME}
ENV JAVA_HOME=${JAVA_HOME}
ENV PATH=$PATH:$MAVEN_HOME/bin:$TOMCAT_HOME/bin:$JAVA_HOME/bin
RUN printenv | grep -E '^(JAVA_HOME|TOMCAT_HOME|MAVEN_HOME|PATH)=' | awk '{print "export " $0}' >> /opt/set_env.env
#COPY . /root/
# Pulling files from the SEMOSS/semoss-artifacts project
RUN apt-get update -y \
&& apt-get install -y lsof git \
&& cd tmp/ && git clone https://github.com/SEMOSS/semoss-artifacts \
&& cd semoss-artifacts \
&& pwd \
&& ls -als artifacts/ \
&& chmod 777 artifacts/dockerBuilder_scripts/*
# fetch <out> <url>...: try each mirror in order with a short timeout. archive.apache.org drops
# connections from the CI runners at times, and bare wget then retries for ~45 min before failing.
RUN fetch() { out="$1"; shift; for url in "$@"; do wget --timeout=30 --tries=2 -O "$out" "$url" && return 0; echo "fetch failed: $url" >&2; done; return 1; } \
&& apt-get -y install apt-transport-https ca-certificates git wget dirmngr gnupg software-properties-common \
&& cd ~/ \
&& apt-get -y install wget procps git \
&& echo "Creating directory for ${JAVA_HOME}.." \
&& mkdir -p $JAVA_HOME \
&& cd /tmp/semoss-artifacts/artifacts/dockerBuilder_scripts/ \
&& chmod +x install_java.sh \
&& /bin/bash install_java.sh \
&& java -version \
&& fetch apache-tomcat-${TOMCAT_VERSION}.tar.gz \
https://repo1.maven.org/maven2/org/apache/tomcat/tomcat/${TOMCAT_VERSION}/tomcat-${TOMCAT_VERSION}.tar.gz \
https://archive.apache.org/dist/tomcat/tomcat-11/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz \
&& tar -zxvf apache-tomcat-${TOMCAT_VERSION}.tar.gz \
&& mkdir $TOMCAT_HOME \
&& mv apache-tomcat-${TOMCAT_VERSION}/* $TOMCAT_HOME/ \
&& rm -r apache-tomcat-${TOMCAT_VERSION}/ \
&& rm -rf $TOMCAT_HOME/webapps/* \
&& rm apache-tomcat-${TOMCAT_VERSION}.tar.gz \
&& rm $TOMCAT_HOME/conf/server.xml \
&& rm $TOMCAT_HOME/conf/web.xml \
&& cp web.xml $TOMCAT_HOME/conf/web.xml \
&& cp server.xml $TOMCAT_HOME/conf/server.xml \
&& chmod +x config.sh \
&& /bin/bash config.sh \
&& echo 'CATALINA_PID="$CATALINA_BASE/bin/catalina.pid"' > $TOMCAT_HOME/bin/setenv.sh \
&& echo '# Let ${VAR} / ${VAR:-default} in server.xml and context.xml resolve against' >> $TOMCAT_HOME/bin/setenv.sh \
&& echo '# container environment variables. Tomcat keeps its SystemPropertySource too,' >> $TOMCAT_HOME/bin/setenv.sh \
&& echo '# so -D overrides still work. Used by the <Manager> in conf/context.xml.' >> $TOMCAT_HOME/bin/setenv.sh \
&& echo 'CATALINA_OPTS="$CATALINA_OPTS -Dorg.apache.tomcat.util.digester.PROPERTY_SOURCE=org.apache.tomcat.util.digester.EnvironmentPropertySource"' >> $TOMCAT_HOME/bin/setenv.sh \
&& fetch apache-maven-3.10.0-bin.tar.gz \
https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.10.0/apache-maven-3.10.0-bin.tar.gz \
https://archive.apache.org/dist/maven/maven-3/3.10.0/binaries/apache-maven-3.10.0-bin.tar.gz \
&& tar -zxvf apache-maven-*.tar.gz \
&& mkdir /opt/apache-maven-3.10.0 \
&& mv apache-maven-3.10.0/* /opt/apache-maven-3.10.0/ \
&& rm -r apache-maven-3.10.0 \
&& rm apache-maven-3.10.0-bin.tar.gz \
&& apt-get -y install nano \
&& echo '#!/bin/sh' > $TOMCAT_HOME/bin/start.sh \
&& echo 'exec catalina.sh run' >> $TOMCAT_HOME/bin/start.sh \
&& echo '#!/bin/sh' > $TOMCAT_HOME/bin/stop.sh \
&& echo 'shutdown.sh -force' >> $TOMCAT_HOME/bin/stop.sh \
&& chmod 777 $TOMCAT_HOME/bin/*.sh \
&& chmod 777 /opt/apache-maven-3.10.0/bin/*.cmd \
&& apt-get clean all
# OpenShell CLI (and its gateway package), used by SANDBOX_MODE=OPENSHELL. The runtime images copy
# /usr/bin/openshell from here; the gateway itself is not run in them.
RUN apt-get update -y \
&& apt-get install -y --no-install-recommends curl ca-certificates \
&& curl -LsSf https://raw.githubusercontent.com/NVIDIA/OpenShell/main/install.sh | sh \
&& rm -rf /var/lib/apt/lists/*
# ---------------------------------------------------------------------------
# FIPS 140-3 wiring. Entirely skipped when FIPS_ENABLED != true.
#
# Step order matters: the truststore is converted BEFORE the provider swap,
# because JKS integrity checking is SHA-1 based and keytool can no longer read
# the source store once approved-only mode is in force.
# ---------------------------------------------------------------------------
RUN if [ "$FIPS_ENABLED" != "true" ]; then echo "FIPS_ENABLED=$FIPS_ENABLED - skipping FIPS wiring"; exit 0; fi; \
set -eux; \
\
# 1. Stage the provider jars. Hashes are pinned here on purpose rather than
# fetched alongside the jars - pulling an artifact and its checksum from
# the same source verifies nothing. A mismatch fails the build, which is
# the intended behavior for a compliance boundary.
M2=https://repo1.maven.org/maven2/org/bouncycastle; \
mkdir -p ${JAVA_HOME}/lib/fips; \
cd ${JAVA_HOME}/lib/fips; \
wget -q $M2/bc-fips/${BC_FIPS_VERSION}/bc-fips-${BC_FIPS_VERSION}.jar; \
wget -q $M2/bctls-fips/${BCTLS_FIPS_VERSION}/bctls-fips-${BCTLS_FIPS_VERSION}.jar; \
wget -q $M2/bcpkix-fips/${BCPKIX_FIPS_VERSION}/bcpkix-fips-${BCPKIX_FIPS_VERSION}.jar; \
wget -q $M2/bcutil-fips/${BCUTIL_FIPS_VERSION}/bcutil-fips-${BCUTIL_FIPS_VERSION}.jar; \
{ echo "f55fc476ede820d5c03fe87e6ced5bb0095e1dc30b387445846a3ff0274f8c0e bc-fips-${BC_FIPS_VERSION}.jar"; \
echo "d2233207daecc2dfccdc317a53bd874b12c21a6dc5be78e390d8826eb726194c bctls-fips-${BCTLS_FIPS_VERSION}.jar"; \
echo "cf161ba2f6b4505816f4661663b3e0771184a50ee5b178a0018c817b336f0e5c bcpkix-fips-${BCPKIX_FIPS_VERSION}.jar"; \
echo "f44904f61552c823bcc3686a24052ee3135212d547754eaa2ff6532e210103cb bcutil-fips-${BCUTIL_FIPS_VERSION}.jar"; } > sha256.manifest; \
sha256sum -c sha256.manifest; \
rm sha256.manifest; \
\
# 2. Convert the truststore while the stock providers can still read JKS.
keytool -importkeystore -noprompt \
-srckeystore ${JAVA_HOME}/lib/security/cacerts -srcstoretype JKS -srcstorepass changeit \
-destkeystore ${JAVA_HOME}/lib/security/cacerts.bcfks -deststoretype BCFKS -deststorepass changeit \
-providername BCFIPS \
-providerclass org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider \
-providerpath ${JAVA_HOME}/lib/fips/bc-fips-${BC_FIPS_VERSION}.jar; \
\
# 3. Write a SEPARATE provider list at java.security.fips and leave the JDK
# default untouched. setenv.sh selects it with
# -Djava.security.properties==<file>, so it applies to the Tomcat JVM only.
#
# Editing java.security in place breaks every other JVM in the image: the
# provider jars are on CLASSPATH via setenv.sh, which only catalina.sh
# sources, so Maven (own -classpath), keytool and the Playwright installer
# would load SUN alone and die with "Default SSLContext not available".
#
# SUN must stay in the list: BCFIPS seeds its DRBG from a core SecureRandom,
# and without SUN that lookup recurses back into BCFIPS and dies with
# StackOverflowError. BCFIPS supplies X.509 CertificateFactory and PKIX
# CertPathValidator itself, so SUN is only an entropy seed source, which is
# fine for compliance - the approved DRBG is still BCFIPS's. Consequence:
# SUN also serves MD5, so approved-only mode cannot block MD5 for
# application code. That has to be caught by code review.
#
# The ssl.*ManagerFactory.algorithm defaults are rewritten to PKIX in the
# same pass. The JDK ships ssl.KeyManagerFactory.algorithm=SunX509, an
# algorithm name only SunJSSE implements - BCJSSE registers PKIX (and its
# X.509 alias) and nothing else. With SunJSSE dropped from the provider
# list, KeyManagerFactory.getDefaultAlgorithm() still returns SunX509 and
# every default-constructed SSLContext dies with NoSuchAlgorithmException,
# typically surfacing as "Default SSLContext not available" on the first
# outbound HTTPS call rather than at startup.
#
# The existing lines are deleted rather than substituted in place: a
# substitution silently no-ops if the JDK ever stops shipping the property,
# which would put us back on the broken default with a green build.
sed -E -e '/^security\.provider\.[0-9]+=/d' -e '/^ssl\.(Key|Trust)ManagerFactory\.algorithm=/d' \
${JAVA_HOME}/conf/security/java.security \
> ${JAVA_HOME}/conf/security/java.security.fips; \
printf '%s\n' \
'security.provider.1=org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider' \
'security.provider.2=org.bouncycastle.jsse.provider.BouncyCastleJsseProvider fips:BCFIPS' \
'security.provider.3=SUN' \
'ssl.KeyManagerFactory.algorithm=PKIX' \
'ssl.TrustManagerFactory.algorithm=PKIX' \
>> ${JAVA_HOME}/conf/security/java.security.fips; \
\
# 4. Providers named in java.security load from the SYSTEM classloader, so the
# jars go on CLASSPATH. $TOMCAT_HOME/lib would not work - that is Tomcat's
# common classloader, which is not consulted for provider loading.
# Fail early if TOMCAT_HOME is unset rather than appending to /bin/setenv.sh,
# and create the file if an earlier step has not already.
test -n "$TOMCAT_HOME" || { echo "TOMCAT_HOME is not set"; exit 1; }; \
mkdir -p "$TOMCAT_HOME/bin"; \
touch "$TOMCAT_HOME/bin/setenv.sh"; \
{ echo ''; \
echo '# --- FIPS 140-3 ---'; \
echo 'FIPS_LIB="$JAVA_HOME/lib/fips"'; \
echo '# Prepended, not assigned, so a container-level CLASSPATH survives. The'; \
echo '# provider jars must come first regardless.'; \
echo "CLASSPATH=\"\$FIPS_LIB/bc-fips-${BC_FIPS_VERSION}.jar:\$FIPS_LIB/bctls-fips-${BCTLS_FIPS_VERSION}.jar:\$FIPS_LIB/bcpkix-fips-${BCPKIX_FIPS_VERSION}.jar:\$FIPS_LIB/bcutil-fips-${BCUTIL_FIPS_VERSION}.jar\${CLASSPATH:+:\$CLASSPATH}\""; \
echo 'export CLASSPATH'; \
echo 'CATALINA_OPTS="$CATALINA_OPTS -Djava.security.properties==$JAVA_HOME/conf/security/java.security.fips"'; \
echo 'CATALINA_OPTS="$CATALINA_OPTS -Dorg.bouncycastle.fips.approved_only=true"'; \
echo 'CATALINA_OPTS="$CATALINA_OPTS -Djavax.net.ssl.trustStore=$JAVA_HOME/lib/security/cacerts.bcfks"'; \
echo 'CATALINA_OPTS="$CATALINA_OPTS -Djavax.net.ssl.trustStoreType=BCFKS"'; \
echo '# BCFKS is registered by BCFIPS only. Naming the provider explicitly'; \
echo '# keeps TrustStoreManager off the "try every provider in order" path,'; \
echo '# where SUN is asked for BCFKS first and the resulting'; \
echo '# KeyStoreException is swallowed into an empty trust anchor set - TLS'; \
echo '# then fails at handshake time with PKIX path building failed instead'; \
echo '# of at load time.'; \
echo 'CATALINA_OPTS="$CATALINA_OPTS -Djavax.net.ssl.trustStoreProvider=BCFIPS"'; \
echo 'CATALINA_OPTS="$CATALINA_OPTS -Djavax.net.ssl.trustStorePassword=changeit"'; \
echo '# Session id entropy. conf/context.xml reads these; without them Tomcat'; \
echo '# defaults to SHA1PRNG, which BCFIPS does not implement, so JSESSIONID'; \
echo '# generation silently falls through to SUN.'; \
echo ': "${SESSION_SECURE_RANDOM_PROVIDER:=BCFIPS}"'; \
echo ': "${SESSION_SECURE_RANDOM_ALGORITHM:=DEFAULT}"'; \
echo 'export SESSION_SECURE_RANDOM_PROVIDER SESSION_SECURE_RANDOM_ALGORITHM'; } \
>> "$TOMCAT_HOME/bin/setenv.sh"
FROM scratch AS final
ARG JAVA_HOME
ARG TOMCAT_HOME
ARG MAVEN_HOME
ARG FIPS_ENABLED
LABEL maintainer="semoss@semoss.org"
# Lets downstream builds and running containers self-identify:
# docker inspect -f '{{index .Config.Labels "org.semoss.fips"}}' <image>
LABEL org.semoss.fips="${FIPS_ENABLED}"
ENV SEMOSS_FIPS=${FIPS_ENABLED}
ENV TOMCAT_HOME=$TOMCAT_HOME
ENV JAVA_HOME=$JAVA_HOME
ENV PATH=$PATH:$MAVEN_HOME/bin:$TOMCAT_HOME/bin:$JAVA_HOME/bin
COPY --from=builder / /
WORKDIR $TOMCAT_HOME/webapps
CMD ["start.sh"]