diff --git a/vault-github/run.sh b/vault-github/run.sh index 896c36f..0c4025f 100644 --- a/vault-github/run.sh +++ b/vault-github/run.sh @@ -1,10 +1,17 @@ #!/usr/bin/env bash +BOLD='\033[0;1m' VAULT_ADDR=${VAULT_ADDR} VERSION=${VERSION} AUTH_PATH=${AUTH_PATH} GITHUB_EXTERNAL_AUTH_ID=${GITHUB_EXTERNAL_AUTH_ID} +# Ensure curl is installed +if ! command -v curl &>/dev/null; then + printf "curl is not installed. Please install curl in your image.\n" + exit 1 +fi + # Fetch latest version of Vault if VERSION is 'latest' if [ "${VERSION}" = "latest" ]; then LATEST_VERSION=$(curl -s https://releases.hashicorp.com/vault/ | grep -oP 'vault/\K[0-9]+\.[0-9]+\.[0-9]+' | sort -V | tail -n 1) @@ -20,23 +27,14 @@ installation_needed=1 if command -v vault &>/dev/null; then CURRENT_VERSION=$(vault version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+') if [ "$CURRENT_VERSION" = "$VERSION" ]; then - printf "Vault version $CURRENT_VERSION is already installed and up-to-date.\n\n" + printf "${BOLD}Vault version $CURRENT_VERSION is already installed and up-to-date.\n\n" installation_needed=0 fi fi if [ $installation_needed -eq 1 ]; then - printf "Installing or updating Vault CLI ...\n\n" - - # Check if curl is installed - if ! command -v curl &>/dev/null; then - printf "curl is not installed. Please install curl in your image.\n" - exit 1 - fi - - # Check if unzip is installed + # Check if unzip or busybox is installed if ! command -v unzip &>/dev/null; then - # Check if busybox is installed and can provide unzip if command -v busybox &>/dev/null && busybox --list | grep -q '^unzip$'; then alias unzip='busybox unzip' printf "Using busybox unzip.\n" @@ -47,23 +45,20 @@ if [ $installation_needed -eq 1 ]; then fi # Download and install Vault - curl -sLo vault.zip https://releases.hashicorp.com/vault/${VERSION}/vault_${VERSION}_linux_amd64.zip - unzip vault.zip - # Try to move vault to /usr/local/bin with sudo if possible otherwise to .local/bin and add to PATH - if sudo mv vault /usr/local/bin/vault 2>/dev/null; then - printf "🥳 Vault installed successfully!\n\n" - else + printf "Installing or updating Vault CLI ...\n\n" + curl -sLo vault.zip "https://releases.hashicorp.com/vault/${VERSION}/vault_${VERSION}_linux_amd64.zip" + unzip -o vault.zip + sudo mv vault /usr/local/bin/vault || { mkdir -p ~/.local/bin mv vault ~/.local/bin/vault - printf "🥳 Vault installed successfully!\n\n" printf "Please add ~/.local/bin to your PATH to use vault CLI.\n" - fi + } + rm vault.zip + printf "🥳 Vault installed successfully!\n\n" fi -# Set up Vault token +# Authenticate with Vault printf "🔑 Authenticating with Vault ...\n\n" -echo "AUTH_PATH: $AUTH_PATH" -echo "GITHUB_EXTERNAL_AUTH_ID: $GITHUB_EXTERNAL_AUTH_ID" GITHUB_TOKEN=$(coder external-auth access-token $GITHUB_EXTERNAL_AUTH_ID) if [ $? -ne 0 ]; then printf "Authentication with Vault failed. Please check your credentials.\n" @@ -72,16 +67,11 @@ fi export VAULT_ADDR=$VAULT_ADDR -# Verify Vault address -printf "🔎 Verifying Vault address...\n\n" -vault status - -# Login to Vault to using GitHub token +# Login to Vault using GitHub token printf "🔑 Logging in to Vault ...\n\n" vault login -no-print -method=github -path=/$AUTH_PATH token=$GITHUB_TOKEN -# Add VAULT_ADDR to shell login scripts if not already present e.g. .bashrc, .zshrc -# This is a temporary fix and will be replaced with https://github.com/coder/coder/issues/10166 +# Add VAULT_ADDR to shell login scripts if not already present # bash if [[ -f ~/.bashrc ]] && ! grep -q "VAULT_ADDR" ~/.bashrc; then printf "\nAdding VAULT_ADDR to ~/.bashrc ...\n"