/
import_certificates.sh
executable file
·59 lines (50 loc) · 2.09 KB
/
import_certificates.sh
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
#!/bin/bash
#
# Copyright (C) 2020 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# License-Filename: LICENSE
# This script imports all certificate files matching the path / file prefix provided as the argument to both the JVM
# keystore and the operating system's certificate store.
FILE_PREFIX=$1
if [ -z "$FILE_PREFIX" ]; then
echo "No certificates specified. Skipping the import."
exit 0
fi
echo "Importing certificates from $FILE_PREFIX..."
KEYTOOL=$(realpath "$(command -v keytool)")
KEYTOOL_DIR=$(dirname "$KEYTOOL")
for KEYSTORE_CANDIDATE in "$KEYTOOL_DIR/../lib/security/cacerts" "$KEYTOOL_DIR/../jre/lib/security/cacerts"; do
KEYSTORE_CANDIDATE_PATH=$(realpath -m "$KEYSTORE_CANDIDATE")
if [ -f "$KEYSTORE_CANDIDATE_PATH" ]; then
KEYSTORE=$KEYSTORE_CANDIDATE_PATH
break
fi
done
if [ -n "$KEYSTORE" ]; then
for CRT_FILE in "$FILE_PREFIX"*; do
echo "Adding the following certificate from '$CRT_FILE' to the JVM's certificate store at '$KEYSTORE':"
cat "$CRT_FILE"
ALIAS=$(basename "$CRT_FILE" .crt)
$KEYTOOL -importcert -noprompt -trustcacerts -alias "$ALIAS" -file "$CRT_FILE" \
-keystore "$KEYSTORE" -storepass changeit
done
else
echo "No JVM keystore found, skipping the import."
fi
# Also add the certificates to the system certificates, e.g. for curl to work.
echo "Adding certificates to the system certificates..."
cp -r "$FILE_PREFIX"* /usr/local/share/ca-certificates/
update-ca-certificates