Files
KNEL-AIMiddleware/discourse-cli/validate.sh
T
mrcharles 1957dcbc7e feat(creds): centralize credentials to ~/.creds/, add KNELCredsManager
Rewire all MCP wrappers, docker-compose services, and validate scripts
to source credentials from ~/.creds/ instead of scattered per-service
.env files. This removes credential duplication and prepares for the
HashiCorp Vault migration.

Add KNELCredsManager under tooling-cli with:
- Containerized Bitwarden CLI (pinned image, host stays clean)
- scripts/bw wrapper with session management and data persistence
- README documenting credential layout, consumer wiring, and roadmap

Also fixes latent bug in MCP wrappers that were silently getting empty
creds from ambient shell env — they now explicitly source ~/.creds/.

Tracked in Redmine #407. Discourse: https://community.turnsys.com/t/308

💘 Generated with Crush

Assisted-by: Crush
2026-08-10 09:14:14 -05:00

212 lines
7.4 KiB
Bash
Executable File

#!/bin/sh
# discourse-cli validation script.
# Runs everything inside containers so no host tooling (curl, etc.) is needed.
#
# Usage: ./validate.sh (reads .env from the discourse-cli directory)
#
# Exit codes: 0 = all checks passed, 1 = one or more checks failed
set -eu
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"
ENV_FILE="${DISCOURSE_ENV_FILE:-/home/reachableceo/.creds/discourse.env}"
# Ensure env file exists
if [ ! -f "$ENV_FILE" ]; then
echo "FAIL: env file not found: $ENV_FILE"
exit 1
fi
IMAGE="${DISCOURSE_CLI_IMAGE:-kneldevstack-aimiddleware-discourse-cli}"
PASS=0
FAIL=0
TOTAL=0
ok() { TOTAL=$((TOTAL+1)); echo "PASS: $1"; PASS=$((PASS+1)); }
fail() { TOTAL=$((TOTAL+1)); echo "FAIL: $1"; FAIL=$((FAIL+1)); }
echo "============================================"
echo " discourse-cli validation"
echo " image: ${IMAGE}"
echo "============================================"
echo ""
# ---------------------------------------------------------------- #
# 0. Credential sanity (raw HTTP via containerized curl)
# ---------------------------------------------------------------- #
echo "--- [0] Credential check (raw HTTP via containerized curl) ---"
# Load env vars from .env for the raw test
DISCOURSE_URL=$(grep -E '^DISCOURSE_URL=' "$ENV_FILE" | cut -d= -f2-)
DISCOURSE_API_KEY=$(grep -E '^DISCOURSE_API_KEY=' "$ENV_FILE" | cut -d= -f2-)
DISCOURSE_API_USERNAME=$(grep -E '^DISCOURSE_API_USERNAME=' "$ENV_FILE" | cut -d= -f2-)
HTTP_CODE=$(docker run --rm curlimages/curl:latest \
-s -o /dev/null -w '%{http_code}' \
-H "Api-Key: ${DISCOURSE_API_KEY}" \
-H "Api-Username: ${DISCOURSE_API_USERNAME}" \
-H "Accept: application/json" \
"${DISCOURSE_URL}/session/current.json" 2>/dev/null || echo "000")
if [ "$HTTP_CODE" = "200" ]; then
ok "raw HTTP credentials accepted (HTTP ${HTTP_CODE})"
else
fail "raw HTTP credentials rejected (HTTP ${HTTP_CODE})"
echo " -> Discourse at ${DISCOURSE_URL} returned ${HTTP_CODE} for /session/current.json"
echo " -> Check: API key active? IP allowlist? Username matches key scope?"
echo ""
echo " Stopping: cannot validate CLI commands without valid credentials."
echo ""
echo "============================================"
echo " RESULTS: ${PASS}/${TOTAL} passed, ${FAIL} failed"
echo "============================================"
exit 1
fi
echo ""
# ---------------------------------------------------------------- #
# 1. whoami (live connection test)
# ---------------------------------------------------------------- #
echo "--- [1] whoami ---"
OUT=$(docker run --rm --env-file "$ENV_FILE" "${IMAGE}" whoami 2>&1) || true
if echo "$OUT" | grep -qE '^username:'; then
ok "whoami returned user info"
echo " $OUT" | head -5
else
fail "whoami did not return expected output"
echo " $OUT" | head -5
fi
echo ""
# ---------------------------------------------------------------- #
# 2. categories (live read)
# ---------------------------------------------------------------- #
echo "--- [2] categories ---"
OUT=$(docker run --rm --env-file "$ENV_FILE" "${IMAGE}" categories 2>&1) || true
if echo "$OUT" | grep -qE '^[0-9]+ category'; then
ok "categories returned data"
echo " $OUT" | head -6
# Grab first category id for later tests
CAT_ID=$(echo "$OUT" | grep -E '^[0-9]' | head -1 | awk '{print $1}')
else
fail "categories did not return expected output"
echo " $OUT" | head -6
CAT_ID=""
fi
echo ""
# ---------------------------------------------------------------- #
# 3. topics / ls (live read)
# ---------------------------------------------------------------- #
echo "--- [3] topics (latest) ---"
OUT=$(docker run --rm --env-file "$ENV_FILE" "${IMAGE}" ls 2>&1) || true
if echo "$OUT" | grep -qE 'topic\(s\)'; then
ok "topics list returned data"
echo " $OUT" | head -6
TOPIC_ID=$(echo "$OUT" | grep -E '^[0-9]' | head -1 | awk '{print $1}')
else
fail "topics list did not return expected output"
echo " $OUT" | head -6
TOPIC_ID=""
fi
echo ""
# ---------------------------------------------------------------- #
# 4. show topic (live read)
# ---------------------------------------------------------------- #
if [ -n "${TOPIC_ID:-}" ]; then
echo "--- [4] show topic ${TOPIC_ID} ---"
OUT=$(docker run --rm --env-file "$ENV_FILE" "${IMAGE}" show "${TOPIC_ID}" 2>&1) || true
if echo "$OUT" | grep -qE '^#'; then
ok "show topic ${TOPIC_ID} returned content"
echo " $OUT" | head -8
else
fail "show topic ${TOPIC_ID} did not return expected output"
echo " $OUT" | head -8
fi
else
echo "--- [4] show topic (SKIPPED - no topic id found in step 3) ---"
fi
echo ""
# ---------------------------------------------------------------- #
# 5. search (live read)
# ---------------------------------------------------------------- #
echo "--- [5] search ---"
OUT=$(docker run --rm --env-file "$ENV_FILE" "${IMAGE}" search "test" 2>&1) || true
if echo "$OUT" | grep -qE 'result\(s\)'; then
ok "search returned results"
echo " $OUT" | head -5
else
fail "search did not return expected output"
echo " $OUT" | head -5
fi
echo ""
# ---------------------------------------------------------------- #
# 6. create + reply + update + delete (live write cycle)
# ---------------------------------------------------------------- #
echo "--- [6] create + reply + update + delete (write cycle) ---"
CREATE_OUT=$(docker run --rm --env-file "$ENV_FILE" "${IMAGE}" create \
${CAT_ID:+-c "$CAT_ID"} \
-t "discourse-cli validation test $(date +%s)" \
-b "This is an automated validation test post. It will be cleaned up shortly after creation." \
2>&1) || true
NEW_TOPIC_ID=$(echo "$CREATE_OUT" | grep -oE 'topic #[0-9]+' | grep -oE '[0-9]+')
if [ -n "${NEW_TOPIC_ID:-}" ]; then
ok "create topic succeeded (topic #${NEW_TOPIC_ID})"
echo " $CREATE_OUT"
# reply
REPLY_OUT=$(docker run --rm --env-file "$ENV_FILE" "${IMAGE}" reply "${NEW_TOPIC_ID}" \
-b "Validation reply test." 2>&1) || true
POST_ID=$(echo "$REPLY_OUT" | grep -oE 'id=[0-9]+' | grep -oE '[0-9]+')
if echo "$REPLY_OUT" | grep -qE 'Posted reply'; then
ok "reply succeeded"
echo " $REPLY_OUT"
else
fail "reply failed"
echo " $REPLY_OUT"
fi
# update
if [ -n "${POST_ID:-}" ]; then
UPD_OUT=$(docker run --rm --env-file "$ENV_FILE" "${IMAGE}" update "${POST_ID}" \
-b "Updated validation reply test." 2>&1) || true
if echo "$UPD_OUT" | grep -qE 'Updated post'; then
ok "update post ${POST_ID} succeeded"
else
fail "update post ${POST_ID} failed"
echo " $UPD_OUT"
fi
# delete
DEL_OUT=$(docker run --rm --env-file "$ENV_FILE" "${IMAGE}" delete "${POST_ID}" 2>&1) || true
if echo "$DEL_OUT" | grep -qE 'Deleted post'; then
ok "delete post ${POST_ID} succeeded"
else
fail "delete post ${POST_ID} failed"
echo " $DEL_OUT"
fi
fi
else
fail "create topic failed"
echo " $CREATE_OUT"
fi
echo ""
# ---------------------------------------------------------------- #
# Summary
# ---------------------------------------------------------------- #
echo "============================================"
echo " RESULTS: ${PASS}/${TOTAL} passed, ${FAIL} failed"
echo "============================================"
if [ "$FAIL" -gt 0 ]; then
exit 1
fi
exit 0