-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateCertificates.sh
executable file
·53 lines (41 loc) · 1.28 KB
/
generateCertificates.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
#!/bin/bash
shopt -s expand_aliases
set -euo pipefail
IFS=$'\n\t'
CA_NAME=${1:-}
SERVICES=${2:-}
if [[ -z "$CA_NAME" || -z "${SERVICES}" ]]; then
echo "CA_NAME and SERVICE[,SERVICE,...] parameters required"
echo "Usage: $0 CA_NAME SERVICE[,SERVICE,...]"
exit 1
fi
# Some certstrap options
DEPOT_PATH=certificates
EXPIRES="5 years"
KEY_BITS=4096
alias certstrap='docker run -v "${PWD}/$DEPOT_PATH:/$DEPOT_PATH" -it nbmaiti/certstrap
certstrap_with_opts () {
certstrap --depot-path "${DEPOT_PATH}" "$@"
}
generateCA () {
if [[ -f "${DEPOT_PATH}/${CA_NAME}.crt" ]]; then
echo "CA \"${CA_NAME}\" already exists"
return
fi
echo "Generate root CA \"${CA_NAME}\""
certstrap_with_opts init --passphrase "" --expires "${EXPIRES}" --common-name "${CA_NAME}"
}
requestAndSignCertificate () {
local SERVICE=$1
echo "Create certificate request for ${SERVICE}"
certstrap_with_opts request-cert --passphrase "" --key "${DEPOT_PATH}/${SERVICE}.key" --key-bits "${KEY_BITS}" --common-name "${SERVICE}"
echo "Sign certificate request for ${SERVICE}"
certstrap_with_opts sign --passphrase "" --expires "${EXPIRES}" --CA "${CA_NAME}" "${SERVICE}"
}
mkdir -p ${DEPOT_PATH}
generateCA
SERVICES=${SERVICES//,/$'\n'}
for SERVICE in $SERVICES
do
requestAndSignCertificate "${SERVICE}"
done