|
| 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 | + if ! command -v unzip > /dev/null 2>&1; then |
| 19 | + apt-get update -qq && apt-get install -y -qq unzip > /dev/null 2>&1 |
| 20 | + fi |
| 21 | + |
| 22 | + curl --connect-timeout 10 --max-time 120 -fsSL \ |
| 23 | + "https://releases.hashicorp.com/vault/${VAULT_VERSION}/vault_${VAULT_VERSION}_linux_amd64.zip" \ |
| 24 | + -o /tmp/vault.zip \ |
| 25 | + && unzip -o -q /tmp/vault.zip -d /usr/local/bin/ \ |
| 26 | + && chmod +x /usr/local/bin/vault \ |
| 27 | + && rm -f /tmp/vault.zip |
| 28 | + |
| 29 | + vault version || echo "WARNING: vault install failed" |
| 30 | +} |
| 31 | + |
| 32 | +start_vault_dev() { |
| 33 | + # Start Vault in dev mode (in-memory, no TLS, root token = root) |
| 34 | + export VAULT_ADDR='http://127.0.0.1:8200' |
| 35 | + export VAULT_TOKEN='root' |
| 36 | + |
| 37 | + # Persist env for ALL future shells (Killercoda's editor terminal is a |
| 38 | + # separate shell that does not inherit from background.sh, and may not |
| 39 | + # source ~/.bashrc — /etc/profile.d/*.sh is loaded by every login shell). |
| 40 | + cat > /etc/profile.d/vault.sh <<'EOF' |
| 41 | +export VAULT_ADDR='http://127.0.0.1:8200' |
| 42 | +export VAULT_TOKEN='root' |
| 43 | +EOF |
| 44 | + chmod +x /etc/profile.d/vault.sh |
| 45 | + # Also append to /root/.bashrc so non-login interactive shells pick it up. |
| 46 | + grep -q "VAULT_ADDR=" /root/.bashrc 2>/dev/null || \ |
| 47 | + cat /etc/profile.d/vault.sh >> /root/.bashrc |
| 48 | + |
| 49 | + vault server -dev -dev-root-token-id=root \ |
| 50 | + -dev-listen-address=0.0.0.0:8200 \ |
| 51 | + > /var/log/vault-dev.log 2>&1 & |
| 52 | + |
| 53 | + echo "Waiting for Vault dev server to be ready..." |
| 54 | + for i in $(seq 1 30); do |
| 55 | + if vault status > /dev/null 2>&1; then |
| 56 | + echo "Vault is ready." |
| 57 | + return 0 |
| 58 | + fi |
| 59 | + sleep 1 |
| 60 | + done |
| 61 | + echo "WARNING: Vault did not become healthy within 30 seconds" |
| 62 | + cat /var/log/vault-dev.log |
| 63 | +} |
| 64 | + |
| 65 | +start_postgres() { |
| 66 | + # Start a Postgres container for dynamic-secret demos. |
| 67 | + # Image: postgres:16. Superuser: root / rootpassword. Listens on 5432. |
| 68 | + if ! command -v docker > /dev/null 2>&1; then |
| 69 | + echo "WARNING: docker not available, cannot start postgres" |
| 70 | + return 1 |
| 71 | + fi |
| 72 | + |
| 73 | + docker rm -f learn-postgres > /dev/null 2>&1 || true |
| 74 | + docker run -d \ |
| 75 | + --name learn-postgres \ |
| 76 | + -e POSTGRES_USER=root \ |
| 77 | + -e POSTGRES_PASSWORD=rootpassword \ |
| 78 | + -p 5432:5432 \ |
| 79 | + --rm \ |
| 80 | + postgres:16 > /dev/null |
| 81 | + |
| 82 | + echo "Waiting for Postgres to be ready..." |
| 83 | + for i in $(seq 1 60); do |
| 84 | + if docker exec learn-postgres pg_isready -U root > /dev/null 2>&1; then |
| 85 | + echo "Postgres is ready." |
| 86 | + # Create the read-only role that dynamic users will inherit from. |
| 87 | + docker exec -i learn-postgres psql -U root -c \ |
| 88 | + "CREATE ROLE \"ro\" NOINHERIT;" > /dev/null 2>&1 || true |
| 89 | + docker exec -i learn-postgres psql -U root -c \ |
| 90 | + "GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"ro\";" > /dev/null 2>&1 || true |
| 91 | + return 0 |
| 92 | + fi |
| 93 | + sleep 1 |
| 94 | + done |
| 95 | + echo "WARNING: Postgres did not become healthy within 60 seconds" |
| 96 | + docker logs learn-postgres || true |
| 97 | +} |
| 98 | + |
| 99 | +finish_setup() { |
| 100 | + touch /tmp/.setup-done |
| 101 | +} |
0 commit comments