Skip to content

Commit 352c1e7

Browse files
committed
fix(init): hint at talm.key recovery path when --decrypt fails
Talm.key absence is a real operational scenario (laptop swap, lost backup, fresh checkout from a repo whose .gitignore properly excludes the key) and the raw "open .../talm.key: no such file" that LoadKey emitted surfaces as if it's an internal bug rather than a "you need to restore your key" condition. Distinguish the not-exist case from other I/O errors at the LoadKey boundary and attach a hint naming both recovery paths the operator can take: restore the backed-up key, or re-run \`talm init\` to regenerate (with the warning that regeneration writes new secrets and the old secrets.encrypted.yaml becomes undecryptable without the original key). Pinned by TestContract_Age_LoadKey_MissingFileSurfacesRecoveryHint. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent a0301e7 commit 352c1e7

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎docs/manual-test-plan.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ Expected: clear error mentioning the missing key path.
9898
rm -rf /tmp/talm-init-test
9999
```
100100

101+
### A6a. `init --decrypt` without `talm.key` surfaces recovery hint
102+
103+
```bash
104+
mkdir -p /tmp/talm-decrypt-test && cd /tmp/talm-decrypt-test
105+
/tmp/talm-safety init --preset cozystack --name test --endpoints 192.0.2.1
106+
mv talm.key /tmp/talm.key.backup
107+
/tmp/talm-safety init --decrypt
108+
mv /tmp/talm.key.backup talm.key # restore for next run
109+
rm -rf /tmp/talm-decrypt-test
110+
```
111+
112+
Expected: error `failed to decrypt secrets: load key: read key file: open <path>/talm.key: no such file or directory` followed by hint `talm.key is required to decrypt secrets.encrypted.yaml. Restore your backed-up key, or re-run \`talm init\` to regenerate (this writes new secrets — the old secrets.encrypted.yaml will not be decryptable without the original key).`
113+
114+
Regression anchor: the hint must name BOTH recovery paths (restore from backup, re-run init to regenerate) AND include the warning that regeneration writes new secrets making the old encrypted secrets undecryptable. A regression that drops either path or the warning silently invites operators to "just run init again" without understanding the data-loss tradeoff.
115+
101116
### A8. `init --update` without `--preset` and without preset dep in Chart.yaml
102117

103118
```bash

‎pkg/age/age.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ func LoadKey(rootDir string) (*age.X25519Identity, error) {
104104

105105
keyData, err := os.ReadFile(keyFile)
106106
if err != nil {
107+
if errors.Is(err, os.ErrNotExist) {
108+
// Talm.key absence is a real operational scenario
109+
// (laptop swap, lost backup, fresh checkout) and the
110+
// raw "open ...: no such file" surfaces as if it's an
111+
// internal bug. Attach the recovery hint naming both
112+
// paths the operator can take so the error reads as
113+
// "what to do next", not "talm is broken".
114+
//nolint:wrapcheck // cockroachdb/errors.WithHint is the project's wrapping/hinting idiom at boundaries.
115+
return nil, errors.WithHint(
116+
errors.Wrap(err, "read key file"),
117+
"talm.key is required to decrypt secrets.encrypted.yaml. Restore your backed-up key, or re-run `talm init` to regenerate (this writes new secrets — the old secrets.encrypted.yaml will not be decryptable without the original key).",
118+
)
119+
}
120+
107121
return nil, errors.Wrap(err, "read key file")
108122
}
109123

‎pkg/age/contract_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"strings"
3737
"testing"
3838

39+
cerrors "github.com/cockroachdb/errors"
3940
"github.com/cozystack/talm/pkg/age"
4041
"gopkg.in/yaml.v3"
4142
)
@@ -181,6 +182,31 @@ func TestContract_Age_LoadKey_MissingFileErrors(t *testing.T) {
181182
}
182183
}
183184

185+
// TestContract_Age_LoadKey_MissingFileSurfacesRecoveryHint pins the
186+
// operator-facing recovery message attached to a missing talm.key.
187+
// Without the hint, operators see a raw "open talm.key: no such
188+
// file" stack-style line and assume it's a bug in talm. The hint
189+
// must name BOTH recovery paths: restore from backup, or re-run
190+
// `talm init` to regenerate (which writes new secrets — old
191+
// secrets.encrypted.yaml becomes undecryptable without the
192+
// original key).
193+
func TestContract_Age_LoadKey_MissingFileSurfacesRecoveryHint(t *testing.T) {
194+
dir := t.TempDir() // no talm.key inside
195+
196+
_, err := age.LoadKey(dir)
197+
if err == nil {
198+
t.Fatal("expected error for missing talm.key")
199+
}
200+
201+
hints := cerrors.GetAllHints(err)
202+
joinedLower := strings.ToLower(strings.Join(hints, "\n"))
203+
for _, want := range []string{"talm.key", "restore", "talm init"} {
204+
if !strings.Contains(joinedLower, want) {
205+
t.Errorf("missing-key hint must mention %q so operator knows the recovery path; got hints:\n%s", want, strings.Join(hints, "\n"))
206+
}
207+
}
208+
}
209+
184210
// === GetPublicKey / GetPublicKeyFromFile ===
185211

186212
// Contract: GetPublicKey returns the recipient string from an

0 commit comments

Comments
 (0)