feat(certgen/tls): add print color functions

This commit is contained in:
oscar 2022-08-11 22:17:18 +12:00
parent c666bdedd1
commit a65651289e

View File

@ -8,7 +8,19 @@ INPUT_COLOR='\033[0;33m';
NO_COLOR='\033[0m'; 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 if [ -z "$OUTPUT_PATH" ]; then
OUTPUT_PATH="$(pwd)/output" OUTPUT_PATH="$(pwd)/output"
@ -19,55 +31,57 @@ if [ -z "$OUTPUT_PATH" ]; then
fi fi
if [[ ! -e "$OUTPUT_PATH" ]]; then if [[ ! -e "$OUTPUT_PATH" ]]; then
printf "${ERROR_COLOR}${OUTPUT_PATH} doesn't exist.\n"; print_error "${OUTPUT_PATH} doesn't exist."
exit; exit;
fi 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 CFSSLEXE=${OUTPUT_PATH}/cfssl
CFSSLJSONEXE=${OUTPUT_PATH}/cfssljson CFSSLJSONEXE=${OUTPUT_PATH}/cfssljson
if [[ "${is_cfssl_installed}" == "y" || "${is_cfssl_installed}" == "Y" ]]; then 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 CFSSLEXE=${TOOL_PATH}/cfssl
CFSSLJSONEXE=${TOOL_PATH}/cfssljson 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 if [ ! -e "$CFSSLEXE" ]; then
printf "${ERROR_COLOR}no cfssl found.\n"; print_error "no cfssl found."
exit; exit;
fi fi
if [ ! -e "$CFSSLJSONEXE" ]; then if [ ! -e "$CFSSLJSONEXE" ]; then
printf "${ERROR_COLOR}no cfssljson found.\n"; print_error "no cfssljson found."
exit; exit;
fi fi
else else
# Download the cfssl for users # 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 if [ -z "$PLATFORM" ]; then
printf "${ERROR_COLOR}Platform must be provided.\n"; print_error "Platform must be provided."
exit; exit;
fi 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" 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" 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" 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" chmod +x "${OUTPUT_PATH}/cfssljson"
printf "${HIGHLIGHT_COLOR}Download the cfssl bundle successfully.${NO_COLOR}\n" print_highlight "Download the cfssl bundle successfully."
fi fi
cd $OUTPUT_PATH 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 CONFIG_CFSSL_JSON=${OUTPUT_PATH}/cfssl.json
@ -82,11 +96,11 @@ cat <<EOF >> ${CONFIG_CFSSL_JSON}
} }
EOF 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} \ echo '{}' | ${CFSSLEXE} gencert -ca=ldap-ca.pem -ca-key=ldap-ca-key.pem -config=${CONFIG_CFSSL_JSON} \
-hostname="${CERT_HOSTNAME}" - | ${CFSSLJSONEXE} -bare ${CERT_NAME} -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" print_highlight "The custom TLS certificates are successfully generated in the path ${OUTPUT_PATH}."