mirror of
https://github.com/block/buzz.git
synced 2026-10-27 20:41:57 +00:00
Remove public relay signing key fallback (#6729)
Require `BUZZ_RELAY_PRIVATE_KEY` on every relay startup and remove the shared public fallback key completely. For local development, `just bootstrap` now generates a random key once in the gitignored `.env` file. Re-running bootstrap preserves that key, so relay-authored events retain the same identity across restarts. The local relay recipes reload `.env`, while isolated CI and E2E launchers pass explicit per-run keys. Startup fails immediately when the key is missing or invalid, before connecting to Postgres or Redis. Deployed relays continue to receive their stable key from the existing chart-managed secret. Validated with: - `./scripts/test-ensure-local-relay-key.sh` - `cargo test -p buzz-relay --bin buzz-relay` - `cargo clippy -p buzz-relay --bin buzz-relay -- -D warnings` - `cargo fmt --all -- --check` - `shellcheck scripts/ensure-local-relay-key.sh scripts/test-ensure-local-relay-key.sh` Signed-off-by: Jordan Mecom <jm@squareup.com>
This commit is contained in:
@@ -336,6 +336,7 @@ export BUZZ_GIT_HOOK_HMAC_SECRET="${HMAC_SECRET}"
|
||||
export BUZZ_BIND_ADDR="${RELAY_HOST}:${RELAY_PORT}"
|
||||
export RELAY_URL="${RELAY_WS}"
|
||||
export RUST_LOG="buzz_relay=warn"
|
||||
export BUZZ_RELAY_PRIVATE_KEY="${BUZZ_RELAY_PRIVATE_KEY:-$(openssl rand -hex 32)}"
|
||||
export BUZZ_REQUIRE_AUTH_TOKEN=false
|
||||
|
||||
# Clean repos dir (isolated test state)
|
||||
|
||||
66
scripts/ensure-local-relay-key.sh
Executable file
66
scripts/ensure-local-relay-key.sh
Executable file
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
ENV_FILE="${1:-.env}"
|
||||
|
||||
if [[ ! -f "${ENV_FILE}" ]]; then
|
||||
echo "error: ${ENV_FILE} does not exist" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
existing_key="$({
|
||||
unset BUZZ_RELAY_PRIVATE_KEY
|
||||
set +u
|
||||
# shellcheck disable=SC1090
|
||||
source "${ENV_FILE}" || exit 1
|
||||
printf '%s' "${BUZZ_RELAY_PRIVATE_KEY:-}"
|
||||
})"
|
||||
|
||||
if [[ -n "${existing_key}" ]]; then
|
||||
chmod 600 "${ENV_FILE}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
relay_key="$(node <<'NODE'
|
||||
const { randomBytes } = require("node:crypto");
|
||||
const curveOrder = BigInt(
|
||||
"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",
|
||||
);
|
||||
|
||||
let bytes;
|
||||
let scalar;
|
||||
do {
|
||||
bytes = randomBytes(32);
|
||||
scalar = BigInt(`0x${bytes.toString("hex")}`);
|
||||
} while (scalar === 0n || scalar >= curveOrder);
|
||||
|
||||
process.stdout.write(bytes.toString("hex"));
|
||||
NODE
|
||||
)"
|
||||
|
||||
temp_file="$(mktemp "${ENV_FILE}.tmp.XXXXXX")"
|
||||
trap 'rm -f "${temp_file}"' EXIT
|
||||
|
||||
awk -v key="${relay_key}" '
|
||||
BEGIN { replaced = 0 }
|
||||
/^[[:space:]]*(export[[:space:]]+)?BUZZ_RELAY_PRIVATE_KEY=/ {
|
||||
if (!replaced) {
|
||||
print "BUZZ_RELAY_PRIVATE_KEY=" key
|
||||
replaced = 1
|
||||
}
|
||||
next
|
||||
}
|
||||
{ print }
|
||||
END {
|
||||
if (!replaced) {
|
||||
if (NR > 0) print ""
|
||||
print "BUZZ_RELAY_PRIVATE_KEY=" key
|
||||
}
|
||||
}
|
||||
' "${ENV_FILE}" > "${temp_file}"
|
||||
|
||||
chmod 600 "${temp_file}"
|
||||
mv "${temp_file}" "${ENV_FILE}"
|
||||
trap - EXIT
|
||||
|
||||
echo "Generated BUZZ_RELAY_PRIVATE_KEY in ${ENV_FILE}."
|
||||
@@ -103,6 +103,7 @@ else
|
||||
RELAY_BIN="${ROOT}/target/ci/buzz-relay"
|
||||
fi
|
||||
log "starting relay at ${RELAY_HTTP_URL}"
|
||||
RELAY_PRIVATE_KEY="$(openssl rand -hex 32)"
|
||||
env \
|
||||
DATABASE_URL="postgres://buzz:buzz_dev@localhost:5432/${DB_NAME}" \
|
||||
REDIS_URL="redis://localhost:6379/${REDIS_DB}" \
|
||||
@@ -110,6 +111,7 @@ env \
|
||||
BUZZ_BIND_ADDR="127.0.0.1:${RELAY_PORT}" \
|
||||
BUZZ_HEALTH_PORT="${HEALTH_PORT}" \
|
||||
BUZZ_METRICS_PORT="${METRICS_PORT}" \
|
||||
BUZZ_RELAY_PRIVATE_KEY="${RELAY_PRIVATE_KEY}" \
|
||||
BUZZ_REQUIRE_AUTH_TOKEN=false \
|
||||
BUZZ_RECONCILE_CHANNELS=true \
|
||||
BUZZ_RATE_LIMIT_HUMAN_MESSAGES_PER_MIN=1000000 \
|
||||
|
||||
@@ -133,6 +133,7 @@ ok "Relay built"
|
||||
# survives (same pattern the perf stack uses). Logs to ${RELAY_LOG}.
|
||||
RELAY_LOG="${RELAY_LOG:-/tmp/dawn-relay-run.log}"
|
||||
TMUX_SESSION="${TMUX_SESSION:-dawn-relay}"
|
||||
RELAY_PRIVATE_KEY="$(openssl rand -hex 32)"
|
||||
tmux kill-session -t "${TMUX_SESSION}" 2>/dev/null || true
|
||||
if command -v lsof >/dev/null 2>&1 && lsof -nP -iTCP:"${RELAY_MAIN}" -sTCP:LISTEN >/dev/null 2>&1; then
|
||||
err "Port ${RELAY_MAIN} is already in use; refusing to report a stale relay as this harness."
|
||||
@@ -151,6 +152,7 @@ tmux new-session -d -s "${TMUX_SESSION}" "cd '${REPO_ROOT}' && env \
|
||||
BUZZ_S3_ACCESS_KEY=buzz_dev \
|
||||
BUZZ_S3_SECRET_KEY=buzz_dev_secret \
|
||||
BUZZ_S3_BUCKET=buzz-media \
|
||||
BUZZ_RELAY_PRIVATE_KEY=${RELAY_PRIVATE_KEY} \
|
||||
BUZZ_REQUIRE_AUTH_TOKEN=false \
|
||||
BUZZ_RECONCILE_CHANNELS=true \
|
||||
'./target/${CARGO_TARGET_PROFILE}/buzz-relay' > '${RELAY_LOG}' 2>&1"
|
||||
|
||||
@@ -152,6 +152,8 @@ fi
|
||||
|
||||
log "Starting relay..."
|
||||
|
||||
TEST_RELAY_PRIVATE_KEY="${BUZZ_RELAY_PRIVATE_KEY:-$(openssl rand -hex 32)}"
|
||||
|
||||
# Optional NIP-43 membership gating: exported by callers that need a
|
||||
# membership-gated relay (e.g. the mesh lifecycle smoke). All three must be
|
||||
# set together — the relay fails fast otherwise.
|
||||
@@ -160,8 +162,8 @@ if [[ "${BUZZ_REQUIRE_RELAY_MEMBERSHIP:-}" == "true" ]]; then
|
||||
MEMBERSHIP_ENV+=(
|
||||
BUZZ_REQUIRE_RELAY_MEMBERSHIP=true
|
||||
RELAY_OWNER_PUBKEY="${RELAY_OWNER_PUBKEY:?RELAY_OWNER_PUBKEY required with BUZZ_REQUIRE_RELAY_MEMBERSHIP=true}"
|
||||
BUZZ_RELAY_PRIVATE_KEY="${BUZZ_RELAY_PRIVATE_KEY:?BUZZ_RELAY_PRIVATE_KEY required with BUZZ_REQUIRE_RELAY_MEMBERSHIP=true}"
|
||||
)
|
||||
: "${BUZZ_RELAY_PRIVATE_KEY:?BUZZ_RELAY_PRIVATE_KEY required with BUZZ_REQUIRE_RELAY_MEMBERSHIP=true}"
|
||||
log "Membership gating enabled (NIP-43)"
|
||||
fi
|
||||
|
||||
@@ -170,6 +172,7 @@ nohup env \
|
||||
REDIS_URL=redis://localhost:6379 \
|
||||
RELAY_URL=ws://localhost:3000 \
|
||||
BUZZ_BIND_ADDR=0.0.0.0:3000 \
|
||||
BUZZ_RELAY_PRIVATE_KEY="${TEST_RELAY_PRIVATE_KEY}" \
|
||||
BUZZ_REQUIRE_AUTH_TOKEN=false \
|
||||
BUZZ_RECONCILE_CHANNELS=true \
|
||||
BUZZ_GIT_PROBE_WRITERS=8 \
|
||||
|
||||
33
scripts/test-ensure-local-relay-key.sh
Executable file
33
scripts/test-ensure-local-relay-key.sh
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
||||
TEST_DIR="$(mktemp -d "${TMPDIR:-/tmp}/buzz-relay-key-test.XXXXXX")"
|
||||
trap 'rm -rf "${TEST_DIR}"' EXIT
|
||||
|
||||
ENV_FILE="${TEST_DIR}/.env"
|
||||
cp "${REPO_ROOT}/.env.example" "${ENV_FILE}"
|
||||
|
||||
"${SCRIPT_DIR}/ensure-local-relay-key.sh" "${ENV_FILE}" >/dev/null
|
||||
first_key="$(sed -n 's/^BUZZ_RELAY_PRIVATE_KEY=//p' "${ENV_FILE}")"
|
||||
|
||||
if [[ ! "${first_key}" =~ ^[0-9a-f]{64}$ ]]; then
|
||||
echo "FAIL: bootstrap did not generate a valid 32-byte hex relay key" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
"${SCRIPT_DIR}/ensure-local-relay-key.sh" "${ENV_FILE}" >/dev/null
|
||||
second_key="$(sed -n 's/^BUZZ_RELAY_PRIVATE_KEY=//p' "${ENV_FILE}")"
|
||||
|
||||
if [[ "${first_key}" != "${second_key}" ]]; then
|
||||
echo "FAIL: bootstrap replaced the existing relay key" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ "$(grep -c '^BUZZ_RELAY_PRIVATE_KEY=' "${ENV_FILE}")" -ne 1 ]]; then
|
||||
echo "FAIL: bootstrap wrote more than one relay key" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "PASS: bootstrap generates one relay key and reuses it"
|
||||
Reference in New Issue
Block a user