Skip to content

Commit 58695fe

Browse files
TheSentinel454Elrondclaude
authored
acceptance: read the probe key from a file; dial a connect address (#9773)
## Summary Follows #9771 (merged), which added the acceptance CronJob. Some NIP-FI issuers bind each key to one identity. A deployment that mints a new identity for each run then needs a new key for each run, written next to the assertion before the probe starts. The probe could only take its key from an argument or the environment, which a Kubernetes init container can't set. - **Probe:** `--private-key-file` (`BUZZ_PRIVATE_KEY_FILE`) reads the key (hex or nsec) from a file. It conflicts with `--private-key`. - **Shared read:** the key file and the assertion file use one bounded reader, `acceptance::read_secret_file`. It needs a regular, non-empty file under a size cap. - **Report:** the JSON report now includes `pubkey`, the hex key the probe ran as. An operator can match a run to the identity that was minted for it. - **Chart:** `acceptance.privateKeyFile` is passed as `--private-key-file`, not as an env var, so a probe that predates the flag fails instead of running with a new key. Exactly one of it and `acceptance.privateKeySecret.name` must be set; rendering fails with neither or both. - **A new key per run needs an agent assertion** (`nip-fi-agent+jwt` whose `nostr_owner_pubkey` is a relay member). A members-only relay refuses a new person key, and an open relay joins it to every auto-join channel. The README and `values.yaml` say so. - **Connect address:** `--connect-addr` (`BUZZ_ACCEPTANCE_CONNECT_ADDR`) dials a plaintext `host:port` for every WebSocket connection and for the NIP-11 fetch, for a probe that can't reach the relay's public host. The relay URL still sets the `Host` header, the NIP-42 relay tag and the NIP-11 `Host`, so the relay serves the same community. On that path, NIP-11 uses no proxy, follows no redirect and requires a 2xx answer. The chart passes `acceptance.connectAddr` as `--connect-addr`, not as an env var, so a probe that predates the flag fails the run instead of checking the public URL. The default is empty. `NostrWsConnection::connect_to_addr` now takes upgrade headers, so the NIP-FI assertion goes on this path too; loadgen passes none. (Folded in from #9830.) - **Chart version 0.1.13** with an `artifacthub.io/changes` entry. It publishes the acceptance CronJob from #9771, the key file and the connect address. - **Follow-up test from the #9767 review:** the fake relay gets an `accept_mismatched` setting, and `access_control_fails_when_the_relay_accepts_another_keys_write` expects `access_control` to fail at `pubkey_mismatch_rejected`. A `pubkey_mismatch_rejected` stage that always returns Ok now fails a test. ## What got simpler The assertion reader's inline bounded read is now the shared `read_secret_file`, so both secret files follow one rule instead of two copies of it. The probe's four connection call sites now share one `open` helper, and `connect_to_addr` reuses the same `upgrade_request` path as `connect_with_headers`. ## Testing - `e2e_acceptance`: `the_binary_authenticates_with_the_key_from_its_key_file` passes against a live relay with the key path in `BUZZ_PRIVATE_KEY_FILE` (the same clap argument as `--private-key-file`). It checks that the report's `pubkey` is the file's key, so a probe that ignored the file and made a new key fails. - `e2e_acceptance`: `the_binary_exits_2_when_it_cannot_run` covers a bad key, a bad key file, a missing key file, and both key flags at once. Each exits 2 with no report and no metrics. - Existing unit tests cover the shared reader through the assertion file (missing, empty, oversized, directory, FIFO). - `connect_addr_dials_the_address_and_keeps_the_relay_url`: the probe runs at an unresolvable `wss://relay.invalid` with the fake relay's address. Every upgrade has `Host: relay.invalid` and the assertion's bearer header, and every AUTH relay tag is `wss://relay.invalid`. - `nip11_goes_to_the_connect_addr_with_the_relay_host` (`GET /relay?route=probe`, `Host: relay.invalid:8443`) and `nip11_at_the_connect_addr_does_not_follow_a_redirect` (a 301 fails `nip11` with no version). - `caller_user_agent_replaces_default` now covers `connect_to_addr` too. - `tests/acceptance_test.yaml`: `acceptance.connectAddr` renders `--connect-addr <addr>` in the args, and the default args do not have it. The key file renders as `--private-key-file <path>` in the args, with neither `BUZZ_PRIVATE_KEY_FILE` nor `BUZZ_PRIVATE_KEY`, a Secret plus a file fails, and no key source fails. 🤖 Generated with [Claude Code](https://claude.com/claude-code) buzz-review-completed --------- Signed-off-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Signed-off-by: Luke Tornquist <tornquist@squareup.com> Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7080811 commit 58695fe

14 files changed

Lines changed: 501 additions & 95 deletions

File tree

‎crates/buzz-test-client/src/acceptance/fake_relay.rs‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use nostr::{Event, PublicKey};
1111
use serde_json::{json, Value};
1212
use tokio::net::TcpListener;
1313
use tokio::sync::mpsc;
14+
use tokio_tungstenite::tungstenite::handshake::server::{Request, Response};
15+
use tokio_tungstenite::tungstenite::http::HeaderMap;
1416
use tokio_tungstenite::tungstenite::Message;
1517

1618
use super::{AUTH_REQUIRED, PUBKEY_MISMATCH};
@@ -33,6 +35,8 @@ pub struct Script {
3335
pub close_after: HashSet<u16>,
3436
/// The answer to a write without AUTH.
3537
pub unauthenticated: Unauthenticated,
38+
/// Accept a write signed by a key other than the authenticated one.
39+
pub accept_mismatched: bool,
3640
/// Serve events of these kinds, stored and live, with altered content
3741
/// under their original id and signature.
3842
pub corrupt: HashSet<u16>,
@@ -45,6 +49,7 @@ impl Default for Script {
4549
no_ack: HashSet::new(),
4650
close_after: HashSet::new(),
4751
unauthenticated: Unauthenticated::Reason(format!("{AUTH_REQUIRED} not authenticated")),
52+
accept_mismatched: false,
4853
corrupt: HashSet::new(),
4954
}
5055
}
@@ -69,6 +74,10 @@ struct Shared {
6974
events: Mutex<Vec<Event>>,
7075
/// Live subscriptions: subscription id and the connection to send to.
7176
live: Mutex<Vec<(String, Outbox)>>,
77+
/// The headers of every WebSocket upgrade, in order.
78+
upgrades: Mutex<Vec<HeaderMap>>,
79+
/// The `relay` tag of every AUTH event, in order.
80+
auth_relays: Mutex<Vec<String>>,
7281
}
7382

7483
/// A running scripted relay.
@@ -83,6 +92,16 @@ impl FakeRelay {
8392
pub fn events(&self) -> Vec<Event> {
8493
self.shared.events.lock().unwrap().clone()
8594
}
95+
96+
/// The headers of every WebSocket upgrade so far, in order.
97+
pub fn upgrades(&self) -> Vec<HeaderMap> {
98+
self.shared.upgrades.lock().unwrap().clone()
99+
}
100+
101+
/// The `relay` tag of every AUTH event so far, in order.
102+
pub fn auth_relays(&self) -> Vec<String> {
103+
self.shared.auth_relays.lock().unwrap().clone()
104+
}
86105
}
87106

88107
/// Starts a relay that follows `script` on a free local port.
@@ -93,6 +112,8 @@ pub async fn start(script: Script) -> FakeRelay {
93112
script,
94113
events: Mutex::new(Vec::new()),
95114
live: Mutex::new(Vec::new()),
115+
upgrades: Mutex::new(Vec::new()),
116+
auth_relays: Mutex::new(Vec::new()),
96117
});
97118
let accept = shared.clone();
98119
tokio::spawn(async move {
@@ -103,9 +124,17 @@ pub async fn start(script: Script) -> FakeRelay {
103124
FakeRelay { url, shared }
104125
}
105126

127+
// The handshake callback's `Result<Response, ErrorResponse>` is fixed by tungstenite.
128+
#[allow(clippy::result_large_err)]
106129
async fn serve(tcp: tokio::net::TcpStream, shared: Arc<Shared>) {
107130
// A plain HTTP request (the NIP-11 fetch) fails the upgrade; that is fine.
108-
let Ok(ws) = tokio_tungstenite::accept_async(tcp).await else {
131+
let upgrade =
132+
tokio_tungstenite::accept_hdr_async(tcp, |request: &Request, response: Response| {
133+
let headers = request.headers().clone();
134+
shared.upgrades.lock().unwrap().push(headers);
135+
Ok(response)
136+
});
137+
let Ok(ws) = upgrade.await else {
109138
return;
110139
};
111140
let (mut sink, mut stream) = ws.split();
@@ -131,6 +160,9 @@ async fn serve(tcp: tokio::net::TcpStream, shared: Arc<Shared>) {
131160
match msg.first().and_then(Value::as_str) {
132161
Some("AUTH") => {
133162
let event: Event = serde_json::from_value(msg[1].clone()).unwrap();
163+
let relay = event.tags.iter().find(|t| t.kind().to_string() == "relay");
164+
let relay = relay.and_then(|t| t.content()).unwrap_or_default();
165+
shared.auth_relays.lock().unwrap().push(relay.to_owned());
134166
authed = Some(event.pubkey);
135167
send(&out, json!(["OK", event.id.to_hex(), true, ""]));
136168
}
@@ -163,7 +195,7 @@ async fn serve(tcp: tokio::net::TcpStream, shared: Arc<Shared>) {
163195
}
164196
continue;
165197
};
166-
if event.pubkey != pubkey {
198+
if event.pubkey != pubkey && !shared.script.accept_mismatched {
167199
send(&out, json!(["OK", id, false, PUBKEY_MISMATCH]));
168200
continue;
169201
}

‎crates/buzz-test-client/src/acceptance/metrics.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ mod tests {
121121
fn report() -> Report {
122122
Report {
123123
relay_url: "wss://relay.example".into(),
124+
pubkey: "00".repeat(32),
124125
relay_version: Some("1.2.3".into()),
125126
passed: false,
126127
seconds: 1.5,

0 commit comments

Comments
 (0)