-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathissue_cert_template.sh
executable file
·149 lines (108 loc) · 4.47 KB
/
issue_cert_template.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
!/bin/bash
# Ensure the script is executed with a HOST_STRING argument
if [ -z "$1" ]; then
printf "\n${RED}%s${NC}\n" "Error: HOST_STRING is required. Please provide a host name."
printf "${YELLOW}%s${NC}\n" "Usage: ./issue_cert_template.sh <hostname>"
echo
exit 1
fi
# edit env.sh as required. Refer to README.md for more details.
source ./env.sh
# Override the HOST_STRING in env.sh with the passed argument ($1)
HOST="$1"
OUT_DIR="${SUBJECT_CN}"
OUT_FILE="${HOST}_csr_signed_output_$(date +%Y%m%d%H%M%S).json"
# Create output directory if it doesn't exist
if [ ! -d "${OUT_DIR}" ]; then
mkdir "${OUT_DIR}"
printf "\n${GREEN}%s${NC}\n" "Created output directory: ${OUT_DIR}"
fi
# Issue certificate
printf "\n${CYAN}%s${NC}\n" "*** Issuing Certificate for ${SUBJECT_CN} ***"
vault write "${NO_TLS}" -format=json pki_int/issue/"${VAULT_ROLE}" \
common_name="${SUBJECT_CN}" \
ip_sans="${IP_SAN1}" \
alt_names="${ALT_NAME1}" \
key_type="${KEY_TYPE}" \
key_bits="${KEY_BITS}" \
ttl="${TTL}" | tee "${OUT_DIR}/${OUT_FILE}"
# Extract certificate and private key
printf "\n${CYAN}%s${NC}\n" "*** Extracting Certificate and Private Key ***"
jq -r '.data.certificate,.data.issuing_ca' "${OUT_DIR}/${OUT_FILE}" > \
"${OUT_DIR}/${HOST}_cert.crt"
jq -r '.data.private_key' "$OUT_DIR/${OUT_FILE}" >"${OUT_DIR}/${HOST}_cert.key"
# Create a timestamp file
touch "${OUT_DIR}/created_$(date +"%Y-%m-%d--%H-%M-%S")"
printf "\n${GREEN}%s${NC}\n" "Timestamp file created in ${OUT_DIR}"
# Provide usage instructions
printf "\n${CYAN}%s${NC}\n" "*** To view ${HOST}_cert.key private certificate execute this command: ***"
printf "${MAGENTA}%s${NC}\n" "openssl pkey -in ${OUT_DIR}/${HOST}_cert.key -check"
printf "\n${CYAN}%s${NC}\n" "*** To view ${HOST}_cert.key public certificate execute this command: ***"
printf "${MAGENTA}%s${NC}\n" "openssl pkey -in ${OUT_DIR}/${HOST}_cert.key -pubout"
printf "\n${CYAN}%s${NC}\n" "*** To view ${HOST}_cert.crt public certificate execute this command: ***"
printf "${MAGENTA}%s${NC}\n" "openssl x509 -in ${OUT_DIR}/${HOST}_cert.crt -text -noout"
# Final confirmation
printf "\n${GREEN}%s${NC}\n" "✅ Certificate and key generated successfully for ${SUBJECT_CN}"
### Docker setup
# Concatenate Certificates
mkdir ./docker
CHAIN_CERT="${OUT_DIR}/${HOST}_chain_cert.crt"
cp "${OUT_DIR}/${HOST}_cert.crt" "${CHAIN_CERT}"
printf "\n${GREEN}%s${NC}\n" "✅ Concatenated leaf and intermediate certificates into: ${CHAIN_CERT}"
# Copy concatenated chain cert and key to Docker directory
cp "${CHAIN_CERT}" ./docker/
cp "${OUT_DIR}/${HOST}_cert.key" ./docker/
printf "\n${YELLOW}%s${NC}\n" "*** Copied concatenated certificate to ./docker/${HOST}_chain_cert.crt ***"
printf "${YELLOW}%s${NC}\n" "*** Copied private key to ./docker/${HOST}_cert.key ***"
# Generate Dockerfile
cat <<EOF >./docker/Dockerfile
# Dockerfile for serving certificates via Nginx
FROM nginx:alpine
# Install OpenSSL for debugging
RUN apk add --no-cache openssl
# Create SSL directory
RUN mkdir -p /etc/nginx/ssl
# Copy certificate chain and key
COPY ${HOST}_chain_cert.crt /etc/nginx/ssl/server.crt
COPY ${HOST}_cert.key /etc/nginx/ssl/server.key
COPY nginx.conf /etc/nginx/nginx.conf
# Expose HTTPS port
EXPOSE 443
CMD ["nginx", "-g", "daemon off;"]
EOF
printf "\n${GREEN}%s${NC}\n" "✅ Dockerfile generated at ./docker/Dockerfile"
# Generate nginx.conf
cat <<EOF >./docker/nginx.conf
# nginx.conf for TLS termination with certificate chain
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
}
EOF
printf "\n${GREEN}%s${NC}\n" "✅ nginx.conf generated at ./docker/nginx.conf"
# Build and Run Docker Container
# Navigate to docker directory
cd ./docker || exit
# Build Docker Image
docker build -t nginx-tls-cert .
# Run Docker Container
docker run -d --name nginx-tls-cert -p 443:443 nginx-tls-cert
printf "\n${GREEN}%s${NC}\n" "✅ Docker container is running with HTTPS enabled."
printf "${CYAN}%s${NC}\n" "🌐 Access the server via: https://localhost"
# Verify SSL Chain (Optional)
printf "\n${CYAN}%s${NC}\n" "*** Verify SSL certificate with OpenSSL command: ***"
printf "${MAGENTA}%s${NC}\n" "openssl s_client -connect localhost:443 -showcerts"