cortexkit-bus-client 0.1.4

Seedless ck-bus credentials and local NATS connections for Rust modules
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
use std::sync::{
    atomic::{AtomicUsize, Ordering},
    Arc, Mutex,
};
use std::time::Duration;

use async_trait::async_trait;
use base64::{
    engine::general_purpose::{STANDARD, URL_SAFE_NO_PAD},
    Engine,
};
use cortexkit_bus_naming::{AccountNames, MODULE_BUCKETS};
use cortexkit_bus_testkit::{
    trust::{self, TrustChain},
    BusOptions, BusServer, PreloadedAccount,
};
use nkeys::KeyPair;
use serde_json::{json, Value};

use crate::{
    credentials::{CredentialManager, CredentialTransport},
    BucketItem, BucketWatch, BusClient, BusClientOptions, Error, Result,
};

fn jwt(signer: &KeyPair, subject: &str, name: &str, nats: Value) -> String {
    let now = trust::unix_now();
    let claims = json!({"jti":format!("{subject}-{now}"), "iat":now,
        "iss":signer.public_key(), "sub":subject, "name":name, "nats":nats});
    let input = format!(
        "{}.{}",
        URL_SAFE_NO_PAD.encode(br#"{"alg":"ed25519-nkey","typ":"JWT"}"#),
        URL_SAFE_NO_PAD.encode(serde_json::to_vec(&claims).unwrap())
    );
    format!(
        "{input}.{}",
        URL_SAFE_NO_PAD.encode(signer.sign(input.as_bytes()).unwrap())
    )
}

pub(super) struct Issuer {
    url: String,
    account: String,
    trust: TrustChain,
    module: &'static str,
    keys: Mutex<Vec<KeyPair>>,
    /// Keys whose nonce signing is refused as revoked, as after a ck-bus restart.
    pub(super) revoked: Mutex<Vec<String>>,
    signing: tokio::sync::Mutex<()>,
    sign_calls: AtomicUsize,
}

impl Issuer {
    fn credential(&self, key: &KeyPair) -> Value {
        let names = AccountNames::derive("box_goldenfixture").unwrap();
        let public = key.public_key();
        // These are the module KV grant subjects emitted by ck-bus, with only
        // the credential-scoped inbox substituted. They allow unnamed consumer
        // creation but neither named consumer creation nor consumer deletion.
        let mut publish = Vec::new();
        for owner in ["aft", "cerebellum"] {
            if self.module != "occipital" && self.module != owner {
                continue;
            }
            let bucket = names.module_bucket_name(owner, "views").unwrap();
            let stream = format!("KV_{bucket}");
            publish.extend([
                format!("$JS.API.CONSUMER.CREATE.{stream}"),
                format!("$JS.API.DIRECT.GET.{stream}.>"),
                format!("$JS.API.STREAM.INFO.{stream}"),
                format!("$JS.API.STREAM.MSG.GET.{stream}"),
                format!("$JS.FC.{stream}.>"),
            ]);
            if self.module == owner {
                publish.push(format!("$KV.{bucket}.>"));
            }
        }
        // A module may read its own EVENT durable.
        let event = &names.streams().event;
        let durable = AccountNames::module_consumer_name(self.module).unwrap();
        publish.extend([
            format!("$JS.API.CONSUMER.INFO.{event}.{durable}"),
            format!("$JS.API.CONSUMER.MSG.NEXT.{event}.{durable}"),
            format!("$JS.ACK.{event}.{durable}.>"),
        ]);
        let now = trust::unix_now();
        let signer = &self.trust.signer.root(&trust::box_root_id()).pair;
        let jwt = jwt(
            signer,
            &public,
            "bucket-fixture-user",
            json!({"type":"user", "version":2, "issuer_account":self.account,
                "pub":{"allow": publish}, "sub":{"allow":[format!("_INBOX.{public}.>")]},
                "subs":-1, "data":-1, "payload":-1}),
        );
        // The credential envelope is the same shape returned by the bus. Its
        // test-only signer holds throwaway keys rather than a module seed.
        json!({"jwt":jwt, "exp":now + 900,
            "acct":names.account(), "credential_public":public,
            "server_url":self.url, "inbox_prefix":format!("_INBOX.{public}"),
            "spawn_generation":1, "credential_epoch":0, "user_jwt_id":"fixture"})
    }
}

#[async_trait]
impl CredentialTransport for Issuer {
    async fn call(&self, method: &str, params: Value) -> Result<Value> {
        let _signing = if method == "ckbus.nonce_sign" {
            self.sign_calls.fetch_add(1, Ordering::SeqCst);
            Some(self.signing.lock().await)
        } else {
            None
        };
        let mut keys = self.keys.lock().unwrap();
        match method {
            "ckbus.credential" => {
                keys.push(KeyPair::new_user());
                Ok(self.credential(keys.last().unwrap()))
            }
            "ckbus.credential_renew" => Ok(self.credential(keys.last().unwrap())),
            "ckbus.nonce_sign" => {
                let public = params["credential_public"].as_str().unwrap();
                if self.revoked.lock().unwrap().iter().any(|key| key == public) {
                    return Err(Error::Refused("ckbus_credential_revoked".into()));
                }
                let key = keys.iter().find(|key| key.public_key() == public).unwrap();
                let nonce = STANDARD
                    .decode(params["nonce_b64"].as_str().unwrap())
                    .unwrap();
                Ok(json!({"signature_b64":STANDARD.encode(key.sign(&nonce).unwrap())}))
            }
            _ => panic!("unexpected method {method}"),
        }
    }
}

pub(super) struct Rig {
    pub(super) server: BusServer,
    account: String,
    pub(super) admin: async_nats::Client,
}

impl Rig {
    pub(super) async fn start() -> Option<Self> {
        let Some(binary) = std::env::var_os("CK_NATS_SERVER_BIN") else {
            assert_ne!(
                std::env::var("CK_REQUIRE_NATS_SERVER").as_deref(),
                Ok("1"),
                "NATS binary required"
            );
            eprintln!("nats-server-absent: bucket live test skipped");
            return None;
        };
        let trust = TrustChain::generate();
        let account = KeyPair::new_account().public_key();
        let server = BusServer::start(BusOptions {
            binary: Some(binary.into()),
            preload: vec![PreloadedAccount {
                public_key: account.clone(),
                signing_keys: vec![trust.box_root_public()],
            }],
            trust: Some(trust),
            ..Default::default()
        })
        .await
        .unwrap();
        assert!(
            server.version.contains("2.15.0"),
            "pinned broker: {}",
            server.version
        );
        // The testkit's preloaded accounts have no JetStream budget. Update
        // this throwaway account through the same system claim API as bootstrap.
        let system_key = KeyPair::new_user();
        let system_jwt = server.trust.user_jwt(
            &trust::system_root_id(),
            &server.system_account,
            &system_key,
            trust::unix_now() - 60,
        );
        let system = async_nats::ConnectOptions::with_jwt(system_jwt, move |nonce| {
            let signature = system_key.sign(&nonce);
            async move { signature.map_err(async_nats::AuthError::new) }
        })
        .connect(&server.url)
        .await
        .unwrap();
        let account_jwt = jwt(
            &server.trust.signer.root(&trust::signer_root_id()).pair,
            &account,
            "bucket-fixture-account",
            json!({"type":"account", "version":2,
                "limits":{"subs":-1,"data":-1,"payload":-1,"imports":-1,"exports":-1,
                    "wildcards":true,"conn":-1,"leaf":-1,"mem_storage":104857600,
                    "disk_storage":104857600,"streams":-1,"consumer":-1},
                "signing_keys":[server.trust.box_root_public()]}),
        );
        let update = system
            .request("$SYS.REQ.CLAIMS.UPDATE", account_jwt.into())
            .await
            .unwrap();
        let update: Value = serde_json::from_slice(&update.payload).unwrap();
        assert!(
            update.get("error").is_none(),
            "account claim update: {update}"
        );
        let key = KeyPair::new_user();
        let jwt = server.trust.user_jwt(
            &trust::box_root_id(),
            &account,
            &key,
            trust::unix_now() - 60,
        );
        let admin = async_nats::ConnectOptions::with_jwt(jwt, move |nonce| {
            let signature = key.sign(&nonce);
            async move { signature.map_err(async_nats::AuthError::new) }
        })
        .connect(&server.url)
        .await
        .unwrap();
        let context = async_nats::jetstream::new(admin.clone());
        let names = AccountNames::derive("box_goldenfixture").unwrap();
        for declaration in MODULE_BUCKETS.iter().filter(|entry| entry.name == "views") {
            // Match bus provisioning, including the shared cap of 16 watchers.
            let bucket = names
                .module_bucket_name(declaration.owner, declaration.name)
                .unwrap();
            context
                .create_key_value(async_nats::jetstream::kv::Config {
                    bucket: bucket.clone(),
                    history: i64::from(declaration.history),
                    max_value_size: i32::try_from(declaration.max_value_bytes).unwrap(),
                    max_bytes: i64::try_from(declaration.max_bytes).unwrap(),
                    max_age: Duration::from_secs(declaration.ttl.unwrap_or(0)),
                    ..Default::default()
                })
                .await
                .unwrap();
            let mut stream = context.get_stream(format!("KV_{bucket}")).await.unwrap();
            let mut config = stream.info().await.unwrap().config.clone();
            config.max_consumers = 16;
            context.update_stream(config).await.unwrap();
        }
        Some(Self {
            server,
            account,
            admin,
        })
    }

    async fn client(&self, module: &'static str) -> (BusClient, Arc<CredentialManager>) {
        let (client, manager, _) = self.gated_client(module).await;
        (client, manager)
    }

    async fn gated_client(
        &self,
        module: &'static str,
    ) -> (BusClient, Arc<CredentialManager>, Arc<Issuer>) {
        self.client_via(module, &self.server.url).await
    }

    /// Connects a client whose credentials point at `url`, which may be a
    /// proxy in front of this rig's server.
    pub(super) async fn client_via(
        &self,
        module: &'static str,
        url: &str,
    ) -> (BusClient, Arc<CredentialManager>, Arc<Issuer>) {
        let issuer = Arc::new(Issuer {
            url: url.into(),
            account: self.account.clone(),
            trust: self.server.trust.clone(),
            module,
            keys: Mutex::new(vec![]),
            revoked: Mutex::new(vec![]),
            signing: tokio::sync::Mutex::new(()),
            sign_calls: AtomicUsize::new(0),
        });
        let manager = CredentialManager::start(issuer.clone()).await.unwrap();
        let client =
            BusClient::connect_manager(manager.clone(), BusClientOptions::new(module).unwrap())
                .await
                .unwrap();
        (client, manager, issuer)
    }
}

async fn next(watch: &mut BucketWatch) -> BucketItem {
    tokio::time::timeout(Duration::from_secs(10), watch.next())
        .await
        .expect("watch timed out")
        .unwrap()
}

#[tokio::test]
async fn bucket_live_snapshot_changes_reissue_and_terminal_renewal() {
    let Some(rig) = Rig::start().await else {
        return;
    };
    let (owner, _) = rig.client("aft").await;
    let (reader, manager, issuer) = rig.gated_client("occipital").await;
    let bucket = owner.bucket("aft", "views").unwrap();
    let read = reader.bucket("aft", "views").unwrap();
    assert!(read.get("missing").await.unwrap().is_none());
    let first = bucket.put("one", "initial").await.unwrap();
    let entry = read.get("one").await.unwrap().unwrap();
    assert_eq!(entry.revision, first);
    assert_eq!(entry.value, "initial");
    let second = bucket.put("two", "second").await.unwrap();
    let mut watch = read.watch().await.unwrap();
    assert_eq!(
        next(&mut watch).await,
        BucketItem::Put {
            key: "one".into(),
            revision: first,
            value: "initial".into()
        }
    );
    assert_eq!(
        next(&mut watch).await,
        BucketItem::Put {
            key: "two".into(),
            revision: second,
            value: "second".into()
        }
    );
    assert_eq!(next(&mut watch).await, BucketItem::CaughtUp);
    let live = bucket.put("one", "live").await.unwrap();
    assert_eq!(
        next(&mut watch).await,
        BucketItem::Put {
            key: "one".into(),
            revision: live,
            value: "live".into()
        }
    );
    let deleted = bucket.delete("two").await.unwrap();
    assert_eq!(
        next(&mut watch).await,
        BucketItem::Delete {
            key: "two".into(),
            revision: deleted
        }
    );
    assert!(read.get("two").await.unwrap().is_none());
    assert!(matches!(
        read.put("forbidden", "value").await,
        Err(Error::BucketNotOwned { .. })
    ));
    let cap = vec![7; 262_144];
    let at_limit = bucket.put("cap", cap.clone()).await.unwrap();
    assert_eq!(read.get("cap").await.unwrap().unwrap().value.as_ref(), cap);
    assert_eq!(
        next(&mut watch).await,
        BucketItem::Put {
            key: "cap".into(),
            revision: at_limit,
            value: cap.into()
        }
    );

    // Close the actual reader socket before writing. History 1 deliberately
    // evicts the intermediate value; re-issue must resume retained state.
    let old = reader.connection();
    old.close(Duration::from_secs(1)).await.unwrap();
    bucket.put("one", "intermediate").await.unwrap();
    let final_revision = bucket.put("one", "final").await.unwrap();
    let delete_revision = bucket.delete("cap").await.unwrap();
    let mut changes = reader.connection_changes();
    manager
        .reissue(old.inbox_prefix().strip_prefix("_INBOX.").unwrap())
        .await
        .unwrap();
    // Closing the current connection also makes the client replace it under
    // the same key, so wait for the connection carrying the re-issued key.
    tokio::time::timeout(Duration::from_secs(10), async {
        while reader.connection().inbox_prefix() == old.inbox_prefix() {
            changes.changed().await.unwrap();
        }
    })
    .await
    .unwrap();
    changes.borrow_and_update();
    assert_ne!(old.inbox_prefix(), reader.connection().inbox_prefix());
    assert_eq!(
        next(&mut watch).await,
        BucketItem::Put {
            key: "one".into(),
            revision: final_revision,
            value: "final".into()
        }
    );
    assert_eq!(
        next(&mut watch).await,
        BucketItem::Delete {
            key: "cap".into(),
            revision: delete_revision
        }
    );
    let sentinel = bucket.put("sentinel", "after reconnect").await.unwrap();
    assert_eq!(
        next(&mut watch).await,
        BucketItem::Put {
            key: "sentinel".into(),
            revision: sentinel,
            value: "after reconnect".into()
        }
    );
    // Hold nonce signing so the existing connection cannot reconnect with its
    // current credential until both overwrites and the delete are stored.
    let paused = issuer.signing.lock().await;
    let sign_calls = issuer.sign_calls.load(Ordering::SeqCst);
    reader
        .connection()
        .client()
        .force_reconnect()
        .await
        .unwrap();
    tokio::time::timeout(Duration::from_secs(5), async {
        while issuer.sign_calls.load(Ordering::SeqCst) == sign_calls {
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    })
    .await
    .expect("reconnect must reach nonce signing");
    let pending = tokio::spawn(async move {
        let item = next(&mut watch).await;
        (watch, item)
    });
    bucket.put("one", "offline intermediate").await.unwrap();
    let raw_revision = bucket.put("one", "offline final").await.unwrap();
    let raw_delete = bucket.delete("sentinel").await.unwrap();
    drop(paused);
    let (mut watch, item) = pending.await.unwrap();
    assert_eq!(
        item,
        BucketItem::Put {
            key: "one".into(),
            revision: raw_revision,
            value: "offline final".into()
        }
    );
    assert_eq!(
        next(&mut watch).await,
        BucketItem::Delete {
            key: "sentinel".into(),
            revision: raw_delete
        }
    );
    assert!(
        !changes.has_changed().unwrap(),
        "same-key reconnect must not replace the connection"
    );
    // With no further writes, a pending read must not replay old revisions.
    assert!(
        tokio::time::timeout(Duration::from_millis(200), watch.next())
            .await
            .is_err()
    );
    reader.task.abort();
    let error = tokio::time::timeout(Duration::from_secs(5), watch.next())
        .await
        .unwrap()
        .unwrap_err();
    assert!(
        matches!(error, Error::Transport(ref text) if text == "credential maintenance stopped")
    );
}

#[tokio::test]
async fn bucket_live_empty_snapshot_and_watch_capacity() {
    let Some(rig) = Rig::start().await else {
        return;
    };
    let (reader, _) = rig.client("occipital").await;
    let bucket = reader.bucket("cerebellum", "views").unwrap();
    let mut watches = Vec::new();
    for _ in 0..16 {
        let mut watch = bucket.watch().await.unwrap();
        assert_eq!(next(&mut watch).await, BucketItem::CaughtUp);
        watches.push(watch);
    }
    let (owner, _) = rig.client("cerebellum").await;
    let revision = owner
        .bucket("cerebellum", "views")
        .unwrap()
        .put("owner", "value")
        .await
        .unwrap();
    assert_eq!(
        next(&mut watches[0]).await,
        BucketItem::Put {
            key: "owner".into(),
            revision,
            value: "value".into()
        }
    );
    let stream_name = "KV_CK_BOX_GOLDENFIXTURE_CEREBELLUM_VIEWS";
    let response = rig.admin.request(format!("$JS.API.CONSUMER.CREATE.{stream_name}"),
        serde_json::to_vec(&json!({"stream_name":stream_name, "config":{"deliver_subject":"_INBOX.probe", "ack_policy":"none"}})).unwrap().into()).await.unwrap();
    let body: Value = serde_json::from_slice(&response.payload).unwrap();
    eprintln!("NATS 2.15.0 full watch response: {}", body["error"]);
    assert_eq!(body["error"]["code"], 400);
    assert_eq!(body["error"]["err_code"], 10026);
    assert_eq!(
        body["error"]["description"],
        "maximum consumers limit reached"
    );
    assert!(matches!(
        tokio::time::timeout(Duration::from_secs(5), bucket.watch())
            .await
            .expect("a new watch must fail without capacity backoff"),
        Err(Error::WatchCapacity { .. })
    ));
    drop(watches);
    // No consumer-delete permission exists. Prove that unsubscribing and the
    // server's inactivity timer free all slots without an administrative delete.
    let context = async_nats::jetstream::new(rig.admin.clone());
    tokio::time::timeout(Duration::from_secs(45), async {
        loop {
            let stream = context.get_stream(stream_name).await.unwrap();
            if stream.cached_info().state.consumer_count == 0 {
                break;
            }
            tokio::time::sleep(Duration::from_millis(200)).await;
        }
    })
    .await
    .expect("inactive watches must release slots");
    bucket.watch().await.unwrap();
}

#[tokio::test]
async fn bucket_live_full_reissue_waits_for_own_slot() {
    let Some(rig) = Rig::start().await else {
        return;
    };
    let (owner, _) = rig.client("aft").await;
    let (reader, manager) = rig.client("occipital").await;
    let bucket = owner.bucket("aft", "views").unwrap();
    let read = reader.bucket("aft", "views").unwrap();
    let mut watches = Vec::new();
    for _ in 0..16 {
        let mut watch = read.watch().await.unwrap();
        assert_eq!(next(&mut watch).await, BucketItem::CaughtUp);
        watches.push(watch);
    }
    let mut watch = watches.remove(0);
    let before = bucket.put("key", "before reissue").await.unwrap();
    assert_eq!(
        next(&mut watch).await,
        BucketItem::Put {
            key: "key".into(),
            revision: before,
            value: "before reissue".into()
        }
    );
    let old = reader.connection();
    let mut changes = reader.connection_changes();
    manager
        .reissue(old.inbox_prefix().strip_prefix("_INBOX.").unwrap())
        .await
        .unwrap();
    tokio::time::timeout(Duration::from_secs(10), changes.changed())
        .await
        .unwrap()
        .unwrap();
    let stream = async_nats::jetstream::new(rig.admin.clone())
        .get_stream("KV_CK_BOX_GOLDENFIXTURE_AFT_VIEWS")
        .await
        .unwrap();
    assert_eq!(
        stream.cached_info().state.consumer_count,
        16,
        "old consumers still occupy every slot"
    );
    bucket.put("key", "intermediate").await.unwrap();
    let after = bucket.put("key", "after reissue").await.unwrap();
    // Opening a replacement must wait for the old consumer's inactivity timer,
    // not expose a transient capacity error to an established reader.
    let item = tokio::time::timeout(Duration::from_secs(65), watch.next())
        .await
        .expect("capacity retry is bounded")
        .unwrap();
    assert_eq!(
        item,
        BucketItem::Put {
            key: "key".into(),
            revision: after,
            value: "after reissue".into()
        }
    );
    let sentinel = bucket.delete("key").await.unwrap();
    assert_eq!(
        next(&mut watch).await,
        BucketItem::Delete {
            key: "key".into(),
            revision: sentinel
        }
    );
    drop(watches);
}