agent (unreleased): a cut write of agent.json cannot leave a blind box — a journal of the old bytes, a repair at start (R-1380)
gates / gates (push) Successful in 42s

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
admin committed 2026-11-15 08:20:49 +01:00
1 parent 54ac2c2ac9
commit db01ae0f2a
12 files changed
+582 -66

No files matched your search

+141
View File
@@ -0,0 +1,141 @@
package main
import (
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"testing"
"gitea.dooplex.hu/admin/felhom-agent/internal/config"
)
// A box whose agent.json was cut in the middle of a write must start (R-1380): main repairs the file from the
// journal BEFORE it reads it. The staging is the state a power cut leaves: a durable journal of the old bytes and a
// half-written config.
func TestMainRecoversACutConfigWrite(t *testing.T) {
dir := t.TempDir()
prev := config.CutWriteDir
config.CutWriteDir = filepath.Join(dir, "state")
t.Cleanup(func() { config.CutWriteDir = prev })
if err := os.MkdirAll(config.CutWriteDir, 0o700); err != nil {
t.Fatal(err)
}
cfgPath := filepath.Join(dir, "agent.json")
old := `{"proxmox":{"node":"pve","tls":{"fingerprint":"AA:BB"}}}`
if err := os.WriteFile(filepath.Join(config.CutWriteDir, "agent.json.write-in-flight"), []byte(old), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(cfgPath, []byte(`{"proxmox":{"node":"pv`), 0o600); err != nil {
t.Fatal(err)
}
if _, err := config.Load(cfgPath); err == nil {
t.Fatal("the staging is wrong: the cut config loads")
}
note, err := recoverCutConfigWrite(cfgPath)
if err != nil || note == "" {
t.Fatalf("note=%q err=%v", note, err)
}
cfg, err := config.Load(cfgPath)
if err != nil {
t.Fatalf("the agent does not start after the repair: %v", err)
}
if cfg.Proxmox.TLS.Fingerprint != "AA:BB" {
t.Fatalf("the repaired config lost the fingerprint: %+v", cfg.Proxmox.TLS)
}
}
// main must call the repair before config.Load, or the repair never runs on the box that needs it (a seam that is
// built and not wired). Read from the source: the call's position in main's body.
func TestMainCallsTheConfigRepairBeforeItLoadsTheConfig(t *testing.T) {
src, err := os.ReadFile("main.go")
if err != nil {
t.Fatal(err)
}
f, err := parser.ParseFile(token.NewFileSet(), "main.go", src, 0)
if err != nil {
t.Fatal(err)
}
var repair, load token.Pos
for _, d := range f.Decls {
fn, ok := d.(*ast.FuncDecl)
if !ok || fn.Name.Name != "main" || fn.Body == nil {
continue
}
ast.Inspect(fn.Body, func(n ast.Node) bool {
c, ok := n.(*ast.CallExpr)
if !ok {
return true
}
if id, ok := c.Fun.(*ast.Ident); ok && id.Name == "recoverCutConfigWrite" && repair == 0 {
repair = c.Pos()
}
if sel, ok := c.Fun.(*ast.SelectorExpr); ok && sel.Sel.Name == "Load" {
if pkg, ok := sel.X.(*ast.Ident); ok && pkg.Name == "config" && load == 0 {
load = c.Pos()
}
}
return true
})
}
if repair == 0 || load == 0 {
t.Fatalf("main: repair call found=%v, config.Load found=%v", repair != 0, load != 0)
}
if repair > load {
t.Fatal("main reads the config BEFORE it repairs a cut write — a cut config stops the agent at start")
}
}
// Every writer of agent.json goes through config.ReplaceInPlace and none truncates the file itself: a writer that
// opens the config with O_TRUNC by hand is the unprotected write again.
func TestAgentJSONWritersUseTheJournalledWrite(t *testing.T) {
for _, w := range []struct{ file, fn string }{
{"../../internal/pvetrust/pvetrust.go", "writeConfig"},
{"../../internal/pbsdr/manager.go", "seedEscrowStorageID"},
{"../../internal/localapi/backup_target.go", "setConfiguredBackupTarget"},
} {
src, err := os.ReadFile(w.file)
if err != nil {
t.Fatal(err)
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, w.file, src, 0)
if err != nil {
t.Fatal(err)
}
found := false
for _, d := range f.Decls {
fn, ok := d.(*ast.FuncDecl)
if !ok || fn.Name.Name != w.fn || fn.Body == nil {
continue
}
found = true
body := string(src[fset.Position(fn.Body.Pos()).Offset:fset.Position(fn.Body.End()).Offset])
calls := 0
ast.Inspect(fn.Body, func(n ast.Node) bool {
if c, ok := n.(*ast.CallExpr); ok {
if sel, ok := c.Fun.(*ast.SelectorExpr); ok && sel.Sel.Name == "ReplaceInPlace" {
if pkg, ok := sel.X.(*ast.Ident); ok && pkg.Name == "config" {
calls++
}
}
if sel, ok := c.Fun.(*ast.SelectorExpr); ok && sel.Sel.Name == "OpenFile" {
t.Errorf("%s %s opens a file by hand — the config is written only by config.ReplaceInPlace", w.file, w.fn)
}
}
return true
})
if calls != 1 {
t.Errorf("%s %s: %d call(s) to config.ReplaceInPlace, want 1", w.file, w.fn, calls)
}
if strings.Contains(body, "O_TRUNC") {
t.Errorf("%s %s truncates a file itself", w.file, w.fn)
}
}
if !found {
t.Errorf("%s: function %s not found — the pin reads nothing", w.file, w.fn)
}
}
}
+24
View File
@@ -217,6 +217,15 @@ func main() {
return
}
// Only the daemon repairs: a selftest can run beside a live agent that is in the middle of a write.
var cutWriteNote string
var cutWriteErr error
if selftest.mode == "" {
cutWriteNote, cutWriteErr = recoverCutConfigWrite(cfgPath)
if cutWriteErr != nil {
fmt.Fprintln(os.Stderr, "config repair:", cutWriteErr)
}
}
cfg, err := config.Load(cfgPath)
if err != nil {
// A missing default config file is fine if env provides the values; only a
@@ -230,6 +239,11 @@ func main() {
// logRing is the always-DEBUG capture ring (v0.83.0 observability): served by the
// local API's GET /debug/logs and the heartbeat log-pull. Selftests ignore it.
logger, logRing := applog.New(cfg.LogLevel)
if cutWriteErr != nil {
logger.Error("config: the last write of the config was cut and could not be repaired", "err", cutWriteErr)
} else if cutWriteNote != "" {
logger.Warn("config: a write of the config was cut by a stop — repaired at start", "result", cutWriteNote)
}
switch selftest.mode {
case "":
@@ -4246,3 +4260,13 @@ func agentStateDir(cfg config.Config) string {
func wireBundlePolicyAfterCommit(m *selfupdate.Manager, e osupdate.ConfigUpdateExecutor, lg *slog.Logger, ver string) {
m.SetOnCommitted(func(ctx context.Context) { e.PolicyPassAfterUpdate(ctx, lg, ver) })
}
// recoverCutConfigWrite repairs agent.json BEFORE it is read, when the last write of it was cut by a power loss
// (config.RecoverCutWrite; R-1380). It runs before the logger exists, so main logs what it returns.
// Pinned by TestMainRecoversACutConfigWrite.
func recoverCutConfigWrite(cfgPath string) (string, error) {
if cfgPath == "" {
return "", nil
}
return config.RecoverCutWrite(cfgPath)
}