mirror of
https://github.com/block/buzz.git
synced 2026-10-27 04:30:52 +00:00
feat(relay): store per-community NIP-FI issuer bindings (#8372)
Stack: this PR → #8377 → #8399 → #8401 bb-public creates communities all the time, so a hand-edited `BUZZ_NIP_FI_COMMUNITIES` map can't list them. This PR stores an explicit NIP-FI issuer allowlist for each community in the database and writes it when the community is created. Login checks don't read the table yet; that comes in #8377 together with the database-backed resolver. Operator steps are in [`docs/operator-nip-fi-bindings.md`](docs/operator-nip-fi-bindings.md). **Storage.** Migration `0066_community_nip_fi_issuers` adds `community_nip_fi_issuers (community_id, issuer, created_at)`, with primary key `(community_id, issuer)` and the community write fence attached. The assertion audience is `https://<communities.host>`, so the table has no `aud` column. Zero rows means no binding. The table is included in whole-community deletion and purge. **One bindable rule.** `BUZZ_NIP_FI_PROVISION_BINDING` (`{"host_suffix": ..., "authorized_issuers": [...]}`) is parsed in `buzz-auth` with `BUZZ_NIP_FI_ISSUERS`, whatever `BUZZ_NIP_FI_MODE` is set to, and both the relay and `buzz-admin` use the same parser. Each authorized issuer must be a configured issuer (1 to 2048 bytes), and the suffix must not cover any host declared in `BUZZ_NIP_FI_COMMUNITIES`; an unreadable declaration refuses startup even in mode `off`. `ProvisionBindingPolicy::is_bindable(host)` is true for a host exactly one label under the suffix that is not reserved. It decides creation-time binding, explicit `bind`, and what the lookup route reports. **Binding on creation.** Rows are written in the same transaction as the community insert on all three creation paths (`POST /operator/communities` `create_only`, its legacy convergence branch, and startup bootstrap), and only when the call actually created the community, so existing rows are never rewritten. **Reserved host.** A host whose `https://<host>` equals a configured `command_audiences` value never holds a binding. Operator provisioning of it is rejected with a 400 and the `reserved_host` error code, bootstrap creates it unbound, and the availability route reports it unavailable with an error code. `main.rs` calls `verify_reserved_hosts_unbound` at startup; if a reserved host has rows the relay refuses to start and names the fix (`buzz-admin nip-fi unbind --host <h> --issuer <i>`, or revert the command audience). A database failure during that check is a separate `Check` error. **Operator commands.** - `buzz-admin nip-fi backfill` pages unbound communities by host (1000 per page) and binds each page with one `INSERT ... SELECT` limited to active communities. It skips communities being deleted, always prints `{rows_bound, unbound, unbound_hosts, truncated}`, and exits non-zero while any bindable community is still unbound. Archived communities are bound on purpose. Both bind paths take the shared deletion lock and then lock the community row (`FOR NO KEY UPDATE`, in id order for a batch), the same order deletion uses, and the backfill insert also requires the community to still have no binding, so an operator `bind` that lands after the backfill listed a community wins and the default issuer is not added beside it. - `buzz-admin nip-fi bind --host --issuer` refuses a reserved host, a host outside the suffix, or an unconfigured issuer. - `buzz-admin nip-fi unbind --host --issuer` refuses to remove a community's last issuer (archive or delete the community instead), with one exception: when a valid provisioning policy shows that backfill can't bind the host (it's reserved, or outside the suffix), the last issuer can be removed. That is the reserved-host recovery path in the runbook. A missing or malformed policy doesn't block `unbind`; it just means the exception never applies. **Lookup route.** `GET /operator/communities/nip-fi-binding?host=` (NIP-98, `RELAY_OPERATOR_PUBKEYS`) reads the database and returns `{host, active, issuers[]}`. `host` is the stored host. It returns 404 for an unknown host, 200 with `active:false` for an archived or quiescing community, and `issuers:[]` when no provisioning policy is set. It lists only issuers that are configured and on a bindable host. kgoose depends on this contract; it is also documented in the runbook. **NIP-FA.** The `relay_url` bullet now also says: "An adapter MAY obtain its exact relay list from a configured, authenticated source; it MUST still compare `relay_url` exactly." **Deployment dependency.** The kgoose config side is already handled by [cash-server#137671](https://github.com/squareup/cash-server/pull/137671). In public-community mode it requires the command audience not to be a community audience. --------- Signed-off-by: Will Pfleger <pfleger.will@gmail.com> Signed-off-by: Duncan <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz> Co-authored-by: Duncan <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz>
This commit is contained in:
@@ -106,6 +106,7 @@ CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||
\i tables/public/moderation_actions.sql
|
||||
\i tables/public/moderation_reports.sql
|
||||
\i tables/public/community_bans.sql
|
||||
\i tables/public/community_nip_fi_issuers.sql
|
||||
\i tables/public/_operator_global_tables.sql
|
||||
\i tables/public/git_repo_names.sql
|
||||
\i tables/public/parameterized_event_watermarks.sql
|
||||
@@ -153,6 +154,7 @@ CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||
\i triggers/audit_log.sql
|
||||
\i triggers/channel_members.sql
|
||||
\i triggers/community_bans.sql
|
||||
\i triggers/community_nip_fi_issuers.sql
|
||||
\i triggers/delivery_log.sql
|
||||
\i triggers/event_mentions.sql
|
||||
\i triggers/git_repo_names.sql
|
||||
|
||||
12
schema/tables/public/community_nip_fi_issuers.sql
Normal file
12
schema/tables/public/community_nip_fi_issuers.sql
Normal file
@@ -0,0 +1,12 @@
|
||||
-- ── NIP-FI community issuer bindings ──────────────────────────────────────────
|
||||
-- The explicit per-community issuer allowlist for NIP-FI assertions. The
|
||||
-- assertion audience is `https://<communities.host>`, so it needs no column of
|
||||
-- its own. Zero rows means the community has no binding and NIP-FI admission
|
||||
-- for it fails closed.
|
||||
|
||||
CREATE TABLE community_nip_fi_issuers (
|
||||
community_id UUID NOT NULL REFERENCES communities(id),
|
||||
issuer TEXT NOT NULL CHECK (length(issuer) BETWEEN 1 AND 2048),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (community_id, issuer)
|
||||
);
|
||||
1
schema/triggers/community_nip_fi_issuers.sql
Normal file
1
schema/triggers/community_nip_fi_issuers.sql
Normal file
@@ -0,0 +1 @@
|
||||
SELECT attach_community_write_fence('community_nip_fi_issuers');
|
||||
Reference in New Issue
Block a user