Skip to content

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

sentinel: register on --no-start installs, leave others' services, keep a chosen Vector

shukieshah203f33b59a03
diff --git a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
index 8ce30f65..cf932437 100644
--- a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
+++ b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
@@ -69,8 +69,8 @@ var (
// reconcileSentinelForwarder and removeSentinelForwarder are the seams for the managed Sentinel
// forwarder. Install always restarts it: an install or repair may have replaced the log with a
// copy (switching log access does), which Vector only notices when it restarts.
- reconcileSentinelForwarder = func(logPath string) (sentinel.ReconcileResult, error) {
- return sentinel.Reconcile(sentinel.ReconcileOptions{LogPath: logPath, Restart: true})
+ reconcileSentinelForwarder = func(logPath string, start bool) (sentinel.ReconcileResult, error) {
+ return sentinel.Reconcile(sentinel.ReconcileOptions{LogPath: logPath, Restart: start, NoStart: !start})
}
removeSentinelForwarder = sentinel.Remove
sentinelForwarderStatus = sentinel.ReadStatus
@@ -851,9 +851,12 @@ func Install(opts InstallOptions) (InstallResult, error) {
}
managedForwarder = &reconciled
}
+ // Unlike the Beacon Cloud forwarder, this runs without StartService too: an MSI upgrade can be
+ // told not to start services, and its uninstall step removed the registration, which only this
+ // puts back. Without StartService it registers the service and leaves it stopped.
var sentinelForwarder *sentinel.ReconcileResult
- if opts.StartService && !cfg.UserMode {
- reconciled, err := reconcileSentinelForwarder(cfg.LogPath)
+ if !cfg.UserMode {
+ reconciled, err := reconcileSentinelForwarder(cfg.LogPath, opts.StartService)
if err != nil && reconciled.Warning == "" {
reconciled.Warning = err.Error()
}
diff --git a/cli/beacon/internal/endpoint/lifecycle/main_test.go b/cli/beacon/internal/endpoint/lifecycle/main_test.go
index bbc0051a..ad91f8d5 100644
--- a/cli/beacon/internal/endpoint/lifecycle/main_test.go
+++ b/cli/beacon/internal/endpoint/lifecycle/main_test.go
@@ -30,7 +30,7 @@ func TestMain(m *testing.M) {
}
// Tests that run a system install or uninstall must not reach a real Sentinel forwarder under
// the machine's system directory; the ones about it set these themselves.
- reconcileSentinelForwarder = func(string) (sentinel.ReconcileResult, error) {
+ reconcileSentinelForwarder = func(string, bool) (sentinel.ReconcileResult, error) {
return sentinel.ReconcileResult{Skipped: "not installed"}, nil
}
removeSentinelForwarder = func(bool) error { return nil }
diff --git a/cli/beacon/internal/endpoint/lifecycle/managed_forwarder_test.go b/cli/beacon/internal/endpoint/lifecycle/managed_forwarder_test.go
index 5e674eac..5fcb4704 100644
--- a/cli/beacon/internal/endpoint/lifecycle/managed_forwarder_test.go
+++ b/cli/beacon/internal/endpoint/lifecycle/managed_forwarder_test.go
@@ -112,7 +112,7 @@ func TestInstallWithoutStartingServicesLeavesTheForwarderToTheCaller(t *testing.
if err != nil {
t.Fatalf("Install: %v", err)
}
- if called || result.ManagedForwarder != nil || result.SentinelForwarder != nil {
+ if called || result.ManagedForwarder != nil {
t.Fatal("a --no-start install reconciled the forwarder")
}
}
diff --git a/cli/beacon/internal/endpoint/sentinel/forwarder.go b/cli/beacon/internal/endpoint/sentinel/forwarder.go
index b274b679..c799495e 100644
--- a/cli/beacon/internal/endpoint/sentinel/forwarder.go
+++ b/cli/beacon/internal/endpoint/sentinel/forwarder.go
@@ -157,7 +157,7 @@ func (o InstallOptions) settings() (Settings, error) {
AuthorityHost: strings.TrimRight(strings.TrimSpace(o.AuthorityHost), "/"),
ReadFrom: strings.TrimSpace(o.ReadFrom),
LogPath: o.LogPath,
- VectorBin: strings.TrimSpace(o.VectorBin),
+ VectorBin: absPath(strings.TrimSpace(o.VectorBin)),
}
if err := httpsURL("--endpoint", s.Endpoint, true); err != nil {
return s, err
@@ -199,6 +199,17 @@ func (o InstallOptions) settings() (Settings, error) {
return s, nil
}
+// absPath makes a path given on the command line absolute, since the service runs elsewhere.
+func absPath(path string) string {
+ if path == "" || !strings.ContainsAny(path, `/\`) {
+ return path // empty, or a bare name for findVector to look up on PATH
+ }
+ if abs, err := filepath.Abs(path); err == nil {
+ return abs
+ }
+ return path
+}
+
// httpsURL accepts an https URL with a host, and with a path only where one makes sense.
func httpsURL(flag, raw string, bare bool) error {
u, err := url.Parse(raw)
@@ -244,8 +255,8 @@ func renderForwarderConfig(o renderOptions) string {
secrets = fmt.Sprintf("\n[secret.%s]\ntype = \"file\"\npath = %s\n", secretBackend, q(o.SecretsPath))
}
vectorLog := fmt.Sprintf(`
-# Vector's own warnings and errors, one file a day, kept for a week. A Windows service has nowhere
-# else to write them.
+# Vector's own warnings and errors, one file a day; install, repair and upgrades delete those over
+# a week old. A Windows service has nowhere else to write them.
[sources.beacon_vector_logs]
type = "internal_logs"
@@ -342,6 +353,11 @@ func Install(o InstallOptions) (InstallResult, error) {
if err != nil {
return InstallResult{}, err
}
+ // A Vector chosen with --vector or BEACON_VECTOR_BIN is recorded as the binary it resolved to,
+ // so reconcile, which runs from packages and repairs without that flag or environment, keeps it.
+ if settings.VectorBin != "" || strings.TrimSpace(os.Getenv(asymptote.VectorBinEnv)) != "" {
+ settings.VectorBin = vector.Path
+ }
var certificate []byte
if settings.Credential == CredentialCertificate {
if certificate, err = readCertificate(o.CertificatePath); err != nil {
@@ -517,8 +533,13 @@ func ReadSettings() (Settings, error) {
// removes the old package this way before installing the new one.
func Remove(keepState bool) error {
settings, err := ReadSettings()
- if err != nil && !errors.Is(err, ErrNotInstalled) {
- // Unreadable settings must not keep the service running; remove it with the default name.
+ if errors.Is(err, ErrNotInstalled) {
+ // Nothing Beacon set up. The macOS label is the one the Jamf guide had people register by
+ // hand, so a service under it without Beacon's settings is not Beacon's to remove.
+ return nil
+ }
+ if err != nil {
+ // Beacon's, but unreadable: it must not keep running. Remove it with the default name.
settings = Settings{}
}
manager := newForwarderService(ForwarderSpec(settings.AuthorityHost))
@@ -538,7 +559,9 @@ type ReconcileResult struct {
Skipped string `json:"skipped,omitempty"`
Unchanged bool `json:"unchanged,omitempty"`
Restarted bool `json:"restarted,omitempty"`
- Warning string `json:"warning,omitempty"`
+ // NotStarted is a NoStart reconcile: the service is registered and left stopped.
+ NotStarted bool `json:"not_started,omitempty"`
+ Warning string `json:"warning,omitempty"`
}
// Message is a one-line summary for install, repair and the reconcile command.
@@ -548,6 +571,8 @@ func (r ReconcileResult) Message() string {
return "Sentinel forwarder: " + r.Skipped
case r.Warning != "":
return "Sentinel forwarder: " + r.Warning
+ case r.NotStarted:
+ return "Sentinel forwarder: registered, not started"
case r.Unchanged:
return "Sentinel forwarder: running, unchanged"
default:
@@ -564,6 +589,9 @@ type ReconcileOptions struct {
// log access does), and Vector, which recognizes a file by its first line, keeps reading the
// replaced file until it restarts. After a restart it resumes in the copy where it left off.
Restart bool
+ // NoStart registers the service without starting it, for an install told not to start
+ // services. The MSI upgrade can be one, and its uninstall step removed the registration.
+ NoStart bool
}
// Reconcile brings an installed forwarder up to date with this version: it re-renders the config
@@ -643,6 +671,9 @@ func Reconcile(opts ReconcileOptions) (ReconcileResult, error) {
return ReconcileResult{Warning: err.Error()}, err
}
}
+ if opts.NoStart {
+ return ReconcileResult{NotStarted: true, Warning: rejected}, nil
+ }
if !opts.Restart && !configChanged && unitCurrent && manager.Status().Running {
return ReconcileResult{Unchanged: rejected == "", Warning: rejected}, nil
}
diff --git a/cli/beacon/internal/endpoint/sentinel/forwarder_test.go b/cli/beacon/internal/endpoint/sentinel/forwarder_test.go
index fbce8603..164d5907 100644
--- a/cli/beacon/internal/endpoint/sentinel/forwarder_test.go
+++ b/cli/beacon/internal/endpoint/sentinel/forwarder_test.go
@@ -467,3 +467,49 @@ func TestForwarderValidatesWithTheCredentialAndRunsWithTheReference(t *testing.T
t.Fatalf("reconcile must check a copy with the password in it")
}
}
+
+// An install told not to start services still registers the forwarder, which an MSI upgrade's
+// uninstall step removed; it is left stopped.
+func TestForwarderReconcileWithoutStartingRegistersTheService(t *testing.T) {
+ f := newForwarderFixture(t)
+ if _, err := Install(validInstall()); err != nil {
+ t.Fatal(err)
+ }
+ if err := Remove(true); err != nil {
+ t.Fatal(err)
+ }
+ f.svc.current = false
+ units, loads := len(f.svc.units), f.svc.loads
+ got, err := Reconcile(ReconcileOptions{NoStart: true})
+ if err != nil || !got.NotStarted || len(f.svc.units) != units+1 || f.svc.loads != loads {
+ t.Fatalf("got %+v %v units=%d loads=%d", got, err, len(f.svc.units), f.svc.loads)
+ }
+}
+
+// Without Beacon's settings there is nothing of Beacon's to remove, even when a service exists
+// under the same name (the Jamf guide had people register the macOS label by hand).
+func TestForwarderRemoveLeavesAServiceBeaconDidNotSetUp(t *testing.T) {
+ f := newForwarderFixture(t)
+ if err := Remove(false); err != nil {
+ t.Fatal(err)
+ }
+ if f.svc.unloads != 0 || f.svc.removed != 0 {
+ t.Fatalf("unloads=%d removed=%d; a service Beacon did not set up was touched", f.svc.unloads, f.svc.removed)
+ }
+}
+
+// A Vector chosen through BEACON_VECTOR_BIN is recorded as the binary it resolved to.
+func TestForwarderRecordsAVectorChosenThroughTheEnvironment(t *testing.T) {
+ newForwarderFixture(t)
+ t.Setenv(asymptote.VectorBinEnv, "/env/vector")
+ findVector = func(explicit string) (asymptote.VectorInfo, error) {
+ return asymptote.VectorInfo{Path: "/env/vector", Version: "0.56.0"}, nil
+ }
+ if _, err := Install(validInstall()); err != nil {
+ t.Fatal(err)
+ }
+ settings, err := ReadSettings()
+ if err != nil || settings.VectorBin != "/env/vector" {
+ t.Fatalf("settings = %+v (%v)", settings, err)
+ }
+}
diff --git a/docs/log-forwarding/microsoft-sentinel.mdx b/docs/log-forwarding/microsoft-sentinel.mdx
index 0cc65262..4ce67a74 100644
--- a/docs/log-forwarding/microsoft-sentinel.mdx
+++ b/docs/log-forwarding/microsoft-sentinel.mdx
@@ -215,7 +215,7 @@ The host needs outbound HTTPS to `login.microsoftonline.com` and to the DCR's `*
sudo /opt/beacon/bin/beacon endpoint sentinel forwarder status --system
```
-`beacon endpoint status --system` shows the same service state. Vector's own warnings and errors are in `forwarders/sentinel/logs/vector-<date>.log` under the endpoint's system directory, kept for a week.
+`beacon endpoint status --system` shows the same service state. Vector's own warnings and errors are in `forwarders/sentinel/logs/vector-<date>.log` under the endpoint's system directory; install, repair and upgrades delete files over a week old.
- To change a setting or rotate the credential, run `forwarder install` again with the new values.
- Repair and package upgrades keep the forwarder. An upgrade restarts it on the new package's Vector.