net-cli 0.35.0

Unified `net-mesh` command-line tool for the Net mesh
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
//! OA-1 CLI flow — `net org keygen` → `issue-cert` →
//! `issue-floors` → `net node adopt` (ORG_CAPABILITY_AUTH_PLAN.md
//! §1.1–1.5).
//!
//! Drives the real binary end-to-end against a tempdir: key
//! material lands 0600, adoption provisions the three authority
//! files, one-node-one-owner refuses a second org loudly, and a
//! floors bundle merges monotonically during adoption.

use assert_cmd::prelude::*;
use std::path::Path;
use std::process::Command;

fn keygen(dir: &Path, name: &str) -> std::path::PathBuf {
    let key = dir.join(name);
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "keygen", "--out"])
        .arg(&key)
        .assert()
        .code(0);
    key
}

fn issue_cert(
    dir: &Path,
    key: &Path,
    member_hex: &str,
    generation: u32,
    name: &str,
) -> std::path::PathBuf {
    let cert = dir.join(name);
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "issue-cert", "--org-key"])
        .arg(key)
        .args(["--member", member_hex])
        .args(["--generation", &generation.to_string()])
        .args(["--out"])
        .arg(&cert)
        .assert()
        .code(0);
    cert
}

/// A stable fake node entity id (any 32 bytes decode as an
/// EntityId; adoption only needs the public half).
const MEMBER_HEX: &str = "2424242424242424242424242424242424242424242424242424242424242424";

#[test]
fn keygen_writes_owner_only_key_and_refuses_overwrite() {
    let dir = tempfile::tempdir().unwrap();
    let key = keygen(dir.path(), "org.toml");

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mode = std::fs::metadata(&key).unwrap().permissions().mode();
        assert_eq!(mode & 0o077, 0, "org key must be owner-only, got {mode:o}");
    }

    // Refuses to clobber without --force.
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "keygen", "--out"])
        .arg(&key)
        .assert()
        .code(2);
}

#[test]
fn full_adopt_flow_provisions_authority_files() {
    let dir = tempfile::tempdir().unwrap();
    let key = keygen(dir.path(), "org.toml");
    let cert = issue_cert(dir.path(), &key, MEMBER_HEX, 0, "node.cert.json");

    let authority = dir.path().join("authority");
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["node", "adopt", "--cert"])
        .arg(&cert)
        .args(["--entity", MEMBER_HEX])
        .args(["--authority-dir"])
        .arg(&authority)
        .assert()
        .code(0);

    for file in [
        "owner-membership.json",
        "owner-audience.key",
        "revocation-state.json",
    ] {
        assert!(
            authority.join(file).exists(),
            "{file} must exist after adopt"
        );
    }

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mode = std::fs::metadata(authority.join("owner-audience.key"))
            .unwrap()
            .permissions()
            .mode();
        assert_eq!(
            mode & 0o077,
            0,
            "owner-audience.key must be owner-only, got {mode:o}"
        );
    }
}

#[test]
fn adopt_refuses_wrong_entity_and_second_owner() {
    let dir = tempfile::tempdir().unwrap();
    let key = keygen(dir.path(), "org.toml");
    let cert = issue_cert(dir.path(), &key, MEMBER_HEX, 0, "node.cert.json");
    let authority = dir.path().join("authority");

    // Cert names MEMBER_HEX; adopting as a different entity is a
    // loud refusal, and nothing is installed.
    let other = "9999999999999999999999999999999999999999999999999999999999999999";
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["node", "adopt", "--cert"])
        .arg(&cert)
        .args(["--entity", other])
        .args(["--authority-dir"])
        .arg(&authority)
        .assert()
        .code(3);
    assert!(!authority.join("owner-membership.json").exists());

    // Proper adoption, then a SECOND org's cert for the same node:
    // one node one owner.
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["node", "adopt", "--cert"])
        .arg(&cert)
        .args(["--entity", MEMBER_HEX])
        .args(["--authority-dir"])
        .arg(&authority)
        .assert()
        .code(0);

    let key_b = keygen(dir.path(), "org-b.toml");
    let cert_b = issue_cert(dir.path(), &key_b, MEMBER_HEX, 0, "node.cert-b.json");
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["node", "adopt", "--cert"])
        .arg(&cert_b)
        .args(["--entity", MEMBER_HEX])
        .args(["--authority-dir"])
        .arg(&authority)
        .assert()
        .code(3);
}

#[test]
fn issue_floors_and_adopt_applies_bundle() {
    let dir = tempfile::tempdir().unwrap();
    let key = keygen(dir.path(), "org.toml");
    // Cert at generation 5 so it survives the floor below.
    let cert = issue_cert(dir.path(), &key, MEMBER_HEX, 5, "node.cert.json");

    let floors = dir.path().join("floors.json");
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "issue-floors", "--org-key"])
        .arg(&key)
        .args(["--floor", &format!("{MEMBER_HEX}=5")])
        .args(["--out"])
        .arg(&floors)
        .assert()
        .code(0);

    let authority = dir.path().join("authority");
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["node", "adopt", "--cert"])
        .arg(&cert)
        .args(["--entity", MEMBER_HEX])
        .args(["--authority-dir"])
        .arg(&authority)
        .args(["--floors"])
        .arg(&floors)
        .assert()
        .code(0);

    // The persisted maxima carry the floor.
    let state = std::fs::read_to_string(authority.join("revocation-state.json")).unwrap();
    assert!(state.contains("\"floor\": 5"), "state: {state}");

    // A generation-0 cert (below the now-persisted floor 5) is
    // refused at re-adoption — the floor outlives the ceremony
    // that installed it.
    let low_cert = issue_cert(dir.path(), &key, MEMBER_HEX, 0, "node.cert-low.json");
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["node", "adopt", "--cert"])
        .arg(&low_cert)
        .args(["--entity", MEMBER_HEX])
        .args(["--authority-dir"])
        .arg(&authority)
        .assert()
        .code(3);
}

/// Review-8 §7 real-CLI red, reproduced as a witness: a certificate
/// the supplied floor bundle immediately revokes must never adopt —
/// nonzero exit, nothing provisioned.
#[test]
fn adopt_refuses_cert_below_supplied_floor() {
    let dir = tempfile::tempdir().unwrap();
    let key = keygen(dir.path(), "org.toml");
    let cert = issue_cert(dir.path(), &key, MEMBER_HEX, 3, "node.cert.json");

    let floors = dir.path().join("floors.json");
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "issue-floors", "--org-key"])
        .arg(&key)
        .args(["--floor", &format!("{MEMBER_HEX}=5")])
        .args(["--out"])
        .arg(&floors)
        .assert()
        .code(0);

    let authority = dir.path().join("authority");
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["node", "adopt", "--cert"])
        .arg(&cert)
        .args(["--entity", MEMBER_HEX])
        .args(["--authority-dir"])
        .arg(&authority)
        .args(["--floors"])
        .arg(&floors)
        .assert()
        .code(3);
    // Nothing was provisioned by the refused ceremony.
    for file in [
        "owner-membership.json",
        "owner-audience.key",
        "revocation-state.json",
    ] {
        assert!(
            !authority.join(file).exists(),
            "{file} must not exist after a refused adoption"
        );
    }
}

/// Review-8 §6: a bundle signed by a foreign org is refused before
/// durable state changes — no foreign floors are ever persisted
/// through the owner-adoption ceremony.
#[test]
fn adopt_refuses_foreign_floor_bundle() {
    let dir = tempfile::tempdir().unwrap();
    let key_a = keygen(dir.path(), "org-a.toml");
    let key_b = keygen(dir.path(), "org-b.toml");
    let cert_a = issue_cert(dir.path(), &key_a, MEMBER_HEX, 0, "node.cert.json");

    // B signs a perfectly valid bundle for the same member.
    let floors_b = dir.path().join("floors-b.json");
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "issue-floors", "--org-key"])
        .arg(&key_b)
        .args(["--floor", &format!("{MEMBER_HEX}=5")])
        .args(["--out"])
        .arg(&floors_b)
        .assert()
        .code(0);

    let authority = dir.path().join("authority");
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["node", "adopt", "--cert"])
        .arg(&cert_a)
        .args(["--entity", MEMBER_HEX])
        .args(["--authority-dir"])
        .arg(&authority)
        .args(["--floors"])
        .arg(&floors_b)
        .assert()
        .code(3);
    assert!(
        !authority.join("revocation-state.json").exists(),
        "no B floor may be persisted"
    );
}

/// Review-8 §11: a skew above the token ceiling is rejected as
/// invalid arguments (exit 2) before anything is written.
#[test]
fn adopt_rejects_over_ceiling_skew_before_writing() {
    let dir = tempfile::tempdir().unwrap();
    let key = keygen(dir.path(), "org.toml");
    let cert = issue_cert(dir.path(), &key, MEMBER_HEX, 0, "node.cert.json");

    let authority = dir.path().join("authority");
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["node", "adopt", "--cert"])
        .arg(&cert)
        .args(["--entity", MEMBER_HEX])
        .args(["--authority-dir"])
        .arg(&authority)
        .args(["--skew-secs", "301"])
        .assert()
        .code(2);
    assert!(!authority.exists() || !authority.join("owner-membership.json").exists());
}

#[test]
fn issue_cert_rejects_overlong_ttl_and_bad_member() {
    let dir = tempfile::tempdir().unwrap();
    let key = keygen(dir.path(), "org.toml");

    // TTL past the 2-year ceiling: refused at issue.
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "issue-cert", "--org-key"])
        .arg(&key)
        .args(["--member", MEMBER_HEX])
        .args(["--ttl-secs", "999999999"])
        .args(["--out"])
        .arg(dir.path().join("never.json"))
        .assert()
        .code(2);

    // Malformed member hex.
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "issue-cert", "--org-key"])
        .arg(&key)
        .args(["--member", "zznothex"])
        .args(["--out"])
        .arg(dir.path().join("never.json"))
        .assert()
        .code(2);
}

// =========================================================================
// §2 — `--force` must never destroy the org root key.
//
// `run_issue_cert` / `run_issue_floors` used to call `refuse_existing`
// (which returns Ok on the first line when `--force` is set) and then
// `tokio::fs::write`, which truncates in place and writes THROUGH a leaf
// symlink. Pointing `--out` at `--org-key` with `--force` therefore replaced
// the org root seed with cert JSON: no node can be re-certified, no
// revocation floor can ever be issued again, and every outstanding
// membership cert stays valid until natural expiry with no way to revoke it.
//
// Each test below asserts the org key SURVIVED BYTE-FOR-BYTE, not merely
// that the command exited non-zero — an exit-code-only assertion passes for
// any of several unrelated reasons (clap's usage code is 2, and so is
// `ExitCodeKind::InvalidArgs`).
// =========================================================================

/// The org key file's exact bytes, for before/after comparison.
fn read_key_bytes(key: &Path) -> Vec<u8> {
    std::fs::read(key).expect("org key readable")
}

#[test]
fn issue_cert_force_refuses_to_alias_the_org_key() {
    let dir = tempfile::tempdir().unwrap();
    let key = keygen(dir.path(), "org.toml");
    let before = read_key_bytes(&key);

    let assert = Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "issue-cert", "--org-key"])
        .arg(&key)
        .args(["--member", MEMBER_HEX])
        .args(["--out"])
        .arg(&key) // <-- the org key itself
        .arg("--force")
        .assert()
        .code(2);

    let stderr = String::from_utf8_lossy(&assert.get_output().stderr).to_string();
    assert!(
        stderr.contains("--org-key") && stderr.contains("--out"),
        "the refusal must name the two aliased flags so the operator can fix \
         the invocation; got: {stderr}",
    );
    assert_eq!(
        read_key_bytes(&key),
        before,
        "the org root key must be byte-for-byte intact after a refused --force",
    );
}

#[test]
fn issue_floors_force_refuses_to_alias_the_org_key() {
    let dir = tempfile::tempdir().unwrap();
    let key = keygen(dir.path(), "org.toml");
    let before = read_key_bytes(&key);

    let assert = Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "issue-floors", "--org-key"])
        .arg(&key)
        .args(["--floor", &format!("{MEMBER_HEX}=3")])
        .args(["--out"])
        .arg(&key)
        .arg("--force")
        .assert()
        .code(2);

    let stderr = String::from_utf8_lossy(&assert.get_output().stderr).to_string();
    assert!(
        stderr.contains("--org-key") && stderr.contains("--out"),
        "the refusal must name the two aliased flags; got: {stderr}",
    );
    assert_eq!(
        read_key_bytes(&key),
        before,
        "the org root key must be byte-for-byte intact after a refused --force",
    );
}

/// The lexical alias guard only compares the two paths it was given. It
/// cannot see that a THIRD path holds key material — a backup copy of the org
/// key, a second checkout, a key restored beside the artifact it signs. With
/// `--force` that destination would be replaced.
///
/// `refuse_replacing_org_key` is the backstop: it refuses on the
/// destination's CONTENT, so the spelling is irrelevant. Exercised here with
/// a copy at a lexically unrelated path — which the alias guard provably
/// cannot catch, so a pass witnesses the content check specifically.
#[test]
fn issue_cert_force_refuses_a_destination_holding_key_material() {
    let dir = tempfile::tempdir().unwrap();
    let key = keygen(dir.path(), "org.toml");

    // A backup copy at an unrelated path — `refuse_aliased_paths` compares
    // `--org-key` against `--out` lexically and sees two different files.
    let backup = dir.path().join("org-key-backup.toml");
    std::fs::copy(&key, &backup).unwrap();
    let before = read_key_bytes(&backup);

    let assert = Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "issue-cert", "--org-key"])
        .arg(&key)
        .args(["--member", MEMBER_HEX])
        .args(["--out"])
        .arg(&backup)
        .arg("--force")
        .assert()
        .code(2);

    let stderr = String::from_utf8_lossy(&assert.get_output().stderr).to_string();
    assert!(
        stderr.contains("org root key material"),
        "the content backstop (not the lexical guard) must be what refuses \
         this destination; got: {stderr}",
    );
    assert!(
        !stderr.contains("seed_hex"),
        "the refusal must not echo the key file's contents: {stderr}",
    );
    assert_eq!(
        read_key_bytes(&backup),
        before,
        "key material at the destination must be byte-for-byte intact",
    );
}

/// Positive control: `--force` still does its job. Certificates are renewable
/// by design (~annual), so refusing `--force` outright the way the grant verbs
/// do would break a real workflow — the fix makes the replace SAFE (staged +
/// atomic rename), not unavailable.
#[test]
fn issue_cert_force_still_renews_an_existing_certificate() {
    let dir = tempfile::tempdir().unwrap();
    let key = keygen(dir.path(), "org.toml");
    let cert = issue_cert(dir.path(), &key, MEMBER_HEX, 0, "node.cert.json");
    let first = std::fs::read(&cert).unwrap();

    // Re-issue at a higher generation over the same path.
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "issue-cert", "--org-key"])
        .arg(&key)
        .args(["--member", MEMBER_HEX])
        .args(["--generation", "7"])
        .args(["--out"])
        .arg(&cert)
        .arg("--force")
        .assert()
        .code(0);

    let second = std::fs::read(&cert).unwrap();
    assert_ne!(first, second, "--force must actually replace the artifact");

    // The published artifact must be COMPLETE, not a torn/interleaved write —
    // the whole point of staging plus an atomic rename. It parses as the
    // versioned envelope, and the cert body carries the new generation: the
    // canonical cert encoding is fixed-offset with `generation` as a
    // little-endian u32, so generation 7 appears as `07000000` in the hex
    // body (a gen-0 cert has `00000000` there).
    let text = String::from_utf8_lossy(&second).to_string();
    let parsed: serde_json::Value =
        serde_json::from_str(&text).expect("published cert is valid JSON");
    assert_eq!(parsed["version"], 1, "versioned envelope preserved");
    let body = parsed["cert"].as_str().expect("cert body is a hex string");
    assert!(
        body.contains("07000000"),
        "the replacement must carry the newly issued generation (LE u32 in the \
         canonical cert encoding); body: {body}",
    );
    // No staging temp left behind.
    let strays: Vec<_> = std::fs::read_dir(dir.path())
        .unwrap()
        .filter_map(|e| e.ok())
        .map(|e| e.file_name().to_string_lossy().to_string())
        .filter(|n| n.contains(".stage."))
        .collect();
    assert!(strays.is_empty(), "staging temps left behind: {strays:?}");
}

/// Without `--force`, publication is no-clobber and must not write THROUGH a
/// leaf symlink onto the org key. `tokio::fs::write` followed symlinks; the
/// staged hard-link publish does not.
#[cfg(unix)]
#[test]
fn issue_cert_never_writes_through_a_symlink_onto_the_org_key() {
    let dir = tempfile::tempdir().unwrap();
    let key = keygen(dir.path(), "org.toml");
    let before = read_key_bytes(&key);

    let link = dir.path().join("cert.json");
    std::os::unix::fs::symlink(&key, &link).unwrap();

    // No --force: no-clobber refuses outright.
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "issue-cert", "--org-key"])
        .arg(&key)
        .args(["--member", MEMBER_HEX])
        .args(["--out"])
        .arg(&link)
        .assert()
        .code(2);
    assert_eq!(read_key_bytes(&key), before, "org key intact (no --force)");

    // With --force: the content backstop refuses, because the symlink
    // resolves to the org key.
    Command::cargo_bin("net-mesh")
        .unwrap()
        .args(["org", "issue-cert", "--org-key"])
        .arg(&key)
        .args(["--member", MEMBER_HEX])
        .args(["--out"])
        .arg(&link)
        .arg("--force")
        .assert()
        .code(2);
    assert_eq!(
        read_key_bytes(&key),
        before,
        "org key intact (with --force)"
    );
}