forked from shaunaa126/azure-resource-manager-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertcert.sh
executable file
·92 lines (75 loc) · 2.17 KB
/
insertcert.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
#set -e
usage()
{
echo usage: insertcert.sh '<password> <certpemfile> <keypemfile> <cacertpemfile>'
echo The cacertpem file is optional. The template will accept a self-signed cert and key.
}
convertcert()
{
local cert=$1
local key=$2
local pfxfile=$3
local pass=$4
echo Creating PFX $pfxfile
openssl pkcs12 -export -out $pfxfile -inkey $key -in $cert -password pass:$pass 2> /dev/null
if [ $? -eq 1 ]
then
echo problem converting $key and $cert to pfx
exit 1
fi
fingerprint=$(openssl x509 -in $cert -noout -fingerprint | cut -d= -f2 | sed 's/://g' )
}
convertcacert()
{
local cert=$1
local pfxfile=$2
local pass=$3
echo Creating PFX $pfxfile
openssl pkcs12 -export -out $pfxfile -nokeys -in $cert -password pass:$pass 2> /dev/null
if [ $? -eq 1 ]
then
echo problem converting $cert to pfx
exit 1
fi
fingerprint=$(openssl x509 -in $cert -noout -fingerprint | cut -d= -f2 | sed 's/://g' )
}
outputcert()
{
local cert=$1
echo Outputing Base64 encoded $cert
cat $cert | base64 >> tmp.txt
encodedstring=$( cat tmp.txt )
rm -f tmp.txt
}
pwd=$1
certfile=$2
keyfile=$3
cacertfile=$4
certpfxfile=${certfile%.*crt}.pfx
cacertpfxfile=${cacertfile%.*crt}.pfx
# converting SSL cert to pfx
convertcert $certfile $keyfile $certpfxfile $pwd
certprint=$fingerprint
echo $certpfxfile fingerprint is $fingerprint
outputcert $certpfxfile
certstring=$encodedstring
echo $certpfxfile encoded string is $certstring
rm -f $certpfxfile
if [ ! -z $cacertfile ]
then
# converting CA cert to pfx
convertcacert $cacertfile $cacertpfxfile $pwd
cacertprint=$fingerprint
echo $cacertpfxfile fingerprint is $fingerprint
outputcert $cacertpfxfile
cacertstring=$encodedstring
echo $cacertpfxfile encoded string is $cacertstring
rm -f $cacertpfxfile
fi
# make sure pattern substitution succeeds
cp ./mainTemplateParameters.json.template ./mainTemplateParameters.json
# update parameters file
sed -i -e 's|REPLACE_BASEENCODEDPFX|'$certstring'|g' ./mainTemplateParameters.json
sed -i -e 's|REPLACE_PFXPASS|'$pwd'|g' ./mainTemplateParameters.json
echo Done