Forked from ivan-pinatti/create-self-signed-certificate.sh
Created
January 24, 2020 19:38
-
-
Save jwcastillo/f60d658e1ec24095e915cf3191446a8d to your computer and use it in GitHub Desktop.
Create self-signed certificate - #linux #openssl #certificate
This file contains hidden or 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 bash | |
: ' Script to create self-signed certificate | |
' | |
# check if debug flag is set | |
if [ "${DEBUG}" = true ]; then | |
set -x # enable print commands and their arguments as they are executed. | |
export # show all declared variables (includes system variables) | |
whoami # print current user | |
else | |
# unset if flag is not set | |
unset DEBUG | |
fi | |
# bash default parameters | |
set -o errexit # make your script exit when a command fails | |
set -o pipefail # exit status of the last command that threw a non-zero exit code is returned | |
set -o nounset # exit when your script tries to use undeclared variables | |
# binaries | |
__MKTEMP=$(which mktemp) | |
__OPENSSL=$(which openssl) | |
# parameters | |
__url="${1:-"www.mycompany.com"}" | |
__company_name="${2:-"My Company Inc"}" | |
__country_code="${3:-"US"}" | |
# create temp folder | |
__temp_folder=$(${__MKTEMP} --directory) | |
# create the key | |
${__OPENSSL} genrsa -out ${__temp_folder}/key.pem | |
# create the certifcate request | |
${__OPENSSL} req -new \ | |
-subj "/CN=${__url}/O=${__company_name}/C=${__country_code}" \ | |
-key ${__temp_folder}/key.pem \ | |
-out ${__temp_folder}/csr.pem | |
# create the certificate | |
${__OPENSSL} x509 -req \ | |
-days 365 \ | |
-in ${__temp_folder}/csr.pem \ | |
-signkey ${__temp_folder}/key.pem \ | |
-out ${__temp_folder}/cert.pem | |
echo -e "\nCertificate and key created sucessfully, please check folder ${__temp_folder}\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment