Files
Pulse/internal/api/auth_env_path.go
rcourtman c5b6d05107 feat(telemetry): add schema v26 map topology API/CLI/MCP telemetry
I added Schema v26 content-free Infrastructure Map topology read telemetry for external live GET /api/agent/topology requests (total read days, CLI days, MCP days, anonymized days, and an explicit availability flag) alongside hardened opt-out zero-write boundaries, credential-binding checks, and read-only SQLite/config guards.

Key changes:
- Added a dedicated root-scoped daily tally store (infrastructure_map_topology_tally.json) with strict schema validation, fail-closed degraded sidecar accounting, and strict zero-write guarantees when telemetry is disabled by settings or PULSE_TELEMETRY=false.
- Added X-Pulse-Agent-Surface attribution for pulse-cli and MCP clients while filtering out first-party browser traffic, mock-mode snapshots, and unverified monitor states.
- Updated Settings payload preview, localization copy, shipped PRIVACY.md disclosure, integration specs, and schema parity tooling for Schema v26.
- Hardened host/agent token hash binding, self-uninstall live authority and durable hash-scoped tombstones, Patrol investigation replay-floor provenance, bounded non-blocking regular-file config reads, and cold read-only legacy SQLite history queries.
2026-12-02 01:41:25 +00:00

149 lines
4.8 KiB
Go

package api
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
)
func resolveAuthEnvPath(configPath string) string {
return filepath.Join(config.ResolveRuntimeDataDir(configPath), ".env")
}
func resolveAuthEnvWritePaths(configPath string, dataPath string) []string {
paths := []string{resolveAuthEnvPath(configPath)}
dataEnvPath := resolveAuthEnvPath(dataPath)
if dataEnvPath != paths[0] {
paths = append(paths, dataEnvPath)
}
return paths
}
// writeAuthEnvFile publishes complete owner-private credentials, never a partial
// in-place rewrite or a write through a canonical symlink/hard link. The configured
// directory may still be a symlink. Keep preferred/fallback path selection intact.
func writeAuthEnvFile(configPath string, dataPath string, content []byte) (string, error) {
paths := resolveAuthEnvWritePaths(configPath, dataPath)
var lastErr error
for _, envPath := range paths {
if err := os.MkdirAll(filepath.Dir(envPath), 0755); err != nil {
lastErr = err
continue
}
if err := writePrivateAuthSnapshot(envPath, content, nil); err != nil {
lastErr = err
continue
}
return envPath, nil
}
if lastErr == nil {
lastErr = fmt.Errorf("no auth env write path available")
}
return "", lastErr
}
// updateAuthEnvFile reads, transforms, and atomically publishes the exact .env
// path selected for writing while the caller holds config.LockAuthEnvFiles. Any
// read error other than os.ErrNotExist aborts immediately so unrelated .env
// settings are never replaced with an auth-only file after a transient failure.
const maxAuthEnvFileSizeBytes int64 = 1 << 20
func updateAuthEnvFile(configPath string, dataPath string, transform func(existing []byte, exists bool) ([]byte, error)) (string, error) {
paths := resolveAuthEnvWritePaths(configPath, dataPath)
var lastErr error
for _, envPath := range paths {
if err := os.MkdirAll(filepath.Dir(envPath), 0755); err != nil {
lastErr = err
continue
}
existing, readErr := config.ReadLimitedRegularFile(envPath, maxAuthEnvFileSizeBytes)
exists := true
if readErr != nil {
if !errors.Is(readErr, os.ErrNotExist) {
return "", fmt.Errorf("read auth environment file %s: %w", envPath, readErr)
}
exists = false
existing = nil
}
updated, err := transform(existing, exists)
if err != nil {
return "", err
}
if int64(len(updated)) > maxAuthEnvFileSizeBytes {
return "", fmt.Errorf("%w: updated auth environment file %s is %d bytes (limit %d)", config.ErrFileTooLarge, envPath, len(updated), maxAuthEnvFileSizeBytes)
}
if err := writePrivateAuthSnapshot(envPath, updated, nil); err != nil {
lastErr = err
continue
}
return envPath, nil
}
if lastErr == nil {
lastErr = fmt.Errorf("no auth env write path available")
}
return "", lastErr
}
func removeAuthEnvFiles(configPath string, dataPath string) error {
paths := resolveAuthEnvWritePaths(configPath, dataPath)
var lastErr error
for _, envPath := range paths {
if err := os.Remove(envPath); err != nil && !os.IsNotExist(err) {
lastErr = err
}
}
return lastErr
}
type authEnvFileSnapshot struct {
path string
data []byte
exists bool
}
func snapshotAuthEnvFiles(configPath string, dataPath string) ([]authEnvFileSnapshot, error) {
paths := resolveAuthEnvWritePaths(configPath, dataPath)
snapshots := make([]authEnvFileSnapshot, 0, len(paths))
for _, path := range paths {
data, err := config.ReadLimitedRegularFile(path, maxAuthEnvFileSizeBytes)
if errors.Is(err, os.ErrNotExist) {
snapshots = append(snapshots, authEnvFileSnapshot{path: path})
continue
}
if err != nil {
return nil, fmt.Errorf("read auth environment file %s: %w", path, err)
}
snapshots = append(snapshots, authEnvFileSnapshot{
path: path,
data: data,
exists: true,
})
}
return snapshots, nil
}
// A failed reset restores exact bytes with the same private publication boundary
// as a normal save, not the old inode or its potentially wider permissions.
func restoreAuthEnvFiles(snapshots []authEnvFileSnapshot) error {
var restoreErr error
for _, snapshot := range snapshots {
if !snapshot.exists {
if err := os.Remove(snapshot.path); err != nil && !errors.Is(err, os.ErrNotExist) {
restoreErr = errors.Join(restoreErr, fmt.Errorf("remove newly created auth environment file %s: %w", snapshot.path, err))
}
continue
}
if err := os.MkdirAll(filepath.Dir(snapshot.path), 0o755); err != nil {
restoreErr = errors.Join(restoreErr, fmt.Errorf("recreate auth environment directory for %s: %w", snapshot.path, err))
continue
}
if err := writePrivateAuthSnapshot(snapshot.path, snapshot.data, nil); err != nil {
restoreErr = errors.Join(restoreErr, fmt.Errorf("restore auth environment file %s: %w", snapshot.path, err))
}
}
return restoreErr
}