The single-domain deployment (errors.bitreport.pt) routed all traffic through the admin-ui, whose auth middleware redirected /v1/ingest/events and /health to /login — making the ingest API unreachable from outside. - middleware.ts: pass through /v1/* and /health without auth redirect - next.config.ts: rewrite /v1/* and /health to ingest-api backend (INGEST_API_URL env, default http://127.0.0.1:3041) - docker-compose.yml: set INGEST_API_URL=http://ingest-api:3041 - env examples: document INGEST_API_URL - scripts/onboard-project.sh: generate project + ingest key SQL Paperclip-Agent: peter-spark Paperclip-Issue: BIT-750 Paperclip-Run: dc5b7949-b24d-4867-a2cd-cf4f47477ef8
99 lines
3.2 KiB
Bash
Executable File
99 lines
3.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# onboard-project.sh — create a new project + ingest key in error_reporting.
|
|
#
|
|
# Usage:
|
|
# onboard-project.sh <slug> <name> [label]
|
|
#
|
|
# Generates a random ingest token, prints the SQL to apply and the
|
|
# plaintext token to configure in the client. Requires: openssl, sha256sum.
|
|
#
|
|
# Example:
|
|
# ./scripts/onboard-project.sh crm "BitReport CRM"
|
|
# ./scripts/onboard-project.sh ott "BitReport OTT" staging
|
|
#
|
|
# Environment:
|
|
# DATABASE_URL If set and mysql client available, applies SQL directly.
|
|
# Otherwise prints SQL to stdout.
|
|
|
|
set -euo pipefail
|
|
|
|
_die() { echo "error: $*" >&2; exit 1; }
|
|
|
|
[[ $# -ge 2 ]] || _die "Usage: onboard-project.sh <slug> <name> [label]"
|
|
|
|
SLUG="$1"
|
|
NAME="$2"
|
|
LABEL="${3:-default}"
|
|
|
|
# Validate slug: lowercase alphanumeric + hyphens, 2-64 chars
|
|
[[ "${SLUG}" =~ ^[a-z0-9][a-z0-9-]{0,62}[a-z0-9]$ ]] \
|
|
|| _die "slug must be 2-64 lowercase alphanumeric/hyphens: ${SLUG}"
|
|
|
|
# Generate a random 32-byte token (er_<slug-prefix>_<random>)
|
|
SLUG_PREFIX="$(echo "${SLUG}" | tr -d '-' | head -c6)"
|
|
RANDOM_PART="$(openssl rand -hex 20)"
|
|
TOKEN="er_${SLUG_PREFIX}_${RANDOM_PART}"
|
|
|
|
# SHA-256 hash (stored in DB)
|
|
TOKEN_HASH="$(printf '%s' "${TOKEN}" | sha256sum | awk '{print $1}')"
|
|
|
|
# Token prefix for display (first 8 chars)
|
|
TOKEN_PREFIX="$(printf '%s' "${TOKEN}" | head -c8)"
|
|
|
|
# Build SQL
|
|
SQL="$(cat <<EOSQL
|
|
-- Project: ${NAME} (${SLUG})
|
|
BEGIN;
|
|
|
|
INSERT INTO er_projects (slug, name, is_active)
|
|
VALUES ('${SLUG}', '${NAME}', TRUE)
|
|
ON DUPLICATE KEY UPDATE name = VALUES(name);
|
|
|
|
INSERT INTO er_ingest_keys (project_id, label, token_prefix, token_hash, status)
|
|
SELECT p.id, '${LABEL}', '${TOKEN_PREFIX}', '${TOKEN_HASH}', 'active'
|
|
FROM er_projects p WHERE p.slug = '${SLUG}'
|
|
ON DUPLICATE KEY UPDATE token_hash = VALUES(token_hash), token_prefix = VALUES(token_prefix), status = 'active';
|
|
|
|
COMMIT;
|
|
EOSQL
|
|
)"
|
|
|
|
echo "================================================================"
|
|
echo " Project : ${NAME} (${SLUG})"
|
|
echo " Label : ${LABEL}"
|
|
echo " Token : ${TOKEN}"
|
|
echo " Hash : ${TOKEN_HASH}"
|
|
echo "================================================================"
|
|
echo ""
|
|
echo "--- SQL (apply to error_reporting database) ---"
|
|
echo "${SQL}"
|
|
echo ""
|
|
echo "--- Client configuration ---"
|
|
echo " INGEST_URL=https://errors.bitreport.pt/v1/ingest/events"
|
|
echo " INGEST_TOKEN=${TOKEN}"
|
|
echo ""
|
|
echo "--- Smoke test ---"
|
|
echo " curl -sf -X POST https://errors.bitreport.pt/v1/ingest/events \\"
|
|
echo " -H 'Authorization: Bearer ${TOKEN}' \\"
|
|
echo " -H 'Content-Type: application/json' \\"
|
|
echo " -d '{\"events\":[{\"schema_version\":1,\"occurred_at\":\"$(date -u +%Y-%m-%dT%H:%M:%S.000Z)\",\"level\":\"info\",\"message\":\"onboard smoke test\"}]}'"
|
|
echo ""
|
|
|
|
# If DATABASE_URL is set, offer to apply directly
|
|
if [[ -n "${DATABASE_URL:-}" ]] && command -v mysql >/dev/null 2>&1; then
|
|
echo "DATABASE_URL is set. Applying SQL..."
|
|
# Parse mysql:// URL
|
|
DB_URL="${DATABASE_URL#mysql://}"
|
|
DB_USER="${DB_URL%%:*}"
|
|
DB_REST="${DB_URL#*:}"
|
|
DB_PASS="${DB_REST%%@*}"
|
|
DB_REST="${DB_REST#*@}"
|
|
DB_HOST="${DB_REST%%:*}"
|
|
DB_REST="${DB_REST#*:}"
|
|
DB_PORT="${DB_REST%%/*}"
|
|
DB_NAME="${DB_REST#*/}"
|
|
|
|
echo "${SQL}" | mysql -h"${DB_HOST}" -P"${DB_PORT}" -u"${DB_USER}" -p"${DB_PASS}" "${DB_NAME}"
|
|
echo "Applied successfully."
|
|
fi
|