1
1
#! /usr/bin/env sh
2
- # Converted to POSIX shell to avoid the need for bash in the image
2
+ # This script defines `sh` as the interpreter, which is available in all POSIX environments. However, it might get
3
+ # started with `bash` as the shell to support dotted.environment.variable.names which are not supported by POSIX, but
4
+ # are supported by `sh` in some Linux flavours.
3
5
4
6
set -e
5
7
8
+ TMPDIR=${TMPDIR:-/ tmp}
9
+
6
10
# JDK truststore location
7
- CACERT =$JAVA_HOME /lib/security/cacerts
11
+ JRE_CACERTS_PATH =$JAVA_HOME /lib/security/cacerts
8
12
9
13
# JDK8 puts its JRE in a subdirectory
10
14
if [ -f " $JAVA_HOME /jre/lib/security/cacerts" ]; then
11
- CACERT =$JAVA_HOME /jre/lib/security/cacerts
15
+ JRE_CACERTS_PATH =$JAVA_HOME /jre/lib/security/cacerts
12
16
fi
13
17
14
18
# Opt-in is only activated if the environment variable is set
15
19
if [ -n " $USE_SYSTEM_CA_CERTS " ]; then
16
20
17
- if [ ! -w /tmp ]; then
18
- echo " Using additional CA certificates requires write permissions to /tmp . Cannot create truststore."
21
+ if [ ! -w " $TMPDIR " ]; then
22
+ echo " Using additional CA certificates requires write permissions to $TMPDIR . Cannot create truststore."
19
23
exit 1
20
24
fi
21
25
22
26
# Figure out whether we can write to the JVM truststore. If we can, we'll add the certificates there. If not,
23
27
# we'll use a temporary truststore.
24
- if [ ! -w " $CACERT " ]; then
28
+ if [ ! -w " $JRE_CACERTS_PATH " ]; then
25
29
# We cannot write to the JVM truststore, so we create a temporary one
26
- CACERT_NEW =$( mktemp)
27
- echo " Using a temporary truststore at $CACERT_NEW "
28
- cp $CACERT $CACERT_NEW
29
- CACERT= $CACERT_NEW
30
+ JRE_CACERTS_PATH_NEW =$( mktemp)
31
+ echo " Using a temporary truststore at $JRE_CACERTS_PATH_NEW "
32
+ cp " $JRE_CACERTS_PATH " " $JRE_CACERTS_PATH_NEW "
33
+ JRE_CACERTS_PATH= $JRE_CACERTS_PATH_NEW
30
34
# If we use a custom truststore, we need to make sure that the JVM uses it
31
- export JAVA_TOOL_OPTIONS=" ${JAVA_TOOL_OPTIONS} -Djavax.net.ssl.trustStore=${CACERT } -Djavax.net.ssl.trustStorePassword=changeit"
35
+ export JAVA_TOOL_OPTIONS=" ${JAVA_TOOL_OPTIONS} -Djavax.net.ssl.trustStore=${JRE_CACERTS_PATH } -Djavax.net.ssl.trustStorePassword=changeit"
32
36
fi
33
37
34
38
tmp_store=$( mktemp)
@@ -37,14 +41,17 @@ if [ -n "$USE_SYSTEM_CA_CERTS" ]; then
37
41
trust extract --overwrite --format=java-cacerts --filter=ca-anchors --purpose=server-auth " $tmp_store "
38
42
39
43
# Add the system CA certificates to the JVM truststore.
40
- keytool -importkeystore -destkeystore " $CACERT " -srckeystore " $tmp_store " -srcstorepass changeit -deststorepass changeit -noprompt # >/dev/null
44
+ keytool -importkeystore -destkeystore " $JRE_CACERTS_PATH " -srckeystore " $tmp_store " -srcstorepass changeit -deststorepass changeit -noprompt
45
+
46
+ # Clean up the temporary truststore
47
+ rm " $tmp_store "
41
48
42
49
# Import the additional certificate into JVM truststore
43
50
for i in /certificates/* crt; do
44
51
if [ ! -f " $i " ]; then
45
52
continue
46
53
fi
47
- keytool -import -noprompt -alias " $( basename " $i " .crt) " -file " $i " -keystore " $CACERT " -storepass changeit # >/dev/null
54
+ keytool -import -noprompt -alias " $( basename " $i " .crt) " -file " $i " -keystore " $JRE_CACERTS_PATH " -storepass changeit # >/dev/null
48
55
done
49
56
50
57
# Add additional certificates to the system CA store. This requires write permissions to several system
@@ -68,12 +75,12 @@ if [ -n "$USE_SYSTEM_CA_CERTS" ]; then
68
75
fi
69
76
70
77
# UBI/CentOS
71
- if which update-ca-trust > /dev/null; then
78
+ if command -v update-ca-trust > /dev/null; then
72
79
update-ca-trust
73
80
fi
74
81
75
82
# Ubuntu/Alpine
76
- if which update-ca-certificates > /dev/null; then
83
+ if command -v update-ca-certificates > /dev/null; then
77
84
update-ca-certificates
78
85
fi
79
86
else
@@ -84,6 +91,6 @@ if [ -n "$USE_SYSTEM_CA_CERTS" ]; then
84
91
fi
85
92
86
93
# Let's provide a variable with the correct path for tools that want or need to use it
87
- export CACERT
94
+ export JRE_CACERTS_PATH
88
95
89
96
exec " $@ "
0 commit comments