Add public nostr submission relay and approved-content sync

Add a second strfry instance and the machinery to promote moderated content
into the members relay, for raucao/marco#97.

New role[strfry_submission] and the kosmos_strfry_submission cookbook run a
separate relay (port 7778, own DB capped at a 4 GiB mapsize) fronted at
submit.nostr.kosmos.org, using the open submission write policy from akkounts
extras/strfry. It is deliberately not role[strfry], so nginx never adds it to
the members relay's _strfry upstream.

Add kosmos_strfry::approved_sync: a dedicated fail-closed sync config plus a
5-minute timer that pulls moderator-approved content from the nuc staging relay
into the members relay. The sync command URL and STAGING_RELAY_URL come from a
single attribute so the policy's byte-exact source match cannot drift.

Also add a role-scoped Node 22 install (the global Node attribute is
untouched), a proxy-scoped firewall rule, an nginx site, a runbook, and a
render regression check for the sync template.

Deployment is gated: approved_sync.enabled defaults to false, and the akkounts
policy files must be on the live branch before converging.
This commit is contained in:
raucao committed 2026-11-17 14:02:16 +04:00
1 parent ea04af784e
commit 8b8dd42b48
17 files changed
+893

No files matched your search

+146
View File
@@ -0,0 +1,146 @@
# Nostr submission relay (Chef)
Chef-side pieces of the public submission relay + image-recognition moderation
MVP ([`raucao/marco#97`](https://gitea.kosmos.org/raucao/marco/issues/97)). The
canonical plan/spec lives in the Marco repo, `doc/nostr/submission-relay-mvp/`.
This document is the Chef runbook: what the code deploys, in which order, and
how to roll it back. **Nothing here has been deployed** — the cookbook changes
are prepared but not converged.
## What the Chef code adds
| Piece | Where | Recipe / role |
| --- | --- | --- |
| Public submission relay (strfry #2, `:7778`) | one node | `role[strfry_submission]` → `kosmos_strfry_submission::default` |
| Firewall for `:7778` | same node | `kosmos_strfry_submission::firewall` |
| `wss://submit.nostr.kosmos.org` vhost + cert | openresty proxy nodes | `kosmos_strfry_submission::nginx` (via `role[openresty_proxy]`) |
| Approved-event sync (staging `:7779` → production DB) | production relay node | `kosmos_strfry::approved_sync` (via `role[strfry]`) |
| Node 22 LTS for both write policies | relay nodes | role-scoped `kosmos_nodejs.version` override |
Two write policies from `kosmos/akkounts` `extras/strfry/` are fetched at
converge time:
* `submission-policy.mjs` — open write gates for the submission relay.
* `sync-policy.mjs` — fail-closed policy for `strfry sync` (see below).
## Design notes / invariants
* **The submission relay is not `role[strfry].`** The openresty proxy builds its
production `_strfry` upstream from `role:strfry`; a submission node must never
be added to it. The new role also avoids `role[ldap_client]`, because the
submission relay is open to all authors and does not do LDAP.
* **The sync is the only non-member write path into production.** It runs
`strfry --config /etc/strfry-sync.conf sync <staging> --dir down`, one-way,
every 5 minutes. `strfry sync --dir down` runs the *local* write policy with
`sourceType="Sync"` and `sourceInfo=<remote relay URL>`
(`src/apps/mesh/cmd_sync.cpp`), so `sync-policy.mjs` can accept only that exact
source and reject everything else. `STAGING_RELAY_URL` is passed to the unit
and must byte-match the URL on the command line — both come from the single
`node["strfry"]["staging_relay_url"]` attribute.
* **The sync uses the production database** (`node["strfry"]["config"]["db"]`)
with a 100-year age floor (`rejectEventsOlderThanSeconds = 3153600000`) so
long-delayed approvals still land. Note that `0` does **not** disable this
check in strfry: strfry computes `earliest = now - rejectEventsOlderThanSeconds`,
so `0` makes `earliest == now` and rejects every event as "created_at too
early" *before the write policy runs*. A large bound is the way to turn it off.
* **The submission relay is reachable only from the openresty proxy nodes.**
The relay trusts nginx's `X-Real-IP` (`realIpHeader = "x-real-ip"`), so a
direct connection could forge that header and defeat the policy's per-IP token
bucket. `kosmos_strfry_submission::firewall` therefore allow-lists the ZT IPs
of the `role:openresty_proxy` nodes instead of all of `10.1.1.0/24` (the
per-author bucket is unaffected). Consequence: anything that talks to the
relay *directly* over ZeroTier must be added explicitly — in particular, if
the WS2 moderator is switched from `wss://submit.nostr.kosmos.org` to the
ZT-direct `ws://<relay>:7778`, add the nuc's address to the allow-list (or
route it through nginx). The member bridge uses loopback and is unaffected.
* **No `--filter` on the sync.** The worker only publishes approved events to
the staging relay; the policy is the gate.
## Attributes
| Attribute | Default | Notes |
| --- | --- | --- |
| `strfry.staging_relay_url` | `ws://10.1.1.183:7779` | nuc ZeroTier IP + staging port |
| `strfry.approved_sync.enabled` | `false` | **no-op until enabled** |
| `strfry_submission.domain` | `submit.nostr.kosmos.org` | |
| `strfry_submission.port` | `7778` | |
| `strfry_submission.config.db_params.mapsize` | `4 GiB` | disk cap for the disposable DB |
## Regression checks
`site-cookbooks/kosmos_strfry/test/strfry_sync_conf_test.rb` renders
`strfry_sync.conf.erb` with the attribute shapes `approved_sync.rb` passes and
fails if the sync config's `rejectEventsOlderThanSeconds` is missing or `<= 0`
(the guard for the "0 rejects every event" trap above). It needs neither a Chef
server nor a converge — only the `chef` gem and stdlib ERB:
```
ruby site-cookbooks/kosmos_strfry/test/strfry_sync_conf_test.rb
```
## Deployment order
0. **HARD PRECONDITION — akkounts `live` branch.** Before *any* converge, the
`live` branch of `kosmos/akkounts` must contain **both**
`extras/strfry/submission-policy.mjs` and `extras/strfry/sync-policy.mjs`.
Chef fetches them from
`https://gitea.kosmos.org/kosmos/akkounts/raw/branch/live/extras/strfry/…` at
converge time; if either is missing, the `remote_file` resource fails and the
converge aborts (with a partially configured host). Verify with:
`curl -fsSI https://gitea.kosmos.org/kosmos/akkounts/raw/branch/live/extras/strfry/submission-policy.mjs`
and the same for `sync-policy.mjs`. These files are produced by WS1, not by
this repo.
1. **DNS (manual).** Add an **A/AAAA record** for `submit.nostr.kosmos.org` at
Gandi pointing at the openresty proxy node(s). Chef does not manage DNS, and
the `gandi_dns` Let's Encrypt challenge cannot succeed without it.
2. **Relay node.** Assign `role[strfry_submission]`, converge. Creates the
binary (or reuses it), DB, config, policy and `strfry-submission.service`.
The firewall allow-lists the current `role:openresty_proxy` node IPs, so the
proxy nodes should already be registered as Chef nodes.
3. **Proxy nodes.** Converge `role[openresty_proxy]`; this adds the
`submit.nostr.kosmos.org` site and requests the certificate.
4. **Enable the sync.** Only after the worker + staging relay (WS2) are live:
set `strfry.approved_sync.enabled = true` (environment or role override) and
converge `role[strfry]`. This deploys `sync-policy.mjs`, the sync config, and
the `strfry-sync.timer`.
5. **Verify.**
* `systemctl status strfry-submission` / `strfry-sync.timer`.
* `curl -s https://submit.nostr.kosmos.org | jq -r .name` →
`Kosmos submission relay`.
* `journalctl -u strfry-sync` shows accepted events after an approval.
* The production relay's NIP-11 and `_strfry` upstream are unchanged.
## Rollback
* **Sync:** set `strfry.approved_sync.enabled = false` and converge, then
`systemctl disable --now strfry-sync.timer`. (The recipe becomes a no-op, but
it does not stop an already-running timer.)
* **Submission relay / proxy:** remove `kosmos_strfry_submission::nginx` from
`role[openresty_proxy]` and `role[strfry_submission]` from the node, converge,
then `systemctl disable --now strfry-submission`. The DB at
`/var/lib/strfry-submission` can be deleted independently; the production
relay and its database are untouched.
## Node.js caveat (check before converging)
Both new policies are Node 22 ESM scripts, so `role[strfry_submission]` and
`role[strfry]` add a **role-scoped** `kosmos_nodejs.version` override to
`22.23.4`. The global default (`14.21.3`) is deliberately **not** raised.
If either role is applied to a host that already runs other Node/JS services,
verify they do not depend on Node 14 *before* converging:
* On **strfry-1**: `blossom` uses Deno (its recipe sets `DENO_DIR`), not Node;
`substr` is the prebuilt Rust binary from the package registry. Neither
currently installs or uses Node, so adding Node 22 is additive. Re-check if
`substr` ever falls back to `deno task server` or if a Node-based service is
added.
* Any other node that gains `role[strfry_submission]` should be checked the same
way.
## Out of scope
* Deploying/converging, DNS changes, certificate issuance, ZeroTier changes.
* The nuc staging relay and the moderator/member-bridge (WS2).
* The write policies themselves (WS1, `kosmos/akkounts`).
+1
View File
@@ -30,6 +30,7 @@ production_run_list = %w(
kosmos_rsk::nginx_testnet
kosmos_rsk::nginx_mainnet
kosmos_strfry::nginx
kosmos_strfry_submission::nginx
kosmos_website::default
kosmos_website::redirects
kosmos-akkounts::nginx
+11
View File
@@ -1,9 +1,20 @@
name "strfry"
override_attributes(
# Node 22 LTS runs the fail-closed approved-sync write policy
# (/opt/strfry/sync-policy.mjs). Scoped to this role so the global
# kosmos_nodejs default (14.21.3) stays untouched everywhere else.
"kosmos_nodejs" => {
"version" => "22.23.4"
}
)
run_list %w(
role[ldap_client]
kosmos-nodejs::default
strfry::default
kosmos_strfry::policies
kosmos_strfry::firewall
kosmos_strfry::substr
kosmos_strfry::approved_sync
)
+19
View File
@@ -0,0 +1,19 @@
name "strfry_submission"
override_attributes(
# Node 22 LTS is required by the submission write policy
# (/opt/strfry/submission-policy.mjs). This override is scoped to this role
# only; the global kosmos_nodejs default (14.21.3) is intentionally left
# untouched for every other node.
"kosmos_nodejs" => {
"version" => "22.23.4"
}
)
run_list %w(
role[base]
role[kvm_guest]
kosmos-nodejs::default
kosmos_strfry_submission::default
kosmos_strfry_submission::firewall
)
+12
View File
@@ -2,3 +2,15 @@ kosmos_strfry
=============
Installs/configures a strfry relay and its reverse proxy config
## Tests
`test/strfry_sync_conf_test.rb` is a standalone regression check for
`templates/strfry_sync.conf.erb` (the `kosmos_strfry::approved_sync` sync
config). It asserts that the sync's `rejectEventsOlderThanSeconds` age floor is
present and positive, because `0` does *not* disable the check in strfry — it
rejects every synced event. No Chef server or converge is required:
```
ruby site-cookbooks/kosmos_strfry/test/strfry_sync_conf_test.rb
```
@@ -1,6 +1,15 @@
node.default["strfry"]["ldap_search_dn"] = "ou=kosmos.org,cn=users,dc=kosmos,dc=org"
node.default["strfry"]["extras_dir"] = "/opt/strfry"
#
# approved sync (WS3): pull worker-approved events from the staging relay into
# the production database. See kosmos_strfry/recipes/approved_sync.rb.
#
node.default["strfry"]["staging_relay_url"] = "ws://10.1.1.183:7779"
node.default["strfry"]["approved_sync"]["enabled"] = false
node.default["strfry"]["approved_sync"]["config_file"] = "/etc/strfry-sync.conf"
node.default["strfry"]["approved_sync"]["policy_url"] = "https://gitea.kosmos.org/kosmos/akkounts/raw/branch/live/extras/strfry/sync-policy.mjs"
# node.default["substr"]["repo"] = "https://gitea.kosmos.org/kosmos/substr.git"
# node.default["substr"]["revision"] = "master"
node.default["substr"]["version"] = "nightly"
@@ -0,0 +1,122 @@
#
# Cookbook Name:: kosmos_strfry
# Recipe:: approved_sync
#
# Pull moderator-approved events from the nuc staging relay (ws://<nuc>:7779)
# into the production LMDB database used by nostr.kosmos.org, without exposing
# a public write path into it. This is the only way non-member, VLM-approved
# content reaches the production relay.
#
# The sync runs against a DEDICATED config (/etc/strfry-sync.conf) that points
# at the SAME database as the production relay but installs a fail-closed write
# policy (sync-policy.mjs): only events with sourceType="Sync" and a byte-exact
# sourceInfo match for STAGING_RELAY_URL are accepted.
#
# This recipe is a no-op until node["strfry"]["approved_sync"]["enabled"] is
# set to true (e.g. in an environment or a node's role overrides).
#
unless node["strfry"]["approved_sync"]["enabled"]
Chef::Log.info("kosmos_strfry::approved_sync is disabled; skipping staged-event sync.")
return
end
config_file = node["strfry"]["approved_sync"]["config_file"]
staging_url = node["strfry"]["staging_relay_url"]
extras_dir = node["strfry"]["extras_dir"]
if staging_url.to_s.empty?
raise "node['strfry']['staging_relay_url'] must be set when approved_sync is enabled"
end
directory extras_dir do
owner node["strfry"]["user"]
group node["strfry"]["group"]
mode "0755"
end
#
# Fail-closed write policy. Deployed here (and not in ::policies) because it is
# only meaningful together with the sync config.
#
remote_file "#{extras_dir}/sync-policy.mjs" do
source node["strfry"]["approved_sync"]["policy_url"]
owner node["strfry"]["user"]
group node["strfry"]["group"]
mode "0755"
end
#
# Config: identical db + settings as the production relay, except that we keep
# arbitrarily old events (approved long after submission) and swap in the
# sync-only write policy.
#
prod_config = node["strfry"]["config"]
prod_db_params = prod_config["db_params"] || {}
template config_file do
source "strfry_sync.conf.erb"
owner node["strfry"]["user"]
group node["strfry"]["group"]
mode "0644"
variables db: prod_config["db"],
maxreaders: prod_db_params["maxreaders"] || "256",
mapsize: prod_db_params["mapsize"] || "10995116277760",
no_read_ahead: prod_db_params["no_read_ahead"] || "false",
plugin: "node #{extras_dir}/sync-policy.mjs",
timeout_seconds: 10
end
#
# Oneshot sync, driven by a timer. `sync --dir down` is one-way (staging ->
# production) and DOES run the local write policy with sourceType="Sync" and
# sourceInfo=<staging URL> (see strfry src/apps/mesh/cmd_sync.cpp). No --filter:
# the worker only ever publishes approved events to the staging relay, and the
# policy rejects everything else.
#
systemd_unit "strfry-sync.service" do
content({
Unit: {
Description: "strfry approved-event sync (staging -> production)",
Documentation: ["https://github.com/hoytech/strfry"],
After: "strfry.service",
Wants: "network-online.target"
},
Service: {
Type: "oneshot",
User: node["strfry"]["user"],
Group: node["strfry"]["group"],
ExecStart: "/usr/local/bin/strfry --config #{config_file} sync #{staging_url} --dir down",
# sync-policy.mjs compares sourceInfo byte-for-byte against this value.
Environment: ["STAGING_RELAY_URL=#{staging_url}"],
Nice: 10,
ProtectHome: "no",
NoNewPrivileges: "yes",
ProtectSystem: "full",
LimitCORE: "0",
TimeoutStartSec: "600"
},
Install: {
WantedBy: "multi-user.target"
}
})
triggers_reload true
action :create
end
systemd_unit "strfry-sync.timer" do
content({
Unit: {
Description: "Run the approved-event sync every 5 minutes"
},
Timer: {
OnCalendar: "*-*-* *:0/5:00",
Persistent: "true"
},
Install: {
WantedBy: "timers.target"
}
})
triggers_reload true
action [:create, :enable, :start]
end
@@ -0,0 +1,40 @@
# Managed by Chef (kosmos_strfry::approved_sync). Do not edit by hand.
#
# Dedicated config for `strfry sync` (staging -> production ingestion). It uses
# the SAME LMDB database as the production relay (<%= @db %>), but installs a
# fail-closed write policy that only accepts events synced from the staging
# relay.
#
# Only the settings that matter for a sync-only process are set here; everything
# else falls back to strfry's compiled-in defaults.
db = "<%= @db %>/"
dbParams {
# This opens the production relay's existing LMDB map file; it is NOT a new
# allocation. maxreaders/mapsize must therefore match (or exceed) what the
# running production relay uses -- hence the fallbacks are the production
# relay's values (strfry's 10 TB default mapsize).
maxreaders = <%= @maxreaders %>
mapsize = <%= @mapsize %>
noReadAhead = <%= @no_read_ahead %>
}
events {
# Approved events reach the staging relay long after they were created, so
# the age floor is effectively disabled here (100 years).
#
# NOTE: 0 does NOT disable this check in strfry. strfry computes
# earliest = now - rejectEventsOlderThanSeconds
# so 0 makes earliest == now and rejects EVERY event with
# "created_at too early" -- before the write policy is even consulted.
# Use a large bound instead.
rejectEventsOlderThanSeconds = 3153600000
}
relay {
writePolicy {
plugin = "<%= @plugin %>"
timeoutSeconds = <%= @timeout_seconds %>
}
}
+110
View File
@@ -0,0 +1,110 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# Regression check for kosmos_strfry/templates/strfry_sync.conf.erb.
#
# Background: `strfry sync` must accept events that were created long before
# they were approved, so the sync config sets a 100-year age floor. The counter
# intuitive part (see doc/nostr-submission-relay.md) is that 0 does NOT disable
# the check in strfry -- it computes `earliest = now - rejectEventsOlderThanSeconds`,
# so 0 rejects every event. This test renders the real template (no Chef server
# needed) with the same attribute shapes that kosmos_strfry::approved_sync
# passes, and fails if the age floor is missing or non-positive.
#
# Run standalone: ruby site-cookbooks/kosmos_strfry/test/strfry_sync_conf_test.rb
#
# No Chef server / converge required: this only needs the chef gem (for
# ImmutableMash, to mirror the recipe's attribute lookups) and stdlib ERB.
require 'erb'
require 'chef' # Chef::Node::ImmutableMash
TEMPLATE = File.expand_path('../templates/strfry_sync.conf.erb', __dir__)
# The comment in the template mentions "0" a few times; only real settings (a
# `key = value` line with the key at the start of the line) must be validated.
SETTING_RE = /^\s*rejectEventsOlderThanSeconds\s*=\s*(-?\d+)\s*$/.freeze
EXPECTED_AGE_FLOOR = 3_153_600_000 # 100 years
def render(node)
prod_config = node['strfry']['config']
prod_db_params = prod_config['db_params'] || {}
# Same variable shapes kosmos_strfry/recipes/approved_sync.rb passes to the
# template resource (keep in sync if that recipe changes).
vars = {
db: prod_config['db'],
maxreaders: prod_db_params['maxreaders'] || '256',
mapsize: prod_db_params['mapsize'] || '10995116277760',
no_read_ahead: prod_db_params['no_read_ahead'] || 'false',
plugin: 'node /opt/strfry/sync-policy.mjs',
timeout_seconds: 10
}
# Chef renders templates with the `variables` hash exposed as instance
# variables (@db, @maxreaders, ...); mirror that with a bare binding.
context = Object.new
vars.each { |name, value| context.instance_variable_set("@#{name}", value) }
ERB.new(File.read(TEMPLATE), trim_mode: '-').result(context.instance_eval { binding })
end
def check_node(description, node)
rendered = render(node)
match = rendered.match(SETTING_RE)
raise "#{description}: render has no `rejectEventsOlderThanSeconds = <int>` setting" if match.nil?
value = match[1].to_i
if value <= 0
raise "#{description}: rejectEventsOlderThanSeconds = #{value} (must be > 0; 0 rejects every synced event)"
end
if value != EXPECTED_AGE_FLOOR
raise "#{description}: rejectEventsOlderThanSeconds = #{value}, expected #{EXPECTED_AGE_FLOOR}"
end
unless rendered.include?('rejectEventsOlderThanSeconds = 3153600000')
raise "#{description}: literal `rejectEventsOlderThanSeconds = 3153600000` not found in render"
end
# Sanity-check the other settings the recipe derives, so a broken fallback
# (e.g. an empty db) is caught too.
unless rendered.include?("db = \"#{node['strfry']['config']['db']}/\"")
raise "#{description}: db setting does not match node attribute"
end
raise "#{description}: writePolicy block missing" unless rendered.include?('writePolicy')
puts "ok - #{description} (rejectEventsOlderThanSeconds = #{value})"
end
# Representative cases, using the attribute shapes kosmos_strfry::approved_sync
# reads (see site-cookbooks/strfry/attributes/default.rb for the production
# config shape).
cases = {
'production relay config with explicit db_params' => Chef::Node::ImmutableMash.new(
'strfry' => {
'config' => {
'db' => '/var/lib/strfry',
'db_params' => {
'maxreaders' => '2048',
'mapsize' => '10995116277760',
'no_read_ahead' => 'false'
}
}
}
),
'production relay config with db_params absent (template fallbacks)' => Chef::Node::ImmutableMash.new(
'strfry' => {
'config' => {
'db' => '/var/lib/strfry'
}
}
)
}
begin
cases.each { |description, node| check_node(description, node) }
puts "\nPASS: strfry_sync.conf.erb keeps a positive 100-year age floor."
rescue StandardError => e
warn "FAIL: #{e.message}"
exit 1
end
@@ -0,0 +1,20 @@
Copyright (c) 2024 Kosmos Developers
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,83 @@
kosmos_strfry_submission
========================
Installs and configures the **public submission relay** for Kosmos: a second,
independent strfry instance that accepts *unreviewed* place photos (Nostr kind
`360`, later `30360`) from anyone, on port `7778`, served publicly as
`wss://submit.nostr.kosmos.org`.
It is intentionally **not** `kosmos_strfry` / `role[strfry]`:
* The openresty proxy builds its production `_strfry` upstream from nodes with
`role[strfry]`. A submission node must never end up in that pool.
* It has its own config, database, systemd unit and write policy.
## Recipes
| Recipe | Purpose |
| --- | --- |
| `default` | strfry binary (reused when `strfry::default` installed it), `/opt/strfry/submission-policy.mjs`, `/etc/strfry-submission.conf`, `strfry-submission.service` |
| `firewall` | allow TCP `7778` from the openresty proxy nodes only (resolved via `search(:node, "role:openresty_proxy")`) |
| `nginx` | runs on `role[openresty_proxy]`: TLS cert + `openresty_site` with its own `_strfry_submission` upstream |
## Attributes
| Attribute | Default | Notes |
| --- | --- | --- |
| `strfry_submission.domain` | `submit.nostr.kosmos.org` | public relay hostname |
| `strfry_submission.port` | `7778` | strfry listen port |
| `strfry_submission.config_file` | `/etc/strfry-submission.conf` | |
| `strfry_submission.config.db` | `/var/lib/strfry-submission` | separate LMDB database |
| `strfry_submission.config.db_params.mapsize` | `4 GiB` | hard cap; strfry has no prune |
| `strfry_submission.config.relay.info.name` | `Kosmos submission relay` | NIP-11 name |
| `strfry_submission.config.relay.write_policy.plugin` | `node /opt/strfry/submission-policy.mjs` | |
| `strfry_submission.policy_url` | akkounts `extras/strfry/submission-policy.mjs` | fetched at converge |
| `strfry_submission.policy_env` | `{}` | optional env for the policy |
The write policy itself lives in `kosmos/akkounts` (`extras/strfry/`) and is
fetched at converge time, exactly like the production Deno policy. Its env var
names are documented there.
## Firewall / exposure
Only the openresty proxy nodes (resolved from `role:openresty_proxy`) may reach
`7778`. Public traffic arrives through nginx as
`wss://submit.nostr.kosmos.org`, and the relay takes the client IP from nginx's
`X-Real-IP` header (`relay.realIpHeader = "x-real-ip"`). Because that header is
trusted, a virgo member that could connect to `:7778` directly could forge it
and defeat the policy's **per-IP** token bucket — hence the narrow allow-list
(the **per-author** bucket, keyed on `event.pubkey`, is unaffected either way).
The member bridge on strfry-1 uses `ws://localhost:7778` (loopback) and is not
affected by the allow-list.
Everything else on the host (production relay `7777`, blossom, substr) is
unaffected.
## Node.js
The write policy is a plain Node ESM script and needs **Node 22 LTS**. Provision
it via `role[strfry_submission]`, which sets a role-scoped
`kosmos_nodejs.version` override. Do not raise the global default (14.21.3).
If this role is applied to a host that also runs blossom/substr (e.g. strfry-1),
verify those services do not depend on Node 14 before converging — see
`doc/nostr-submission-relay.md`.
## Deployment
1. Manual DNS: add an A/AAAA record for `submit.nostr.kosmos.org` at Gandi
pointing at the openresty proxy node(s). **Chef does not manage DNS**, and the
Let's Encrypt certificate cannot be issued without it.
2. Provision/assign `role[strfry_submission]` and converge the relay node.
3. Converge the openresty proxy nodes (`role[openresty_proxy]`), which adds the
`kosmos_strfry_submission::nginx` site.
4. Verify: `curl -s https://submit.nostr.kosmos.org` returns the NIP-11 document
with name `Kosmos submission relay`; `systemctl status strfry-submission`.
Rollback: remove `kosmos_strfry_submission::nginx` from the proxy run list,
converge, and stop/disable `strfry-submission` on the relay node. The database
lives in `/var/lib/strfry-submission` and can be deleted independently from the
production relay.
No deployment is performed by this cookbook change; see
`doc/nostr-submission-relay.md` for the full runbook.
@@ -0,0 +1,49 @@
#
# Cookbook Name:: kosmos_strfry_submission
# Attributes:: default
#
node.default["strfry_submission"]["domain"] = "submit.nostr.kosmos.org"
node.default["strfry_submission"]["port"] = 7778
node.default["strfry_submission"]["config_file"] = "/etc/strfry-submission.conf"
# Same directory as the production Deno policy: both live under /opt/strfry.
node.default["strfry_submission"]["extras_dir"] = "/opt/strfry"
# Node write policy, fetched from kosmos/akkounts.
node.default["strfry_submission"]["policy_url"] = "https://gitea.kosmos.org/kosmos/akkounts/raw/branch/live/extras/strfry/submission-policy.mjs"
# Extra environment variables for the write policy plugin (inherited from the
# strfry process). Empty means "use the policy's built-in defaults".
node.default["strfry_submission"]["policy_env"] = {}
node.default["strfry_submission"]["config"] = {
db: "/var/lib/strfry-submission",
db_params: {
# This relay is a small, disposable landing area for *unreviewed* content,
# so cap the LMDB map well below the production relay's 10 TB: a flood must
# not be able to fill the disk (strfry has no prune).
mapsize: 4 * 1024 * 1024 * 1024, # 4 GiB
maxreaders: 128
},
events: {
max_event_size: 65536
},
relay: {
bind: "0.0.0.0",
port: 7778,
real_ip_header: "x-real-ip",
info: {
name: "Kosmos submission relay",
description: "Public relay for submitted place photos. Content is reviewed before it is republished to nostr.kosmos.org.",
contact: "ops@kosmos.org"
},
write_policy: {
plugin: "node /opt/strfry/submission-policy.mjs",
timeout_seconds: 10
},
logging: {
invalid_events: true
}
}
}
@@ -0,0 +1,13 @@
name 'kosmos_strfry_submission'
maintainer 'Kosmos'
maintainer_email 'ops@kosmos.org'
license 'MIT'
description 'Installs/configures the public nostr submission relay (a second strfry instance)'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '0.1.0'
chef_version '>= 18.0'
depends 'firewall'
depends 'kosmos-base'
depends 'kosmos_openresty'
depends 'strfry'
@@ -0,0 +1,158 @@
#
# Cookbook Name:: kosmos_strfry_submission
# Recipe:: default
#
# A second, independent strfry instance: the public submission relay
# (submit.nostr.kosmos.org, port 7778). Writes are open to everyone and gated
# only by the Node write policy in /opt/strfry/submission-policy.mjs.
#
# This recipe deliberately does NOT run strfry::default / role[strfry]: the
# openresty proxy builds its production `_strfry` upstream from nodes with
# role[strfry], and this relay must never end up in it.
#
unless platform?("ubuntu")
raise "This recipe only supports Ubuntu installs at the moment"
end
extras_dir = node["strfry_submission"]["extras_dir"]
config_file = node["strfry_submission"]["config_file"]
db_dir = node["strfry_submission"]["config"]["db"]
#
# Reuse the strfry binary that strfry::default installs on a shared host, and
# install the same binary (same repo/revision/download_url attributes) when
# this role owns a dedicated node. strfry::default stays the source of truth
# for upgrades; we only fill the gap when the binary is not already present.
#
unless ::File.exist?("/usr/local/bin/strfry")
if node["strfry"]["download_url"]
remote_file "/usr/local/bin/strfry" do
source node["strfry"]["download_url"]
checksum node["strfry"]["checksum"]
mode "0755"
show_progress true
notifies :restart, "service[strfry-submission]", :delayed
end
else
%w[
git build-essential libssl-dev zlib1g-dev liblmdb-dev libflatbuffers-dev
libsecp256k1-dev libzstd-dev
].each do |pkg|
package pkg
end
git "/usr/local/src/strfry" do
repository node["strfry"]["repo"]
revision node["strfry"]["revision"]
enable_submodules true
notifies :run, "execute[compile strfry]", :immediately
end
execute "compile strfry" do
cwd "/usr/local/src/strfry"
command "make setup-golpe && make -j$(nproc)"
action :nothing
notifies :create, "link[/usr/local/bin/strfry]", :immediately
notifies :restart, "service[strfry-submission]", :delayed
end
link "/usr/local/bin/strfry" do
to "/usr/local/src/strfry/strfry"
action :nothing
end
end
end
group node["strfry"]["group"]
user node["strfry"]["user"] do
gid node["strfry"]["group"]
manage_home true
shell "/bin/bash"
end
directory db_dir do
owner node["strfry"]["user"]
group node["strfry"]["group"]
mode "0755"
end
directory extras_dir do
owner node["strfry"]["user"]
group node["strfry"]["group"]
mode "0755"
end
#
# Write policy (plain Node ESM, no dependencies), fetched from kosmos/akkounts
# exactly like kosmos_strfry::policies fetches the production Deno policy.
#
remote_file "#{extras_dir}/submission-policy.mjs" do
source node["strfry_submission"]["policy_url"]
owner node["strfry"]["user"]
group node["strfry"]["group"]
mode "0755"
notifies :restart, "service[strfry-submission]", :delayed
end
#
# Config. Rendered from the generic strfry config template so it always stays
# in sync with the config options supported by the installed strfry.
#
template config_file do
source "strfry.conf.erb"
cookbook "strfry"
owner node["strfry"]["user"]
group node["strfry"]["group"]
mode "0644"
variables config: node["strfry_submission"]["config"]
notifies :restart, "service[strfry-submission]", :delayed
end
service "strfry-submission" do
action :nothing
end
service_settings = {
Type: "simple",
User: node["strfry"]["user"],
Group: node["strfry"]["group"],
ExecStart: "/usr/local/bin/strfry --config #{config_file} relay",
Restart: "on-failure",
RestartSec: "5",
ProtectHome: "no",
NoNewPrivileges: "yes",
ProtectSystem: "full",
LimitCORE: "1000000000",
LimitNOFILE: "524288",
# Best-effort caps so this open, potentially abused relay cannot starve the
# production relay and blossom/substr that share the host.
Nice: 10,
MemoryHigh: "2G",
MemoryMax: "3G"
}
policy_env = node["strfry_submission"]["policy_env"] || {}
unless policy_env.empty?
service_settings[:Environment] = policy_env.map { |key, value| "#{key}=#{value}" }
end
systemd_unit "strfry-submission.service" do
content({
Unit: {
Description: "strfry public submission relay",
Documentation: ["https://github.com/hoytech/strfry"]
},
Service: service_settings,
Install: {
WantedBy: "multi-user.target"
}
})
triggers_reload true
action :create
end
service "strfry-submission" do
action [:enable, :start]
end
@@ -0,0 +1,32 @@
#
# Cookbook Name:: kosmos_strfry_submission
# Recipe:: firewall
#
# Only the openresty proxy nodes may reach the relay directly. The relay trusts
# the client IP from the `X-Real-IP` header (relay.realIpHeader = "x-real-ip"),
# so any other virgo member with direct access to :7778 could forge that header
# and defeat the per-IP token bucket in the write policy. (The per-author bucket
# is keyed on event.pubkey and is unaffected.) Everything else must go through
# nginx.
#
include_recipe "kosmos-base::firewall"
relay_port = node["strfry_submission"]["port"]
proxy_hosts = search(:node, "role:openresty_proxy").map do |n|
n["knife_zero"] && n["knife_zero"]["host"]
end.compact.uniq.sort
if proxy_hosts.empty?
Chef::Log.warn("No node found with 'openresty_proxy' role; not opening port #{relay_port}")
else
proxy_hosts.each do |host|
firewall_rule "strfry-submission-#{host}" do
port relay_port
source host
protocol :tcp
command :allow
end
end
end
@@ -0,0 +1,38 @@
#
# Cookbook Name:: kosmos_strfry_submission
# Recipe:: nginx
#
# Reverse proxy for the public submission relay. Runs on the openresty proxy
# nodes (role[openresty_proxy]) and uses its own `_strfry_submission` upstream,
# so the submission relay is never mixed into the production `_strfry` pool.
#
# NOTE: submit.nostr.kosmos.org needs a manual A/AAAA record at Gandi that
# points at the proxy node(s) before the Let's Encrypt certificate can be
# issued. Chef does not manage DNS here.
#
domain = node["strfry_submission"]["domain"]
upstream_hosts = []
search(:node, "role:strfry_submission").each do |node|
upstream_hosts << node["knife_zero"]["host"]
end
if upstream_hosts.empty?
Chef::Log.warn("No node found with 'strfry_submission' role. Not configuring nginx site.")
return
end
tls_cert_for domain do
auth "gandi_dns"
action :create
end
openresty_site domain do
template "nginx_conf_strfry_submission.erb"
variables domain: domain,
upstream_port: node["strfry_submission"]["port"],
upstream_hosts: upstream_hosts,
ssl_cert: "/etc/letsencrypt/live/#{domain}/fullchain.pem",
ssl_key: "/etc/letsencrypt/live/#{domain}/privkey.pem"
end
@@ -0,0 +1,30 @@
upstream _strfry_submission {
<% @upstream_hosts.each do |host| %>
server <%= host %>:<%= @upstream_port || "7778" %>;
<% end %>
}
server {
server_name <%= @domain %>;
listen <%= "#{node['openresty']['listen_ip']}:" if node['openresty']['listen_ip'] %>443 ssl http2;
listen <%= "[#{node['openresty']['listen_ipv6']}]" %>:443 ssl http2;
access_log "/var/log/nginx/<%= @domain %>.access.log";
error_log "/var/log/nginx/<%= @domain %>.error.log";
ssl_certificate <%= @ssl_cert %>;
ssl_certificate_key <%= @ssl_key %>;
location /metrics {
return 404;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://_strfry_submission;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}