Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Preserve native Proxmox guest handoff

rcourtman01ff388c7eab
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index 68b6596425..ebc0b9bc73 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -1305,6 +1305,21 @@ and indeterminate Proxmox dispatch requires target-state recovery inspection.
The package unit alone permits setuid/setgid file creation and kernel-package
payload writes because apt/dpkg legitimately require them; every other runner
hardening property and the exact package command catalog remain enforced.
+Contained Proxmox stop/shutdown units alone add `AF_NETLINK` to their
+address-family allowlist because PVE teardown helpers can configure bridged
+networking through route netlink; status and QEMU reboot retain the narrower
+family set. QEMU/container start and container reboot use a distinct fixed
+`proxmox-handoff` catalog: PID 1 launches the same hidden validator in an exact
+transient unit, but the unit omits sandbox settings that would otherwise be
+inherited by PVE-managed VM/container processes after they move to native PVE
+scopes. The handoff launcher deliberately does not become a child subreaper;
+the exact transient unit must disappear before return, and the lifecycle may
+report success only after a separate contained status read proves the requested
+VMID state. Cancellation after a provider handoff remains indeterminate and
+requires recovery inspection. Package and containment-probe units never gain
+`AF_NETLINK` or the handoff relaxation. This provider-aware path remains
+unqualified until disposable-PVE proof establishes the native scope handoff,
+bridged networking, postcondition, and cancellation behavior.
Docker/Podman lifecycle remains on the already-connected daemon API operator;
the action runner does not fall back to a killable external runtime CLI.
Host storage-pressure cleanup is a second closed agent operation. The report
diff --git a/internal/hostagent/command_client_test.go b/internal/hostagent/command_client_test.go
index 10b8be113b..685de6cc3e 100644
--- a/internal/hostagent/command_client_test.go
+++ b/internal/hostagent/command_client_test.go
@@ -3,11 +3,73 @@ package hostagent
import (
"context"
"io"
+ "reflect"
"testing"
+ "github.com/rcourtman/pulse-go-rewrite/internal/agentexec"
"github.com/rs/zerolog"
)
+func TestCommandClientProxmoxRunnerUsesProviderHandoffOnlyForGuestCreation(t *testing.T) {
+ tests := []struct {
+ name string
+ kind string
+ operation string
+ before string
+ after string
+ mutationCatalog typedActionCatalog
+ }{
+ {name: "VM start hands off", kind: "vm", operation: "start", before: "stopped", after: "running", mutationCatalog: typedActionCatalogProxmoxHandoff},
+ {name: "container start hands off", kind: "ct", operation: "start", before: "stopped", after: "running", mutationCatalog: typedActionCatalogProxmoxHandoff},
+ {name: "VM reboot stays contained", kind: "vm", operation: "reboot", before: "running", after: "running", mutationCatalog: typedActionCatalogProxmox},
+ {name: "container reboot hands off", kind: "ct", operation: "reboot", before: "running", after: "running", mutationCatalog: typedActionCatalogProxmoxHandoff},
+ {name: "graceful shutdown stays contained", kind: "ct", operation: "shutdown", before: "running", after: "stopped", mutationCatalog: typedActionCatalogProxmox},
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ payload := agentexec.ProxmoxGuestLifecyclePayload{
+ RequestID: "attempt-" + test.kind + "-" + test.operation,
+ ActionID: "action-" + test.kind + "-" + test.operation,
+ Operation: test.operation, GuestKind: test.kind, VMID: 141,
+ ExpectedStatus: test.before, Timeout: 30,
+ }
+ if err := agentexec.BindProxmoxGuestLifecyclePayload(&payload); err != nil {
+ t.Fatal(err)
+ }
+ type invocation struct {
+ catalog typedActionCatalog
+ name string
+ args []string
+ }
+ var calls []invocation
+ manager := newProxmoxGuestLifecycleManagerWithTypedRunner(func(_ context.Context, _ []string, catalog typedActionCatalog, name string, args ...string) typedActionCommandResult {
+ calls = append(calls, invocation{catalog: catalog, name: name, args: append([]string(nil), args...)})
+ if args[0] == "status" {
+ status := test.before
+ if len(calls) == 3 {
+ status = test.after
+ }
+ return typedActionCommandResult{stdout: "status: " + status, exitCode: 0}
+ }
+ return typedActionCommandResult{exitCode: 0}
+ })
+ result := manager.Apply(context.Background(), payload)
+ if result.ExecutionPhase != agentexec.ProxmoxGuestPhaseComplete || !result.MutationCompleted || !result.ReadbackRan {
+ t.Fatalf("result=%+v", result)
+ }
+ tool := "qm"
+ if test.kind == "ct" {
+ tool = "pct"
+ }
+ if len(calls) != 3 || calls[0].catalog != typedActionCatalogProxmox || calls[0].name != tool || !reflect.DeepEqual(calls[0].args, []string{"status", "141"}) ||
+ calls[1].catalog != test.mutationCatalog || calls[1].name != tool || !reflect.DeepEqual(calls[1].args, []string{test.operation, "141"}) ||
+ calls[2].catalog != typedActionCatalogProxmox || calls[2].name != tool || !reflect.DeepEqual(calls[2].args, []string{"status", "141"}) {
+ t.Fatalf("typed action calls=%+v", calls)
+ }
+ })
+ }
+}
+
func TestNew_DefaultPulseURLUsedForCommandClient(t *testing.T) {
logger := zerolog.New(io.Discard)
diff --git a/internal/hostagent/proxmox_guest_lifecycle.go b/internal/hostagent/proxmox_guest_lifecycle.go
index edf5bacf34..ed9a59afef 100644
--- a/internal/hostagent/proxmox_guest_lifecycle.go
+++ b/internal/hostagent/proxmox_guest_lifecycle.go
@@ -15,6 +15,7 @@ import (
)
type proxmoxGuestCommandRunner func(context.Context, string, ...string) ([]byte, error)
+type proxmoxTypedActionRunner func(context.Context, []string, typedActionCatalog, string, ...string) typedActionCommandResult
type proxmoxGuestLifecycleManager struct {
run proxmoxGuestCommandRunner
@@ -22,15 +23,30 @@ type proxmoxGuestLifecycleManager struct {
}
func newProxmoxGuestLifecycleManager() *proxmoxGuestLifecycleManager {
+ return newProxmoxGuestLifecycleManagerWithTypedRunner(runTypedActionCommand)
+}
+
+func newProxmoxGuestLifecycleManagerWithTypedRunner(run proxmoxTypedActionRunner) *proxmoxGuestLifecycleManager {
return &proxmoxGuestLifecycleManager{
run: func(ctx context.Context, name string, args ...string) ([]byte, error) {
- result := runTypedActionCommand(ctx, nil, typedActionCatalogProxmox, name, args...)
+ catalog := typedActionCatalogProxmox
+ if len(args) > 0 {
+ catalog = typedActionCatalogForProxmoxInvocation(name, args[0])
+ }
+ result := run(ctx, nil, catalog, name, args...)
return []byte(result.stdout), result.err
},
now: time.Now,
}
}
+func typedActionCatalogForProxmoxInvocation(tool, verb string) typedActionCatalog {
+ if isProxmoxHandoffLifecycleInvocation(tool, verb) {
+ return typedActionCatalogProxmoxHandoff
+ }
+ return typedActionCatalogProxmox
+}
+
func (m *proxmoxGuestLifecycleManager) Apply(ctx context.Context, req agentexec.ProxmoxGuestLifecyclePayload) (result agentexec.ProxmoxGuestLifecycleResultPayload) {
started := time.Now()
result = agentexec.ProxmoxGuestLifecycleResultPayload{
diff --git a/internal/hostagent/typed_action_command.go b/internal/hostagent/typed_action_command.go
index 121b8b8acc..af92c6999b 100644
--- a/internal/hostagent/typed_action_command.go
+++ b/internal/hostagent/typed_action_command.go
@@ -9,9 +9,10 @@ import (
type typedActionCatalog string
const (
- typedActionCatalogPackage typedActionCatalog = "package"
- typedActionCatalogProxmox typedActionCatalog = "proxmox"
- typedActionCatalogProbe typedActionCatalog = "probe"
+ typedActionCatalogPackage typedActionCatalog = "package"
+ typedActionCatalogProxmox typedActionCatalog = "proxmox"
+ typedActionCatalogProxmoxHandoff typedActionCatalog = "proxmox-handoff"
+ typedActionCatalogProbe typedActionCatalog = "probe"
)
const (
@@ -57,7 +58,11 @@ func validateTypedActionInvocation(catalog typedActionCatalog, name string, args
}
}
case typedActionCatalogProxmox:
- if (name == "qm" || name == "pct") && len(args) == 2 && isProxmoxLifecycleVerb(args[0]) && isPositiveDecimalID(args[1]) {
+ if (name == "qm" || name == "pct") && len(args) == 2 && isProxmoxContainedLifecycleInvocation(name, args[0]) && isPositiveDecimalID(args[1]) {
+ return nil
+ }
+ case typedActionCatalogProxmoxHandoff:
+ if (name == "qm" || name == "pct") && len(args) == 2 && isProxmoxHandoffLifecycleInvocation(name, args[0]) && isPositiveDecimalID(args[1]) {
return nil
}
case typedActionCatalogProbe:
@@ -68,10 +73,23 @@ func validateTypedActionInvocation(catalog typedActionCatalog, name string, args
return fmt.Errorf("typed action invocation is outside the closed %s catalog", catalog)
}
-func isProxmoxLifecycleVerb(value string) bool {
- switch value {
- case "status", "start", "stop", "reboot":
+func isProxmoxContainedLifecycleInvocation(tool, verb string) bool {
+ switch verb {
+ case "status", "stop", "shutdown":
+ return true
+ case "reboot":
+ return tool == "qm"
+ default:
+ return false
+ }
+}
+
+func isProxmoxHandoffLifecycleInvocation(tool, verb string) bool {
+ switch verb {
+ case "start":
return true
+ case "reboot":
+ return tool == "pct"
default:
return false
}
diff --git a/internal/hostagent/typed_action_command_linux.go b/internal/hostagent/typed_action_command_linux.go
index 445cf6a2a2..cd89fdd4f0 100644
--- a/internal/hostagent/typed_action_command_linux.go
+++ b/internal/hostagent/typed_action_command_linux.go
@@ -50,6 +50,7 @@ type typedActionLauncherResult struct {
Signal int `json:"signal,omitempty"`
ScanComplete bool `json:"scan_complete"`
DescendantsObserved bool `json:"descendants_observed"`
+ ProviderHandoff bool `json:"provider_handoff,omitempty"`
Error string `json:"error,omitempty"`
}
@@ -131,6 +132,10 @@ func runTypedActionCommandPlatform(ctx context.Context, env []string, catalog ty
result.err = errors.Join(errTypedActionContainmentIndeterminate, waitResult.err, fmt.Errorf("typed action launcher result unavailable: %w", resultErr))
return result
}
+ if launcherResult.ProviderHandoff != (catalog == typedActionCatalogProxmoxHandoff) {
+ result.err = errors.Join(errTypedActionContainmentIndeterminate, waitResult.err, errors.New("typed action launcher reported a mismatched provider handoff mode"))
+ return result
+ }
result.exitCode = launcherResult.ExitCode
if !launcherResult.TargetStarted || !launcherResult.ScanComplete || launcherResult.Error != "" {
result.err = errors.Join(errTypedActionContainmentIndeterminate, waitResult.err, fmt.Errorf("typed action launcher could not establish terminal state: %s", launcherResult.Error))
@@ -209,6 +214,7 @@ func typedActionSystemdRunArgs(unit, runnerPath, resultPath string, env []string
}
restrictSUIDSGID := "yes"
protectKernelModules := "yes"
+ addressFamilies := "AF_UNIX AF_INET AF_INET6"
if catalog == typedActionCatalogPackage {
// apt/dpkg must be able to install setuid/setgid files and kernel
// package payloads. This relaxation remains inside the exact closed
@@ -216,18 +222,31 @@ func typedActionSystemdRunArgs(unit, runnerPath, resultPath string, env []string
restrictSUIDSGID = "no"
protectKernelModules = "no"
}
+ if catalog == typedActionCatalogProxmox && len(args) == 2 && (args[0] == "stop" || args[0] == "shutdown") {
+ // PVE's qm/pct stop and shutdown paths can tear down guest networking
+ // through route netlink. Keep that family confined to the contained
+ // Proxmox catalog; package and containment probes do not need it.
+ addressFamilies += " AF_NETLINK"
+ }
result := []string{
"--no-ask-password", "--quiet", "--wait", "--pipe", "--collect", "--service-type=exec", "--unit=" + unit,
"--property=User=root", "--property=Group=root", "--property=UMask=0077", "--property=Restart=no",
"--property=KillMode=control-group", "--property=KillSignal=SIGTERM", "--property=SendSIGKILL=yes", "--property=FinalKillSignal=SIGKILL",
"--property=TimeoutStopSec=" + typedActionSystemdStopGrace, "--property=RuntimeMaxSec=" + typedActionRuntimeMaximum,
"--property=BindsTo=pulse-agent-runner.service", "--property=After=pulse-agent-runner.service", "--property=PartOf=pulse-agent-runner.service",
- "--property=NoNewPrivileges=yes", "--property=PrivateTmp=yes", "--property=ProtectHome=yes", "--property=ProtectSystem=no",
- "--property=ProtectKernelTunables=yes", "--property=ProtectKernelModules=" + protectKernelModules, "--property=ProtectControlGroups=yes",
- "--property=LockPersonality=yes", "--property=RestrictSUIDSGID=" + restrictSUIDSGID, "--property=RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6",
- "--property=SystemCallArchitectures=native", "--property=WorkingDirectory=/",
- "--setenv=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "--setenv=LC_ALL=C",
}
+ if catalog != typedActionCatalogProxmoxHandoff {
+ result = append(result,
+ "--property=NoNewPrivileges=yes", "--property=PrivateTmp=yes", "--property=ProtectHome=yes", "--property=ProtectSystem=no",
+ "--property=ProtectKernelTunables=yes", "--property=ProtectKernelModules="+protectKernelModules, "--property=ProtectControlGroups=yes",
+ "--property=LockPersonality=yes", "--property=RestrictSUIDSGID="+restrictSUIDSGID, "--property=RestrictAddressFamilies="+addressFamilies,
+ "--property=SystemCallArchitectures=native",
+ )
+ }
+ result = append(result,
+ "--property=WorkingDirectory=/",
+ "--setenv=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "--setenv=LC_ALL=C",
+ )
for _, value := range env {
result = append(result, "--setenv="+value)
}
@@ -349,10 +368,13 @@ func RunTypedActionLauncher(args []string) int {
return typedActionLauncherSupervisorFailureExit
}
result := typedActionLauncherResult{Version: typedActionResultVersion, ExitCode: -1}
- if err := typedActionBecomeSubreaper(); err != nil {
- result.Error = "establish typed action child subreaper"
- _ = writeTypedActionLauncherResult(resultPath, result)
- return typedActionLauncherSupervisorFailureExit
+ providerHandoff := catalog == typedActionCatalogProxmoxHandoff
+ if !providerHandoff {
+ if err := typedActionBecomeSubreaper(); err != nil {
+ result.Error = "establish typed action child subreaper"
+ _ = writeTypedActionLauncherResult(resultPath, result)
+ return typedActionLauncherSupervisorFailureExit
+ }
}
target, err := typedActionResolveTarget(catalog, name)
if err != nil {
@@ -385,12 +407,23 @@ func RunTypedActionLauncher(args []string) int {
result.Error = "wait for fixed-catalog target"
}
}
- descendants, scanErr := typedActionInspectDescendants()
- if scanErr != nil {
- result.Error = "inspect transient service cgroup"
- } else {
+ var scanErr error
+ if providerHandoff {
+ // Proxmox start/reboot deliberately hand VM/container processes to
+ // PVE-owned scopes. PID 1's collection of this exact transient unit
+ // proves no process remains in Pulse's unit; the lifecycle manager must
+ // then establish the requested state through an independent status read.
result.ScanComplete = true
- result.DescendantsObserved = descendants
+ result.ProviderHandoff = true
+ } else {
+ var descendants bool
+ descendants, scanErr = typedActionInspectDescendants()
+ if scanErr != nil {
+ result.Error = "inspect transient service cgroup"
+ } else {
+ result.ScanComplete = true
+ result.DescendantsObserved = descendants
+ }
}
if err := writeTypedActionLauncherResult(resultPath, result); err != nil {
return typedActionLauncherSupervisorFailureExit
@@ -398,7 +431,7 @@ func RunTypedActionLauncher(args []string) int {
if scanErr != nil {
return typedActionLauncherInspectionFailureExit
}
- if descendants {
+ if result.DescendantsObserved {
return typedActionLauncherDescendantExit
}
if result.Signal != 0 || result.ExitCode < 0 {
@@ -424,9 +457,10 @@ func parseTypedActionLauncherArgs(args []string) (typedActionCatalog, string, st
func resolveTypedActionTarget(catalog typedActionCatalog, name string) (string, error) {
allowed := map[typedActionCatalog]map[string][]string{
- typedActionCatalogPackage: {"apt-get": {"/usr/bin/apt-get"}, "dpkg": {"/usr/bin/dpkg"}},
- typedActionCatalogProxmox: {"qm": {"/usr/sbin/qm", "/usr/bin/qm"}, "pct": {"/usr/sbin/pct", "/usr/bin/pct"}},
- typedActionCatalogProbe: {"true": {"/usr/bin/true", "/bin/true"}},
+ typedActionCatalogPackage: {"apt-get": {"/usr/bin/apt-get"}, "dpkg": {"/usr/bin/dpkg"}},
+ typedActionCatalogProxmox: {"qm": {"/usr/sbin/qm", "/usr/bin/qm"}, "pct": {"/usr/sbin/pct", "/usr/bin/pct"}},
+ typedActionCatalogProxmoxHandoff: {"qm": {"/usr/sbin/qm", "/usr/bin/qm"}, "pct": {"/usr/sbin/pct", "/usr/bin/pct"}},
+ typedActionCatalogProbe: {"true": {"/usr/bin/true", "/bin/true"}},
}
for _, candidate := range allowed[catalog][name] {
resolved, err := filepath.EvalSymlinks(candidate)
diff --git a/internal/hostagent/typed_action_command_linux_test.go b/internal/hostagent/typed_action_command_linux_test.go
index 7eab05b26e..4e6d215d8d 100644
--- a/internal/hostagent/typed_action_command_linux_test.go
+++ b/internal/hostagent/typed_action_command_linux_test.go
@@ -12,9 +12,25 @@ import (
"syscall"
"testing"
"time"
+
+ "golang.org/x/sys/unix"
)
func TestMain(m *testing.M) {
+ if os.Getenv("PULSE_TEST_TYPED_ACTION_NETLINK_CANARY") == "1" {
+ if os.Getenv("PULSE_TEST_TYPED_ACTION_NO_NEW_PRIVS_CANARY") == "1" {
+ value, _, errno := syscall.Syscall6(syscall.SYS_PRCTL, uintptr(unix.PR_GET_NO_NEW_PRIVS), 0, 0, 0, 0, 0)
+ if errno != 0 || value != 0 {
+ os.Exit(96)
+ }
+ }
+ fd, err := unix.Socket(unix.AF_NETLINK, unix.SOCK_RAW|unix.SOCK_CLOEXEC, unix.NETLINK_ROUTE)
+ if err != nil {
+ os.Exit(97)
+ }
+ _ = unix.Close(fd)
+ os.Exit(0)
+ }
if len(os.Args) > 1 && os.Args[1] == typedActionLauncherCommand {
if target := os.Getenv("PULSE_TEST_TYPED_ACTION_TARGET"); target != "" {
typedActionResolveTarget = func(typedActionCatalog, string) (string, error) { return target, nil }
@@ -95,6 +111,113 @@ func TestTypedActionNonPackageCatalogKeepsKernelAndSUIDRestrictions(t *testing.T
}
}
+func TestTypedActionAddressFamiliesAreCatalogSpecific(t *testing.T) {
+ tests := []struct {
+ name string
+ catalog typedActionCatalog
+ command string
+ args []string
+ want string
+ }{
+ {name: "package", catalog: typedActionCatalogPackage, command: "apt-get", args: []string{"update"}, want: "--property=RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6"},
+ {name: "probe", catalog: typedActionCatalogProbe, command: "true", want: "--property=RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6"},
+ {name: "proxmox status", catalog: typedActionCatalogProxmox, command: "qm", args: []string{"status", "100"}, want: "--property=RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6"},
+ {name: "proxmox stop", catalog: typedActionCatalogProxmox, command: "qm", args: []string{"stop", "100"}, want: "--property=RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK"},
+ {name: "proxmox shutdown", catalog: typedActionCatalogProxmox, command: "pct", args: []string{"shutdown", "100"}, want: "--property=RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK"},
+ {name: "proxmox handoff", catalog: typedActionCatalogProxmoxHandoff, command: "qm", args: []string{"start", "100"}, want: ""},
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ args, err := typedActionSystemdRunArgs(
+ "pulse-agent-action-0123456789abcdef0123456789abcdef.service",
+ "/usr/local/bin/pulse-agent-runner", "/var/lib/pulse-agent-runner/typed-actions/result.json",
+ nil, test.catalog, test.command, test.args,
+ )
+ if err != nil {
+ t.Fatal(err)
+ }
+ var got string
+ for _, arg := range args {
+ if strings.HasPrefix(arg, "--property=RestrictAddressFamilies=") {
+ if got != "" {
+ t.Fatalf("duplicate address-family property: %v", args)
+ }
+ got = arg
+ }
+ }
+ if got != test.want {
+ t.Fatalf("address-family property=%q want=%q", got, test.want)
+ }
+ if test.catalog != typedActionCatalogProxmox && strings.Contains(got, "AF_NETLINK") {
+ t.Fatalf("%s catalog unexpectedly received route-netlink access", test.catalog)
+ }
+ })
+ }
+}
+
+func TestTypedActionProxmoxHandoffOmitsInheritableSandbox(t *testing.T) {
+ args, err := typedActionSystemdRunArgs(
+ "pulse-agent-action-0123456789abcdef0123456789abcdef.service",
+ "/usr/local/bin/pulse-agent-runner", "/var/lib/pulse-agent-runner/typed-actions/result.json",
+ nil, typedActionCatalogProxmoxHandoff, "qm", []string{"start", "100"},
+ )
+ if err != nil {
+ t.Fatal(err)
+ }
+ joined := strings.Join(args, "\n")
+ for _, required := range []string{
+ "--property=KillMode=control-group", "--property=SendSIGKILL=yes",
+ "--property=BindsTo=pulse-agent-runner.service", "--property=PartOf=pulse-agent-runner.service",
+ "--catalog\nproxmox-handoff", "--\nqm\nstart\n100",
+ } {
+ if !strings.Contains(joined, required) {
+ t.Fatalf("Proxmox handoff arguments missing %q:\n%s", required, joined)
+ }
+ }
+ for _, forbidden := range []string{
+ "NoNewPrivileges=", "PrivateTmp=", "ProtectHome=", "ProtectSystem=", "ProtectKernel",
+ "ProtectControlGroups=", "LockPersonality=", "RestrictSUIDSGID=", "RestrictAddressFamilies=", "SystemCallArchitectures=",
+ } {
+ if strings.Contains(joined, forbidden) {
+ t.Fatalf("Proxmox handoff inherited incompatible sandbox property %q:\n%s", forbidden, joined)
+ }
+ }
+}
+
+func TestTypedActionLauncherProviderHandoffSkipsSubreaperAndScan(t *testing.T) {
+ withTypedActionLinuxHooks(t)
+ tmp := t.TempDir()
+ target := filepath.Join(tmp, "target.sh")
+ if err := os.WriteFile(target, []byte("#!/bin/sh\nexit 0\n"), 0o700); err != nil {
+ t.Fatal(err)
+ }
+ var subreaperCalls atomic.Int32
+ var inspectCalls atomic.Int32
+ typedActionResolveTarget = func(typedActionCatalog, string) (string, error) { return target, nil }
+ typedActionBecomeSubreaper = func() error {
+ subreaperCalls.Add(1)
+ return errors.New("provider handoff must not become a subreaper")
+ }
+ typedActionInspectDescendants = func() (bool, error) {
+ inspectCalls.Add(1)
+ return false, errors.New("provider handoff must not scan adopted descendants")
+ }
+ resultPath := filepath.Join(tmp, "handoff.json")
+ if got := RunTypedActionLauncher([]string{"--catalog", "proxmox-handoff", "--result-file", resultPath, "--", "qm", "start", "100"}); got != 0 {
+ t.Fatalf("provider handoff launcher exit=%d", got)
+ }
+ result, err := readTypedActionLauncherResult(resultPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !result.ProviderHandoff || !result.ScanComplete || result.DescendantsObserved || result.ExitCode != 0 {
+ t.Fatalf("provider handoff sideband=%#v", result)
+ }
+ if subreaperCalls.Load() != 0 || inspectCalls.Load() != 0 {
+ t.Fatalf("provider handoff used ordinary descendant inspection: subreaper=%d inspect=%d", subreaperCalls.Load(), inspectCalls.Load())
+ }
+}
+
func TestTypedActionLauncherSidebandDisambiguatesReservedExitAndDescendants(t *testing.T) {
withTypedActionLinuxHooks(t)
tmp := t.TempDir()
@@ -615,3 +738,61 @@ func TestTypedActionRealSystemdPackageUnitCanInstallSetuidPayload(t *testing.T)
t.Fatalf("package fixture mode=%v, setuid bit was not preserved", info.Mode())
}
}
+
+func TestTypedActionRealSystemdProxmoxNetlinkIsCatalogSpecific(t *testing.T) {
+ if os.Getenv("PULSE_TEST_SYSTEMD_TYPED_ACTION") != "1" {
+ t.Skip("set PULSE_TEST_SYSTEMD_TYPED_ACTION=1 inside a root systemd host")
+ }
+ if os.Geteuid() != 0 {
+ t.Fatal("real transient-service proof must run as root")
+ }
+ withTypedActionLinuxHooks(t)
+ stateDir, err := os.MkdirTemp("/run", "pulse-agent-runner-test-")
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = os.RemoveAll(stateDir) })
+ t.Setenv("PULSE_AGENT_RUNNER_STATE_DIR", stateDir)
+ self, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ testBinary := filepath.Join(stateDir, "hostagent.test")
+ binary, err := os.ReadFile(self)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(testBinary, binary, 0o700); err != nil {
+ t.Fatal(err)
+ }
+ target := filepath.Join(stateDir, "netlink-target")
+ targetScript := "#!/bin/sh\nexport PULSE_TEST_TYPED_ACTION_NETLINK_CANARY=1\nif [ \"$1\" = start ]; then export PULSE_TEST_TYPED_ACTION_NO_NEW_PRIVS_CANARY=1; fi\nexec " + testBinary + " \"$@\"\n"
+ if err := os.WriteFile(target, []byte(targetScript), 0o700); err != nil {
+ t.Fatal(err)
+ }
+ runnerPath := filepath.Join(stateDir, "pulse-agent-runner-test")
+ runnerScript := "#!/bin/sh\nexport PULSE_TEST_TYPED_ACTION_TARGET=" + target + "\nexec " + testBinary + " \"$@\"\n"
+ if err := os.WriteFile(runnerPath, []byte(runnerScript), 0o700); err != nil {
+ t.Fatal(err)
+ }
+ typedActionCurrentExecutable = func() (string, error) { return runnerPath, nil }
+
+ ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
+ defer cancel()
+ allowed := runTypedActionCommand(ctx, nil, typedActionCatalogProxmox, "qm", "stop", "100")
+ if allowed.err != nil || allowed.exitCode != 0 {
+ t.Fatalf("Proxmox route-netlink canary exit=%d err=%v stderr=%q", allowed.exitCode, allowed.err, allowed.stderr)
+ }
+ handoffCtx, handoffCancel := context.WithTimeout(context.Background(), 20*time.Second)
+ defer handoffCancel()
+ handoff := runTypedActionCommand(handoffCtx, nil, typedActionCatalogProxmoxHandoff, "qm", "start", "100")
+ if handoff.err != nil || handoff.exitCode != 0 {
+ t.Fatalf("Proxmox native handoff canary exit=%d err=%v stderr=%q", handoff.exitCode, handoff.err, handoff.stderr)
+ }
+ deniedCtx, deniedCancel := context.WithTimeout(context.Background(), 20*time.Second)
+ defer deniedCancel()
+ denied := runTypedActionCommand(deniedCtx, nil, typedActionCatalogProbe, "true")
+ if denied.exitCode != 97 || denied.err == nil || !strings.Contains(denied.err.Error(), "status 97") {
+ t.Fatalf("probe catalog unexpectedly opened route-netlink: exit=%d err=%v stderr=%q", denied.exitCode, denied.err, denied.stderr)
+ }
+}
diff --git a/internal/hostagent/typed_action_command_test.go b/internal/hostagent/typed_action_command_test.go
index d30a8731af..902f76e34c 100644
--- a/internal/hostagent/typed_action_command_test.go
+++ b/internal/hostagent/typed_action_command_test.go
@@ -19,7 +19,14 @@ func TestTypedActionCatalogRejectsArgumentInjection(t *testing.T) {
{typedActionCatalogPackage, "apt-get", []string{"clean", "--simulate"}, false},
{typedActionCatalogPackage, "/tmp/apt-get", []string{"update"}, false},
{typedActionCatalogProxmox, "qm", []string{"status", "100"}, true},
- {typedActionCatalogProxmox, "pct", []string{"reboot", "2147483647"}, true},
+ {typedActionCatalogProxmox, "pct", []string{"stop", "2147483647"}, true},
+ {typedActionCatalogProxmox, "qm", []string{"shutdown", "100"}, true},
+ {typedActionCatalogProxmox, "qm", []string{"reboot", "100"}, true},
+ {typedActionCatalogProxmox, "pct", []string{"reboot", "2147483647"}, false},
+ {typedActionCatalogProxmoxHandoff, "qm", []string{"start", "100"}, true},
+ {typedActionCatalogProxmoxHandoff, "pct", []string{"reboot", "2147483647"}, true},
+ {typedActionCatalogProxmoxHandoff, "qm", []string{"reboot", "100"}, false},
+ {typedActionCatalogProxmoxHandoff, "qm", []string{"status", "100"}, false},
{typedActionCatalogProxmox, "qm", []string{"start", "01"}, false},
{typedActionCatalogProxmox, "qm", []string{"start", "100", "--skiplock"}, false},
{typedActionCatalogProxmox, "sh", []string{"-c", "id"}, false},
@@ -33,3 +40,23 @@ func TestTypedActionCatalogRejectsArgumentInjection(t *testing.T) {
}
}
}
+
+func TestTypedActionCatalogForProxmoxInvocationSeparatesProviderHandoff(t *testing.T) {
+ for _, test := range []struct {
+ tool string
+ verb string
+ want typedActionCatalog
+ }{
+ {tool: "qm", verb: "status", want: typedActionCatalogProxmox},
+ {tool: "pct", verb: "stop", want: typedActionCatalogProxmox},
+ {tool: "qm", verb: "shutdown", want: typedActionCatalogProxmox},
+ {tool: "qm", verb: "start", want: typedActionCatalogProxmoxHandoff},
+ {tool: "pct", verb: "start", want: typedActionCatalogProxmoxHandoff},
+ {tool: "qm", verb: "reboot", want: typedActionCatalogProxmox},
+ {tool: "pct", verb: "reboot", want: typedActionCatalogProxmoxHandoff},
+ } {
+ if got := typedActionCatalogForProxmoxInvocation(test.tool, test.verb); got != test.want {
+ t.Fatalf("%s %s catalog=%q want=%q", test.tool, test.verb, got, test.want)
+ }
+ }
+}