Skip to content

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

asymptoteobserve: settle archives toward the log's mode in both directions

Claudebd0d5bd89e23
diff --git a/cli/beacon-hooks/internal/logging/logging.go b/cli/beacon-hooks/internal/logging/logging.go
index 0c5440b3..58161bbf 100644
--- a/cli/beacon-hooks/internal/logging/logging.go
+++ b/cli/beacon-hooks/internal/logging/logging.go
@@ -727,7 +727,7 @@ func rotateEndpointLogIfNeeded(path string, maxSize int64, archives int, nextWri
if err := os.Rename(path, path+".1"); err != nil {
return err
}
- asymptoteobserve.TightenRuntimeLogArchives(path, archives)
+ asymptoteobserve.SettleRuntimeLogArchives(path, archives)
return nil
}
diff --git a/cli/beacon/internal/endpoint/writer/writer.go b/cli/beacon/internal/endpoint/writer/writer.go
index 6d6c9519..4b169aba 100644
--- a/cli/beacon/internal/endpoint/writer/writer.go
+++ b/cli/beacon/internal/endpoint/writer/writer.go
@@ -371,7 +371,7 @@ func rotateIfNeeded(path string, maxSize int64, archives int, nextWriteBytes int
if err := os.Rename(path, rotated); err != nil {
return err
}
- asymptoteobserve.TightenRuntimeLogArchives(path, archives)
+ asymptoteobserve.SettleRuntimeLogArchives(path, archives)
return nil
}
diff --git a/collector-builder/exporter/beaconjsonexporter/writer.go b/collector-builder/exporter/beaconjsonexporter/writer.go
index aadc0b2f..2e84977b 100644
--- a/collector-builder/exporter/beaconjsonexporter/writer.go
+++ b/collector-builder/exporter/beaconjsonexporter/writer.go
@@ -283,7 +283,7 @@ func rotateIfNeeded(path string, maxSize int64, archives int, nextWriteBytes int
if err := os.Rename(path, path+".1"); err != nil {
return err
}
- asymptoteobserve.TightenRuntimeLogArchives(path, archives)
+ asymptoteobserve.SettleRuntimeLogArchives(path, archives)
return nil
}
diff --git a/pkg/asymptoteobserve/runtimelogaccess_unix_test.go b/pkg/asymptoteobserve/runtimelogaccess_unix_test.go
index 2bba9a81..191ff738 100644
--- a/pkg/asymptoteobserve/runtimelogaccess_unix_test.go
+++ b/pkg/asymptoteobserve/runtimelogaccess_unix_test.go
@@ -300,7 +300,7 @@ func TestTightenRestrictedRuntimeLogArchives(t *testing.T) {
}
}
writeArchives()
- TightenRuntimeLogArchives(log, 3)
+ SettleRuntimeLogArchives(log, 3)
for i := 1; i <= 3; i++ {
if info, _ := os.Stat(fmt.Sprintf("%s.%d", log, i)); info.Mode().Perm() != 0o666 {
t.Fatalf("a shared log's archive %d was changed to %o", i, info.Mode().Perm())
@@ -310,7 +310,7 @@ func TestTightenRestrictedRuntimeLogArchives(t *testing.T) {
if err := SetRuntimeLogAccess(log, LogAccessRestricted); err != nil {
t.Fatal(err)
}
- TightenRuntimeLogArchives(log, 3)
+ SettleRuntimeLogArchives(log, 3)
for i := 1; i <= 3; i++ {
if info, _ := os.Stat(fmt.Sprintf("%s.%d", log, i)); info.Mode().Perm() != RestrictedRuntimeLogFileMode {
t.Fatalf("restricted archive %d mode = %o", i, info.Mode().Perm())
@@ -361,3 +361,29 @@ func TestARestrictedLogsLockIsRootsAlone(t *testing.T) {
t.Fatalf("archive mode = %o", got)
}
}
+
+// Back to shared, archives rotated while the log was restricted are readable again.
+func TestSettleRuntimeLogArchivesWidensThemWhenTheLogIsSharedAgain(t *testing.T) {
+ trustMarkersFrom(t, os.Geteuid())
+ log := systemLog(t)
+ mkLogDir(t, log)
+ if err := SetRuntimeLogAccess(log, LogAccessRestricted); err != nil {
+ t.Fatal(err)
+ }
+ for i := 1; i <= 2; i++ {
+ path := fmt.Sprintf("%s.%d", log, i)
+ if err := os.WriteFile(path, nil, 0o640); err != nil {
+ t.Fatal(err)
+ }
+ _ = os.Chmod(path, 0o640)
+ }
+ if err := SetRuntimeLogAccess(log, LogAccessShared); err != nil {
+ t.Fatal(err)
+ }
+ SettleRuntimeLogArchives(log, 2)
+ for i := 1; i <= 2; i++ {
+ if info, _ := os.Stat(fmt.Sprintf("%s.%d", log, i)); info.Mode().Perm() != SharedRuntimeLogFileMode {
+ t.Fatalf("archive %d mode = %o after the log went back to shared", i, info.Mode().Perm())
+ }
+ }
+}
diff --git a/pkg/asymptoteobserve/runtimelogmode.go b/pkg/asymptoteobserve/runtimelogmode.go
index 3093d3f7..b7814795 100644
--- a/pkg/asymptoteobserve/runtimelogmode.go
+++ b/pkg/asymptoteobserve/runtimelogmode.go
@@ -211,15 +211,14 @@ func settleRuntimeLogFileMode(path, logPath string, created bool, stat func(stri
return nil
}
-// TightenRuntimeLogArchives narrows the rotated archives of a private or restricted log (path.1 ..
-// path.archives) the way SettleRuntimeLogFileMode narrows the live file. Archives rotated before
-// the log was private or restricted still carry the old shared mode, and they hold the same
-// content as the live file. Best effort, like SettleRuntimeLogFileMode on an existing file; a
-// shared log's archives are left alone.
-func TightenRuntimeLogArchives(path string, archives int) {
- if !RuntimeLogIsPrivate(path) && !RuntimeLogAccessRestricted(path) {
- return
- }
+// SettleRuntimeLogArchives brings the rotated archives of a log (path.1 .. path.archives) to the
+// mode the log calls for, the way SettleRuntimeLogFileMode settles the live file, in both
+// directions: archives rotated before the log was private or restricted still carry the shared
+// mode and the same content as the live file, and archives rotated while a system log was
+// restricted must be readable again once it is shared, or every reader of the log's history stops
+// at them. Best effort, like SettleRuntimeLogFileMode on an existing file: only a file this process
+// owns is changed, and a link is never followed.
+func SettleRuntimeLogArchives(path string, archives int) {
for i := 1; i <= archives; i++ {
archive := fmt.Sprintf("%s.%d", path, i)
info, err := os.Lstat(archive)
diff --git a/pkg/asymptoteobserve/runtimelogmode_test.go b/pkg/asymptoteobserve/runtimelogmode_test.go
index 239025a5..14302c04 100644
--- a/pkg/asymptoteobserve/runtimelogmode_test.go
+++ b/pkg/asymptoteobserve/runtimelogmode_test.go
@@ -58,7 +58,7 @@ func TestSystemLogIsNeverPrivate(t *testing.T) {
}
}
-func TestTightenRuntimeLogArchives(t *testing.T) {
+func TestSettleRuntimeLogArchives(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Unix permission bits do not exist on Windows")
}
@@ -76,7 +76,7 @@ func TestTightenRuntimeLogArchives(t *testing.T) {
}
writeWorldWritable(path + ".1")
writeWorldWritable(path + ".2")
- TightenRuntimeLogArchives(path, 5)
+ SettleRuntimeLogArchives(path, 5)
for _, archive := range []string{path + ".1", path + ".2"} {
info, err := os.Stat(archive)
if err != nil {
@@ -90,7 +90,7 @@ func TestTightenRuntimeLogArchives(t *testing.T) {
// A shared log's archives are left alone.
shared := filepath.Join(t.TempDir(), "runtime.jsonl")
writeWorldWritable(shared + ".1")
- TightenRuntimeLogArchives(shared, 5)
+ SettleRuntimeLogArchives(shared, 5)
info, err := os.Stat(shared + ".1")
if err != nil {
t.Fatal(err)