From 7dff2dee0ff9bf8502bf74ccabfff20161537995 Mon Sep 17 00:00:00 2001 From: gberl002 Date: Thu, 9 Dec 2021 09:25:26 -0500 Subject: [PATCH] commands that require a parameter now prompt user for input when argument not provided added console outputs when a command is executed successfully (stopZitiController, stopAllEdgeRouters) Added a single checkEnvVariable function so it can be used repeatedly for consistent output All functions that create a file now output the file location --- quickstart/docker/image/ziti-cli-functions.sh | 518 +++++++++++------- 1 file changed, 330 insertions(+), 188 deletions(-) diff --git a/quickstart/docker/image/ziti-cli-functions.sh b/quickstart/docker/image/ziti-cli-functions.sh index dd7a1aef1..878db6316 100755 --- a/quickstart/docker/image/ziti-cli-functions.sh +++ b/quickstart/docker/image/ziti-cli-functions.sh @@ -45,13 +45,11 @@ function zitiLogin { "${ZITI_BIN_DIR-}/ziti" edge login "${ZITI_EDGE_CONTROLLER_API}" -u "${ZITI_USER-}" -p "${ZITI_PWD}" -c "${ZITI_PKI_OS_SPECIFIC}/${ZITI_EDGE_CONTROLLER_ROOTCA_NAME}/certs/${ZITI_EDGE_CONTROLLER_INTERMEDIATE_NAME}.cert" } function cleanZitiController { - ziti_home="${ZITI_HOME-}" - if [[ "${ziti_home}" == "" ]]; then - echo -e " * ERROR: $(RED "ZITI_HOME is not set") " + if [ "$(checkEnvVariable ZITI_HOME)" ]; then return 1 fi - rm -rf "${ziti_home}/db" - mkdir "${ziti_home}/db" + rm -rf "${ZITI_HOME}/db" + mkdir "${ZITI_HOME}/db" initializeController } function initializeController { @@ -65,18 +63,26 @@ function startZitiController { echo -e "ziti-controller started as process id: $pid. log located at: $(BLUE "${ZITI_HOME-}/ziti-edge-controller.log")" return $pid } + function stopZitiController { killall ziti-controller + # shellcheck disable=SC2181 + if [[ $? == 0 ]]; then echo "Controller stopped."; fi } + function startExpressEdgeRouter { "${ZITI_BIN_DIR}/ziti-router" run "${ZITI_HOME_OS_SPECIFIC}/${ZITI_EDGE_ROUTER_RAWNAME}.yaml" > "${ZITI_HOME_OS_SPECIFIC}/${ZITI_EDGE_ROUTER_RAWNAME}.log" 2>&1 & pid=$! echo -e "Express Edge Router started as process id: $pid. log located at: $(BLUE "${ZITI_HOME_OS_SPECIFIC}/${ZITI_EDGE_ROUTER_RAWNAME}.log")" return $pid } + function stopAllEdgeRouters { - killall ziti-router + killall ziti-controller + # shellcheck disable=SC2181 + if [[ $? == 0 ]]; then echo "Router(s) stopped."; fi } + function checkHostsFile { ctrlexists=$(grep -c "${ZITI_CONTROLLER_HOSTNAME}" /etc/hosts) edgectrlexists=$(grep -c "${ZITI_EDGE_CONTROLLER_HOSTNAME}" /etc/hosts) @@ -111,6 +117,8 @@ function checkHostsFile { echo " " echo "add these entries to your hosts file, and rerun the script when ready" return 1 + else + echo -e "$(GREEN "Hosts file looks good")" fi } @@ -140,8 +148,7 @@ function getLatestZitiVersion { function getLatestZiti { setupZitiHome - if [[ "${ZITI_HOME-}" == "" ]]; then - echo "ERROR: ZITI_HOME is not set!" + if [ "$(checkEnvVariable ZITI_HOME)" ]; then return 1 fi @@ -157,12 +164,7 @@ function getLatestZiti { return 1 fi - ziti_bin_ver="${ZITI_BINARIES_VERSION-}" - if [[ "${ziti_bin_ver}" == "" ]]; then - echo "ERROR: ZITI_BINARIES_VERSION is not set!" - return 1 - fi - export ZITI_BIN_DIR="${ziti_bin_root}/ziti-${ziti_bin_ver}" + export ZITI_BIN_DIR="${ziti_bin_root}/ziti-${ZITI_BINARIES_VERSION}" ZITI_BINARIES_FILE_ABSPATH="${ZITI_HOME-}/ziti-bin/${ZITI_BINARIES_FILE}" if ! test -f "${ZITI_BINARIES_FILE_ABSPATH}"; then @@ -220,10 +222,9 @@ function checkPrereqs { echo " " echo " " return 1 + else + echo -e "$(GREEN "Prerequisites confirmed")" fi - echo "Let's get started creating your local development network!" - echo "" - echo "" } function checkControllerName { @@ -235,7 +236,12 @@ function checkControllerName { } function unsetZitiEnv { - for zEnvVar in $(set | grep -e "^ZITI_" | sort); do envvar="$(echo "${zEnvVar}" | cut -d '=' -f1)"; echo unsetting "[${envvar}]${zEnvVar}"; unset "${envvar}"; done + if [[ "${1-}" == "-s" ]]; then + # Silent... + for zEnvVar in $(set | grep -e "^ZITI_" | sort); do envvar="$(echo "${zEnvVar}" | cut -d '=' -f1)"; unset "${envvar}"; done + else + for zEnvVar in $(set | grep -e "^ZITI_" | sort); do envvar="$(echo "${zEnvVar}" | cut -d '=' -f1)"; echo unsetting "[${envvar}]${zEnvVar}"; unset "${envvar}"; done + fi unset ZITIx_EXPRESS_COMPLETE } @@ -290,7 +296,7 @@ function setupZitiNetwork { function setupZitiHome { if [[ "${ZITI_HOME-}" == "" ]]; then export ZITI_HOME="${HOME}/.ziti/quickstart/${ZITI_NETWORK-}" - echo "using default ZITI_HOME: ${ZITI_HOME}" + echo -e "using default ZITI_HOME: $(BLUE "${ZITI_HOME}")" fi } @@ -318,7 +324,7 @@ function generateEnvFile { export ENV_FILE="${ZITI_HOME-}/${ZITI_NETWORK-}.env" - echo -e "environment file created and sourced from: $(BLUE "${ENV_FILE}")" + echo -e "environment file sourced from: $(BLUE "${ENV_FILE}")" } function ziti_expressConfiguration { @@ -326,10 +332,10 @@ function ziti_expressConfiguration { echo -e "$(RED " --- It looks like you've run an express install in this shell already. ---")" echo -en "Would you like to clear existing Ziti variables and continue (y/N)? " read -r - echo + echo " " if [[ "${REPLY}" =~ [Yy]$ ]]; then echo -e "$(GREEN "Clearing existing Ziti variables and continuing with express install")" - unsetZitiEnv + unsetZitiEnv "-s" else echo -e "$(RED " --- Exiting express install ---")" return 1 @@ -356,6 +362,9 @@ function ziti_expressConfiguration { if [[ "${ZITI_EDGE_ROUTER_HOSTNAME-}" != "" ]]; then echo "ZITI_EDGE_ROUTER_HOSTNAME OVERIDDEN: $ZITI_EDGE_ROUTER_HOSTNAME"; fi if [[ "${ZITI_EDGE_ROUTER_PORT-}" != "" ]]; then echo "ZITI_EDGE_ROUTER_PORT OVERIDDEN: $ZITI_EDGE_ROUTER_PORT"; fi + echo " " + echo " " + echo -e "******** Setting Up Environment ********" if [[ "${1-}" == "" ]]; then nw="$(hostname)" else @@ -364,7 +373,7 @@ function ziti_expressConfiguration { setupZitiNetwork "${nw}" setupZitiHome - echo "ZITI HOME SET TO: ${ZITI_HOME}" + echo "ZITI HOME SET TO: $(BLUE "${ZITI_HOME}")" if ! getLatestZiti "no"; then echo -e "$(RED "getLatestZiti failed")" @@ -376,9 +385,16 @@ function ziti_expressConfiguration { fi #checkHostsFile + echo " " + echo " " + echo -e "******** Setting Up Public Key Infrastructure ********" createPki - createControllerConfig - #createControllerSystemdFile + + echo " " + echo " " + echo -e "******** Setting Up Controller ********" + createControllerConfig "controller" + #createControllerSystemdFile controller initializeController startZitiController echo "waiting for the controller to come online to allow the edge router to enroll" @@ -386,6 +402,9 @@ function ziti_expressConfiguration { zitiLogin + echo " " + echo " " + echo -e "******** Setting Up Edge Routers ********" echo -e "---------- Creating an edge router policy allowing all identities to connect to routers with a $(GREEN "#public") attribute" "${ZITI_BIN_DIR-}/ziti" edge delete edge-router-policy allEdgeRouters > /dev/null "${ZITI_BIN_DIR-}/ziti" edge create edge-router-policy allEdgeRouters --edge-router-roles '#public' --identity-roles '#all' > /dev/null @@ -402,12 +421,12 @@ function ziti_expressConfiguration { "${ZITI_BIN_DIR-}/ziti" edge delete edge-router "${ZITI_EDGE_ROUTER_RAWNAME}" > /dev/null "${ZITI_BIN_DIR-}/ziti" edge create edge-router "${ZITI_EDGE_ROUTER_RAWNAME}" -o "${ZITI_HOME_OS_SPECIFIC}/${ZITI_EDGE_ROUTER_RAWNAME}.jwt" -t -a "public"> /dev/null sleep 1 - echo "---------- Enrolling edge-router ${ZITI_EDGE_ROUTER_RAWNAME}...." + echo "---------- Enrolling edge-router ${ZITI_EDGE_ROUTER_RAWNAME}...." "${ZITI_BIN_DIR-}/ziti-router" enroll "${ZITI_HOME_OS_SPECIFIC}/${ZITI_EDGE_ROUTER_RAWNAME}.yaml" --jwt "${ZITI_HOME_OS_SPECIFIC}/${ZITI_EDGE_ROUTER_RAWNAME}.jwt" &> "${ZITI_HOME_OS_SPECIFIC}/${ZITI_EDGE_ROUTER_RAWNAME}.enrollment.log" echo "" - killall ziti-controller + stopZitiController echo "Edge Router enrolled. Controller stopped." echo "" @@ -490,6 +509,10 @@ function expressInstall { #make sure the user has all the necessary commands to be successful checkPrereqs + echo "Let's get started creating your local development network!" + echo "" + echo "" + #prompt the user for input and do what they want/need decideOperation 1 "${1-}" } @@ -550,13 +573,11 @@ function pki_create_intermediate { fi echo " " } -function printUsage() -{ +function printUsage() { echo "Usage: ${1-} [cert to test] [ca pool to use]" } -function verifyCertAgainstPool() -{ +function verifyCertAgainstPool() { if [[ "" == "${1-}" ]] then printUsage "verifyCertAgainstPool" @@ -585,8 +606,7 @@ function verifyCertAgainstPool() fi } -function showIssuerAndSubjectForPEM() -{ +function showIssuerAndSubjectForPEM() { echo "Displaying Issuer and Subject for cert pool:" echo " ${1-}" openssl crl2pkcs7 -nocrl -certfile "${1-}" | openssl pkcs7 -print_certs -text -noout | grep -E "(Subject|Issuer)" @@ -597,18 +617,29 @@ function createRouterPki { } function createPrivateRouterConfig { + # Allow router name to be passed in as arg router_name="${1-}" if [[ "${router_name}" == "" ]]; then - echo -e " * ERROR: $(RED "createPrivateRouterConfig requires a parameter to be supplied") " + + # If router name is not passed as arg, prompt user for input + echo -e "$(YELLOW "createPrivateRouterConfig requires a router name to be supplied") " + default_router_name="${ZITI_EDGE_ROUTER_RAWNAME}" + echo -en "Enter router name (${default_router_name}):" + read -r router_name + + # Accept the default if no name provided + if [[ "${router_name}" == "" ]]; then + router_name="${default_router_name}" + fi + fi + + # Make sure necessary env variables are set + if [ "$(checkEnvVariable ZITI_HOME ZITI_BIN_DIR)" ]; then return 1 fi - ziti_home="${ZITI_HOME-}" - if [[ "${ziti_home}" == "" ]]; then - echo -e " * ERROR: $(RED "ZITI_HOME is not set") " - return 1 - fi -cat > "${ZITI_HOME-}/${ZITI_EDGE_ROUTER_RAWNAME}.yaml" < "${output_file}" < "${ZITI_HOME}/${ZITI_EDGE_ROUTER_RAWNAME}.yaml" < "${output_file}" < "${ZITI_HOME-}/${router_name}.yaml" < "${output_file}" < "${ziti_home}/${router_name}.yaml" < "${output_file}" < "${ZITI_HOME}/identities.yml" < "${output_file}" < "${ZITI_PKI_OS_SPECIFIC}/cas.pem" cat "${ZITI_PKI_OS_SPECIFIC}/${ZITI_SIGNING_INTERMEDIATE_NAME}/certs/${ZITI_SIGNING_INTERMEDIATE_NAME}.cert" >> "${ZITI_PKI_OS_SPECIFIC}/cas.pem" -echo "wrote CA file to: ${ZITI_PKI_OS_SPECIFIC}/cas.pem" +echo "wrote CA file to: $(BLUE "${ZITI_PKI_OS_SPECIFIC}/cas.pem")" -cat > "${ziti_home}/controller.yaml" < "${output_file}" < "${systemd_file}" < "${output_file}" < "${systemd_file}" < "${output_file}" < "${launchd_file}" < "${output_file}" < Label - ziti-controller-${ZITI_BINARIES_VERSION} + ziti-controller-${controller_name} ProgramArguments - ./ziti-controller - + $ZITI_BIN_DIR/ziti-controller run - ./ctrl.with.edge.yml - - WorkingDirectory - ${ZITI_HOME} - KeepAlive - - SuccessfulExit - - - StandardOutPath - /Users/scarey/Library/Logs/ziti-controller-0.22.9.log - StandardErrorPath - /Users/scarey/Library/Logs/ziti-controller-0.22.9.log - - -HeredocForLaunchd - echo "Router launchd file written to: ${launchd_file}" -} -function createRouterLaunchdFile { - router_name="${1-}" - if [[ "${router_name}" == "" ]]; then - echo -e " * ERROR: $(RED "createRouterLaunchdFile requires parameter [router_name] to be supplied") " - return 1 - fi - - ziti_home="${ZITI_HOME-}" - if [[ "${ziti_home}" == "" ]]; then - echo -e " * ERROR: $(RED "ZITI_HOME is not set") " - return 1 - fi - - ziti_bin_dir="${ZITI_BIN_DIR-}" - if [[ "${ziti_bin_dir}" == "" ]]; then - echo -e " * ERROR: $(RED "ZITI_BIN_DIR is not set") " - return 1 - fi -launchd_file="${ziti_home}/ziti-router-${router_name}.launchd" -cat > "${launchd_file}" < - - - - Label - ziti-controller-0.22.9 - ProgramArguments - - ./ziti-controller - - run - ./ctrl.with.edge.yml + $ZITI_HOME/${controller_name}.yaml WorkingDirectory ${ZITI_HOME} @@ -1412,38 +1471,99 @@ cat > "${launchd_file}" < StandardOutPath - /Users/scarey/Library/Logs/ziti-controller-0.22.9.log + ${ZITI_HOME}/Logs/ziti-router-$ZITI_BINARIES_VERSION.log StandardErrorPath - /Users/scarey/Library/Logs/ziti-controller-0.22.9.log + ${ZITI_HOME}/Logs/ziti-router-$ZITI_BINARIES_VERSION.log HeredocForLaunchd - echo "Router launchd file written to: ${launchd_file}" + echo "Controller launchd file written to: $(BLUE "${output_file}")" + + showLaunchdMessage } +function createRouterLaunchdFile { + # Allow router name to be passed in as arg + router_name="${1-}" + if [[ "${router_name}" == "" ]]; then + + # If router name is not passed as arg, prompt user for input + echo -e "$(YELLOW "createRouterLaunchdFile requires a router name to be supplied") " + default_router_name="${ZITI_EDGE_ROUTER_RAWNAME}" + echo -en "Enter router name (${default_router_name}):" + read -r router_name + + # Accept the default if no name provided + if [[ "${router_name}" == "" ]]; then + router_name="${default_router_name}" + fi + fi + + # Make sure necessary env variables are set + if [ "$(checkEnvVariable ZITI_HOME ZITI_BIN_DIR)" ]; then + return 1 + fi + +output_file="${ziti_home}/ziti-router-${router_name}.plist" +cat > "${output_file}" < + + + + Label + ziti-router-$router_name + ProgramArguments + + $ZITI_BIN_DIR/ziti-router + run + $ZITI_HOME/ctrl.with.edge.yml + + WorkingDirectory + ${ZITI_HOME} + KeepAlive + + + PathState + + ${ZITI_HOME}/launchd-enabled + + + + StandardOutPath + ${ZITI_HOME}/Logs/ziti-router-$ZITI_BINARIES_VERSION.log + StandardErrorPath + ${ZITI_HOME}/Logs/ziti-router-$ZITI_BINARIES_VERSION.log + + +HeredocForLaunchd + echo "Router launchd file written to: $(BLUE "${output_file}")" + + showLaunchdMessage +} + +function showLaunchdMessage { + echo -e " " + echo -e "$(YELLOW "The generated launchd file is designed to keep the service alive while the file")" + echo -e "$(BLUE "${ZITI_HOME}/launchd-enabled") $(YELLOW "remains present.")" + echo -e "$(YELLOW "If this file is not present, the service will end")" +} function createZacSystemdFile { - ziti_home="${ZITI_HOME-}" - if [[ "${ziti_home}" == "" ]]; then - echo -e " * ERROR: $(RED "ZITI_HOME is not set") " + + if [ "$(checkEnvVariable ZITI_HOME )" ]; then return 1 fi - ziti_bin_dir="${ZITI_BIN_DIR-}" - if [[ "${ziti_bin_dir}" == "" ]]; then - echo -e " * ERROR: $(RED "ZITI_BIN_DIR is not set") " - return 1 - fi -systemd_file="${ziti_home}/ziti-console.service" -cat > "${systemd_file}" < "${output_file}" <