This commit is contained in:
Andy 2018-07-15 20:17:18 +02:00
parent 987ce068f4
commit 6b47f6be4d
Signed by: arno
GPG Key ID: 9076D5E6B31AE99C
2 changed files with 110 additions and 103 deletions

View File

@ -2,7 +2,7 @@
# Filename: gencert.sh # Filename: gencert.sh
# Description: This script generates x509 server certificate (with all IPs in # Description: This script generates x509 server certificate (with all IPs in
# SAN) signed by a self-signed CA. # SAN) signed by a self-signed CA.
# Version: 1.1 - 2018 July 15 # Version: 1.2 - 2018 July 15
# Author: Andrey Arapov <andrey.arapov@nixaid.com> # Author: Andrey Arapov <andrey.arapov@nixaid.com>
# License: GPLv3 # License: GPLv3
@ -18,18 +18,20 @@ print_help() {
--cakey - CA key name\t\t\t(default: ca.key) --cakey - CA key name\t\t\t(default: ca.key)
--ca - CA cert name\t\t\t(default: ca.crt) --ca - CA cert name\t\t\t(default: ca.crt)
--cadays - CA cert expiration in days\t(default: 3650)\n --cadays - CA cert expiration in days\t(default: 3650)\n
--nosan - do not write SAN records\n" --nosan - do not write SAN records\n
--debug - show extra information\n"
} }
# Parse command line arguments # Parse command line arguments
## ##
parse_arguments() {
# A POSIX variable # A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell. OPTIND=1 # Reset in case getopts has been used previously in the shell.
# read arguments # read arguments
opts=$(getopt \ opts=$(getopt \
--longoptions "help,cn:,key:,cert:,days:,cakey:,ca:,cadays:,nosan," \ --longoptions "help,cn:,key:,cert:,days:,cakey:,ca:,cadays:,nosan,debug" \
--name "$(basename "$0")" \ --name "$(basename "$0")" \
--options "" \ --options "" \
-- "$@" -- "$@"
@ -84,6 +86,11 @@ while [ $# -gt 0 ]; do
shift 2 shift 2
;; ;;
--debug)
ARG_DEBUG=1
shift 2
;;
*) *)
break break
;; ;;
@ -97,14 +104,17 @@ if [ -z "${ARG_CN}" ]; then
fi fi
# For debugging purposes # For debugging purposes
# echo ARG_CN=$ARG_CN if [ $ARG_DEBUG -eq 1 ]; then
# echo ARG_KEY=$ARG_KEY echo ARG_CN=$ARG_CN
# echo ARG_CERT=$ARG_CERT echo ARG_KEY=$ARG_KEY
# echo ARG_DAYS=$ARG_DAYS echo ARG_CERT=$ARG_CERT
# echo ARG_CAKEY=$ARG_CAKEY echo ARG_DAYS=$ARG_DAYS
# echo ARG_CA=$ARG_CA echo ARG_CAKEY=$ARG_CAKEY
# echo ARG_CADAYS=$ARG_CADAYS echo ARG_CA=$ARG_CA
# echo ARG_NOSAN=$ARG_NOSAN echo ARG_CADAYS=$ARG_CADAYS
echo ARG_NOSAN=$ARG_NOSAN
echo ARG_DEBUG=$ARG_DEBUG
fi
# prepare common variables # prepare common variables
## ##
@ -116,15 +126,12 @@ CA_DAYS="${ARG_CADAYS:-3650}"
SERVER_KEY="${ARG_KEY:-private.key}" SERVER_KEY="${ARG_KEY:-private.key}"
SERVER_CERT="${ARG_CERT:-public.crt}" SERVER_CERT="${ARG_CERT:-public.crt}"
DAYS="${ARG_DAYS:-365}" DAYS="${ARG_DAYS:-365}"
}
# set -x
set -e
# install openssl # install openssl
## ##
has_openssl() { has_openssl() {
set +e
type openssl >/dev/null 2>&1 type openssl >/dev/null 2>&1
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
return; return;
@ -154,14 +161,12 @@ has_openssl() {
echo "[${ME}] ERROR: Could not install openssl. Exitting." echo "[${ME}] ERROR: Could not install openssl. Exitting."
exit 1 exit 1
fi fi
set -e
} }
# install getopt # install getopt
## ##
has_getopt() { has_getopt() {
set +e
type getopt >/dev/null 2>&1 type getopt >/dev/null 2>&1
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
return; return;
@ -192,7 +197,6 @@ has_getopt() {
echo "[${ME}] ERROR: Could not install getopt. Exitting." echo "[${ME}] ERROR: Could not install getopt. Exitting."
exit 1 exit 1
fi fi
set -e
} }
# generate openssl config # generate openssl config
@ -250,8 +254,11 @@ gen_server_x509() {
start() { start() {
echo "[${ME}] Started in ${PWD} directory." echo "[${ME}] Started in ${PWD} directory."
has_openssl;
has_getopt; has_getopt;
has_openssl;
parse_arguments "$@";
gen_openssl_config; gen_openssl_config;
if [ ! -f "${CA_KEY}" ]; then if [ ! -f "${CA_KEY}" ]; then
@ -282,4 +289,4 @@ start() {
# script starts here # script starts here
## ##
start; start "$@";

View File

@ -8,7 +8,7 @@ pushd "${TMPDIR}"
for DISTRO in alpine:3.4 alpine:3.7 ubuntu:bionic debian:stretch centos:7; do for DISTRO in alpine:3.4 alpine:3.7 ubuntu:bionic debian:stretch centos:7; do
printf "\n\n\nTesting the script with ${DISTRO} ...\n\n\n" printf "\n\n\nTesting the script with ${DISTRO} ...\n\n\n"
rm -vf openssl.cnf private.key public.crt ca.crt ca.key ca.srl rm -vf openssl.cnf private.key public.crt ca.crt ca.key ca.srl
docker run --rm -ti -v ${PWD}:/w -w /w ${DISTRO} sh gencert.sh --cn test.example.com docker run --rm -ti -v ${PWD}:/w -w /w ${DISTRO} sh gencert.sh --cn test.example.com --debug
printf "\n\n\n" printf "\n\n\n"
done done