Commit c9a7870
authored
feat(encryption): Stage 6D-2 — ErrNodeIDCollision + ErrLocalEpochRollback primitives + probe-node-id CLI (#788)
## Summary
Stage 6D-2 per the [6D design doc
§5](https://github.com/bootjp/elastickv/blob/main/docs/design/2026_05_18_proposed_6d_enable_storage_envelope.md)
(landed in PR #786). Ships the two bundled 6C-3 startup-guard primitives
as pure functions in the encryption package, plus the `probe-node-id`
CLI helper from §5.1 mitigation #2.
**Operator-inert until 6D-6 wires the primitives into `main.go`'s
startup-guard phase.** The `probe-node-id` CLI is reachable today but
only computes locally — no operator action against the cluster.
## What this ships
### Two new sentinel errors (`internal/encryption/errors.go`)
- `ErrNodeIDCollision` — two distinct `full_node_id` values hash to the
same 16-bit `node_id` (would reuse GCM nonces under the active DEK).
Triage message points at `probe-node-id` for pre-join verification.
- `ErrLocalEpochRollback` — sidecar `local_epoch <=
registry.LastSeenLocalEpoch` (strict-ahead invariant from §5.2). Triage
message points at `resync-sidecar` or DEK rotation.
### Two pure-function guard primitives
- `CheckNodeIDCollision(fullNodeIDs []uint64) error`
(`node_id_collision.go`)
- `CheckLocalEpochRollback(registry, fullNodeID, dekID,
sidecarLocalEpoch) error` (`local_epoch_rollback.go`)
Both use the shipped `uint16(full_node_id & 0xFFFF)` narrowing (matches
`applier.go::nodeIDMask`). Local-state-only — no RPC, no engine access.
Same-value-twice is NOT a collision (duplicate-membership-entry
defense). Equality fires `ErrLocalEpochRollback` per the §5.2
strict-ahead invariant (replay would reuse the same `(node_id,
local_epoch)` prefix).
### CLI: `elastickv-admin encryption probe-node-id`
Per §5.1 mitigation #2 — operator runs against a candidate
`full_node_id` BEFORE joining a node to verify the value is free in the
cluster's allocated `node_id` space. Accepts decimal or `0x`-prefixed
hex input; prints both the 16-hex-digit canonical `full_node_id` and the
derived `node_id`. No RPC; pure local computation.
## Test coverage
19 new tests:
| File | Tests |
|---|---|
| `node_id_collision_test.go` | Empty, SingleNode, NoCollision,
Collision, DuplicateNotCollision (same value twice ≠ collision),
OrderIndependent, ErrorIncludesBothIDs |
| `local_epoch_rollback_test.go` | NoRow (freshly-joined-learner skip),
SidecarStrictlyAhead, SidecarLessThan, SidecarEqual (strict-ahead replay
case), PebbleError (not classified as rollback),
NarrowingMatchesShippedConvention |
| `encryption_test.go` (new tests added) | ProbeNodeID_Hex,
ProbeNodeID_Decimal, RequiresFlag, RejectsBadInput,
ProbeNodeIDSubcommand (dispatch reaches handler) |
## Caller audit
No semantic changes to existing functions; only additions:
- New sentinels in `errors.go` — exported package-surface additions, no
callers to update.
- New pure functions — no callers yet (operator-inert until 6D-6).
- New CLI subcommand `probe-node-id` — additive; no existing subcommand
changed.
## Five-lens self-review
1. **Data loss** — net-positive. Both guards catch nonce-reuse scenarios
BEFORE any encrypted write that would corrupt confidentiality.
2. **Concurrency / distributed failures** — pure functions over local
snapshots; no shared state. Run before any goroutine fan-out.
3. **Performance** — O(N) map walk for collision check; single Pebble
Get for rollback check. Paid once per process start on happy path.
4. **Data consistency** — narrowing matches the shipped `nodeIDMask =
0xFFFF` convention from `applier.go`. The
`NarrowingMatchesShippedConvention` test pins this against a future
change that would diverge.
5. **Test coverage** — 19 new unit tests cover every documented branch +
operationally important edges (equality for strict-ahead, narrowing
equivalence, error propagation).
## Verification
- `go test -race -timeout=60s ./internal/encryption/
./cmd/elastickv-admin/` — PASS
- `golangci-lint run ./internal/encryption/ ./cmd/elastickv-admin/` — 0
issues
- `go build ./...` — PASS
## Plan
Per the 6D design doc §7 decomposition, the next sub-PRs are:
- **6D-3** — Voters ∪ Learners `CapabilityFanout` helper (used by 6D-6's
pre-flight gate and by Stage 6E)
- **6D-4** — Wire format addition (`RotateSubEnableStorageEnvelope =
0x04`) + FSM apply path with `len(Wrapped) == 0` check + sidecar
`StorageEnvelopeCutoverIndex` write
- **6D-5** — §6.2 storage-layer toggle (`PutAt` reads
`StorageEnvelopeActive`)
- **6D-6** — `EnableStorageEnvelope` admin RPC + CLI + integration test
+ `main.go` wiring of THIS PR's startup-guard primitives
## Test plan
- [x] `go test -race -timeout=60s ./internal/encryption/
./cmd/elastickv-admin/` — PASS (19 new tests + existing)
- [x] `golangci-lint run ./internal/encryption/ ./cmd/elastickv-admin/`
— 0 issues
- [x] `go build ./...` — PASS
- [ ] Jepsen — not relevant for an operator-inert PR; the 6D-6
integration test will exercise the wired guards end-to-end
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added a CLI subcommand to display a provided full node identifier
(decimal or hex) and its derived 16-bit node id.
* Added startup validation checks that detect 16-bit node-id collisions
and local-epoch rollback conditions to prevent unsafe encryption states.
* **Tests**
* Added unit and integration tests covering the new CLI parsing,
collision detection, and local-epoch rollback behaviors.
<!-- review_stack_entry_start -->
[](https://app.coderabbit.ai/change-stack/bootjp/elastickv/pull/788?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)
<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->7 files changed
Lines changed: 861 additions & 2 deletions
File tree
- cmd/elastickv-admin
- internal/encryption
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| 46 | + | |
| 47 | + | |
45 | 48 | | |
46 | 49 | | |
47 | 50 | | |
48 | 51 | | |
49 | 52 | | |
50 | | - | |
| 53 | + | |
51 | 54 | | |
52 | 55 | | |
53 | 56 | | |
54 | 57 | | |
55 | 58 | | |
56 | | - | |
| 59 | + | |
57 | 60 | | |
58 | 61 | | |
59 | 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 | + | |
60 | 139 | | |
61 | 140 | | |
62 | 141 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
462 | 462 | | |
463 | 463 | | |
464 | 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
208 | 208 | | |
209 | 209 | | |
210 | 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 | + | |
211 | 272 | | |
0 commit comments