Skip to content

Commit 8c431f9

Browse files
committed
test(age): rename misleading test, surface Phase 3 rollback errors
Address branch-review observations on the previous commit: 1. The test previously named CleanupFailureSurfacesError did not actually exercise the Phase 5 cleanup path. Staging a directory at the backup destination triggers Phase 0 refusal (os.Stat returns nil error for directories), not a Phase 3 rename failure or a Phase 5 os.Remove failure. Renamed to RefusesWhenDirectoryBlocksBackupPath to match what it actually pins. Comment now explicitly states the Phase 5 path is not reachable through public-API fault injection — the contract there is documented in the function docstring. 2. The Phase 3 rollback rename (between failing-encrypted-rename and the error return) silently swallowed its own error. If the rollback failed the operator was left with keyBackup but no keyFile, and the caller-facing error said only 'failed to back up encrypted file' without flagging the partial state. Apply the same pattern as the restore() closure in Phase 4 — capture the rollback rename's error and include it in the wrapped message with manual-recovery instructions if non-nil. Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 72227f3 commit 8c431f9

2 files changed

Lines changed: 31 additions & 35 deletions

File tree

‎pkg/age/age.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,15 @@ func RotateKeys(rootDir string) error {
576576
}
577577
if err := os.Rename(encryptedFile, encryptedBackup); err != nil {
578578
// Roll back the key rename so the project is untouched.
579-
_ = os.Rename(keyBackup, keyFile)
580-
return fmt.Errorf("failed to back up encrypted file before rotation: %w", err)
579+
// Capture the rollback error too — if it fails the
580+
// operator is left with keyBackup but no keyFile, and the
581+
// caller-facing error must say so explicitly (otherwise
582+
// a Phase 0 refusal on retry would be the first sign of
583+
// the partial state).
584+
if rbErr := os.Rename(keyBackup, keyFile); rbErr != nil {
585+
return fmt.Errorf("failed to back up encrypted file before rotation: %w; AND rollback of key-file rename failed: %v — manual recovery: rename %q -> %q", err, rbErr, keyBackup, keyFile)
586+
}
587+
return fmt.Errorf("failed to back up encrypted file before rotation: %w (key file rename rolled back)", err)
581588
}
582589

583590
// restore is a recovery helper used when any later step fails:

‎pkg/age/contract_test.go‎

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -494,25 +494,26 @@ func TestContract_Age_RotateKeys_NoLeftoverBackups(t *testing.T) {
494494
}
495495
}
496496

497-
// Contract: when the post-commit cleanup of `*.rotation-backup`
498-
// files fails (e.g. a permissions flip on the parent dir between
499-
// the commit and the os.Remove), RotateKeys returns a non-nil
500-
// error explaining "rotation committed but cleanup failed" and
501-
// instructing the operator to remove the leftovers manually
502-
// before the next rotation. The new key + encrypted file on disk
503-
// are still the rotated pair — only the backups linger.
497+
// Contract: when something already occupies a path the rotation
498+
// will need to use (here we stage a directory at the backup
499+
// destination), RotateKeys refuses immediately and leaves the
500+
// originals untouched on disk. The directory case is interesting
501+
// because os.Stat returns nil error for directories just as for
502+
// regular files, so the Phase 0 leftover-backup check fires and
503+
// rejects the run before any rename happens. Pinning that the
504+
// originals survive any such early refusal.
504505
//
505-
// Failure injection: chmod the project dir to 0o500 right before
506-
// the cleanup step would run. We approximate this by triggering
507-
// the cleanup-failed code path indirectly: stage a directory at
508-
// the backup path so os.Remove (which expects a regular file)
509-
// fails. This is brittle but the only injection point reachable
510-
// without modifying the function signature.
506+
// The genuine Phase 5 cleanup-failure path (where rotation
507+
// commits but os.Remove of a backup file errors) is not
508+
// reachable through public-API fault injection — it requires
509+
// either a chmod between Phase 4 and Phase 5 or a swap of the
510+
// backup file for a directory in the same window, neither of
511+
// which is exposed. The Phase 5 error wording is exercised by
512+
// inspection only (see the docstring of RotateKeys).
511513
//
512-
// Skipped under root because chmod-on-directory-content does not
513-
// constrain euid 0; the test would always succeed in unhelpful
514-
// ways.
515-
func TestContract_Age_RotateKeys_CleanupFailureSurfacesError(t *testing.T) {
514+
// Skipped under root because directory permissions used in
515+
// adjacent injection paths are ignored by the kernel for euid 0.
516+
func TestContract_Age_RotateKeys_RefusesWhenDirectoryBlocksBackupPath(t *testing.T) {
516517
if os.Geteuid() == 0 {
517518
t.Skip("running as root — directory permissions and os.Remove behaviour differ")
518519
}
@@ -524,20 +525,10 @@ func TestContract_Age_RotateKeys_CleanupFailureSurfacesError(t *testing.T) {
524525
t.Fatal(err)
525526
}
526527

527-
// Render the parent directory read-only AFTER first rotation
528-
// is fully done to make a SECOND rotation fail in cleanup.
529-
// A read-only dir prevents new files / renames in either
530-
// direction, which is too aggressive — the second rotation
531-
// cannot even Phase 3-rename. So instead we exercise the
532-
// path differently: place a directory exactly where one of
533-
// the backups will land so os.Rename fails at Phase 3, and
534-
// confirm Phase 0 / Phase 3 produce a clean error (no
535-
// half-committed state). The cleanup-failure path itself is
536-
// straightforward (two os.Remove calls + error wrap), and
537-
// the integration test covered by NoLeftoverBackups proves
538-
// the happy path. This test pins the contract that ANY
539-
// rename-aside failure during Phase 3 returns an error and
540-
// leaves the originals on disk untouched.
528+
// Stage a directory at the destination of one of the backup
529+
// renames. Phase 0 sees something at the path (os.Stat returns
530+
// nil error for directories) and refuses with the leftover
531+
// message. The originals are not touched.
541532
dirAtBackupPath := filepath.Join(dir, "talm.key.rotation-backup")
542533
if err := os.MkdirAll(dirAtBackupPath, 0o755); err != nil {
543534
t.Fatal(err)
@@ -556,8 +547,6 @@ func TestContract_Age_RotateKeys_CleanupFailureSurfacesError(t *testing.T) {
556547
if err := age.RotateKeys(dir); err == nil {
557548
t.Fatal("expected RotateKeys to fail when a directory blocks the backup path")
558549
}
559-
// Originals must still be on disk untouched (Phase 0 refusal
560-
// or Phase 3 rollback preserved them).
561550
gotKey, err := os.ReadFile(filepath.Join(dir, "talm.key"))
562551
if err != nil {
563552
t.Fatalf("talm.key missing after failed rotation: %v", err)

0 commit comments

Comments
 (0)