|
| 1 | +#!/bin/bash |
| 2 | +# ───────────────────────────────────────────────────────── |
| 3 | +# setup-common.sh — shared setup functions for Killercoda scenarios |
| 4 | +# |
| 5 | +# This file is the SINGLE SOURCE OF TRUTH for common setup logic. |
| 6 | +# It is copied into each scenario's assets/ directory by: |
| 7 | +# npm run sync-setup (or automatically via prebuild) |
| 8 | +# |
| 9 | +# Usage in background.sh: |
| 10 | +# source /root/setup-common.sh |
| 11 | +# install_vault |
| 12 | +# finish_setup |
| 13 | +# ───────────────────────────────────────────────────────── |
| 14 | + |
| 15 | +VAULT_VERSION="${VAULT_VERSION:-1.19.2}" |
| 16 | + |
| 17 | +install_vault() { |
| 18 | + # Idempotency guard: skip the 70MB download if the requested version is |
| 19 | + # already installed (saves ~30s on container warm restarts). |
| 20 | + if command -v vault > /dev/null 2>&1 \ |
| 21 | + && vault version 2>/dev/null | grep -q "v${VAULT_VERSION}"; then |
| 22 | + echo "vault ${VAULT_VERSION} already installed, skipping download." |
| 23 | + return 0 |
| 24 | + fi |
| 25 | + |
| 26 | + if ! command -v unzip > /dev/null 2>&1; then |
| 27 | + apt-get update -qq && apt-get install -y -qq unzip > /dev/null 2>&1 |
| 28 | + fi |
| 29 | + |
| 30 | + curl --connect-timeout 10 --max-time 120 -fsSL \ |
| 31 | + "https://releases.hashicorp.com/vault/${VAULT_VERSION}/vault_${VAULT_VERSION}_linux_amd64.zip" \ |
| 32 | + -o /tmp/vault.zip \ |
| 33 | + && unzip -o -q /tmp/vault.zip -d /usr/local/bin/ \ |
| 34 | + && chmod +x /usr/local/bin/vault \ |
| 35 | + && rm -f /tmp/vault.zip |
| 36 | + |
| 37 | + vault version || echo "WARNING: vault install failed" |
| 38 | +} |
| 39 | + |
| 40 | +start_vault_dev() { |
| 41 | + # Start Vault in dev mode (in-memory, no TLS, root token = root) |
| 42 | + export VAULT_ADDR='http://127.0.0.1:8200' |
| 43 | + export VAULT_TOKEN='root' |
| 44 | + |
| 45 | + # Persist env for ALL future shells (Killercoda's editor terminal is a |
| 46 | + # separate shell that does not inherit from background.sh, and may not |
| 47 | + # source ~/.bashrc — /etc/profile.d/*.sh is loaded by every login shell). |
| 48 | + cat > /etc/profile.d/vault.sh <<'EOF' |
| 49 | +export VAULT_ADDR='http://127.0.0.1:8200' |
| 50 | +export VAULT_TOKEN='root' |
| 51 | +EOF |
| 52 | + chmod +x /etc/profile.d/vault.sh |
| 53 | + # Also append to /root/.bashrc so non-login interactive shells pick it up. |
| 54 | + grep -q "VAULT_ADDR=" /root/.bashrc 2>/dev/null || \ |
| 55 | + cat /etc/profile.d/vault.sh >> /root/.bashrc |
| 56 | + |
| 57 | + vault server -dev -dev-root-token-id=root \ |
| 58 | + -dev-listen-address=0.0.0.0:8200 \ |
| 59 | + > /var/log/vault-dev.log 2>&1 & |
| 60 | + |
| 61 | + echo "Waiting for Vault dev server to be ready..." |
| 62 | + for i in $(seq 1 30); do |
| 63 | + if vault status > /dev/null 2>&1; then |
| 64 | + echo "Vault is ready." |
| 65 | + return 0 |
| 66 | + fi |
| 67 | + sleep 1 |
| 68 | + done |
| 69 | + echo "WARNING: Vault did not become healthy within 30 seconds" |
| 70 | + cat /var/log/vault-dev.log |
| 71 | +} |
| 72 | + |
| 73 | +start_postgres() { |
| 74 | + # Start a Postgres container for dynamic-secret demos. |
| 75 | + # Image: postgres:16. Superuser: root / rootpassword. Listens on 5432. |
| 76 | + if ! command -v docker > /dev/null 2>&1; then |
| 77 | + echo "WARNING: docker not available, cannot start postgres" |
| 78 | + return 1 |
| 79 | + fi |
| 80 | + |
| 81 | + docker rm -f learn-postgres > /dev/null 2>&1 || true |
| 82 | + docker run -d \ |
| 83 | + --name learn-postgres \ |
| 84 | + -e POSTGRES_USER=root \ |
| 85 | + -e POSTGRES_PASSWORD=rootpassword \ |
| 86 | + -p 5432:5432 \ |
| 87 | + --rm \ |
| 88 | + postgres:16 > /dev/null |
| 89 | + |
| 90 | + echo "Waiting for Postgres to be ready..." |
| 91 | + for i in $(seq 1 60); do |
| 92 | + if docker exec learn-postgres pg_isready -U root > /dev/null 2>&1; then |
| 93 | + echo "Postgres is ready." |
| 94 | + # Create the read-only role that dynamic users will inherit from. |
| 95 | + docker exec -i learn-postgres psql -U root -c \ |
| 96 | + "CREATE ROLE \"ro\" NOINHERIT;" > /dev/null 2>&1 || true |
| 97 | + docker exec -i learn-postgres psql -U root -c \ |
| 98 | + "GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"ro\";" > /dev/null 2>&1 || true |
| 99 | + return 0 |
| 100 | + fi |
| 101 | + sleep 1 |
| 102 | + done |
| 103 | + echo "WARNING: Postgres did not become healthy within 60 seconds" |
| 104 | + docker logs learn-postgres || true |
| 105 | +} |
| 106 | + |
| 107 | +install_awscli() { |
| 108 | + # Install AWS CLI v2 (official binary). Idempotent. |
| 109 | + if command -v aws > /dev/null 2>&1; then |
| 110 | + echo "aws CLI already installed: $(aws --version 2>&1)" |
| 111 | + else |
| 112 | + if ! command -v unzip > /dev/null 2>&1; then |
| 113 | + apt-get update -qq && apt-get install -y -qq unzip > /dev/null 2>&1 |
| 114 | + fi |
| 115 | + curl --connect-timeout 10 --max-time 120 -fsSL \ |
| 116 | + "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" \ |
| 117 | + -o /tmp/awscliv2.zip \ |
| 118 | + && unzip -o -q /tmp/awscliv2.zip -d /tmp/ \ |
| 119 | + && /tmp/aws/install --update > /dev/null 2>&1 \ |
| 120 | + && rm -rf /tmp/awscliv2.zip /tmp/aws |
| 121 | + |
| 122 | + aws --version || echo "WARNING: awscli install failed" |
| 123 | + fi |
| 124 | + |
| 125 | + # Disable AWS CLI pager globally so output prints directly in Killercoda terminal. |
| 126 | + mkdir -p /root/.aws |
| 127 | + cat > /root/.aws/config <<'AWSCFG' |
| 128 | +[default] |
| 129 | +region = us-east-1 |
| 130 | +output = json |
| 131 | +cli_pager = |
| 132 | +AWSCFG |
| 133 | + |
| 134 | + # Install awscli-local (provides the 'awslocal' command pointing at LocalStack on :4566). |
| 135 | + if ! command -v awslocal > /dev/null 2>&1; then |
| 136 | + pip3 install --break-system-packages awscli-local > /dev/null 2>&1 \ |
| 137 | + || { |
| 138 | + # Fallback: shell wrapper if pip is unavailable. |
| 139 | + cat > /usr/local/bin/awslocal <<'WRAPPER' |
| 140 | +#!/bin/bash |
| 141 | +export AWS_PAGER="" |
| 142 | +exec aws --endpoint-url=http://localhost:4566 --region us-east-1 "$@" |
| 143 | +WRAPPER |
| 144 | + chmod +x /usr/local/bin/awslocal |
| 145 | + } |
| 146 | + fi |
| 147 | + |
| 148 | + awslocal --version || echo "WARNING: awslocal install failed" |
| 149 | +} |
| 150 | + |
| 151 | +finish_setup() { |
| 152 | + touch /tmp/.setup-done |
| 153 | +} |
0 commit comments