The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
Remove the package forwarders on endpoint uninstall
shukieshah1aaca5416302
diff --git a/cli/beacon/cmd/endpoint_diagnostics.go b/cli/beacon/cmd/endpoint_diagnostics.goindex 5e47a08e..0e44bebd 100644--- a/cli/beacon/cmd/endpoint_diagnostics.go+++ b/cli/beacon/cmd/endpoint_diagnostics.go@@ -8,6 +8,7 @@ import ("os""path/filepath""regexp"+ "runtime""strconv""strings""time"@@ -908,6 +909,12 @@ func plannedUninstallActions() []plannedAction {{Action: "unload_service", Message: "stop endpoint collector service if present"},{Action: "unload_service", Message: "remove scheduled inventory heartbeat job if present"},}+ if !cfg.UserMode && runtime.GOOS == "darwin" {+ actions = append(actions, plannedAction{Action: "unload_service", Message: "stop and remove the S3, GCS and Falcon forwarders if present"})+ if !endpointOpts.keepConfig {+ actions = append(actions, plannedAction{Action: "remove_file", Target: service.ScriptForwarderConfigDir(), Message: "forwarder config and credentials"})+ }+ }if !endpointOpts.keepConfig {actions = append(actions, plannedAction{Action: "restore_backup", Message: "restore backed up harness configs when available"})}diff --git a/cli/beacon/internal/endpoint/lifecycle/inventory_job_test.go b/cli/beacon/internal/endpoint/lifecycle/inventory_job_test.goindex 41fe667f..1a1cad66 100644--- a/cli/beacon/internal/endpoint/lifecycle/inventory_job_test.go+++ b/cli/beacon/internal/endpoint/lifecycle/inventory_job_test.go@@ -317,3 +317,32 @@ func TestStableProgramPathPrefersTheLinkedHomebrewBinary(t *testing.T) {t.Fatalf("without a linked binary the keg path stays, got %q", got)}}++// The package-helper forwarders are system LaunchDaemons. A user-mode uninstall has no business+// with them, and must not reach for /Library.+func TestUserUninstallLeavesScriptForwardersAlone(t *testing.T) {+ testenv.SetHome(t, t.TempDir())+ old := removeScriptForwarders+ called := false+ removeScriptForwarders = func(bool) error { called = true; return nil }+ t.Cleanup(func() { removeScriptForwarders = old })+ if err := Uninstall(UninstallOptions{UserMode: true}); err != nil {+ t.Fatal(err)+ }+ if called {+ t.Fatal("a user-mode uninstall removed the system S3/GCS/Falcon forwarders")+ }+}++// Repair tears the endpoint down and installs it again, and Install never recreates the S3, GCS or+// Falcon forwarders. If the teardown removed them, `beacon endpoint repair --system` and the Fleet+// and Jamf repair scripts would silently stop a customer's forwarding.+func TestRepairTeardownKeepsScriptForwarders(t *testing.T) {+ opts := repairUninstallOptions(InstallOptions{UserMode: false})+ if !opts.KeepForwarders {+ t.Fatal("repair's uninstall step must keep the package-helper forwarders")+ }+ if !opts.KeepConfig || !opts.KeepLogs || !opts.KeepUpdater {+ t.Fatalf("repair's uninstall step changed what it keeps: %+v", opts)+ }+}diff --git a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go b/cli/beacon/internal/endpoint/lifecycle/lifecycle.goindex 8bd2194c..ba4ff6de 100644--- a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go+++ b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go@@ -48,6 +48,10 @@ var (_ = job.Unload()job.RemoveUnits()}+ // removeScriptForwarders is the seam for the S3, GCS and Falcon forwarders the package+ // helpers install, so tests can see whether an uninstall reaches them without touching+ // /Library.+ removeScriptForwarders = service.RemoveScriptForwarders)// inventoryJobController is what lifecycle needs from service.InventoryManager.@@ -207,6 +211,10 @@ type UninstallOptions struct {KeepLogs boolKeepConfig boolKeepUpdater bool+ // KeepForwarders leaves the package-helper forwarders (S3, GCS, Falcon) running. Repair+ // sets it: it uninstalls and reinstalls the endpoint, and nothing in Install puts those+ // forwarders back.+ KeepForwarders bool}type InstallResult struct {@@ -612,6 +620,11 @@ func Uninstall(opts UninstallOptions) error {removeUpdaterJob()}removeInventoryJob(cfg.UserMode)+ // The S3, GCS and Falcon forwarders run from /opt/beacon and read the customer's credentials+ // from their env files, so they go with the endpoint the same way the managed forwarder does.+ if !cfg.UserMode && !opts.KeepForwarders {+ fail("remove package forwarders", removeScriptForwarders(opts.KeepConfig))+ }// The managed-ingest forwarder and its credentials go with the endpoint; server-side the// device stays registered until it is revoked from the dashboard.fail("disconnect managed ingest", asymptote.Disconnect(asymptote.DisconnectOptions{@@ -667,6 +680,19 @@ func runtimeLogFiles(logPath string) []string {return paths}+// repairUninstallOptions is the teardown half of Repair: everything the reinstall recreates goes,+// and everything it does not recreate stays.+func repairUninstallOptions(opts InstallOptions) UninstallOptions {+ return UninstallOptions{+ UserMode: opts.UserMode,+ LogPath: opts.LogPath,+ KeepLogs: true,+ KeepConfig: true,+ KeepUpdater: true,+ KeepForwarders: true,+ }+}+func Repair(opts InstallOptions) (InstallResult, error) {configPath := endpointconfig.ConfigPath(opts.UserMode)configSnapshot := snapshotFile(configPath)@@ -676,7 +702,7 @@ func Repair(opts InstallOptions) (InstallResult, error) {priorAutoUpdateMode = mode}}- _ = Uninstall(UninstallOptions{UserMode: opts.UserMode, LogPath: opts.LogPath, KeepLogs: true, KeepConfig: true, KeepUpdater: true})+ _ = Uninstall(repairUninstallOptions(opts))result, err := Install(opts)if err != nil {restoreFile(configPath, configSnapshot)diff --git a/cli/beacon/internal/endpoint/service/script_forwarders.go b/cli/beacon/internal/endpoint/service/script_forwarders.gonew file mode 100644index 00000000..a1a24918--- /dev/null+++ b/cli/beacon/internal/endpoint/service/script_forwarders.go@@ -0,0 +1,89 @@+package service++import (+ "errors"+ "fmt"+ "os"+ "path/filepath"+ "runtime"+)++// The S3, GCS and Falcon forwarders are installed by the shell helpers the macOS package ships+// under /opt/beacon/jamf/claude/<name>/install-forwarder.sh, not by the CLI, so no manager here+// writes them. The CLI still has to stop and remove them: they are KeepAlive LaunchDaemons, and+// their env files hold the customer's AWS keys, GCS credential path or Falcon HEC token.+// Uninstall used to leave all of that behind, and only the Jamf full-cleanup script removed it.+var (+ ScriptForwarderLabels = []string{+ "com.beacon.endpoint.falcon-forwarder",+ "com.beacon.endpoint.s3-forwarder",+ "com.beacon.endpoint.gcs-forwarder",+ }+ // scriptForwarderDaemonsDir and scriptForwarderConfigDir are variables so tests can point+ // them at a temp dir; the helpers accept the same overrides (BEACON_LAUNCHDAEMONS_DIR,+ // BEACON_FORWARDER_BASE_DIR) for the same reason.+ scriptForwarderDaemonsDir = "/Library/LaunchDaemons"+ scriptForwarderConfigDir = "/Library/Application Support/Beacon/Forwarders"+)++// ScriptForwarderConfigDir is where the package helpers keep each forwarder's env file, Vector+// config and buffered data.+func ScriptForwarderConfigDir() string {+ return scriptForwarderConfigDir+}++func scriptForwarderPlistPath(label string) string {+ return filepath.Join(scriptForwarderDaemonsDir, label+".plist")+}++// RemoveScriptForwarders stops every package-helper forwarder and deletes its LaunchDaemon.+// Unless keepConfig is set it also deletes their config directory, which holds each forwarder's+// env file, Vector config and buffered data. That mirrors how uninstall treats the managed-ingest+// forwarder: the service always goes, the credentials only when config is not being kept.+func RemoveScriptForwarders(keepConfig bool) error {+ if runtime.GOOS != "darwin" {+ return nil+ }+ var errs []error+ for _, label := range ScriptForwarderLabels {+ path := scriptForwarderPlistPath(label)+ if _, err := os.Stat(path); os.IsNotExist(err) {+ continue+ }+ if err := bootoutLaunchdJob("system", label); err != nil {+ errs = append(errs, err)+ }+ if err := os.Remove(path); err != nil && !os.IsNotExist(err) {+ errs = append(errs, err)+ }+ }+ if !keepConfig {+ if err := os.RemoveAll(scriptForwarderConfigDir); err != nil {+ errs = append(errs, err)+ }+ }+ return errors.Join(errs...)+}++// RestoreScriptForwarders loads every package-helper forwarder whose LaunchDaemon is on disk.+//+// The package preinstall boots these out before an upgrade and the postinstall loads them again.+// A self-update that fails and rolls back never reaches that postinstall, so without this the+// forwarders stay stopped until the next reboot. A forwarder that is already loaded is reloaded,+// the same as a bootstrap the postinstall performs.+func RestoreScriptForwarders() error {+ if runtime.GOOS != "darwin" {+ return nil+ }+ var errs []error+ for _, label := range ScriptForwarderLabels {+ path := scriptForwarderPlistPath(label)+ if _, err := os.Stat(path); os.IsNotExist(err) {+ continue+ }+ if err := bootstrapLaunchdJob("system", label, path); err != nil {+ errs = append(errs, fmt.Errorf("restore %s: %w", label, err))+ }+ }+ return errors.Join(errs...)+}diff --git a/cli/beacon/internal/endpoint/service/script_forwarders_test.go b/cli/beacon/internal/endpoint/service/script_forwarders_test.gonew file mode 100644index 00000000..1b1a3d0d--- /dev/null+++ b/cli/beacon/internal/endpoint/service/script_forwarders_test.go@@ -0,0 +1,99 @@+package service++import (+ "errors"+ "os"+ "path/filepath"+ "runtime"+ "strings"+ "testing"+)++// scriptForwarderFixture points the forwarder paths at a temp dir, installs the named forwarders'+// plists and a config dir holding a credential file, and records every launchctl call.+func scriptForwarderFixture(t *testing.T, installed ...string) (configDir string, calls *[]string) {+ t.Helper()+ if runtime.GOOS != "darwin" {+ t.Skip("package-helper forwarders are macOS only")+ }+ shrinkLaunchdWaits(t)+ root := t.TempDir()+ oldDaemons, oldConfig := scriptForwarderDaemonsDir, scriptForwarderConfigDir+ scriptForwarderDaemonsDir = filepath.Join(root, "LaunchDaemons")+ scriptForwarderConfigDir = filepath.Join(root, "Forwarders")+ t.Cleanup(func() { scriptForwarderDaemonsDir, scriptForwarderConfigDir = oldDaemons, oldConfig })++ for _, dir := range []string{scriptForwarderDaemonsDir, scriptForwarderConfigDir} {+ if err := os.MkdirAll(dir, 0o755); err != nil {+ t.Fatal(err)+ }+ }+ for _, label := range installed {+ if err := os.WriteFile(scriptForwarderPlistPath(label), []byte("<plist/>"), 0o644); err != nil {+ t.Fatal(err)+ }+ }+ if err := os.WriteFile(filepath.Join(scriptForwarderConfigDir, "s3-vector.env"), []byte("AWS_SECRET_ACCESS_KEY=x\n"), 0o600); err != nil {+ t.Fatal(err)+ }++ recorded := []string{}+ oldRun := runLaunchctlCommand+ runLaunchctlCommand = func(args ...string) (string, error) {+ recorded = append(recorded, strings.Join(args, " "))+ if args[0] == "print" {+ return "Could not find service", errors.New("exit status 113")+ }+ return "", nil+ }+ t.Cleanup(func() { runLaunchctlCommand = oldRun })+ return scriptForwarderConfigDir, &recorded+}++func TestRemoveScriptForwardersRemovesDaemonsAndCredentials(t *testing.T) {+ configDir, calls := scriptForwarderFixture(t, "com.beacon.endpoint.s3-forwarder")++ if err := RemoveScriptForwarders(false); err != nil {+ t.Fatal(err)+ }+ if want := []string{"bootout system/com.beacon.endpoint.s3-forwarder"}; strings.Join(*calls, "|") != strings.Join(want, "|") {+ t.Errorf("launchctl calls = %v, want %v: only the installed forwarder should be booted out", *calls, want)+ }+ if _, err := os.Stat(scriptForwarderPlistPath("com.beacon.endpoint.s3-forwarder")); !os.IsNotExist(err) {+ t.Errorf("the S3 forwarder LaunchDaemon should be removed, stat err = %v", err)+ }+ if _, err := os.Stat(configDir); !os.IsNotExist(err) {+ t.Errorf("the forwarder config dir holds AWS keys and should be removed, stat err = %v", err)+ }+}++func TestRemoveScriptForwardersKeepConfigKeepsCredentials(t *testing.T) {+ configDir, _ := scriptForwarderFixture(t, "com.beacon.endpoint.falcon-forwarder", "com.beacon.endpoint.gcs-forwarder")++ if err := RemoveScriptForwarders(true); err != nil {+ t.Fatal(err)+ }+ for _, label := range []string{"com.beacon.endpoint.falcon-forwarder", "com.beacon.endpoint.gcs-forwarder"} {+ if _, err := os.Stat(scriptForwarderPlistPath(label)); !os.IsNotExist(err) {+ t.Errorf("%s LaunchDaemon should be removed even when config is kept, stat err = %v", label, err)+ }+ }+ if _, err := os.Stat(filepath.Join(configDir, "s3-vector.env")); err != nil {+ t.Errorf("keepConfig should keep the forwarder config dir: %v", err)+ }+}++func TestRestoreScriptForwardersLoadsOnlyInstalledOnes(t *testing.T) {+ _, calls := scriptForwarderFixture(t, "com.beacon.endpoint.s3-forwarder", "com.beacon.endpoint.gcs-forwarder")++ if err := RestoreScriptForwarders(); err != nil {+ t.Fatal(err)+ }+ want := []string{+ "bootstrap system " + scriptForwarderPlistPath("com.beacon.endpoint.s3-forwarder"),+ "bootstrap system " + scriptForwarderPlistPath("com.beacon.endpoint.gcs-forwarder"),+ }+ if strings.Join(*calls, "|") != strings.Join(want, "|") {+ t.Errorf("launchctl calls = %v, want %v", *calls, want)+ }+}diff --git a/packaging/macos/README.md b/packaging/macos/README.mdindex 499d5e4d..834b6a98 100644--- a/packaging/macos/README.md+++ b/packaging/macos/README.md@@ -671,8 +671,12 @@ preserve harness telemetry configuration./opt/beacon/fleet/scripts/uninstall.sh "$@"```-The endpoint uninstall removes service/configuration state. Package payload-removal remains under the MDM/package receipt lifecycle.+The endpoint uninstall removes service/configuration state. That includes the+S3, GCS and Falcon forwarder LaunchDaemons and, unless config is kept, their+env files under `/Library/Application Support/Beacon/Forwarders`, which hold+the forwarding credentials. `beacon endpoint repair` leaves the forwarders+running. Package payload removal remains under the MDM/package receipt+lifecycle.## Troubleshooting