secureperm

package
v0.25.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package secureperm writes and locks down files that hold secrets (age private keys, decrypted secrets.yaml, talosconfig, kubeconfig, rendered Talos machine configs) so that only the file owner can read them.

WriteFile is atomic on both platforms via write-to-tmp + rename: it creates a hidden sibling tmp file in the same directory with the final permissions already in place, writes the bytes, then renames the tmp over the target. os.Rename is atomic on POSIX and on NTFS when source and destination share a filesystem (which they do by construction here). On any pre-rename failure the tmp is removed and the destination is left in its prior state — important because secrets are not reconstructible (a corrupted secrets.yaml forces a cluster PKI reissue).

On Unix the tmp is created via os.CreateTemp (O_CREATE|O_EXCL|O_RDWR with mode 0o600 by construction) plus an explicit Chmod so the contract survives any future stdlib change.

On Windows os.Chmod does not translate to NTFS DACLs — files ordinarily inherit ACLs from their parent, which may leave secrets readable by non-owner principals such as BUILTIN\Users. The tmp is created via CreateFile with CREATE_NEW and a SECURITY_ATTRIBUTES descriptor carrying a protected owner-only DACL. CREATE_NEW is the key: per MSDN, CreateFile only honors the lpSecurityDescriptor argument when the file is newly created, not when it opens an existing file — so the retry loop picks a name that does not exist, guaranteeing the DACL actually lands. The rename step then overwrites the destination with the tmp, carrying the tmp's owner-only DACL over in place of whatever permissive inherited DACL the destination held. The bytes never exist on disk under a lax DACL, and because the tmp handle is opened with share mode 0 no other process can open it between creation and rename.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LockDown

func LockDown(path string) error

LockDown narrows an existing file's permissions to 0o600.

func WriteFile

func WriteFile(path string, data []byte) error

WriteFile writes data to path atomically with mode 0o600.

Atomic in the sense that either the write fully succeeds and path references the new content, or it fails and path is left in its prior state — secrets files are not reconstructible (losing secrets.yaml means reissuing cluster PKI), so the helper must not destroy the existing file if the write can't complete.

Strategy: create a hidden sibling tmp file in the same directory via os.CreateTemp (which uses O_CREATE|O_EXCL|O_RDWR with mode 0o600, so the tmp is already owner-only), write the bytes, then rename over the target. Rename is atomic on POSIX when both paths live on the same filesystem, which they do by construction.

Ownership note: tmp + rename produces a file owned by the calling process's uid/gid, which differs from os.WriteFile's open-with- O_TRUNC behaviour where the existing inode's owner is preserved. Running talm under a different uid than a previous invocation (e.g. once via sudo, then as the unprivileged user) will therefore change ownership on the secrets file. The single-user workstation flow this helper targets is unaffected; mixed-uid setups should invoke talm under a consistent identity.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL