Skip to content

feat(app): manage SSH private keys in a key manager - #308

Open
TooonyChen wants to merge 4 commits into
mainfrom
feature/key-manager
Open

TooonyChen wants to merge 4 commits into
mainfrom
feature/key-manager

Conversation

@TooonyChen

@TooonyChen TooonyChen commented Dec 3, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds the key manager from #306. You import a private key once, together with its passphrase, into the system's credential store. Connections then pick it by name, and the original file is no longer needed.

  • Connection manager. A text button with a key icon now sits right after Import. In Chinese it reads "密钥管理"; in English the visible text is "Keys", and the tooltip and accessible name are "Key manager" (see the differences below). It opens the key manager window, which is modal to the connection manager the same way "Import connections" is.
  • Key manager window. A list with Name, Type and Length, sorted by name, plus Import…, Edit and Delete. A line under the list shows the chosen key's short SHA256 fingerprint (the full one is in the tooltip), whether it has a passphrase, and how many connections use it. A note says where keys are kept, and there is a single Close button. With no keys the list says "No keys yet. Click “Import…” to add one." and Edit/Delete are disabled. Double-clicking a row opens Edit.
  • Import key / Edit key dialog. This is a modal window of its own, over the key manager.
    • Fields: the key file (path or Browse…), whose type, length and passphrase state show as soon as it is read; a name, filled in from the file name; and the passphrase, shown only for an encrypted key.
    • Errors appear under their field: wrong passphrase, missing passphrase, a key already imported (shows its name), a name already taken, a public key or certificate, a file that is not a key (including PuTTY .ppk, which key-file login cannot read either), a file over 64 KB, and an unreadable file.
    • Edit can rename the key and can replace its content from another file. Hosts that use the key keep its ID and get the new fingerprint.
  • Delete. Asks first, and names the connections that use the key. It deletes from the credential store first, then from keys.json.
  • Connection editor. For private key login there is a new "Key" select: managed keys by name with type and length, a divider, then "Key file" (variant B1).
    • "Key file" brings back the path row with Browse…, the passphrase and "Remember passphrase", working as they do today.
    • With a managed key chosen, the passphrase and Remember rows are hidden.
    • A new host defaults to the first key, or to "Key file" when there are no keys.
    • Jump hosts are saved hosts, so they use the same select in their own editor, and connecting through them loads their key.
  • Connecting reads the managed keys of the host and of every jump host on the secret queue (never on the UI thread). The session goroutine waits for them, gives them to sshx.Dial through a new Options.Key, and zeroes the bytes once the signers have parsed them. A managed-key host never opens a passphrase prompt.

Design decisions

  • Credential store layout and limits. I checked MyGo v0.10.0. mygo.Secrets.Set refuses values over MaxSecretSize = 2,560 bytes on every platform, not just Windows, and refuses keys longer than 256 bytes.
    • The backends: Windows Credential Manager blob, whose own limit is 2,560; macOS Keychain generic password, no small limit of its own; libsecret secret_password_store_binary, also none. The effective limit is therefore 2,560 everywhere.
    • The new internal/keystore package stores the payload {"key": <file bytes, base64>, "passphrase": "…"} in parts named sshkey/<id>/<gen>/<n>, each at most 2,048 bytes. A manifest sshkey/<id> = v1 <gen> <parts> <sha256 hex> is written last.
    • A key is used only when the manifest parses and every part is present, has the right size and matches the checksum. Otherwise it reads as "not found" or "incomplete", which the app treats as missing.
    • A failed write deletes the parts it wrote. Replace writes a new random generation, swaps the manifest in one write, then deletes the old parts, so a failure leaves the old key whole.
    • Delete removes the manifest first, then every part, even if one part fails.
    • An RSA 4096 key with a passphrase takes three parts.
  • keys.json (config folder, this device only):
    • Format: {"version":1,"keys":[{"id","name","type","bits","fingerprint","encrypted","createdAt"}]}. Nothing secret is in it.
    • It is not read by sync, and the sync payload carries only hosts and settings. Managed keys are never added to the synced passwords, because they are not in hostSecretNames.
  • How a host refers to a key. New host fields keyId and keyFingerprint, with keyPath cleared.
    • Lookup (KeyRing.Resolve) tries the ID first, then the fingerprint. So a host synced from another device, where the key has a different ID, works once a key with the same fingerprint is imported.
    • Saving the editor without changing the choice keeps the other device's ID, so two devices do not keep rewriting each other's reference.
    • Older versions keep both fields through the existing unknown-keys mechanism. They see such a host as a key host with no file.
  • Missing key.
    • In the editor: the select shows a red "Key not found" with "This computer does not have this key (SHA256:…): import it in the key manager, or choose another." OK still saves, and keyId/keyFingerprint stay as they are unless another key or "Key file" is chosen, so the reference is never changed silently.
    • When connecting: it fails at once with "This computer does not have the private key of “host”", with no prompt.
    • A key listed in keys.json that the store does not have counts as missing too.
    • The editor updates while it is open: a key deleted meanwhile becomes missing, and a missing key that gets imported is chosen again.
  • Edit → replace content. The key keeps its ID, and type, length and fingerprint are updated. Every live host that resolves to the key gets keyId/keyFingerprint set to this key's ID and new fingerprint, and hosts.json is saved. Other devices then see the new fingerprint and show the key missing until the rotated key is imported there.
  • No credential store. The key manager's note turns into "The system's credential store (…) is not available: keys cannot be imported." and Import… is disabled. A listed key that cannot be read fails to connect with that reason.
  • Secrets. Key bytes and passphrases never go into logs, errors, tooltips, the clipboard or temp files. Errors from the parser and the store carry no key material. Read buffers are zeroed on dialog close, after storing and after login. Limitation: the Platform secret API takes strings, so the store calls hold immutable string copies that cannot be zeroed.

Differences from the mockup

  • English toolbar label is "Keys". "Key manager" pushed Import and the new button into the toolbar's overflow (»). The label is "Keys", with "Key manager" as tooltip and accessible name; Chinese shows "密钥管理". The manager now opens 1000 px wide, and the folder filter and search field shrink to fit (at most 150/220, at least 80/90), sized from measured text with a margin. TestManagerToolbarFits checks font scales 1 to 1.5 in both languages.
  • Missing-key wording. The text says "Key not found / 找不到密钥" plus "this computer does not have this key (fingerprint)" instead of "“legacy-jump” 已从密钥管理中删除". A deleted key and a key never imported on this device cannot be told apart, and the host only stores the ID and fingerprint, not the name.
  • No red asterisk on the passphrase label. The field's description says it is required.
  • Fingerprint shortening. It reuses the app's existing format (6 characters…4 characters).
  • Field label rename. The editor's key file row is now labeled "Key file / 私钥文件" (was "Private key / 私钥"), to match the mockup and to avoid clashing with the new "Key / 密钥" select.

Closes #306

Testing

  • go build ./..., go vet ./..., GOOS=windows go vet ., GOOS=darwin go vet ., go tool mygo vet, and the toolchain's gofmt: all clean.
  • go test -race -count=1 -timeout 30m ./...: all packages pass.

Tests added, by acceptance criterion:

  • Import, rename, replace, delete; list and detail line for RSA, ECDSA, Ed25519, with and without passphrase. TestKeyImport covers RSA 2048, RSA 4096 with passphrase, ECDSA 256 and 384 with passphrase, Ed25519, and legacy PEM RSA with passphrase, all through the dialog. Also TestKeyManagerList, TestKeyEdit, TestKeyDelete, TestKeyDeleteUnused, and sshx TestInspectKey.
  • Login with a managed key against the in-process server, through a jump host, after the file is deleted. TestManagedKeyLogin (two sshtest servers) and sshx TestDialManagedKey.
  • Refusals. TestKeyImportErrors covers wrong passphrase, missing passphrase, duplicate key (including a legacy PEM key that is compared after its passphrase is given), duplicate name in another case, empty name, public key, text file, and missing file. Also sshx TestInspectKeyRefuses.
  • RSA 4096 through a store limited to 2,560 bytes per entry. keystore TestRSA4096RoundTripsUnderTheLimit. The app's fake store also enforces 2,560 in TestKeyImport and TestManagedKeyLogin. Plus TestFailedWriteLeavesNoHalfKey, TestIncompleteKeyDetected, TestDeleteManifestFirst, TestNoStore, TestErrorsHoldNoSecret.
  • Deleting a key in use names the connections; they then show it missing. TestKeyDelete.
  • A synced host without the key shows it missing and recovers after import. TestSyncedHostMissingKey: raw JSON with another device's ID; editor and connect show the missing state; importing the same key makes it connect; the saved reference keeps the foreign ID; the sync payload carries the reference and nothing from the key manager. Also config TestKeyResolve and TestKeyReferenceKeptByOlderVersions.
  • Existing key-file connections are unchanged; "Key file" works. TestEditorKeyFileHostUnchanged, TestEditorKeySelect, and the existing editor and passphrase tests, which pass unchanged.
  • No credential store. TestKeyManagerNoStore.
  • Toolbar button. TestKeyManagerButton.
  • Logs. The import and login tests check that captured logs hold no key or passphrase.
  • Test keys are generated in the tests (sshtest.NewKey); none are committed. UI checks look for texts and accessibility nodes, not pixel widths.

Screenshots: go run ./tools/screenshots on this branch and on unmodified main. Only connection-manager-en.png and connection-manager-zh-CN.png differ (the new button); they are committed.

Not tested: real Windows Credential Manager, macOS Keychain and Secret Service, which cannot run here. A fake store stands in for them (fakeSecrets in the app tests, limitedStore in keystore), refusing values over 2,560 bytes and, in some tests, failing set/delete calls or reporting errors.ErrUnsupported like a missing store. Also not tested: native nested sheets on macOS (the key dialog is modal to the key manager, which is modal to the connection manager). The sizes are chosen so each sheet fits inside its parent.


Generated by Claude Code

Import a private key once, with its passphrase, into the system's
credential store, split over entries of at most 2,048 bytes with a
manifest written last; connections choose it by name in the editor's
new Key select, whose last item "Key file" keeps today's key file row.
keys.json lists the keys on this device only; hosts refer to a key by
ID and fingerprint, and show it missing until one with the same
fingerprint is imported.

Closes #306

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015nMqJEvqPnRmPgi6dnraZM
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-03T03:50:48.929475Z 4e014e5 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4e014e5cc8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/app/keyimport.go
Comment on lines +307 to +310
if err := a.saveKeyRing(); err != nil {
a.keys = before
if d.edit == nil { // the new key is not listed: not kept either
a.deleteKeySecret(id, func(error) {})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore the stored key when replacement metadata cannot be saved

When replacing an existing key, if writing keys.json fails, this branch restores only the old metadata. By this point putKey has replaced the credential-store payload and deleted the old generation, so the old fingerprint now identifies different private-key material. Existing connections consequently attempt authentication with the replacement despite the dialog reporting failure, and the original managed key may be irrecoverable if its source file was deleted. Retain the previous secret until metadata persistence succeeds, or restore it when persistence fails.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 603776f. When keys.json cannot be saved, a new key is removed from the ring and deleted from the credential store; an edited key gets its old ring entry back and its old content is written back to the store. If that restore fails too, the dialog says so. Covered by TestKeyReplaceRollback.


Generated by Claude Code

Comment thread internal/app/keyimport.go Outdated
for _, h := range users {
h.KeyID, h.KeyFingerprint = k.ID, k.Fingerprint
}
a.saveHosts(a.hosts)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle failures when persisting rotated host references

When a key replacement succeeds but saving hosts.json fails, the error is discarded and the dialog closes as though everything was saved. This breaks connections after restart when they previously resolved the key by fingerprint using another device's ID: their persisted reference still contains the old fingerprint, while the local key now has the replacement fingerprint, so KeyRing.Resolve finds neither. Check this save result and retain a recoverable error state rather than silently reporting success.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 603776f. A failed hosts.json save after a replace now shows the error and keeps the dialog open; the hosts stay updated in memory and saving again writes them. Covered by TestKeyReplaceHostsSaveFails.


Generated by Claude Code

Comment thread internal/app/keyimport.go Outdated
Comment on lines +323 to +324
a.keyUI.sel = id
a.closeWindow(WindowKeyDialog)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid closing a newer dialog from an earlier store callback

While a credential-store write is pending, Cancel and the window-close shortcuts remain available. If the user closes that dialog and opens another Import or Edit dialog before the write completes, this callback unconditionally closes the newer dialog, discarding its inputs. Unlike the preceding inspection callback, the store completion does not check a.keyUI.dlg == d. Complete any necessary persistence independently, but only close the dialog that initiated the operation.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 603776f. The store callback only changes or closes the dialog that started it. If that dialog is gone, an error is shown in the key manager instead, and Cancel stays enabled while the store is busy. Covered by TestKeyImportDialogClosedWhileStoring.


Generated by Claude Code

The missing-key state in the connection editor is a warning now: OK
saves the rest of the host and keeps keyId and keyFingerprint unless
another key or "Key file" is chosen. A replace whose keys.json cannot
be saved puts the key's old content back in the store; a failed
hosts.json save keeps the dialog open with the error; a store write
that finishes after its dialog closed no longer touches the dialog
opened since. The connection manager opens 1000 wide so that its
toolbar keeps the Keys button out of the overflow with wider fonts.

Refs #306

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015nMqJEvqPnRmPgi6dnraZM
The folder filter and the search field now give way, down to 80 and 90
DIPs, before any toolbar button goes into the overflow menu: their
widths come from the room left beside the buttons, measured with
ui.ShapeText in the theme's font plus a 15% margin. The fit test runs
with text 1.2, 1.35 and 1.5 times larger, in both languages.

Refs #306

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015nMqJEvqPnRmPgi6dnraZM
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015nMqJEvqPnRmPgi6dnraZM

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(app): manage SSH private keys in a key manager

2 participants