Skip to content

cli: key-gen --force regenerates node.secret before the eth keystore with no rollback — failure leaves a mismatched half-rotated key pair #844

Closed

Description

@thiras

Surfaced by a full-repository review (2026-06-09).

Finding

With --force, key_gen (crates/cli/src/commands/key_gen.rs:59-69) archives the old node.secret, writes a fresh node key via load_or_generate, and only then runs generate_and_persist for the eth keystore. If keystore generation fails (disk full, encryption error), the node identity has already been replaced while the keystore is archived/stale — a confusing partial-success state on the very rotation path the archiving exists to make safe. The no-force pre-flight (lines 23-36) deliberately avoids partial writes; the force path reintroduces the hazard. (The password is read before any write, so the most likely failure is already excluded — the window is real but narrow.)

Fix

Generate both key materials into temp files first, then commit (archive-old + rename-new) for both only after both succeed.

Activity

  1. added
    kind:bug
    prio:medium
    area:cli
    on Jun 9, 2026
  2. github-actions commented on Jun 9, 2026

    @github-actions

    🎯 Triage report

    The --force path in key_gen (crates/cli/src/commands/key_gen.rs:59-69) performs a non-atomic two-step write: it archives and replaces node.secret (lines 60–64) before invoking generate_and_persist for the eth keystore (line 67). If keystore generation fails after the Ed25519 key is already committed, the node ends up with a new node.secret but no corresponding keystore.json — exactly the mismatched state the archival runbook is designed to prevent. The password is read before any write (mitigating the most common interactive failure), but disk-full and encrypt errors are real edge cases.

    📊 Assessment

    Dimension Value Reasoning
    Type Bug Confirmed: partial-success state on the key rotation path.
    Labels bug, medium, cli (unchanged) Already accurate; security is borderline but this is an atomicity/reliability bug on the force path, not an exploitable surface.
    Coding agent Suitable Fix is clearly specified (generate both to temp, then commit both), scope is one function in one file, and success criterion is unambiguous.
    💡 Notes and suggestions

    Root cause walk-through

    key_gen --force
      └─ move_aside(key_path)           # node.secret → node.secret.bak.<ts>  [COMMITTED]
      └─ load_or_generate(output_dir)   # writes new node.secret               [COMMITTED]
      └─ generate_and_persist(...)      # ← fails here (disk full / encrypt)
          └─ move_aside(keystore_path)  # keystore.json → keystore.json.bak.<ts>  [may have run]
          └─ encrypt_keystore(...)      # ← actual failure point
    

    After the failure: node.secret is new, keystore.json is either archived or absent — neither matches.

    Suggested fix (in key_gen.rs)

    The cleanest approach, consistent with how generate_and_persist already handles atomicity internally (temp file → chmod → rename):

    1. Refactor identity::load_or_generate to have a lower-level variant (e.g. generate_to_temp(data_dir) -> (SecretKey, TempPath)) that writes to a random temp name in data_dir without touching node.secret.
    2. Refactor eth_identity::generate_and_persist similarly — accept an explicit temp path, or expose a generate_to_temp helper that returns (Address, TempPath) without archiving the live keystore.
    3. In key_gen.rs --force path, sequence:
      • generate_new_node_key_to_temp() → (key, node_temp)
      • generate_new_eth_keystore_to_temp() → (address, eth_temp) (both succeed before committing)
      • move_aside(key_path)
      • rename(node_temp, key_path)
      • move_aside(keystore_path)
      • rename(eth_temp, keystore_path)

    Alternatively, a simpler but equivalent approach: run generate_and_persist (step 3 in the original code) first (its own temp→rename is already atomic at the file level), skip its internal archive by passing a new archive: false flag, then do both move_aside calls and the node key write only after generate_and_persist succeeds. The ordering in the current code's comment ("mirrors decdn run startup") is not a hard constraint.

    Checklist

    • Extract temp-write helper(s) from load_or_generate / generate_and_persist
    • Rewrite --force path in key_gen.rs to: gen-both-to-temp → archive-both-old → rename-both-new
    • Verify cleanup on partial failure (temp files removed if any step fails after generation)
    • Add test: simulate a failure after node key write but before keystore write; assert node.secret and keystore.json remain unchanged (i.e. archives are restored or originals never touched)
    • Update appendix-operator-key-rotation.md if the observable archive timing changes

    Generated by Agentic Triage for issue #844 · sonnet46 767.6K · ◷

    Add this agentic workflows to your repo

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/issue-triage.md@94ec5db57374c5b04a4b8eef8b4413f9af44d63f
    
  3. self-assigned this
    on Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:clikind:bugprio:medium

Type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions