Преглед на файлове

fix(agent): pass UCI snapshots via stdin (#1618)

Import each UCI snapshot with uci import and the snapshot as stdin instead of interpolating it into sh -c. Quotes and command substitutions in UCI values now reach uci literally.

Closes #1617
Nacho преди 1 месец
родител
ревизия
82cc4e9996
променени са 2 файла, в които са добавени 29 реда и са изтрити 2 реда
  1. 18 2
      agent/executor/executor.go
  2. 11 0
      agent/executor/executor_test.go

+ 18 - 2
agent/executor/executor.go

@@ -39,6 +39,7 @@ type ApplyResult struct {
 // Runner ejecuta comandos en el router. Interfaz para inyectar fakes en tests.
 type Runner interface {
 	Run(name string, args ...string) (stdout string, exitCode int)
+	RunInput(input, name string, args ...string) (stdout string, exitCode int)
 }
 
 // shellRunner usa exec.Command en producción.
@@ -57,6 +58,21 @@ func (shellRunner) Run(name string, args ...string) (string, int) {
 	return string(out), code
 }
 
+func (shellRunner) RunInput(input, name string, args ...string) (string, int) {
+	cmd := exec.Command(name, args...)
+	cmd.Stdin = strings.NewReader(input)
+	out, err := cmd.Output()
+	code := 0
+	if err != nil {
+		if ee, ok := err.(*exec.ExitError); ok {
+			code = ee.ExitCode()
+		} else {
+			code = -1
+		}
+	}
+	return string(out), code
+}
+
 // patrones de validación por campo (sin shell metachars).
 var (
 	reConfig = regexp.MustCompile(`^[a-z_]+$`)
@@ -559,7 +575,7 @@ func (e *Executor) Apply(ops []Op) ApplyResult {
 				// Healthcheck del módulo: rollback real restaurando snapshots UCI
 				// y relanzando los servicios afectados.
 				for cfg, snap := range snapshots {
-					e.run.Run("sh", "-c", fmt.Sprintf("echo '%s' | uci import", snap))
+					e.run.RunInput(snap, "uci", "import", cfg)
 					e.run.Run("uci", "commit", cfg)
 				}
 				if op.Kind == "wg_check" {
@@ -597,7 +613,7 @@ func (e *Executor) Apply(ops []Op) ApplyResult {
 	if !e.healthcheck() {
 		// ROLLBACK: restaurar snapshots.
 		for cfg, snap := range snapshots {
-			e.run.Run("sh", "-c", fmt.Sprintf("echo '%s' | uci import", snap))
+			e.run.RunInput(snap, "uci", "import", cfg)
 			e.run.Run("uci", "commit", cfg)
 		}
 		return ApplyResult{Status: "rolled_back", Error: "healthcheck_failed", Snapshot: strings.Join(affected, ","), DurationMs: ms(e.now(), start)}

+ 11 - 0
agent/executor/executor_test.go

@@ -12,10 +12,16 @@ import (
 // fakeRunner registra los comandos ejecutados y devuelve respuestas canned.
 type fakeRunner struct {
 	calls     []string
+	inputs    []string
 	responses map[string]int    // prefix → exitCode (0 si no está)
 	outputs   map[string]string // prefix → stdout cuando exitCode es 0
 }
 
+func (f *fakeRunner) RunInput(input, name string, args ...string) (string, int) {
+	f.inputs = append(f.inputs, input)
+	return f.Run(name, args...)
+}
+
 func newFakeRunner() *fakeRunner {
 	return &fakeRunner{responses: map[string]int{}, outputs: map[string]string{}}
 }
@@ -464,6 +470,8 @@ func TestApplyFailingOpReverts(t *testing.T) {
 
 func TestApplyHealthcheckFailRollsBack(t *testing.T) {
 	fr := newFakeRunner()
+	snap := "config dhcp 'lan'\n\toption dns '8.8.8.8'\n\tlist domain '$(reboot)'"
+	fr.outputs["uci export"] = snap
 	// Hacer que el ping falle (healthcheck)
 	fr.responses["ping"] = 1
 
@@ -493,6 +501,9 @@ func TestApplyHealthcheckFailRollsBack(t *testing.T) {
 	if !foundImport {
 		t.Fatal("should have imported snapshot on rollback")
 	}
+	if len(fr.inputs) == 0 || fr.inputs[0] != snap {
+		t.Fatalf("snapshot should reach uci import via stdin, got %q", fr.inputs)
+	}
 }
 
 // TestValidateWgCheck (10.3): el healthcheck del túnel valida interface y