Created
January 17, 2020 11:24
-
-
Save choestelus/0c42a39319fad48db93f4676519c8f59 to your computer and use it in GitHub Desktop.
helper script for PostgreSQL certificate generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env /bin/bash | |
set -e | |
PREFIX=$1 | |
DOMAIN=$2 | |
CA_PREFIX=$3 | |
SERVER_PREFIX=$4 | |
CLIENT_CN=$5 | |
MODE=$6 | |
CA_FILE=$1_ca | |
SERVER_FILE=$1_server | |
CLIENT_FILE=$1_client | |
if [[ -z $1 ]] | |
then | |
CA_FILE=ca | |
SERVER_FILE=server | |
CLIENT_FILE=client | |
fi | |
if [[ -z $2 ]] || [[ -z $3 ]] || [[ -z $4 ]] || [[ -z $5 ]] || [[ -z $6 ]] | |
then | |
printf 'missing one or more parameters\n' | |
printf 'got: DOMAIN=[%s] CA_PREFIX=[%s] SERVER_PREFIX=[%s] CLIENT_CN=[%s]\n' $2 $3 $4 $5 | |
printf 'mode: [%s]\n' $6 | |
exit 1 | |
fi | |
printf 'certificates will be generated with following parameters:\n' | |
printf 'Filename -> CA: [%s], SERVER: [%s], CLIENT: [%s]\n' $CA_FILE $SERVER_FILE $CLIENT_FILE | |
printf 'CA Canonical Name -> [%s.%s]\n' ${CA_PREFIX} ${DOMAIN} | |
printf 'SERVER Canonical Name -> [%s.%s]\n' ${SERVER_PREFIX} ${DOMAIN} | |
printf 'CLIENT Canonical Name -> [%s]\n' ${CLIENT_CN} | |
case $MODE in | |
generate) | |
echo 'generating certificates' | |
;; | |
*) | |
printf 'mode %s\n' $MODE | |
printf 'this is preview of generated result\n' | |
printf 'to generate certificate, set this parameter to "generate"\n' | |
exit 1 | |
esac | |
printf 'generating %s.key %s.crt\n' $CA_FILE $CA_FILE | |
openssl genrsa -out ${CA_FILE}.key 4096 | |
openssl req -new -key ${CA_FILE}.key -x509 -days 3650 -out ${CA_FILE}.crt -subj /CN="${CA_PREFIX}.${DOMAIN}" | |
printf 'generating %s.key %s.csr %s.crt\n' $SERVER_FILE $SERVER_FILE $SERVER_FILE | |
openssl genrsa -out ${SERVER_FILE}.key 4096 | |
openssl req -new -nodes -key ${SERVER_FILE}.key -out ${SERVER_FILE}.csr -subj /CN="${SERVER_PREFIX}.${DOMAIN}" | |
openssl x509 -req -in ${SERVER_FILE}.csr -days 3650 -CA ${CA_FILE}.crt -CAkey ${CA_FILE}.key -CAcreateserial -out ${SERVER_FILE}.crt | |
printf 'generating %s.key %s.csr %s.crt\n' $CLIENT_FILE $CLIENT_FILE $CLIENT_FILE | |
openssl genrsa -out ${CLIENT_FILE}.key 4096 | |
openssl req -new -nodes -key ${CLIENT_FILE}.key -out ${CLIENT_FILE}.csr -subj /CN="${CLIENT_CN}" | |
openssl x509 -req -in ${CLIENT_FILE}.csr -days 3650 -CA ${CA_FILE}.crt -CAkey ${CA_FILE}.key -CAcreateserial -out ${CLIENT_FILE}.crt | |
printf 'certificates generation is done\n' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Certificate generation scripts
Simple set of scripts to generate SSL/TLS certificates chain, includes
general certificate generation script
Use
gen_cert.sh
, see example belowcertificate generation for postgresql script
Use
gen_postgres_cert.sh
, see example belowPositional parameter list
FILENAME_PREFIX
: prefix of certificate file that will be generated. for example, usingpg
as argument will produce following files.CERT_DOMAIN_NAME
: certificate domain name that will used in canonical name of server, CA, client certificate, which will be append to server, client, root CA canonical name.CA_SUBDOMAIN
: subdomain of root CA, will be append withCERT_DOMAIN_NAME
.SERVER_SUBDOMAIN
: subdomain of server certificate, will be append withCERT_DOMAIN_NAME
.CLIENT_SUBDOMAIN
: (gen_cert.sh
only) subdomain of client certificate, will be append withCERT_DOMAIN_NAME
.CLIENT_CANONICAL_NAME
: (gen_postgres_cert.sh
only) client canonical name for client certificate, normally it is the same as DB user that will connect to postgresql database. e.g. userpostgres
will be verified if certificate CN ispostgres
or not when using certificate authentication method.*|generate
]: can be anything, unless it isgenerate
it will be a dry run mode for script.