Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(config): write endpoint config atomically (#856)

Rudy Mizrahi Celekli7c0c38086e90
diff --git a/cli/beacon/cmd/endpoint_diagnostics.go b/cli/beacon/cmd/endpoint_diagnostics.go
index 8ce76642..06b21506 100644
--- a/cli/beacon/cmd/endpoint_diagnostics.go
+++ b/cli/beacon/cmd/endpoint_diagnostics.go
@@ -15,6 +15,7 @@ import (
"github.com/spf13/cobra"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/atomicfile"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/dshsession"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/embedded"
@@ -1732,7 +1733,7 @@ func restoreRepairFile(path string, snapshot repairFileSnapshot) error {
if mode == 0 {
mode = 0600
}
- return os.WriteFile(path, snapshot.data, mode)
+ return atomicfile.Write(path, snapshot.data, mode)
}
func checkLogWritable(cfg endpointconfig.Config) diagnostics.Check {
diff --git a/cli/beacon/cmd/endpoint_update_service.go b/cli/beacon/cmd/endpoint_update_service.go
index 4ce09231..835c285d 100644
--- a/cli/beacon/cmd/endpoint_update_service.go
+++ b/cli/beacon/cmd/endpoint_update_service.go
@@ -12,6 +12,7 @@ import (
"github.com/spf13/cobra"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/atomicfile"
endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/lifecycle"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/selfupdate"
@@ -95,7 +96,7 @@ func setConfigAutoUpdateModeAt(path, mode string) error {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
- return os.WriteFile(path, data, 0644)
+ return atomicfile.Write(path, data, 0644)
}
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
@@ -114,7 +115,7 @@ func setConfigAutoUpdateModeAt(path, mode string) error {
if err != nil {
return err
}
- return os.WriteFile(path, out, perm)
+ return atomicfile.Write(path, out, perm)
}
func requireRootForUpdater() error {
diff --git a/cli/beacon/internal/atomicfile/atomicfile.go b/cli/beacon/internal/atomicfile/atomicfile.go
new file mode 100644
index 00000000..905ab511
--- /dev/null
+++ b/cli/beacon/internal/atomicfile/atomicfile.go
@@ -0,0 +1,38 @@
+// Package atomicfile replaces files so readers never observe partial content.
+package atomicfile
+
+import (
+ "os"
+ "path/filepath"
+)
+
+// Write writes via a temp file in the same directory and renames it over path, so a
+// concurrent reader or a crash never sees a half-written file. The mode is set before
+// the rename so the content is never readable with wider permissions.
+func Write(path string, data []byte, mode os.FileMode) error {
+ tmp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*")
+ if err != nil {
+ return err
+ }
+ tmpName := tmp.Name()
+ cleanup := func() { _ = os.Remove(tmpName) }
+ if err := tmp.Chmod(mode); err != nil {
+ tmp.Close()
+ cleanup()
+ return err
+ }
+ if _, err := tmp.Write(data); err != nil {
+ tmp.Close()
+ cleanup()
+ return err
+ }
+ if err := tmp.Close(); err != nil {
+ cleanup()
+ return err
+ }
+ if err := os.Rename(tmpName, path); err != nil {
+ cleanup()
+ return err
+ }
+ return nil
+}
diff --git a/cli/beacon/internal/endpoint/asymptote/backfill.go b/cli/beacon/internal/endpoint/asymptote/backfill.go
index f65e30c2..343e7242 100644
--- a/cli/beacon/internal/endpoint/asymptote/backfill.go
+++ b/cli/beacon/internal/endpoint/asymptote/backfill.go
@@ -9,6 +9,7 @@ import (
"path/filepath"
"time"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/atomicfile"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/siempack"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"
@@ -93,7 +94,7 @@ func StageBackfill(logPath string, opts BackfillOptions) (BackfillResult, error)
for _, line := range lines {
data = append(append(data, line...), '\n')
}
- if err := writeFileAtomic(result.Path, data, 0o600); err != nil {
+ if err := atomicfile.Write(result.Path, data, 0o600); err != nil {
return result, fmt.Errorf("stage the session backfill: %w", err)
}
result.Events = len(lines)
diff --git a/cli/beacon/internal/endpoint/asymptote/connect.go b/cli/beacon/internal/endpoint/asymptote/connect.go
index 8d4659be..e2cf7dcc 100644
--- a/cli/beacon/internal/endpoint/asymptote/connect.go
+++ b/cli/beacon/internal/endpoint/asymptote/connect.go
@@ -10,6 +10,7 @@ import (
"strings"
"time"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/atomicfile"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/auth"
endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/service"
@@ -304,7 +305,7 @@ func Connect(ctx context.Context, opts ConnectOptions) (_ *ConnectResult, err er
return nil, fmt.Errorf("could not remove the staged session backfill: %w", err)
}
}
- if err := writeFileAtomic(configPath, []byte(rendered), 0o644); err != nil {
+ if err := atomicfile.Write(configPath, []byte(rendered), 0o644); err != nil {
return nil, err
}
firstConnectForwarderTouched = !reconnect
diff --git a/cli/beacon/internal/endpoint/asymptote/enrollment.go b/cli/beacon/internal/endpoint/asymptote/enrollment.go
index e009bf73..e88b63fc 100644
--- a/cli/beacon/internal/endpoint/asymptote/enrollment.go
+++ b/cli/beacon/internal/endpoint/asymptote/enrollment.go
@@ -9,6 +9,7 @@ import (
"strings"
"time"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/atomicfile"
endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"
)
@@ -66,7 +67,7 @@ func markConnectPending(userMode bool, at time.Time, reconnect bool) error {
if err != nil {
return err
}
- return writeFileAtomic(ConnectPendingPath(userMode), append(data, '\n'), 0o600)
+ return atomicfile.Write(ConnectPendingPath(userMode), append(data, '\n'), 0o600)
}
func loadConnectPending(userMode bool) (connectPending, error) {
@@ -103,7 +104,7 @@ func WriteInstallID(userMode bool, installID string) error {
if err := ensureDir(userMode); err != nil {
return err
}
- return writeFileAtomic(InstallIDPath(userMode), []byte(installID+"\n"), 0o600)
+ return atomicfile.Write(InstallIDPath(userMode), []byte(installID+"\n"), 0o600)
}
// Connected reports whether this endpoint is enrolled and has its forwarder configured: both
@@ -170,7 +171,7 @@ func SaveEnrollment(userMode bool, enrollment Enrollment) error {
if err != nil {
return err
}
- return writeFileAtomic(EnrollmentPath(userMode), append(data, '\n'), 0o600)
+ return atomicfile.Write(EnrollmentPath(userMode), append(data, '\n'), 0o600)
}
// WriteSecrets stores the device key in the file Vector's secret backend reads.
@@ -181,7 +182,7 @@ func WriteSecrets(userMode bool, deviceKey string) error {
if err := ensureDir(userMode); err != nil {
return err
}
- return writeFileAtomic(SecretsPath(userMode), []byte(SecretsFileContent(deviceKey)), 0o600)
+ return atomicfile.Write(SecretsPath(userMode), []byte(SecretsFileContent(deviceKey)), 0o600)
}
// ReadDeviceKey returns the stored device key, for the credential check.
@@ -232,34 +233,3 @@ func ensureDir(userMode bool) error {
// by an older tool or by hand.
return os.Chmod(dir, 0o700)
}
-
-// writeFileAtomic writes via a temp file in the same directory so a crash never leaves a
-// half-written credential, and sets the mode before the rename so the content is never
-// readable with wider permissions.
-func writeFileAtomic(path string, data []byte, mode os.FileMode) error {
- tmp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*")
- if err != nil {
- return err
- }
- tmpName := tmp.Name()
- cleanup := func() { _ = os.Remove(tmpName) }
- if err := tmp.Chmod(mode); err != nil {
- tmp.Close()
- cleanup()
- return err
- }
- if _, err := tmp.Write(data); err != nil {
- tmp.Close()
- cleanup()
- return err
- }
- if err := tmp.Close(); err != nil {
- cleanup()
- return err
- }
- if err := os.Rename(tmpName, path); err != nil {
- cleanup()
- return err
- }
- return nil
-}
diff --git a/cli/beacon/internal/endpoint/asymptote/reconcile.go b/cli/beacon/internal/endpoint/asymptote/reconcile.go
index 495602cb..8b486cd6 100644
--- a/cli/beacon/internal/endpoint/asymptote/reconcile.go
+++ b/cli/beacon/internal/endpoint/asymptote/reconcile.go
@@ -5,6 +5,7 @@ import (
"fmt"
"os"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/atomicfile"
endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/service"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/managedprivacy"
@@ -175,7 +176,7 @@ func ReconcileForwarder(opts ReconcileOptions) (ReconcileResult, error) {
result.Warning = fmt.Sprintf("kept the existing config because Vector rejected the new one: %v", err)
return result, nil
}
- if err := writeFileAtomic(configPath, []byte(rendered), 0o644); err != nil {
+ if err := atomicfile.Write(configPath, []byte(rendered), 0o644); err != nil {
return result, fmt.Errorf("could not write the validated forwarder config %s: %w", configPath, err)
}
result.Updated = true
diff --git a/cli/beacon/internal/endpoint/config/config.go b/cli/beacon/internal/endpoint/config/config.go
index 27f6d62b..9da35171 100644
--- a/cli/beacon/internal/endpoint/config/config.go
+++ b/cli/beacon/internal/endpoint/config/config.go
@@ -7,6 +7,8 @@ import (
"path/filepath"
"runtime"
"strings"
+
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/atomicfile"
)
const (
@@ -369,12 +371,9 @@ func Save(cfg Config) (string, error) {
if HasSecretDestinations(cfg) {
perm = 0600
}
- if err := os.WriteFile(path, data, perm); err != nil {
+ if err := atomicfile.Write(path, data, perm); err != nil {
return "", err
}
- if HasSecretDestinations(cfg) {
- return path, os.Chmod(path, perm)
- }
return path, nil
}
diff --git a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
index c05ef4a5..56988b70 100644
--- a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
+++ b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
@@ -8,6 +8,7 @@ import (
"strings"
"time"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/atomicfile"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/brewpath"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/asymptote"
endpointcollector "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/collector"
@@ -844,7 +845,7 @@ func setAutoUpdateModeInConfigFile(path, mode string) error {
if info, err := os.Stat(path); err == nil {
perm = info.Mode().Perm()
}
- return os.WriteFile(path, out, perm)
+ return atomicfile.Write(path, out, perm)
}
func GetStatus(userMode bool, logPath string) Status {
@@ -1084,7 +1085,7 @@ func restoreFile(path string, snapshot fileSnapshot) {
if mode == 0 {
mode = 0600
}
- _ = os.WriteFile(path, snapshot.Data, mode)
+ _ = atomicfile.Write(path, snapshot.Data, mode)
}
func preflight(cfg endpointconfig.Config, startService bool, kind service.Kind) error {