From a65651289e9e0e981d2db37d5f0367816d42930b Mon Sep 17 00:00:00 2001 From: oscar Date: Thu, 11 Aug 2022 22:17:18 +1200 Subject: [PATCH] feat(certgen/tls): add print color functions --- custom_tls_cert_gen/generate-custom-tls.sh | 46 ++++++++++++++-------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/custom_tls_cert_gen/generate-custom-tls.sh b/custom_tls_cert_gen/generate-custom-tls.sh index 4ea63ea..3a9cf7d 100755 --- a/custom_tls_cert_gen/generate-custom-tls.sh +++ b/custom_tls_cert_gen/generate-custom-tls.sh @@ -8,7 +8,19 @@ INPUT_COLOR='\033[0;33m'; NO_COLOR='\033[0m'; -read -p "$(echo -e ${INPUT_COLOR}Specify the output path: ${NO_COLOR})" OUTPUT_PATH +function print_highlight() { + printf "${HIGHLIGHT_COLOR}$1${NO_COLOR}\n" +} + +function print_error() { + printf "${ERROR_COLOR}$1${NO_COLOR}\n" +} + +function input() { + read -p "$(echo -e ${INPUT_COLOR}$1 ${NO_COLOR})" $2 +} + +input "Specify the output path:" OUTPUT_PATH if [ -z "$OUTPUT_PATH" ]; then OUTPUT_PATH="$(pwd)/output" @@ -19,55 +31,57 @@ if [ -z "$OUTPUT_PATH" ]; then fi if [[ ! -e "$OUTPUT_PATH" ]]; then - printf "${ERROR_COLOR}${OUTPUT_PATH} doesn't exist.\n"; + print_error "${OUTPUT_PATH} doesn't exist." exit; fi -read -p "Do you have cfssl installed?(y/n): " is_cfssl_installed +input "Do you have cfssl installed?(y/n): " is_cfssl_installed CFSSLEXE=${OUTPUT_PATH}/cfssl CFSSLJSONEXE=${OUTPUT_PATH}/cfssljson if [[ "${is_cfssl_installed}" == "y" || "${is_cfssl_installed}" == "Y" ]]; then - read -p "Specify the path where the cfssl and cfssljson are placed: " TOOL_PATH + input "Specify the path where the cfssl and cfssljson are placed: " TOOL_PATH CFSSLEXE=${TOOL_PATH}/cfssl CFSSLJSONEXE=${TOOL_PATH}/cfssljson - printf "${HIGHLIGHT_COLOR}Your cfssl binary path is ${CFSSLEXE}${NO_COLOR}\n" + print_highlight "Your cfssl binary path is ${CFSSLEXE}" if [ ! -e "$CFSSLEXE" ]; then - printf "${ERROR_COLOR}no cfssl found.\n"; + print_error "no cfssl found." exit; fi if [ ! -e "$CFSSLJSONEXE" ]; then - printf "${ERROR_COLOR}no cfssljson found.\n"; + print_error "no cfssljson found." exit; fi else # Download the cfssl for users - read -p "Specify your platform(darwin/linux/windows): " PLATFORM + input "Specify your platform(darwin/linux/windows): " PLATFORM - if [ -Z "$PLATFORM" ]; then - printf "${ERROR_COLOR}Platform must be provided.\n"; + if [ -z "$PLATFORM" ]; then + print_error "Platform must be provided." exit; fi - printf "${HIGHLIGHT_COLOR}Only amd64 is supported${NO_COLOR}\n" + print_highlight "Only amd64 is supported" wget "https://github.com/cloudflare/cfssl/releases/download/v1.6.1/cfssl_1.6.1_${PLATFORM}_amd64" -O "${OUTPUT_PATH}/cfssl" chmod +x "${OUTPUT_PATH}/cfssl" wget "https://github.com/cloudflare/cfssl/releases/download/v1.6.1/cfssljson_1.6.1_${PLATFORM}_amd64" -O "${OUTPUT_PATH}/cfssljson" chmod +x "${OUTPUT_PATH}/cfssljson" - printf "${HIGHLIGHT_COLOR}Download the cfssl bundle successfully.${NO_COLOR}\n" + print_highlight "Download the cfssl bundle successfully." fi cd $OUTPUT_PATH -${CFSSLEXE} print-defaults csr | ${CFSSLEXE} gencert -initca - | ${CFSSLJSONEXE} -bare ldap-ca +input "Give a name to the CA certificate: " CA_CERT_NAME + +${CFSSLEXE} print-defaults csr | ${CFSSLEXE} gencert -initca - | ${CFSSLJSONEXE} -bare ${CA_CERT_NAME}-ca CONFIG_CFSSL_JSON=${OUTPUT_PATH}/cfssl.json @@ -82,11 +96,11 @@ cat <> ${CONFIG_CFSSL_JSON} } EOF -read -p "Give a name to the certificate: " CERT_NAME +input "Give a name to the certificate: " CERT_NAME -read -p "Input the hostname(example.org,127.0.0.1): " CERT_HOSTNAME +input "Input the hostname(example.org,127.0.0.1): " CERT_HOSTNAME echo '{}' | ${CFSSLEXE} gencert -ca=ldap-ca.pem -ca-key=ldap-ca-key.pem -config=${CONFIG_CFSSL_JSON} \ -hostname="${CERT_HOSTNAME}" - | ${CFSSLJSONEXE} -bare ${CERT_NAME} -printf "${HIGHLIGHT_COLOR}The custom TLS certificates are successfully generated in the path ${OUTPUT_PATH}.${NO_COLOR}\n" \ No newline at end of file +print_highlight "The custom TLS certificates are successfully generated in the path ${OUTPUT_PATH}."