Merge "[COMMON] Add custom certs into AAF truststore"
[oom.git] / kubernetes / common / cert-wrapper / resources / import-custom-certs.sh
1 #!/bin/bash
2 {{/*
3
4 # Copyright © 2020 Bell Canada
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 */}}
18
19 CERTS_DIR=${CERTS_DIR:-/certs}
20 MORE_CERTS_DIR=${MORE_CERTS_DIR:-/more_certs}
21 WORK_DIR=${WORK_DIR:-/updatedTruststore}
22 ONAP_TRUSTSTORE=${ONAP_TRUSTSTORE:-truststoreONAPall.jks}
23 JRE_TRUSTSTORE=${JRE_TRUSTSTORE:-$JAVA_HOME/lib/security/cacerts}
24 TRUSTSTORE_OUTPUT_FILENAME=${TRUSTSTORE_OUTPUT_FILENAME:-truststore.jks}
25
26 mkdir -p $WORK_DIR
27
28 # Decrypt and move relevant files to WORK_DIR
29 for f in $CERTS_DIR/*; do
30   export canonical_name_nob64=$(echo $f | sed 's/.*\/\([^\/]*\)/\1/')
31   export canonical_name_b64=$(echo $f | sed 's/.*\/\([^\/]*\)\(\.b64\)/\1/')
32   if [ "$AAF_ENABLED" = "false" ] && [ "$canonical_name_b64" = "$ONAP_TRUSTSTORE" ]; then
33     # Dont use onap truststore when aaf is disabled
34     continue
35   fi
36   if [ "$AAF_ENABLED" = "false" ] && [ "$canonical_name_nob64" = "$ONAP_TRUSTSTORE" ]; then
37     # Dont use onap truststore when aaf is disabled
38     continue
39   fi
40   if [ ${f: -3} = ".sh" ]; then
41     continue
42   fi
43   if [ ${f: -4} = ".b64" ]
44     then
45       base64 -d $f > $WORK_DIR/`basename $f .b64`
46     else
47       cp $f $WORK_DIR/.
48   fi
49 done
50
51 for f in $MORE_CERTS_DIR/*; do
52   if [ ${f: -4} == ".pem" ]
53     then
54       cp $f $WORK_DIR/.
55   fi
56 done
57
58 # Prepare truststore output file
59 if [ "$AAF_ENABLED" = "true" ]
60   then
61     echo "AAF is enabled, use 'AAF' truststore"
62     export TRUSTSTORE_OUTPUT_FILENAME=${ONAP_TRUSTSTORE}
63   else
64     echo "AAF is disabled, using JRE truststore"
65     cp $JRE_TRUSTSTORE $WORK_DIR/$TRUSTSTORE_OUTPUT_FILENAME
66 fi
67
68 # Import Custom Certificates
69 for f in $WORK_DIR/*; do
70   if [ ${f: -4} = ".pem" ]; then
71     echo "importing certificate: $f"
72     keytool -import -file $f -alias `basename $f` -keystore $WORK_DIR/$TRUSTSTORE_OUTPUT_FILENAME -storepass $TRUSTSTORE_PASSWORD -noprompt
73     if [ $? != 0 ]; then
74       echo "failed importing certificate: $f"
75       exit 1
76     fi
77   fi
78 done