HV-VES expects passwords to be placed in files
[integration/csit.git] / plans / dcaegen2-collectors-hv-ves / testsuites / collector / ssl / gen-certs.sh
1 #!/usr/bin/env bash
2 # ============LICENSE_START=======================================================
3 # csit-dcaegen2-collectors-hv-ves
4 # ================================================================================
5 # Copyright (C) 2018 NOKIA
6 # ================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #      http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
19
20
21 set -eu -o pipefail -o xtrace
22
23 STORE_PASS=onaponap
24 CN_PREFIX=dcaegen2-hvves
25 DNAME_PREFIX="C=PL,ST=DL,L=Wroclaw,O=Nokia,OU=MANO,CN=${CN_PREFIX}"
26
27 store_opts="-storetype PKCS12 -storepass ${STORE_PASS} -noprompt"
28
29 function gen_key() {
30   local key_name="$1"
31   local ca="$2"
32   local keystore="-keystore ${key_name}.p12 ${store_opts}"
33   keytool -genkey -alias ${key_name} \
34       ${keystore} \
35       -keyalg RSA \
36       -validity 730 \
37       -keysize 2048 \
38       -dname "${DNAME_PREFIX}-${key_name}"
39   keytool -import -trustcacerts -alias ${ca} -file ${ca}.crt ${keystore}
40
41   keytool -certreq -alias ${key_name} -keyalg RSA ${keystore} | \
42       keytool -alias ${ca} -gencert -ext "san=dns:${CN_PREFIX}-${ca}" ${store_opts} -keystore ${ca}.p12 | \
43       keytool -alias ${key_name} -importcert ${keystore}
44
45   printf ${STORE_PASS} > ${key_name}.pass
46 }
47
48
49 function gen_ca() {
50   local ca="$1"
51   keytool -genkeypair ${store_opts} -alias ${ca} -dname "${DNAME_PREFIX}-${ca}" -keystore ${ca}.p12
52   keytool -export -alias ${ca} -file ${ca}.crt ${store_opts} -keystore ${ca}.p12
53 }
54
55 function gen_truststore() {
56   local name="$1"
57   local trusted_ca="$2"
58   keytool -import -trustcacerts -alias ca -file ${trusted_ca}.crt ${store_opts} -keystore ${name}.p12
59   printf ${STORE_PASS} > ${name}.pass
60 }
61
62 function clean() {
63   rm -f *.crt *.p12 *.pass
64 }
65
66 if [[ $# -eq 0 ]]; then
67   gen_ca ca
68   gen_ca untrustedca
69   gen_truststore trust ca
70   gen_truststore untrustedtrust untrustedca
71   gen_key client ca
72   gen_key server ca
73   gen_key untrustedclient untrustedca
74 elif [[ $1 == "clean" ]]; then
75   clean
76 else
77   echo "usage: $0 [clean]"
78   exit 1
79 fi