Files
Pulse/internal/api/auth_snapshot.go
pulse-triage[bot] 6452506880 Preserve auth inventories with incomplete credential records
Validate required identities across each session, CSRF and recovery inventory before admitting rows or publishing a legacy migration. Route null or unusable credential records through the existing private preservation path rather than silently replacing their evidence on a later save. Keep complete empty formats, ordinary expiry filtering and legacy key/field fallback precedence.

Exercise real initializers, mixed row order, private byte/inode retention, fresh independent grants, durable withdrawal and recovery consumption, storage failure/recovery and complete-format compatibility. Retain every existing save/shutdown and nonregular-file boundary.

Contract-Neutral: Apply existing unknown-inventory preservation and fail-closed admission to incomplete credential identities without changing API/rendered payloads, grants, storage formats, token lifetimes or agent lifecycle. Update the security-privacy guarantee and executable controls together.
Change-source: pulse-maintainer
2026-11-16 22:18:38 +00:00

73 lines
2.6 KiB
Go

package api
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
var errIncompleteAuthInventory = errors.New("auth inventory contains an incomplete credential record")
// authSnapshotWriteFunc keeps the actual descriptor write independently
// exercisable by per-store fixtures, without replacing snapshot publication or
// sharing a mutable global hook. Runtime stores use the default file.Write.
type authSnapshotWriteFunc func(*os.File, []byte) (int, error)
// readAuthSnapshot refuses nonregular targets before opening them: ReadFile on
// a named pipe can wait forever before the store's failed-read handling runs.
// Follow links as ReadFile did, so existing links to regular inventories keep
// working. This is a startup check, not protection against concurrent path
// replacement or a timeout for an unavailable filesystem.
func readAuthSnapshot(path string) ([]byte, error) {
info, err := os.Stat(path)
if err != nil {
return nil, err
}
if !info.Mode().IsRegular() {
return nil, fmt.Errorf("auth inventory is not a regular file: %s", path)
}
return os.ReadFile(path)
}
// writePrivateAuthSnapshot publishes already-encoded authority using a new
// owner-private regular file. Never reuse path+".tmp": WriteFile would follow a
// stale symlink or keep an old file's wider mode. The caller owns serialization
// and directory preparation; this helper owns only its exclusive descriptor and
// temporary pathname. It does not promise directory-fsync/power-loss durability.
func writePrivateAuthSnapshot(path string, data []byte, write authSnapshotWriteFunc) (retErr error) {
file, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*.tmp")
if err != nil {
return fmt.Errorf("create private snapshot: %w", err)
}
closed, published := false, false
defer func() {
if !closed {
retErr = errors.Join(retErr, file.Close())
}
if !published {
if err := os.Remove(file.Name()); err != nil && !os.IsNotExist(err) {
retErr = errors.Join(retErr, fmt.Errorf("clean up private snapshot: %w", err))
}
}
}()
if write == nil {
write = func(file *os.File, data []byte) (int, error) { return file.Write(data) }
}
if n, err := write(file, data); err != nil {
return fmt.Errorf("write private snapshot: %w", err)
} else if n != len(data) {
return fmt.Errorf("write private snapshot: %w", io.ErrShortWrite)
}
closed = true
if err := file.Close(); err != nil {
return fmt.Errorf("close private snapshot: %w", err)
}
if err := os.Rename(file.Name(), path); err != nil {
return fmt.Errorf("publish private snapshot: %w", err)
}
published = true
return nil
}