Skip to content

Commit ef886b6

Browse files
committed
fix(age): EncryptSecretsFile and EncryptYAMLFile also write mode 0600
Address review feedback from gemini-code-assist on pkg/age/age.go:613: the same file (secrets.encrypted.yaml) is written by three code paths (EncryptSecretsFile, RotateKeys, EncryptYAMLFile). RotateKeys was switched to secureperm.WriteFile (mode 0o600) earlier in this PR; the other two still used os.WriteFile with 0o644, which made the on-disk mode depend on which code path last touched the file. Switch both to secureperm.WriteFile so every code path that writes encrypted secrets material agrees on the same defense-in-depth permission. Same rationale as the existing 0o600 on talm.key itself. Tests pin the mode invariant for all three paths: - EncryptSecretsFile_Mode0600 (new) - EncryptYAMLFile_Mode0600 (new) - RotateKeys_BothFilesMode0600 (existing) All three skip on Windows because NTFS does not honour Unix permission bits; secureperm has a Windows-side DACL test that covers the equivalent contract. Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent db2236d commit ef886b6

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎pkg/age/age.go‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,13 @@ func EncryptSecretsFile(rootDir string) error {
227227
return fmt.Errorf("failed to marshal encrypted secrets: %w", err)
228228
}
229229

230-
// Write encrypted file
231-
if err := os.WriteFile(encryptedFile, encryptedData, 0o644); err != nil {
230+
// Write encrypted file via secureperm.WriteFile so it lands at
231+
// mode 0o600 (defense-in-depth — age encryption is the security
232+
// layer, but world-readable secrets material on shared
233+
// workstations invites mistakes). RotateKeys uses the same
234+
// helper for the same file, keeping the on-disk mode invariant
235+
// across every code path that writes secrets.encrypted.yaml.
236+
if err := secureperm.WriteFile(encryptedFile, encryptedData); err != nil {
232237
return fmt.Errorf("failed to write encrypted file: %w", err)
233238
}
234239

@@ -713,8 +718,11 @@ func EncryptYAMLFile(rootDir, plainFile, encryptedFile string) error {
713718
return fmt.Errorf("failed to marshal encrypted YAML: %w", err)
714719
}
715720

716-
// Write encrypted file
717-
if err := os.WriteFile(encryptedFilePath, encryptedData, 0o644); err != nil {
721+
// Write encrypted file via secureperm.WriteFile (mode 0o600).
722+
// Same defense-in-depth rationale as EncryptSecretsFile and
723+
// RotateKeys — every code path that writes encrypted secrets
724+
// material agrees on the same on-disk permission.
725+
if err := secureperm.WriteFile(encryptedFilePath, encryptedData); err != nil {
718726
return fmt.Errorf("failed to write encrypted file: %w", err)
719727
}
720728

‎pkg/age/contract_test.go‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,55 @@ func TestContract_Age_RotateKeys_BothFilesMode0600(t *testing.T) {
483483
}
484484
}
485485

486+
// Contract: EncryptSecretsFile produces secrets.encrypted.yaml at
487+
// mode 0o600. Pinning so all three code paths that write the same
488+
// file (this function, RotateKeys, EncryptYAMLFile) agree on the
489+
// same defense-in-depth permission. Skipped on Windows for the
490+
// same NTFS reason as BothFilesMode0600.
491+
func TestContract_Age_EncryptSecretsFile_Mode0600(t *testing.T) {
492+
if runtime.GOOS == "windows" {
493+
t.Skip("Unix permission bits are not honoured on NTFS; secureperm has a Windows-side DACL test that covers the equivalent contract")
494+
}
495+
dir := t.TempDir()
496+
if err := os.WriteFile(filepath.Join(dir, "secrets.yaml"), []byte("secret: x\n"), 0o600); err != nil {
497+
t.Fatal(err)
498+
}
499+
if err := age.EncryptSecretsFile(dir); err != nil {
500+
t.Fatal(err)
501+
}
502+
info, err := os.Stat(filepath.Join(dir, "secrets.encrypted.yaml"))
503+
if err != nil {
504+
t.Fatal(err)
505+
}
506+
if got := info.Mode().Perm(); got != 0o600 {
507+
t.Errorf("secrets.encrypted.yaml mode = %o, want 0600", got)
508+
}
509+
}
510+
511+
// Contract: EncryptYAMLFile produces its target encrypted file at
512+
// mode 0o600. Same defense-in-depth contract as
513+
// EncryptSecretsFile — the function is the generic kubeconfig /
514+
// arbitrary-YAML variant of the secrets-encrypt path.
515+
func TestContract_Age_EncryptYAMLFile_Mode0600(t *testing.T) {
516+
if runtime.GOOS == "windows" {
517+
t.Skip("Unix permission bits are not honoured on NTFS; secureperm has a Windows-side DACL test that covers the equivalent contract")
518+
}
519+
dir := t.TempDir()
520+
if err := os.WriteFile(filepath.Join(dir, "kubeconfig.yaml"), []byte("k: v\n"), 0o600); err != nil {
521+
t.Fatal(err)
522+
}
523+
if err := age.EncryptYAMLFile(dir, "kubeconfig.yaml", "kubeconfig.encrypted.yaml"); err != nil {
524+
t.Fatal(err)
525+
}
526+
info, err := os.Stat(filepath.Join(dir, "kubeconfig.encrypted.yaml"))
527+
if err != nil {
528+
t.Fatal(err)
529+
}
530+
if got := info.Mode().Perm(); got != 0o600 {
531+
t.Errorf("kubeconfig.encrypted.yaml mode = %o, want 0600", got)
532+
}
533+
}
534+
486535
// Contract: a successful rotation leaves NO `*.rotation-backup`
487536
// files in the project root. Backups are an in-progress recovery
488537
// artefact only; their presence after the function returns nil

0 commit comments

Comments
 (0)