Skip to content

Commit df47494

Browse files
authored
chore: Import certificates on the start (#240)
* chore: Import certificates on the start Signed-off-by: Anatolii Bazko <[email protected]>
1 parent 4b34b0b commit df47494

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

base/ubi10/entrypoint.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,46 @@ replace_user_home() {
66
echo "$1" | sed "s|^/home/tooling|$HOME|"
77
}
88

9+
jdk_import_ca_bundle() {
10+
CA_BUNDLE="${JDK_CA_BUNDLE:-/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem}"
11+
KEYSTORE_PASSWORD="${JDK_KEYSTORE_PASSWORD:-changeit}"
12+
13+
if ! command -v keytool >/dev/null 2>&1; then
14+
return
15+
fi
16+
17+
if [ ! -f "$CA_BUNDLE" ]; then
18+
echo "[jdk] Failed to import CA certificates from ${CA_BUNDLE}. File doesn't exist"
19+
return
20+
fi
21+
22+
bundle_name=$(basename "$CA_BUNDLE")
23+
certs_imported=0
24+
cert_index=0
25+
tmp_file=/tmp/cert.pem
26+
is_cert=false
27+
echo "[jdk] Importing certificates..."
28+
while IFS= read -r line; do
29+
if [ "$line" = "-----BEGIN CERTIFICATE-----" ]; then
30+
is_cert=true
31+
cert_index=$((cert_index+1))
32+
echo "$line" > ${tmp_file}
33+
elif [ "$line" = "-----END CERTIFICATE-----" ]; then
34+
is_cert=false
35+
echo "$line" >> ${tmp_file}
36+
if keytool -import -trustcacerts -cacerts -storepass "$KEYSTORE_PASSWORD" -noprompt -alias "${bundle_name}_${cert_index}" -file $tmp_file; then
37+
certs_imported=$((certs_imported+1))
38+
fi
39+
certs_imported=$((certs_imported+1))
40+
elif [ "$is_cert" = true ]; then
41+
echo "$line" >> ${tmp_file}
42+
fi
43+
done < "$CA_BUNDLE"
44+
45+
echo "[jdk] Imported ${certs_imported} certificates from ${CA_BUNDLE}"
46+
rm -f $tmp_file
47+
}
48+
949
# Ensure $HOME exists when starting
1050
if [ ! -d "${HOME}" ]; then
1151
mkdir -p "${HOME}"
@@ -219,4 +259,6 @@ if [ -d /home/tooling/.config ]; then
219259
echo "Finished creating .config symlinks."
220260
fi
221261

262+
jdk_import_ca_bundle &
263+
222264
exec "$@"

base/ubi9/entrypoint.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,46 @@ replace_user_home() {
55
echo "$1" | sed "s|^/home/tooling|$HOME|"
66
}
77

8+
jdk_import_ca_bundle() {
9+
CA_BUNDLE="${JDK_CA_BUNDLE:-/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem}"
10+
KEYSTORE_PASSWORD="${JDK_KEYSTORE_PASSWORD:-changeit}"
11+
12+
if ! command -v keytool >/dev/null 2>&1; then
13+
return
14+
fi
15+
16+
if [ ! -f "$CA_BUNDLE" ]; then
17+
echo "[jdk] Failed to import CA certificates from ${CA_BUNDLE}. File doesn't exist"
18+
return
19+
fi
20+
21+
bundle_name=$(basename "$CA_BUNDLE")
22+
certs_imported=0
23+
cert_index=0
24+
tmp_file=/tmp/cert.pem
25+
is_cert=false
26+
echo "[jdk] Importing certificates..."
27+
while IFS= read -r line; do
28+
if [ "$line" = "-----BEGIN CERTIFICATE-----" ]; then
29+
is_cert=true
30+
cert_index=$((cert_index+1))
31+
echo "$line" > ${tmp_file}
32+
elif [ "$line" = "-----END CERTIFICATE-----" ]; then
33+
is_cert=false
34+
echo "$line" >> ${tmp_file}
35+
if keytool -import -trustcacerts -cacerts -storepass "$KEYSTORE_PASSWORD" -noprompt -alias "${bundle_name}_${cert_index}" -file $tmp_file; then
36+
certs_imported=$((certs_imported+1))
37+
fi
38+
certs_imported=$((certs_imported+1))
39+
elif [ "$is_cert" = true ]; then
40+
echo "$line" >> ${tmp_file}
41+
fi
42+
done < "$CA_BUNDLE"
43+
44+
echo "[jdk] Imported ${certs_imported} certificates from ${CA_BUNDLE}"
45+
rm -f $tmp_file
46+
}
47+
848
# Ensure $HOME exists when starting
949
if [ ! -d "${HOME}" ]; then
1050
mkdir -p "${HOME}"
@@ -218,4 +258,6 @@ if [ -d /home/tooling/.config ]; then
218258
echo "Finished creating .config symlinks."
219259
fi
220260

261+
jdk_import_ca_bundle &
262+
221263
exec "$@"

0 commit comments

Comments
 (0)