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
endpoint: --log-access restricted for system installs
Claude65c624f390ee
diff --git a/cli/beacon/cmd/endpoint.go b/cli/beacon/cmd/endpoint.goindex ab6a3cbb..9dc0fb31 100644--- a/cli/beacon/cmd/endpoint.go+++ b/cli/beacon/cmd/endpoint.go@@ -58,6 +58,7 @@ var endpointOpts struct {vscodeCaptureContent boolelasticPackDir stringhookLevel string+ logAccess stringcontentRetention stringsplunkHECEndpoint stringsplunkHECToken string@@ -445,6 +446,7 @@ func init() {endpointInstallCmd.Flags().StringVar(&endpointOpts.contentRetention, "content-retention", "", "Deprecated no-op; Beacon always captures full content subject to redaction and size limits")_ = endpointInstallCmd.Flags().MarkHidden("content-retention")_ = endpointInstallCmd.Flags().MarkDeprecated("content-retention", "Beacon now always captures full content; this flag is ignored")+ endpointInstallCmd.Flags().StringVar(&endpointOpts.logAccess, "log-access", "", logAccessUsage)registerSplunkFlags(endpointInstallCmd)registerFalconFlags(endpointInstallCmd)endpointRepairCmd.Flags().StringVar(&endpointOpts.harnesses, "harness", endpointHarnessAuto, "Harnesses to configure: auto (detected runtimes), all, or a comma-separated list")@@ -461,6 +463,7 @@ func init() {endpointRepairCmd.Flags().StringVar(&endpointOpts.contentRetention, "content-retention", "", "Deprecated no-op; Beacon always captures full content subject to redaction and size limits")_ = endpointRepairCmd.Flags().MarkHidden("content-retention")_ = endpointRepairCmd.Flags().MarkDeprecated("content-retention", "Beacon now always captures full content; this flag is ignored")+ endpointRepairCmd.Flags().StringVar(&endpointOpts.logAccess, "log-access", "", logAccessUsage)registerSplunkFlags(endpointRepairCmd)registerFalconFlags(endpointRepairCmd)endpointDashboardCmd.Flags().BoolVar(&endpointOpts.userMode, "user", true, "Use per-user endpoint paths")diff --git a/cli/beacon/cmd/endpoint_install.go b/cli/beacon/cmd/endpoint_install.goindex 876b7576..5912e001 100644--- a/cli/beacon/cmd/endpoint_install.go+++ b/cli/beacon/cmd/endpoint_install.go@@ -18,6 +18,7 @@ import ("github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/onboarding""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/version"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve""github.com/spf13/cobra")@@ -132,6 +133,7 @@ func endpointInstallOptions(selection endpointTargetSelection, serviceKind serviSplunkHEC: splunkHECOptions(),FalconHEC: falconHECOptions(),ServiceKind: serviceKind,+ LogAccess: strings.ToLower(strings.TrimSpace(endpointOpts.logAccess)),}}@@ -199,6 +201,7 @@ func runEndpointInstall(cmd *cobra.Command, args []string) error {case result.InventoryJobDetail != "":fmt.Fprintf(details, "Scheduled inventory job not installed: %s\n", result.InventoryJobDetail)}+ reportLogAccess(details, result)reportManagedForwarder(details, cmd.ErrOrStderr(), result.ManagedForwarder)printLingerGap(cmd.ErrOrStderr(), result)installHookTargetsFromEndpointInstall(details, cmd.ErrOrStderr(), selection.Hooks)@@ -352,10 +355,18 @@ func runEndpointStatus(cmd *cobra.Command, args []string) error {fmt.Printf("Diagnostic: %s %s (%s)\n", check.Name, check.Status, check.Message)}}- if status.LastEvent == "" {- fmt.Println("Last event: none")- } else {+ if status.LogAccess != "" {+ fmt.Printf("Runtime log access: %s\n", status.LogAccess)+ }+ switch {+ case status.LastEvent != "":fmt.Println("Last event: present")+ case status.LogAccess == asymptoteobserve.LogAccessRestricted && !readable(status.LogPath):+ // A restricted log is unreadable to an account outside its readers group, which is not the+ // same as empty.+ fmt.Println("Last event: unknown (this account cannot read the restricted log; run with sudo)")+ default:+ fmt.Println("Last event: none")}for _, forwarder := range status.ScriptForwarders {fmt.Println(scriptForwarderStatusLine(forwarder))@@ -451,6 +462,7 @@ func runEndpointRepair(cmd *cobra.Command, args []string) error {return err}fmt.Printf("Endpoint repaired. Manifest: %s\n", result.ManifestPath)+ reportLogAccess(os.Stdout, result)reportManagedForwarder(os.Stdout, cmd.ErrOrStderr(), result.ManagedForwarder)printLingerGap(cmd.ErrOrStderr(), result)installHookTargetsFromEndpointInstall(os.Stdout, cmd.ErrOrStderr(), selection.Hooks)diff --git a/cli/beacon/cmd/endpoint_logaccess.go b/cli/beacon/cmd/endpoint_logaccess.gonew file mode 100644index 00000000..c4988999--- /dev/null+++ b/cli/beacon/cmd/endpoint_logaccess.go@@ -0,0 +1,51 @@+package cmd++import (+ "fmt"+ "io"+ "os"+ "runtime"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/lifecycle"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/loggroup"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/localingest"+)++const logAccessUsage = "System log access: shared (hooks append to a world-writable log; the default) or restricted " ++ "(only the root collector writes it, hooks send events over a local socket, and only root and the " ++ asymptoteobserve.RuntimeLogReadersGroup + " group can read it). Empty keeps the current mode. System installs on macOS and Linux only"++// reportLogAccess says what the system log's access mode means for the people who read it. A+// restricted log is unreadable to an existing log shipper that does not run as root until its+// account joins the readers group, which nothing else on the screen would tell them.+func reportLogAccess(w io.Writer, result lifecycle.InstallResult) {+ switch result.LogAccess {+ case asymptoteobserve.LogAccessRestricted:+ fmt.Fprintf(w, "Runtime log access: restricted (0640 root:%s; hooks send events to %s)\n", loggroup.Name, localingest.DefaultSocketPath)+ if result.LogReadersGroupCreated {+ fmt.Fprintf(w, "Created the %s group. Add each account that should read the log, such as a log shipper's: %s\n", loggroup.Name, addToGroupHint("<account>"))+ } else {+ fmt.Fprintf(w, "Accounts that read the log need to be in the %s group: %s\n", loggroup.Name, addToGroupHint("<account>"))+ }+ case asymptoteobserve.LogAccessShared:+ fmt.Fprintln(w, "Runtime log access: shared (any local account can append to and read the log; --log-access restricted narrows it)")+ }+}++func addToGroupHint(account string) string {+ if runtime.GOOS == "darwin" {+ return fmt.Sprintf("sudo dseditgroup -o edit -a %s -t user %s", account, loggroup.Name)+ }+ return fmt.Sprintf("sudo usermod -aG %s %s", loggroup.Name, account)+}++// readable reports whether this account can open path for reading.+func readable(path string) bool {+ f, err := os.Open(path)+ if err != nil {+ return false+ }+ _ = f.Close()+ return true+}diff --git a/cli/beacon/cmd/endpoint_logaccess_test.go b/cli/beacon/cmd/endpoint_logaccess_test.gonew file mode 100644index 00000000..baba76df--- /dev/null+++ b/cli/beacon/cmd/endpoint_logaccess_test.go@@ -0,0 +1,56 @@+package cmd++import (+ "bytes"+ "strings"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/lifecycle"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/service"+)++func TestLogAccessFlagReachesTheInstallOptions(t *testing.T) {+ old := endpointOpts+ t.Cleanup(func() { endpointOpts = old })+ for flag, want := range map[string]string{"": "", "restricted": "restricted", " Restricted ": "restricted", "shared": "shared"} {+ endpointOpts.logAccess = flag+ if got := endpointInstallOptions(endpointTargetSelection{}, service.KindAuto).LogAccess; got != want {+ t.Errorf("--log-access %q gave LogAccess %q, want %q", flag, got, want)+ }+ }+}++func TestLogAccessFlagIsOnInstallAndRepair(t *testing.T) {+ if endpointInstallCmd.Flags().Lookup("log-access") == nil {+ t.Fatal("endpoint install has no --log-access")+ }+ if endpointRepairCmd.Flags().Lookup("log-access") == nil {+ t.Fatal("endpoint repair has no --log-access")+ }+}++func TestReportLogAccess(t *testing.T) {+ var out bytes.Buffer+ reportLogAccess(&out, lifecycle.InstallResult{LogAccess: "restricted", LogReadersGroupCreated: true})+ text := out.String()+ for _, want := range []string{"restricted", "beacon-log", "Created the beacon-log group", "<account>"} {+ if !strings.Contains(text, want) {+ t.Errorf("restricted report missing %q:\n%s", want, text)+ }+ }+ out.Reset()+ reportLogAccess(&out, lifecycle.InstallResult{LogAccess: "restricted"})+ if strings.Contains(out.String(), "Created") || !strings.Contains(out.String(), "need to be in the beacon-log group") {+ t.Errorf("existing group report:\n%s", out.String())+ }+ out.Reset()+ reportLogAccess(&out, lifecycle.InstallResult{LogAccess: "shared"})+ if !strings.Contains(out.String(), "shared") || !strings.Contains(out.String(), "--log-access restricted") {+ t.Errorf("shared report:\n%s", out.String())+ }+ out.Reset()+ reportLogAccess(&out, lifecycle.InstallResult{})+ if out.Len() != 0 {+ t.Errorf("a user-mode install reported a log access mode: %q", out.String())+ }+}diff --git a/cli/beacon/internal/endpoint/collector/collector.go b/cli/beacon/internal/endpoint/collector/collector.goindex 82c05bf7..5005598a 100644--- a/cli/beacon/internal/endpoint/collector/collector.go+++ b/cli/beacon/internal/endpoint/collector/collector.go@@ -130,6 +130,10 @@ func WriteConfig(cfg endpointconfig.Config) error {return nil}+// IngestExtension is the collector extension that serves the local ingest socket in the restricted+// log access mode.+const IngestExtension = "beacon_ingest"+func ConfigYAML(cfg endpointconfig.Config) string {endpointconfig.NormalizeDestinations(&cfg)exporterNames := []string{"beaconjson"}@@ -169,6 +173,14 @@ func ConfigYAML(cfg endpointconfig.Config) string {modelContextYAML = " capture_model_context: true\n"grpcLimitYAML = " max_recv_msg_size_mib: 32\n"}+ // The restricted log access mode makes the collector the only writer of the system log: hooks+ // send their events to beacon_ingest over a local socket instead of appending, so the log can+ // be 0640 root:beacon-log. Its rotation matches beaconjson's, since both append to one log.+ ingestYAML, extensions := "", "[health_check]"+ if cfg.RestrictedLogAccess() {+ ingestYAML = fmt.Sprintf(" %s:\n log_path: %q\n rotate_bytes: 10485760\n rotate_archives: 5\n", IngestExtension, cfg.LogPath)+ extensions = "[health_check, " + IngestExtension + "]"+ }return fmt.Sprintf(`receivers:otlp:protocols:@@ -197,12 +209,12 @@ processors:extensions:health_check:endpoint: 127.0.0.1:%d-+%sservice:telemetry:metrics:level: none- extensions: [health_check]+ extensions: %spipelines:logs:receivers: [otlp]@@ -216,7 +228,7 @@ service:receivers: [otlp]processors: [memory_limiter, %sbatch]exporters: %s-`, cfg.Collector.GRPCPort, grpcLimitYAML, cfg.Collector.HTTPPort, modelContextYAML, privacyYAML, cfg.LogPath, runtimeMetricsYAML+codexSpansYAML+modelContextYAML+splunkExporter+falconExporter, endpointconfig.HealthCheckPort(cfg.Collector), privacyStep, exporters, privacyStep, exporters, privacyStep, exporters)+`, cfg.Collector.GRPCPort, grpcLimitYAML, cfg.Collector.HTTPPort, modelContextYAML, privacyYAML, cfg.LogPath, runtimeMetricsYAML+codexSpansYAML+modelContextYAML+splunkExporter+falconExporter, endpointconfig.HealthCheckPort(cfg.Collector), ingestYAML, extensions, privacyStep, exporters, privacyStep, exporters, privacyStep, exporters)}func splunkHECYAML(cfg endpointconfig.Config) string {diff --git a/cli/beacon/internal/endpoint/collector/components.go b/cli/beacon/internal/endpoint/collector/components.goindex 043112eb..ab517068 100644--- a/cli/beacon/internal/endpoint/collector/components.go+++ b/cli/beacon/internal/endpoint/collector/components.go@@ -29,7 +29,10 @@ var listComponents = func(ctx context.Context, binary string) ([]byte, error) {// A binary that cannot list its components -- a stand-in, or a build that does not support the// command -- is not refused: there is nothing to judge it on, and it fails at start as it would// have before.-func CheckComponents(binary string) error {+//+// extensions names the extensions the config will use beyond health_check -- beacon_ingest, in the+// restricted log access mode -- which are refused the same way.+func CheckComponents(binary string, extensions ...string) error {ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)defer cancel()out, err := listComponents(ctx, binary)@@ -40,6 +43,9 @@ func CheckComponents(binary string) error {Processors []struct {Name string `yaml:"name"`} `yaml:"processors"`+ Extensions []struct {+ Name string `yaml:"name"`+ } `yaml:"extensions"`}if yaml.Unmarshal(out, &listed) != nil || len(listed.Processors) == 0 {return nil@@ -54,5 +60,15 @@ func CheckComponents(binary string) error {"use the %s from the same Beacon release, or pass --collector with one", binary, name, BinaryName)}}+ haveExtension := map[string]bool{}+ for _, extension := range listed.Extensions {+ haveExtension[extension.Name] = true+ }+ for _, name := range extensions {+ if !haveExtension[name] {+ return fmt.Errorf("collector %s is older than this beacon CLI: it has no %q extension, which the restricted log access mode needs; "++ "use the %s from the same Beacon release, or pass --collector with one", binary, name, BinaryName)+ }+ }return nil}diff --git a/cli/beacon/internal/endpoint/collector/logaccess_test.go b/cli/beacon/internal/endpoint/collector/logaccess_test.gonew file mode 100644index 00000000..d92a22fc--- /dev/null+++ b/cli/beacon/internal/endpoint/collector/logaccess_test.go@@ -0,0 +1,86 @@+package collector++import (+ "context"+ "runtime"+ "strings"+ "testing"++ "gopkg.in/yaml.v3"++ endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"+)++func restrictedConfig(userMode bool) endpointconfig.Config {+ cfg := endpointconfig.Default(userMode, "/var/log/beacon-agent/runtime.jsonl")+ cfg.LogAccess = "restricted"+ return cfg+}++func parsedYAML(t *testing.T, text string) map[string]interface{} {+ t.Helper()+ var doc map[string]interface{}+ if err := yaml.Unmarshal([]byte(text), &doc); err != nil {+ t.Fatalf("collector config is not YAML: %v\n%s", err, text)+ }+ return doc+}++func TestConfigYAMLServesTheIngestSocketInTheRestrictedMode(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("no restricted mode on Windows")+ }+ doc := parsedYAML(t, ConfigYAML(restrictedConfig(false)))+ extensions := doc["extensions"].(map[string]interface{})+ ingest, ok := extensions[IngestExtension].(map[string]interface{})+ if !ok {+ t.Fatalf("no %s extension: %v", IngestExtension, extensions)+ }+ if ingest["log_path"] != "/var/log/beacon-agent/runtime.jsonl" {+ t.Fatalf("log_path = %v", ingest["log_path"])+ }+ // The exporter and the extension append to one log, so they must rotate it the same way.+ exporter := doc["exporters"].(map[string]interface{})["beaconjson"].(map[string]interface{})+ if ingest["rotate_bytes"] != exporter["rotate_bytes"] || ingest["rotate_archives"] != exporter["rotate_archives"] {+ t.Fatalf("rotation differs: ingest %v/%v, exporter %v/%v", ingest["rotate_bytes"], ingest["rotate_archives"], exporter["rotate_bytes"], exporter["rotate_archives"])+ }+ service := doc["service"].(map[string]interface{})+ enabled := service["extensions"].([]interface{})+ if len(enabled) != 2 || enabled[0] != "health_check" || enabled[1] != IngestExtension {+ t.Fatalf("service extensions = %v", enabled)+ }+}++func TestConfigYAMLLeavesTheIngestSocketOutOtherwise(t *testing.T) {+ for name, cfg := range map[string]endpointconfig.Config{+ "shared": endpointconfig.Default(false, "/var/log/beacon-agent/runtime.jsonl"),+ "restricted in user mode": restrictedConfig(true),+ } {+ text := ConfigYAML(cfg)+ if strings.Contains(text, IngestExtension) {+ t.Errorf("%s: config names %s:\n%s", name, IngestExtension, text)+ }+ service := parsedYAML(t, text)["service"].(map[string]interface{})+ if enabled := service["extensions"].([]interface{}); len(enabled) != 1 || enabled[0] != "health_check" {+ t.Errorf("%s: service extensions = %v", name, enabled)+ }+ }+}++func TestCheckComponentsRequiresTheIngestExtensionWhenAsked(t *testing.T) {+ prev := listComponents+ t.Cleanup(func() { listComponents = prev })+ listed := "processors:\n - name: claude_api_body\n - name: beacon_privacy\nextensions:\n - name: health_check\n"+ listComponents = func(context.Context, string) ([]byte, error) { return []byte(listed), nil }+ if err := CheckComponents("/opt/beacon/bin/beacon-otelcol"); err != nil {+ t.Fatalf("shared mode: %v", err)+ }+ err := CheckComponents("/opt/beacon/bin/beacon-otelcol", IngestExtension)+ if err == nil || !strings.Contains(err.Error(), IngestExtension) {+ t.Fatalf("an old collector without %s = %v", IngestExtension, err)+ }+ listed += " - name: beacon_ingest\n"+ if err := CheckComponents("/opt/beacon/bin/beacon-otelcol", IngestExtension); err != nil {+ t.Fatalf("a collector with %s: %v", IngestExtension, err)+ }+}diff --git a/cli/beacon/internal/endpoint/config/config.go b/cli/beacon/internal/endpoint/config/config.goindex ef220afb..8a89715c 100644--- a/cli/beacon/internal/endpoint/config/config.go+++ b/cli/beacon/internal/endpoint/config/config.go@@ -114,6 +114,17 @@ type Config struct {Destinations *Destinations `json:"destinations,omitempty"`ManagedIngest *ManagedIngest `json:"managed_ingest,omitempty"`AutoUpdate *AutoUpdate `json:"auto_update,omitempty"`+ // LogAccess is the system log's access mode: "shared" (empty, the default) or "restricted".+ // Read it through RestrictedLogAccess, which also accounts for user mode and the platform.+ LogAccess string `json:"log_access,omitempty"`+}++// RestrictedLogAccess reports whether this endpoint's system log is in the restricted access mode:+// written only by the root collector, readable by root and the beacon-log group, with hooks+// sending their events over the local ingest socket. It is never true in user mode, whose log is+// already private, or on Windows, which has no ingest socket.+func (c Config) RestrictedLogAccess() bool {+ return !c.UserMode && runtime.GOOS != "windows" && strings.EqualFold(strings.TrimSpace(c.LogAccess), "restricted")}// ManagedIngest records that this endpoint forwards to Asymptote managed ingest. It holdsdiff --git a/cli/beacon/internal/endpoint/config/logaccess_test.go b/cli/beacon/internal/endpoint/config/logaccess_test.gonew file mode 100644index 00000000..c4b9453c--- /dev/null+++ b/cli/beacon/internal/endpoint/config/logaccess_test.go@@ -0,0 +1,25 @@+package config++import (+ "runtime"+ "testing"+)++func TestRestrictedLogAccess(t *testing.T) {+ cases := []struct {+ cfg Config+ want bool+ }{+ {Config{}, false},+ {Config{LogAccess: "shared"}, false},+ {Config{LogAccess: "restricted"}, runtime.GOOS != "windows"},+ {Config{LogAccess: " Restricted "}, runtime.GOOS != "windows"},+ {Config{UserMode: true, LogAccess: "restricted"}, false},+ {Config{LogAccess: "restricted-ish"}, false},+ }+ for _, c := range cases {+ if got := c.cfg.RestrictedLogAccess(); got != c.want {+ t.Errorf("%+v.RestrictedLogAccess() = %v, want %v", c.cfg, got, c.want)+ }+ }+}diff --git a/cli/beacon/internal/endpoint/diagnostics/diagnostics.go b/cli/beacon/internal/endpoint/diagnostics/diagnostics.goindex e9ff44c8..0f6c54eb 100644--- a/cli/beacon/internal/endpoint/diagnostics/diagnostics.go+++ b/cli/beacon/internal/endpoint/diagnostics/diagnostics.go@@ -12,6 +12,7 @@ import (endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/service""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/localingest")type Check struct {@@ -40,9 +41,12 @@ func Run(cfg endpointconfig.Config) []Check {checkFile("config", endpointconfig.ConfigPath(cfg.UserMode), true),checkFile("collector_config", cfg.Collector.ConfigPath, true),checkFile("runtime_log", cfg.LogPath, false),- checkLogPermissions(cfg.LogPath, cfg.UserMode),+ checkLogPermissionsFor(cfg),checkCollectorHealth(cfg),}+ if !cfg.UserMode {+ checks = append(checks, checkLogAccess(cfg, localingest.Deliverer{}))+ }// The service definition check is named after whichever manager is actually in use, so// the output is interpretable on a mixed fleet and a Linux host is not told to look for a// launchd plist. Supervised mode has no unit file to check -- its pidfile is runtimediff --git a/cli/beacon/internal/endpoint/diagnostics/logaccess.go b/cli/beacon/internal/endpoint/diagnostics/logaccess.gonew file mode 100644index 00000000..141c4387--- /dev/null+++ b/cli/beacon/internal/endpoint/diagnostics/logaccess.go@@ -0,0 +1,129 @@+package diagnostics++import (+ "errors"+ "fmt"+ "io/fs"+ "os"+ "path/filepath"++ endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/loggroup"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/localingest"+)++const repairSystemAction = "sudo beacon endpoint repair --system"++// logMarkedRestricted reads the log's restricted marker. A variable because only a marker root owns+// counts, and tests cannot be root.+var logMarkedRestricted = asymptoteobserve.RuntimeLogAccessRestricted++// checkLogPermissionsFor asks the question that fits the log's access mode. A shared log must be+// appendable by every hook; a restricted one must be appendable by none, which is the point of it.+func checkLogPermissionsFor(cfg endpointconfig.Config) Check {+ if cfg.RestrictedLogAccess() {+ return checkRestrictedLogPermissions(cfg.LogPath)+ }+ return checkLogPermissions(cfg.LogPath, cfg.UserMode)+}++// checkRestrictedLogPermissions reports a restricted log any account but root and the readers group+// can reach. Doctor usually runs with sudo; run without it, it cannot look inside the directory,+// which is itself the restricted mode working, and says so instead of guessing.+func checkRestrictedLogPermissions(path string) Check {+ dir := filepath.Dir(path)+ dirInfo, err := os.Stat(dir)+ if err != nil {+ return Check{Name: "runtime_log_permissions", Target: dir, Status: StatusFail, Severity: SeverityMedium,+ Message: "the log directory is missing: " + err.Error(), Evidence: "log_dir_missing", Action: repairSystemAction}+ }+ if mode := dirInfo.Mode(); mode.Perm()&0o007 != 0 || mode.Perm()&0o020 != 0 {+ return Check{Name: "runtime_log_permissions", Target: dir, Status: StatusFail, Severity: SeverityHigh,+ Message: fmt.Sprintf("the restricted log's directory is mode %o, which lets other accounts reach or change it", mode.Perm()),+ Evidence: "restricted_log_dir_too_open", Action: repairSystemAction}+ }+ info, err := os.Stat(path)+ switch {+ case errors.Is(err, fs.ErrPermission):+ return Check{Name: "runtime_log_permissions", Target: path, Status: StatusOK, Severity: SeverityInfo,+ Message: "this account cannot open the restricted log, as intended; run doctor with sudo to inspect it",+ Evidence: "restricted_log_not_readable_by_caller"}+ case err != nil:+ return Check{Name: "runtime_log_permissions", Target: path, Status: StatusWarn, Severity: SeverityLow,+ Message: "runtime log not created yet", Evidence: "runtime_log_missing"}+ }+ if mode := info.Mode().Perm(); mode&0o007 != 0 || mode&0o020 != 0 {+ return Check{Name: "runtime_log_permissions", Target: path, Status: StatusFail, Severity: SeverityHigh,+ Message: fmt.Sprintf("the restricted log is mode %o, so accounts outside root and the %s group can read or change it", mode, loggroup.Name),+ Evidence: "restricted_log_too_open", Action: repairSystemAction}+ }+ return Check{Name: "runtime_log_permissions", Target: path, Status: StatusOK, Severity: SeverityInfo,+ Message: fmt.Sprintf("mode %o, restricted", info.Mode().Perm()), Evidence: fmt.Sprintf("mode_%o", info.Mode().Perm())}+}++// checkLogAccess reports the system log's access mode and whether everything the mode depends on+// is in place: in the restricted mode, the marker that keeps the root writers from widening the+// log again, and the collector's ingest socket, without which every hook spools its events instead+// of recording them.+func checkLogAccess(cfg endpointconfig.Config, ingest localingest.Deliverer) Check {+ path := cfg.LogPath+ const name = "runtime_log_access"+ markerReadable := true+ if _, err := os.Stat(filepath.Dir(path)); errors.Is(err, fs.ErrPermission) {+ markerReadable = false+ } else if _, err := os.Stat(asymptoteobserve.LogAccessMarkerPath(path)); errors.Is(err, fs.ErrPermission) {+ markerReadable = false+ }+ marked := logMarkedRestricted(path)++ if !cfg.RestrictedLogAccess() {+ if marked {+ return Check{Name: name, Target: path, Status: StatusWarn, Severity: SeverityMedium,+ Message: "the log is marked restricted but the endpoint config says shared, so hooks cannot append to it",+ Evidence: "log_access_mismatch", Action: repairSystemAction + " --log-access shared"}+ }+ if pending, err := ingest.Pending(path); err == nil && pending > 0 {+ // Left from a time the log was restricted, or refused this account; the account's+ // next event appends them.+ return Check{Name: name, Target: localingest.DefaultSpoolDir(), Status: StatusWarn, Severity: SeverityLow,+ Message: fmt.Sprintf("shared; %d events from this account are waiting in its spool and go to the log with its next event", pending),+ Evidence: "ingest_spool_pending"}+ }+ return Check{Name: name, Target: path, Status: StatusOK, Severity: SeverityInfo,+ Message: "shared: hooks append directly, and any local account can modify or read the log; `--log-access restricted` narrows it",+ Evidence: "log_access_shared"}+ }++ if markerReadable && !marked {+ return Check{Name: name, Target: path, Status: StatusFail, Severity: SeverityHigh,+ Message: "the endpoint config says restricted but the log is not marked, so the collector will make it world-writable again at its next write",+ Evidence: "log_access_marker_missing", Action: repairSystemAction}+ }+ switch err := ingest.Client.Available(); {+ case errors.Is(err, localingest.ErrNoSocket):+ return Check{Name: name, Target: ingestSocketPath(ingest), Status: StatusFail, Severity: SeverityHigh,+ Message: "the collector is not serving the ingest socket, so hooks cannot record events and are spooling them; start the collector",+ Evidence: "ingest_socket_missing", Action: repairSystemAction}+ case err != nil:+ return Check{Name: name, Target: ingestSocketPath(ingest), Status: StatusFail, Severity: SeverityHigh,+ Message: "hooks will not send to the ingest socket: " + err.Error(), Evidence: "ingest_socket_untrusted", Action: repairSystemAction}+ }+ if pending, err := ingest.Pending(path); err == nil && pending > 0 {+ return Check{Name: name, Target: localingest.DefaultSpoolDir(), Status: StatusWarn, Severity: SeverityLow,+ Message: fmt.Sprintf("restricted; %d events from this account are waiting in its spool and go to the collector with the next event", pending),+ Evidence: "ingest_spool_pending"}+ }+ message := fmt.Sprintf("restricted: only the collector writes the log; root and the %s group read it", loggroup.Name)+ if !markerReadable {+ message += " (run doctor with sudo to inspect the log itself)"+ }+ return Check{Name: name, Target: path, Status: StatusOK, Severity: SeverityInfo, Message: message, Evidence: "log_access_restricted"}+}++func ingestSocketPath(ingest localingest.Deliverer) string {+ if ingest.Client.SocketPath != "" {+ return ingest.Client.SocketPath+ }+ return localingest.DefaultSocketPath+}diff --git a/cli/beacon/internal/endpoint/diagnostics/logaccess_unix_test.go b/cli/beacon/internal/endpoint/diagnostics/logaccess_unix_test.gonew file mode 100644index 00000000..6e01a70f--- /dev/null+++ b/cli/beacon/internal/endpoint/diagnostics/logaccess_unix_test.go@@ -0,0 +1,205 @@+//go:build linux || darwin++package diagnostics++import (+ "io/fs"+ "os"+ "path/filepath"+ "testing"+ "time"++ endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/localingest"+)++func markRestricted(t *testing.T, marked bool) {+ t.Helper()+ prev := logMarkedRestricted+ logMarkedRestricted = func(string) bool { return marked }+ t.Cleanup(func() { logMarkedRestricted = prev })+}++type ingestEnv struct {+ deliverer localingest.Deliverer+ sock string+}++func newIngestEnv(t *testing.T, serve bool) ingestEnv {+ t.Helper()+ base, err := os.MkdirTemp("", "bd")+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = os.RemoveAll(base) })+ _ = os.Chmod(base, 0o755)+ sock := filepath.Join(base, "run", "ingest.sock")+ env := ingestEnv{sock: sock, deliverer: localingest.Deliverer{+ Client: localingest.Client{SocketPath: sock, TrustedOwner: os.Geteuid(), Timeout: time.Second},+ Spool: localingest.Spool{Dir: filepath.Join(t.TempDir(), "spool")},+ }}+ if serve {+ l, err := localingest.Listen(sock)+ if err != nil {+ t.Fatal(err)+ }+ s := &localingest.Server{Append: func([]byte) error { return nil }}+ go func() { _ = s.Serve(l) }()+ t.Cleanup(func() { _ = s.Close() })+ }+ return env+}++func systemConfig(t *testing.T, access string) endpointconfig.Config {+ t.Helper()+ testenv.SetHome(t, t.TempDir())+ dir := filepath.Join(t.TempDir(), "beacon-agent")+ if err := os.MkdirAll(dir, 0o755); err != nil {+ t.Fatal(err)+ }+ return endpointconfig.Config{LogPath: filepath.Join(dir, "runtime.jsonl"), LogAccess: access}+}++func TestLogAccessShared(t *testing.T) {+ markRestricted(t, false)+ c := checkLogAccess(systemConfig(t, ""), newIngestEnv(t, false).deliverer)+ if c.Status != StatusOK || c.Evidence != "log_access_shared" {+ t.Fatalf("check = %+v", c)+ }+}++func TestLogAccessMarkedButConfiguredShared(t *testing.T) {+ markRestricted(t, true)+ c := checkLogAccess(systemConfig(t, "shared"), newIngestEnv(t, false).deliverer)+ if c.Status != StatusWarn || c.Evidence != "log_access_mismatch" || c.Action == "" {+ t.Fatalf("check = %+v", c)+ }+}++func TestLogAccessRestrictedWithoutItsMarker(t *testing.T) {+ markRestricted(t, false)+ c := checkLogAccess(systemConfig(t, "restricted"), newIngestEnv(t, true).deliverer)+ if c.Status != StatusFail || c.Evidence != "log_access_marker_missing" {+ t.Fatalf("check = %+v", c)+ }+}++func TestLogAccessRestrictedWithoutTheSocket(t *testing.T) {+ markRestricted(t, true)+ env := newIngestEnv(t, false)+ c := checkLogAccess(systemConfig(t, "restricted"), env.deliverer)+ if c.Status != StatusFail || c.Evidence != "ingest_socket_missing" || c.Target != env.sock {+ t.Fatalf("check = %+v", c)+ }+}++func TestLogAccessRestrictedWithAnUntrustedSocket(t *testing.T) {+ markRestricted(t, true)+ env := newIngestEnv(t, true)+ env.deliverer.Client.TrustedOwner = os.Geteuid() + 1+ c := checkLogAccess(systemConfig(t, "restricted"), env.deliverer)+ if c.Status != StatusFail || c.Evidence != "ingest_socket_untrusted" {+ t.Fatalf("check = %+v", c)+ }+}++func TestLogAccessRestrictedAndHealthy(t *testing.T) {+ markRestricted(t, true)+ c := checkLogAccess(systemConfig(t, "restricted"), newIngestEnv(t, true).deliverer)+ if c.Status != StatusOK || c.Evidence != "log_access_restricted" {+ t.Fatalf("check = %+v", c)+ }+}++func TestLogAccessRestrictedWithSpooledEvents(t *testing.T) {+ markRestricted(t, true)+ env := newIngestEnv(t, true)+ cfg := systemConfig(t, "restricted")+ spoolTwo(t, env.deliverer, cfg.LogPath)+ c := checkLogAccess(cfg, env.deliverer)+ if c.Status != StatusWarn || c.Evidence != "ingest_spool_pending" {+ t.Fatalf("check = %+v", c)+ }+}++func TestRestrictedLogPermissions(t *testing.T) {+ testenv.RequirePOSIXFileModes(t)+ cfg := systemConfig(t, "restricted")+ dir := filepath.Dir(cfg.LogPath)++ if c := checkRestrictedLogPermissions(cfg.LogPath); c.Evidence != "restricted_log_dir_too_open" || c.Status != StatusFail {+ t.Fatalf("0755 directory: %+v", c)+ }+ if err := os.Chmod(dir, 0o750|os.ModeSetgid); err != nil {+ t.Fatal(err)+ }+ if c := checkRestrictedLogPermissions(cfg.LogPath); c.Evidence != "runtime_log_missing" {+ t.Fatalf("no log yet: %+v", c)+ }+ if err := os.WriteFile(cfg.LogPath, nil, 0o644); err != nil {+ t.Fatal(err)+ }+ _ = os.Chmod(cfg.LogPath, 0o644)+ if c := checkRestrictedLogPermissions(cfg.LogPath); c.Evidence != "restricted_log_too_open" || c.Status != StatusFail {+ t.Fatalf("0644 log: %+v", c)+ }+ _ = os.Chmod(cfg.LogPath, 0o640)+ if c := checkRestrictedLogPermissions(cfg.LogPath); c.Status != StatusOK {+ t.Fatalf("0640 log: %+v", c)+ }+ // The world-writable check a shared log needs must not fire on a restricted one.+ if c := checkLogPermissionsFor(cfg); c.Status != StatusOK {+ t.Fatalf("checkLogPermissionsFor = %+v", c)+ }+}++func TestRestrictedLogPermissionsAsAnAccountThatCannotLook(t *testing.T) {+ if os.Geteuid() == 0 {+ t.Skip("root can always look")+ }+ cfg := systemConfig(t, "restricted")+ dir := filepath.Dir(cfg.LogPath)+ if err := os.WriteFile(cfg.LogPath, nil, 0o640); err != nil {+ t.Fatal(err)+ }+ if err := os.Chmod(dir, 0o710); err != nil {+ t.Fatal(err)+ }+ defer os.Chmod(dir, 0o750)+ _ = os.Chmod(cfg.LogPath, 0o000)+ c := checkRestrictedLogPermissions(cfg.LogPath)+ if c.Status == StatusFail {+ t.Fatalf("an unreadable restricted log is the mode working, not a failure: %+v", c)+ }+}++// spoolTwo leaves two events in the deliverer's spool for logPath, the way a hook does when a+// restricted log refuses it and no collector answers.+func spoolTwo(t *testing.T, d localingest.Deliverer, logPath string) {+ t.Helper()+ d.Euid = func() int { return 1000 }+ d.FailureBackoff = -1+ // No collector answers while these are written; the spool is per log, not per socket.+ d.Client.SocketPath = filepath.Join(t.TempDir(), "absent.sock")+ refuse := func([]byte) error { return &fs.PathError{Op: "open", Path: logPath, Err: fs.ErrPermission} }+ for i := 0; i < 2; i++ {+ if err := d.Write(logPath, []byte(`{"a":1}`), refuse); err != nil {+ t.Fatal(err)+ }+ }+ if n, _ := d.Pending(logPath); n != 2 {+ t.Fatalf("fixture: Pending = %d", n)+ }+}++func TestLogAccessSharedWithSpooledEvents(t *testing.T) {+ markRestricted(t, false)+ env := newIngestEnv(t, false)+ cfg := systemConfig(t, "")+ spoolTwo(t, env.deliverer, cfg.LogPath)+ c := checkLogAccess(cfg, env.deliverer)+ if c.Status != StatusWarn || c.Evidence != "ingest_spool_pending" {+ t.Fatalf("check = %+v", c)+ }+}diff --git a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go b/cli/beacon/internal/endpoint/lifecycle/lifecycle.goindex 3ea561a2..1825407c 100644--- a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go+++ b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go@@ -24,6 +24,7 @@ import ("github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/hermessession""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/version"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve")var (@@ -320,6 +321,9 @@ type InstallOptions struct {// ServiceKind selects the service manager. Empty auto-detects: launchd on macOS,// systemd when it is PID 1, otherwise a supervised child process.ServiceKind service.Kind+ // LogAccess sets the system log's access mode, "shared" or "restricted". Empty keeps what the+ // existing config records, so repair and a package upgrade never change it.+ LogAccess string}type UninstallOptions struct {@@ -354,8 +358,13 @@ type InstallResult struct {LingerRemediation string `json:"linger_remediation,omitempty"`// The scheduled inventory job is reconciled on every install; a host without a scheduler// reports why in InventoryJobDetail and the install still succeeds.- InventoryJobPath string `json:"inventory_job_path,omitempty"`- InventoryJobDetail string `json:"inventory_job_detail,omitempty"`+ InventoryJobPath string `json:"inventory_job_path,omitempty"`+ // LogAccess is the system log's access mode after the install; empty in user mode.+ LogAccess string `json:"log_access,omitempty"`+ // LogReadersGroupCreated is true when this install created the readers group a restricted log+ // is readable by, which then has no members yet.+ LogReadersGroupCreated bool `json:"log_readers_group_created,omitempty"`+ InventoryJobDetail string `json:"inventory_job_detail,omitempty"`// ManagedForwarder reports the Beacon Cloud forwarder reconcile. It never fails the install:// a config Vector rejects leaves the running forwarder on its old one and is reported here.ManagedForwarder *asymptote.ReconcileResult `json:"managed_forwarder,omitempty"`@@ -380,6 +389,8 @@ type Status struct {// ScriptForwarders are the S3, GCS and Falcon forwarders the package helpers install, one entry// per forwarder that is set up on this machine.ScriptForwarders []service.Status `json:"script_forwarders,omitempty"`+ // LogAccess is the system log's access mode, "shared" or "restricted"; empty in user mode.+ LogAccess string `json:"log_access,omitempty"`}type DestinationStatus struct {@@ -453,8 +464,12 @@ type installRollback struct {// it, and on systemd the disable survives reboot, so a machine that was collecting fine before// the attempt is left collecting nothing afterwards.ServiceWasRunning bool- files []string- snapshots map[string]fileSnapshot+ // RestoreLogAccess puts the system log back in the access mode it had before this install, so+ // a rollback cannot leave a restricted log world-writable while the restored config and+ // collector still say restricted, or the reverse. Nil in user mode.+ RestoreLogAccess func()+ files []string+ snapshots map[string]fileSnapshot}func newInstallRollback(manager service.Manager) *installRollback {@@ -501,6 +516,9 @@ func (r *installRollback) Rollback(manifest Manifest) {path := r.files[i]restoreFile(path, r.snapshots[path])}+ if r.RestoreLogAccess != nil {+ r.RestoreLogAccess()+ }// Bring back a service that was running before this install, now that the configuration it was// running is back on disk. A collector reads its config at startup, so this has to come after@@ -534,10 +552,16 @@ func Install(opts InstallOptions) (InstallResult, error) {if err != nil {return InstallResult{}, err}- if err := endpointcollector.CheckComponents(collectorBinary); err != nil {+ var extensions []string+ if cfg.RestrictedLogAccess() {+ extensions = append(extensions, endpointcollector.IngestExtension)+ }+ if err := endpointcollector.CheckComponents(collectorBinary, extensions...); err != nil {return InstallResult{}, err}+ logReadersGroupCreated := false+ var restoreLogAccess func()manifest := Manifest{CreatedAt: time.Now().UTC().Format(time.RFC3339),UserMode: cfg.UserMode,@@ -569,12 +593,18 @@ func Install(opts InstallOptions) (InstallResult, error) {if err := sysacl.SecureSystemTree(root, filepath.Dir(cfg.LogPath)); err != nil {return InstallResult{}, fmt.Errorf("secure %s: %w", root, err)}- if err := writer.EnsureSystemLogWritable(cfg.LogPath); err != nil {+ restore := logAccessRestorer(cfg)+ created, err := prepareSystemLog(cfg)+ if err != nil {+ restore()return InstallResult{}, fmt.Errorf("prepare the log directory: %w", err)}+ logReadersGroupCreated = created+ restoreLogAccess = restore}tx := newInstallRollback(manager)+ tx.RestoreLogAccess = restoreLogAccesstx.Track(cfg.Collector.ConfigPath)manifest.Files = append(manifest.Files, cfg.Collector.ConfigPath)if err := writeCollectorConfig(cfg); err != nil {@@ -722,19 +752,21 @@ func Install(opts InstallOptions) (InstallResult, error) {managedForwarder = &reconciled}return InstallResult{- ManagedForwarder: managedForwarder,- ConfigPath: configPath,- CollectorConfigPath: cfg.Collector.ConfigPath,- PlistPath: plistPath,- LogPath: cfg.LogPath,- ManifestPath: manifestPath,- HarnessConfigPaths: harnessPaths,- LingerApplicable: lingerOutcome.Applicable,- LingerEnabled: lingerOutcome.Enabled,- LingerDetail: lingerOutcome.Detail,- LingerRemediation: lingerOutcome.Remediation,- InventoryJobPath: inventoryResult.UnitPath,- InventoryJobDetail: inventoryDetail,+ ManagedForwarder: managedForwarder,+ ConfigPath: configPath,+ CollectorConfigPath: cfg.Collector.ConfigPath,+ PlistPath: plistPath,+ LogPath: cfg.LogPath,+ ManifestPath: manifestPath,+ HarnessConfigPaths: harnessPaths,+ LingerApplicable: lingerOutcome.Applicable,+ LingerEnabled: lingerOutcome.Enabled,+ LingerDetail: lingerOutcome.Detail,+ LingerRemediation: lingerOutcome.Remediation,+ InventoryJobPath: inventoryResult.UnitPath,+ InventoryJobDetail: inventoryDetail,+ LogAccess: logAccessOf(cfg),+ LogReadersGroupCreated: logReadersGroupCreated,}, nil}@@ -819,6 +851,11 @@ func Uninstall(opts UninstallOptions) error {for _, path := range runtimeLogFiles(cfg.LogPath) {fail("remove "+path, os.Remove(path))}+ // With the logs gone there is nothing left for the restricted mode to protect, and a+ // marker left behind would narrow the log a later shared install starts.+ if !cfg.UserMode {+ fail("remove the log access marker", os.Remove(asymptoteobserve.LogAccessMarkerPath(cfg.LogPath)))+ }// The local history holds what the log held, and more, so it goes with the logs. It lives// in the user's own endpoint directory, which a system uninstall does not own.if cfg.UserMode {@@ -1006,6 +1043,7 @@ func buildStatus(requestedUserMode bool, effectiveCfg endpointconfig.Config, runManagedIngest: asymptote.Status(requestedUserMode, managedOpts),InventoryHeartbeat: inventoryHeartbeatStatus(requestedUserMode, runtimeLog.RequestedLogPath),ScriptForwarders: scriptForwarderStatuses(),+ LogAccess: logAccessOf(effectiveCfg),}}@@ -1066,6 +1104,12 @@ func buildConfig(opts InstallOptions) endpointconfig.Config {if existing.Inventory != nil {cfg.Inventory = existing.Inventory}+ // The log access mode is an operator's security choice; a repair or upgrade that dropped+ // it would silently make a restricted log world-writable again.+ cfg.LogAccess = existing.LogAccess+ }+ if opts.LogAccess != "" {+ cfg.LogAccess = opts.LogAccess}if opts.Harnesses != nil {cfg.Harnesses = opts.Harnesses@@ -1208,6 +1252,9 @@ func preflight(cfg endpointconfig.Config, startService bool, kind service.Kind)if err := endpointconfig.ValidateDestinations(cfg.Destinations); err != nil {return err}+ if err := validateLogAccess(cfg); err != nil {+ return err+ }// Replaces a blanket "macOS only" refusal. What actually matters is whether a service// manager can run the collector here, which is a property of the host rather than of the// operating system name: a Linux VM with systemd is fully supported, and a containerdiff --git a/cli/beacon/internal/endpoint/lifecycle/logaccess.go b/cli/beacon/internal/endpoint/lifecycle/logaccess.gonew file mode 100644index 00000000..b2a45185--- /dev/null+++ b/cli/beacon/internal/endpoint/lifecycle/logaccess.go@@ -0,0 +1,94 @@+package lifecycle++import (+ "fmt"+ "runtime"++ endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/loggroup"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/localingest"+)++// Seams for tests, which cannot be root or change the account database.+var (+ ensureLogReadersGroup = loggroup.Ensure+ logIsRestricted = asymptoteobserve.RuntimeLogAccessRestricted+ removeStaleIngestSocket = localingest.RemoveStale+ restrictSystemLog = writer.RestrictSystemLog+ shareSystemLog = writer.ShareSystemLog+)++// validateLogAccess refuses a log access mode the endpoint cannot have, before install writes+// anything.+func validateLogAccess(cfg endpointconfig.Config) error {+ mode, err := asymptoteobserve.ParseLogAccess(cfg.LogAccess)+ if err != nil {+ return err+ }+ if mode != asymptoteobserve.LogAccessRestricted {+ return nil+ }+ if cfg.UserMode {+ return fmt.Errorf("--log-access %s applies to a system install; a user-mode log is already private to its user", mode)+ }+ if runtime.GOOS == "windows" {+ return fmt.Errorf("--log-access %s is not available on Windows yet; the system log keeps its shared access", mode)+ }+ return nil+}++// logAccessOf is the mode an install reports: the system log's, or nothing in user mode.+func logAccessOf(cfg endpointconfig.Config) string {+ if cfg.UserMode {+ return ""+ }+ if cfg.RestrictedLogAccess() {+ return asymptoteobserve.LogAccessRestricted+ }+ return asymptoteobserve.LogAccessShared+}++// prepareSystemLog readies the system log directory for the configured access mode before anything+// writes there, and reports whether it created the readers group.+//+// Shared is what install has always done: a directory every hook can reach and a log every hook can+// append to. Switching back from restricted also removes the marker and widens the files again, so+// hooks can append the moment the collector stops serving the socket. Restricted creates the+// beacon-log group if it is missing, then narrows the directory and every log file to root and that+// group, after writing the marker that keeps the root writers from widening them again.+func prepareSystemLog(cfg endpointconfig.Config) (groupCreated bool, err error) {+ if !cfg.RestrictedLogAccess() {+ // A collector that was killed while restricted leaves its socket behind. Hooks fall back+ // to appending directly either way, but each would try the dead socket first.+ _ = removeStaleIngestSocket(localingest.DefaultSocketPath)+ return false, shareSystemLog(cfg.LogPath)+ }+ gid, created, err := ensureLogReadersGroup(loggroup.Name)+ if err != nil {+ return false, fmt.Errorf("the %s group: %w", loggroup.Name, err)+ }+ if err := restrictSystemLog(cfg.LogPath, gid); err != nil {+ return created, err+ }+ return created, nil+}++// logAccessRestorer records the system log's access mode before an install changes it and returns+// what puts it back. Restoring is best effort, like the rest of a rollback.+func logAccessRestorer(cfg endpointconfig.Config) func() {+ wasRestricted := logIsRestricted(cfg.LogPath)+ return func() {+ if logIsRestricted(cfg.LogPath) == wasRestricted {+ return+ }+ if wasRestricted {+ if gid, _, err := ensureLogReadersGroup(loggroup.Name); err == nil {+ _ = restrictSystemLog(cfg.LogPath, gid)+ }+ return+ }+ _ = shareSystemLog(cfg.LogPath)+ }+}diff --git a/cli/beacon/internal/endpoint/lifecycle/logaccess_test.go b/cli/beacon/internal/endpoint/lifecycle/logaccess_test.gonew file mode 100644index 00000000..28852f56--- /dev/null+++ b/cli/beacon/internal/endpoint/lifecycle/logaccess_test.go@@ -0,0 +1,190 @@+package lifecycle++import (+ "errors"+ "runtime"+ "strings"+ "testing"++ endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/loggroup"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/service"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv"+)++// A repair or package upgrade re-runs install, which rebuilds config.json. A restricted log that+// came back shared would be world-writable again with nobody having asked for it.+func TestBuildConfigKeepsTheLogAccessMode(t *testing.T) {+ testenv.SetHome(t, t.TempDir())+ writeInventoryConfig(t, true, `{"user_mode":true,"log_path":"/tmp/runtime.jsonl","log_access":"restricted"}`)+ if cfg := buildConfig(InstallOptions{UserMode: true}); cfg.LogAccess != "restricted" {+ t.Fatalf("LogAccess = %q, want the existing mode carried over", cfg.LogAccess)+ }+ if cfg := buildConfig(InstallOptions{UserMode: true, LogAccess: "shared"}); cfg.LogAccess != "shared" {+ t.Fatalf("LogAccess = %q, want the explicit option to win", cfg.LogAccess)+ }+}++func TestBuildConfigWithoutAnExistingConfigIsShared(t *testing.T) {+ testenv.SetHome(t, t.TempDir())+ if cfg := buildConfig(InstallOptions{UserMode: true}); cfg.LogAccess != "" || cfg.RestrictedLogAccess() {+ t.Fatalf("a fresh install has log access %q", cfg.LogAccess)+ }+}++func TestValidateLogAccess(t *testing.T) {+ system := func(mode string) endpointconfig.Config {+ return endpointconfig.Config{UserMode: false, LogAccess: mode}+ }+ for _, mode := range []string{"", "shared", "Shared"} {+ if err := validateLogAccess(system(mode)); err != nil {+ t.Errorf("validateLogAccess(%q) = %v", mode, err)+ }+ }+ if err := validateLogAccess(system("open")); err == nil {+ t.Error("accepted an unknown mode")+ }+ err := validateLogAccess(endpointconfig.Config{UserMode: true, LogAccess: "restricted"})+ if err == nil || !strings.Contains(err.Error(), "system install") {+ t.Errorf("restricted in user mode = %v, want a refusal naming the system install", err)+ }+ err = validateLogAccess(system("restricted"))+ if runtime.GOOS == "windows" {+ if err == nil {+ t.Error("restricted was accepted on Windows")+ }+ } else if err != nil {+ t.Errorf("restricted on a system install = %v", err)+ }+}++type logAccessCalls struct {+ ensured []string+ restricted []int+ shared int+ staleRemoved int+}++func fakeLogAccess(t *testing.T, gid int, created bool, ensureErr, restrictErr error) *logAccessCalls {+ t.Helper()+ calls := &logAccessCalls{}+ prevEnsure, prevRestrict, prevShare := ensureLogReadersGroup, restrictSystemLog, shareSystemLog+ t.Cleanup(func() { ensureLogReadersGroup, restrictSystemLog, shareSystemLog = prevEnsure, prevRestrict, prevShare })+ ensureLogReadersGroup = func(name string) (int, bool, error) {+ calls.ensured = append(calls.ensured, name)+ return gid, created, ensureErr+ }+ restrictSystemLog = func(path string, g int) error {+ calls.restricted = append(calls.restricted, g)+ return restrictErr+ }+ shareSystemLog = func(string) error {+ calls.shared+++ return nil+ }+ prevStale := removeStaleIngestSocket+ removeStaleIngestSocket = func(string) error { calls.staleRemoved++; return nil }+ t.Cleanup(func() { removeStaleIngestSocket = prevStale })+ return calls+}++func TestPrepareSystemLogRestricts(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("no restricted mode on Windows")+ }+ calls := fakeLogAccess(t, 4242, true, nil, nil)+ created, err := prepareSystemLog(endpointconfig.Config{LogPath: "/var/log/beacon-agent/runtime.jsonl", LogAccess: "restricted"})+ if err != nil || !created {+ t.Fatalf("prepareSystemLog = %v, %v", created, err)+ }+ if len(calls.ensured) != 1 || calls.ensured[0] != loggroup.Name {+ t.Fatalf("ensured groups %v", calls.ensured)+ }+ if len(calls.restricted) != 1 || calls.restricted[0] != 4242 || calls.shared != 0 {+ t.Fatalf("calls = %+v", calls)+ }+}++func TestPrepareSystemLogShares(t *testing.T) {+ calls := fakeLogAccess(t, 0, false, nil, nil)+ if _, err := prepareSystemLog(endpointconfig.Config{LogPath: "/var/log/beacon-agent/runtime.jsonl"}); err != nil {+ t.Fatal(err)+ }+ if calls.shared != 1 || calls.staleRemoved != 1 || len(calls.ensured) != 0 || len(calls.restricted) != 0 {+ t.Fatalf("calls = %+v", calls)+ }+}++func TestPrepareSystemLogStopsWhenTheGroupCannotBeMade(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("no restricted mode on Windows")+ }+ calls := fakeLogAccess(t, 0, false, errors.New("groupadd: permission denied"), nil)+ _, err := prepareSystemLog(endpointconfig.Config{LogPath: "/var/log/beacon-agent/runtime.jsonl", LogAccess: "restricted"})+ if err == nil || !strings.Contains(err.Error(), loggroup.Name) {+ t.Fatalf("err = %v", err)+ }+ if len(calls.restricted) != 0 {+ t.Fatal("narrowed the log without a group to give readers")+ }+}++func TestLogAccessOf(t *testing.T) {+ if got := logAccessOf(endpointconfig.Config{UserMode: true, LogAccess: "restricted"}); got != "" {+ t.Fatalf("user mode = %q", got)+ }+ if got := logAccessOf(endpointconfig.Config{}); got != "shared" {+ t.Fatalf("system default = %q", got)+ }+ if runtime.GOOS != "windows" {+ if got := logAccessOf(endpointconfig.Config{LogAccess: "restricted"}); got != "restricted" {+ t.Fatalf("restricted = %q", got)+ }+ }+}++func TestLogAccessRestorerPutsBackWhatAFailedInstallChanged(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("no restricted mode on Windows")+ }+ restricted := false+ prev := logIsRestricted+ logIsRestricted = func(string) bool { return restricted }+ t.Cleanup(func() { logIsRestricted = prev })+ calls := fakeLogAccess(t, 77, false, nil, nil)+ cfg := endpointconfig.Config{LogPath: "/var/log/beacon-agent/runtime.jsonl"}++ // Shared before; the failed install restricted it.+ restore := logAccessRestorer(cfg)+ restricted = true+ restore()+ if calls.shared != 1 || len(calls.restricted) != 0 {+ t.Fatalf("restoring a shared log: %+v", calls)+ }++ // Restricted before; the failed install shared it.+ restricted = true+ restore = logAccessRestorer(cfg)+ restricted = false+ restore()+ if len(calls.restricted) != 1 || calls.restricted[0] != 77 {+ t.Fatalf("restoring a restricted log: %+v", calls)+ }++ // Unchanged: nothing to do.+ restore = logAccessRestorer(cfg)+ restore()+ if calls.shared != 1 || len(calls.restricted) != 1 {+ t.Fatalf("an unchanged mode was touched: %+v", calls)+ }+}++func TestRollbackRestoresTheLogAccessMode(t *testing.T) {+ called := false+ tx := newInstallRollback(service.Manager{UserMode: true, Kind: service.KindSupervised})+ tx.RestoreLogAccess = func() { called = true }+ tx.Rollback(Manifest{})+ if !called {+ t.Fatal("Rollback did not restore the log access mode")+ }+}diff --git a/cli/beacon/internal/endpoint/loggroup/loggroup.go b/cli/beacon/internal/endpoint/loggroup/loggroup.gonew file mode 100644index 00000000..9c38a2f5--- /dev/null+++ b/cli/beacon/internal/endpoint/loggroup/loggroup.go@@ -0,0 +1,114 @@+// Package loggroup manages the group that may read a restricted system runtime log.+//+// In the restricted log access mode the system log is 0640 root:beacon-log: root writes it, members+// of the group read it, nobody else has access. Install creates the group when it is missing and+// puts nobody in it; an administrator adds the accounts that should read every user's sessions,+// such as a log shipper's service account. Uninstall leaves the group, since it may have members an+// administrator chose.+//+// Accounts and groups on both platforms can come from a directory service (SSSD, LDAP, AD, macOS+// Directory Services) that Go's CGO_ENABLED=0 os/user never consults, so lookups go through the+// system's own tools -- getent on Linux, dscl on macOS -- the way package osuser resolves accounts.+package loggroup++import (+ "context"+ "errors"+ "fmt"+ "os/exec"+ "strconv"+ "strings"+ "time"++ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"+)++// Name is the readers group.+const Name = asymptoteobserve.RuntimeLogReadersGroup++const commandTimeout = 10 * time.Second++// ErrUnsupported is returned where Beacon cannot manage groups.+var ErrUnsupported = errors.New("managing the log readers group is not supported on this platform")++// run executes a system tool by absolute path and returns its standard output. A variable so tests+// can stand in for the account database, which a test cannot change portably.+var run = func(path string, args ...string) (string, error) {+ ctx, cancel := context.WithTimeout(context.Background(), commandTimeout)+ defer cancel()+ cmd := exec.CommandContext(ctx, path, args...)+ out, err := cmd.Output()+ if err != nil {+ var exitErr *exec.ExitError+ if errors.As(err, &exitErr) && len(exitErr.Stderr) > 0 {+ return string(out), fmt.Errorf("%s %s: %w: %s", path, strings.Join(args, " "), err, strings.TrimSpace(string(exitErr.Stderr)))+ }+ return string(out), fmt.Errorf("%s %s: %w", path, strings.Join(args, " "), err)+ }+ return string(out), nil+}++// lookPath finds the first of candidates that exists. A variable for tests.+var lookPath = func(candidates ...string) (string, bool) {+ for _, candidate := range candidates {+ if path, err := exec.LookPath(candidate); err == nil {+ return path, true+ }+ }+ return "", false+}++// Ensure returns the group's gid, creating it as a system group when it does not exist.+func Ensure(name string) (gid int, created bool, err error) {+ if err := validName(name); err != nil {+ return 0, false, err+ }+ gid, found, err := Lookup(name)+ if err != nil {+ return 0, false, err+ }+ if found {+ return gid, false, nil+ }+ if err := create(name); err != nil {+ return 0, false, fmt.Errorf("create group %s: %w", name, err)+ }+ gid, found, err = Lookup(name)+ if err != nil {+ return 0, false, err+ }+ if !found {+ return 0, false, fmt.Errorf("group %s was created but cannot be found", name)+ }+ return gid, true, nil+}++// Lookup returns the group's gid and whether it exists.+func Lookup(name string) (gid int, found bool, err error) {+ if err := validName(name); err != nil {+ return 0, false, err+ }+ return lookup(name)+}++// validName accepts only what both platforms accept as a group name, so a name is never read as an+// option or a path by the tools it is passed to.+func validName(name string) error {+ if name == "" || len(name) > 32 || strings.HasPrefix(name, "-") {+ return fmt.Errorf("invalid group name %q", name)+ }+ for _, r := range name {+ if !(r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '-' || r == '_' || r == '.') {+ return fmt.Errorf("invalid group name %q", name)+ }+ }+ return nil+}++func parseGID(field string) (int, error) {+ gid, err := strconv.Atoi(strings.TrimSpace(field))+ if err != nil || gid < 0 {+ return 0, fmt.Errorf("unexpected gid %q", field)+ }+ return gid, nil+}diff --git a/cli/beacon/internal/endpoint/loggroup/loggroup_darwin.go b/cli/beacon/internal/endpoint/loggroup/loggroup_darwin.gonew file mode 100644index 00000000..f8018d5c--- /dev/null+++ b/cli/beacon/internal/endpoint/loggroup/loggroup_darwin.go@@ -0,0 +1,43 @@+//go:build darwin++package loggroup++import (+ "fmt"+ "strings"+)++const (+ dscl = "/usr/bin/dscl"+ dseditgroup = "/usr/sbin/dseditgroup"+)++// lookup reads the group from the local directory node, where dseditgroup creates it.+func lookup(name string) (int, bool, error) {+ out, err := run(dscl, ".", "-read", "/Groups/"+name, "PrimaryGroupID")+ if err != nil {+ // dscl reports a missing record on stderr and exits non-zero; anything else it says on+ // stdout is output we cannot use.+ if strings.Contains(err.Error(), "eDSRecordNotFound") || strings.Contains(err.Error(), "does not exist") {+ return 0, false, nil+ }+ return 0, false, err+ }+ for _, line := range strings.Split(out, "\n") {+ if value, ok := strings.CutPrefix(strings.TrimSpace(line), "PrimaryGroupID:"); ok {+ gid, err := parseGID(value)+ if err != nil {+ return 0, false, err+ }+ return gid, true, nil+ }+ }+ return 0, false, fmt.Errorf("no PrimaryGroupID in %q", strings.TrimSpace(out))+}++func create(name string) error {+ if _, err := run(dseditgroup, "-o", "create", "-r", "Beacon log readers", name); err != nil {+ return err+ }+ return nil+}diff --git a/cli/beacon/internal/endpoint/loggroup/loggroup_darwin_test.go b/cli/beacon/internal/endpoint/loggroup/loggroup_darwin_test.gonew file mode 100644index 00000000..56725248--- /dev/null+++ b/cli/beacon/internal/endpoint/loggroup/loggroup_darwin_test.go@@ -0,0 +1,79 @@+//go:build darwin++package loggroup++import (+ "errors"+ "strings"+ "testing"+)++func fakeDirectory(t *testing.T, groups map[string]int) *[]string {+ t.Helper()+ var calls []string+ prev := run+ t.Cleanup(func() { run = prev })+ run = func(path string, args ...string) (string, error) {+ calls = append(calls, path+" "+strings.Join(args, " "))+ switch path {+ case dscl:+ name := strings.TrimPrefix(args[2], "/Groups/")+ gid, ok := groups[name]+ if !ok {+ return "", errors.New("dscl: <dscl_cmd> DS Error: -14136 (eDSRecordNotFound)")+ }+ return "PrimaryGroupID: " + itoa(gid) + "\n", nil+ case dseditgroup:+ groups[args[len(args)-1]] = 401+ return "", nil+ }+ t.Fatalf("unexpected command %s %v", path, args)+ return "", nil+ }+ return &calls+}++func itoa(n int) string {+ if n == 0 {+ return "0"+ }+ var b []byte+ for n > 0 {+ b = append([]byte{byte('0' + n%10)}, b...)+ n /= 10+ }+ return string(b)+}++func TestDarwinEnsureCreatesTheGroup(t *testing.T) {+ calls := fakeDirectory(t, map[string]int{})+ gid, created, err := Ensure("beacon-log")+ if err != nil || gid != 401 || !created {+ t.Fatalf("Ensure = %d, %v, %v", gid, created, err)+ }+ want := dseditgroup + " -o create -r Beacon log readers beacon-log"+ found := false+ for _, c := range *calls {+ found = found || c == want+ }+ if !found {+ t.Fatalf("calls = %v", *calls)+ }+}++func TestDarwinEnsureFindsTheGroup(t *testing.T) {+ calls := fakeDirectory(t, map[string]int{"beacon-log": 502})+ gid, created, err := Ensure("beacon-log")+ if err != nil || gid != 502 || created || len(*calls) != 1 {+ t.Fatalf("Ensure = %d, %v, %v, calls %v", gid, created, err, *calls)+ }+}++func TestDarwinLookupOnThisSystem(t *testing.T) {+ if gid, found, err := Lookup("wheel"); err != nil || !found || gid != 0 {+ t.Fatalf("Lookup(wheel) = %d, %v, %v", gid, found, err)+ }+ if _, found, err := Lookup("beacon-no-such-group-x9"); err != nil || found {+ t.Fatalf("Lookup(missing) = %v, %v", found, err)+ }+}diff --git a/cli/beacon/internal/endpoint/loggroup/loggroup_linux.go b/cli/beacon/internal/endpoint/loggroup/loggroup_linux.gonew file mode 100644index 00000000..3fc2dee9--- /dev/null+++ b/cli/beacon/internal/endpoint/loggroup/loggroup_linux.go@@ -0,0 +1,49 @@+//go:build linux++package loggroup++import (+ "errors"+ "fmt"+ "os/exec"+ "strings"+)++// lookup asks getent, which consults NSS. getent exits 2 when the key is not found.+func lookup(name string) (int, bool, error) {+ getent, ok := lookPath("getent", "/usr/bin/getent", "/bin/getent")+ if !ok {+ return 0, false, errors.New("getent is not available to look up groups")+ }+ out, err := run(getent, "group", name)+ if err != nil {+ var exitErr *exec.ExitError+ if errors.As(err, &exitErr) && exitErr.ExitCode() == 2 {+ return 0, false, nil+ }+ return 0, false, err+ }+ // name:password:gid:members+ fields := strings.Split(strings.TrimSpace(strings.SplitN(out, "\n", 2)[0]), ":")+ if len(fields) < 3 || fields[0] != name {+ return 0, false, fmt.Errorf("unexpected getent output %q", strings.TrimSpace(out))+ }+ gid, err := parseGID(fields[2])+ if err != nil {+ return 0, false, err+ }+ return gid, true, nil+}++// create adds a local system group with groupadd, or busybox addgroup where that is all there is.+func create(name string) error {+ if groupadd, ok := lookPath("groupadd", "/usr/sbin/groupadd", "/sbin/groupadd"); ok {+ _, err := run(groupadd, "--system", name)+ return err+ }+ if addgroup, ok := lookPath("addgroup", "/usr/sbin/addgroup", "/sbin/addgroup"); ok {+ _, err := run(addgroup, "-S", name)+ return err+ }+ return errors.New("neither groupadd nor addgroup is available")+}diff --git a/cli/beacon/internal/endpoint/loggroup/loggroup_linux_test.go b/cli/beacon/internal/endpoint/loggroup/loggroup_linux_test.gonew file mode 100644index 00000000..1a4951be--- /dev/null+++ b/cli/beacon/internal/endpoint/loggroup/loggroup_linux_test.go@@ -0,0 +1,167 @@+//go:build linux++package loggroup++import (+ "errors"+ "os/exec"+ "strings"+ "testing"+)++type fakeSystem struct {+ groups map[string]string // name -> getent line+ calls []string+ failAdd error+ tools map[string]bool+}++// exitCode returns a real *exec.ExitError with the given code, the shape run returns.+func exitCode(t *testing.T, code int) error {+ t.Helper()+ err := exec.Command("sh", "-c", "exit "+string(rune('0'+code))).Run()+ var exitErr *exec.ExitError+ if !errors.As(err, &exitErr) {+ t.Fatalf("could not produce an exit error: %v", err)+ }+ return err+}++func install(t *testing.T, sys *fakeSystem) {+ t.Helper()+ prevRun, prevLook := run, lookPath+ t.Cleanup(func() { run, lookPath = prevRun, prevLook })+ lookPath = func(candidates ...string) (string, bool) {+ for _, c := range candidates {+ if sys.tools[c] {+ return "/usr/sbin/" + c, true+ }+ }+ return "", false+ }+ run = func(path string, args ...string) (string, error) {+ call := path + " " + strings.Join(args, " ")+ sys.calls = append(sys.calls, call)+ switch {+ case strings.HasSuffix(path, "getent"):+ if line, ok := sys.groups[args[1]]; ok {+ return line + "\n", nil+ }+ return "", exitCode(t, 2)+ case strings.HasSuffix(path, "groupadd"), strings.HasSuffix(path, "addgroup"):+ if sys.failAdd != nil {+ return "", sys.failAdd+ }+ name := args[len(args)-1]+ sys.groups[name] = name + ":x:998:"+ return "", nil+ }+ t.Fatalf("unexpected command %s", call)+ return "", nil+ }+}++func TestEnsureFindsAnExistingGroup(t *testing.T) {+ sys := &fakeSystem{groups: map[string]string{"beacon-log": "beacon-log:x:1234:splunk,fluentd"}, tools: map[string]bool{"getent": true, "groupadd": true}}+ install(t, sys)+ gid, created, err := Ensure("beacon-log")+ if err != nil || gid != 1234 || created {+ t.Fatalf("Ensure = %d, %v, %v", gid, created, err)+ }+ for _, call := range sys.calls {+ if strings.Contains(call, "groupadd") {+ t.Fatal("created a group that already existed")+ }+ }+}++func TestEnsureCreatesASystemGroup(t *testing.T) {+ sys := &fakeSystem{groups: map[string]string{}, tools: map[string]bool{"getent": true, "groupadd": true}}+ install(t, sys)+ gid, created, err := Ensure("beacon-log")+ if err != nil || gid != 998 || !created {+ t.Fatalf("Ensure = %d, %v, %v", gid, created, err)+ }+ if !contains(sys.calls, "/usr/sbin/groupadd --system beacon-log") {+ t.Fatalf("calls = %v", sys.calls)+ }+}++func TestEnsureFallsBackToBusyboxAddgroup(t *testing.T) {+ sys := &fakeSystem{groups: map[string]string{}, tools: map[string]bool{"getent": true, "addgroup": true}}+ install(t, sys)+ if _, created, err := Ensure("beacon-log"); err != nil || !created {+ t.Fatalf("Ensure = %v, %v", created, err)+ }+ if !contains(sys.calls, "/usr/sbin/addgroup -S beacon-log") {+ t.Fatalf("calls = %v", sys.calls)+ }+}++func TestEnsureReportsAFailedCreate(t *testing.T) {+ sys := &fakeSystem{groups: map[string]string{}, tools: map[string]bool{"getent": true, "groupadd": true}, failAdd: errors.New("permission denied")}+ install(t, sys)+ if _, _, err := Ensure("beacon-log"); err == nil || !strings.Contains(err.Error(), "permission denied") {+ t.Fatalf("Ensure = %v", err)+ }+}++func TestEnsureWithoutTools(t *testing.T) {+ install(t, &fakeSystem{groups: map[string]string{}, tools: map[string]bool{"getent": true}})+ if _, _, err := Ensure("beacon-log"); err == nil {+ t.Fatal("Ensure succeeded with no way to create a group")+ }+ install(t, &fakeSystem{groups: map[string]string{}, tools: map[string]bool{}})+ if _, _, err := Lookup("beacon-log"); err == nil {+ t.Fatal("Lookup succeeded without getent")+ }+}++func TestLookupRejectsOutputForAnotherGroup(t *testing.T) {+ install(t, &fakeSystem{groups: map[string]string{"beacon-log": "other:x:5:"}, tools: map[string]bool{"getent": true}})+ if _, _, err := Lookup("beacon-log"); err == nil {+ t.Fatal("accepted getent output naming another group")+ }+}++func TestLookupRejectsAGarbledGID(t *testing.T) {+ install(t, &fakeSystem{groups: map[string]string{"beacon-log": "beacon-log:x:abc:"}, tools: map[string]bool{"getent": true}})+ if _, _, err := Lookup("beacon-log"); err == nil {+ t.Fatal("accepted a non-numeric gid")+ }+}++func TestInvalidNamesNeverReachATool(t *testing.T) {+ sys := &fakeSystem{groups: map[string]string{}, tools: map[string]bool{"getent": true, "groupadd": true}}+ install(t, sys)+ for _, name := range []string{"", "-rf", "a b", "a/b", "a;b", strings.Repeat("x", 33), "naïve"} {+ if _, _, err := Ensure(name); err == nil {+ t.Errorf("Ensure(%q) accepted", name)+ }+ }+ if len(sys.calls) != 0 {+ t.Fatalf("invalid names reached a tool: %v", sys.calls)+ }+}++// Against the real account database: root's group always exists, and a random name never does.+func TestLookupOnThisSystem(t *testing.T) {+ if _, err := exec.LookPath("getent"); err != nil {+ t.Skip("no getent")+ }+ if gid, found, err := Lookup("root"); err != nil || !found || gid != 0 {+ t.Fatalf("Lookup(root) = %d, %v, %v", gid, found, err)+ }+ if _, found, err := Lookup("beacon-no-such-group-x9"); err != nil || found {+ t.Fatalf("Lookup(missing) = %v, %v", found, err)+ }+}++func contains(calls []string, want string) bool {+ for _, c := range calls {+ if c == want {+ return true+ }+ }+ return false+}diff --git a/cli/beacon/internal/endpoint/loggroup/loggroup_other.go b/cli/beacon/internal/endpoint/loggroup/loggroup_other.gonew file mode 100644index 00000000..afe14982--- /dev/null+++ b/cli/beacon/internal/endpoint/loggroup/loggroup_other.go@@ -0,0 +1,7 @@+//go:build !linux && !darwin++package loggroup++func lookup(string) (int, bool, error) { return 0, false, ErrUnsupported }++func create(string) error { return ErrUnsupported }diff --git a/cli/beacon/internal/endpoint/writer/logaccess_other.go b/cli/beacon/internal/endpoint/writer/logaccess_other.gonew file mode 100644index 00000000..3d94b784--- /dev/null+++ b/cli/beacon/internal/endpoint/writer/logaccess_other.go@@ -0,0 +1,14 @@+//go:build !unix++package writer++import "errors"++// RestrictSystemLog is unavailable here: the restricted mode needs the local ingest socket and+// kernel peer credentials, which Beacon does not offer on Windows.+func RestrictSystemLog(string, int) error {+ return errors.New("the restricted log access mode is not available on this platform")+}++// ShareSystemLog is EnsureSystemLogWritable here, where the log is always shared.+func ShareSystemLog(path string) error { return EnsureSystemLogWritable(path) }diff --git a/cli/beacon/internal/endpoint/writer/logaccess_unix.go b/cli/beacon/internal/endpoint/writer/logaccess_unix.gonew file mode 100644index 00000000..4f58b1cc--- /dev/null+++ b/cli/beacon/internal/endpoint/writer/logaccess_unix.go@@ -0,0 +1,252 @@+//go:build unix++package writer++import (+ "context"+ "errors"+ "fmt"+ "io"+ "os"+ "path/filepath"+ "slices"+ "strconv"+ "strings"+ "syscall"+ "time"++ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/filelock"+)++// logOwnerUID owns a restricted log; tests that cannot be root use their own uid.+var logOwnerUID = 0++// restrictLockWait bounds how long RestrictSystemLog waits for the log's lock.+var restrictLockWait = 5 * time.Second++// RestrictSystemLog puts the system log at path in the restricted access mode: the directory+// becomes root:gid 02750, the marker that tells every root writer the mode is written, and the log+// and its archives become root:gid 0640 and its lock root-only 0600. From then on only root writes+// the log and only root and the readers group read it; hooks reach it through the collector's+// ingest socket.+//+// The marker is written first, so a root writer that opens the log in between already creates or+// settles it in the restricted mode rather than widening it back. Hooks that try to append+// directly in the meantime are refused and spool their events until the collector serves the+// socket.+//+// A log that was shared is not narrowed in place. Any account could have opened it, or its lock,+// while it was 0666, and a descriptor opened then keeps working whatever the file's mode becomes:+// it could still rewrite events, read new ones, or hold the lock and stall the collector. So each+// file is copied into a new root-created file with the restricted mode and renamed over the old+// one, under the log's lock, leaving any such descriptor on an inode nothing writes or reads any+// more. A log already restricted, whose files already carry the right owner and mode, is left as it+// is, so repair and upgrades do not copy the log every time.+func RestrictSystemLog(path string, gid int) error {+ dir := filepath.Dir(path)+ if err := os.MkdirAll(dir, 0o750); err != nil {+ return fmt.Errorf("create the log directory %s: %w", dir, err)+ }+ if err := ensureRealDirectory(dir); err != nil {+ return err+ }+ if err := os.Lchown(dir, logOwnerUID, gid); err != nil {+ return fmt.Errorf("set the owner of %s: %w", dir, err)+ }+ if err := os.Chmod(dir, asymptoteobserve.RestrictedRuntimeLogDirMode); err != nil {+ return fmt.Errorf("set the mode of %s: %w", dir, err)+ }+ wasRestricted := asymptoteobserve.RuntimeLogAccessRestricted(path)+ if err := asymptoteobserve.SetRuntimeLogAccess(path, asymptoteobserve.LogAccessRestricted); err != nil {+ return fmt.Errorf("record the restricted mode: %w", err)+ }++ // Writers append under the lock, so holding it keeps a line from landing in the old log after+ // its copy was taken. The lock itself is replaced last, while still held.+ // The wait is bounded: while the log was shared any account could take that lock and keep it,+ // and the restriction must not hang on it. Past the bound it goes ahead unlocked, at the risk+ // of one line written in between going to the old file.+ lock, err := os.OpenFile(path+".lock", os.O_RDWR|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)+ if err == nil {+ defer lock.Close()+ ctx, cancel := context.WithTimeout(context.Background(), restrictLockWait)+ held, err := filelock.ExclusiveContext(ctx, lock)+ cancel()+ if err == nil {+ defer held.Release()+ }+ }+ base := filepath.Base(path)+ return forLogFamily(path, func(name string) error {+ mode := asymptoteobserve.RestrictedRuntimeLogFileMode+ if name == base+".lock" {+ mode = asymptoteobserve.RestrictedRuntimeLogLockMode+ }+ return restrictOne(filepath.Join(dir, name), mode, gid, wasRestricted)+ }, base+".lock")+}++// restrictOne gives the file at path the restricted owner and mode: in place when the log was+// already restricted and the file already matches, and otherwise by replacing it with a copy.+func restrictOne(path string, mode os.FileMode, gid int, wasRestricted bool) error {+ src, err := os.OpenFile(path, os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)+ if err != nil {+ if errors.Is(err, syscall.ELOOP) || errors.Is(err, os.ErrNotExist) {+ return nil // a link where a log file belongs is left alone+ }+ return err+ }+ defer src.Close()+ info, err := src.Stat()+ if err != nil {+ return err+ }+ if !info.Mode().IsRegular() {+ return nil+ }+ stat, ok := info.Sys().(*syscall.Stat_t)+ if !ok {+ return errors.New("cannot read the file's owner")+ }+ if wasRestricted && int(stat.Uid) == logOwnerUID && int(stat.Gid) == gid && info.Mode().Perm() == mode && stat.Nlink == 1 {+ return nil+ }+ tmp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".restrict-*")+ if err != nil {+ return err+ }+ tmpName := tmp.Name()+ defer os.Remove(tmpName)+ if err := tmp.Chown(logOwnerUID, gid); err != nil {+ _ = tmp.Close()+ return err+ }+ if err := tmp.Chmod(mode); err != nil {+ _ = tmp.Close()+ return err+ }+ if _, err := io.Copy(tmp, src); err != nil {+ _ = tmp.Close()+ return err+ }+ if err := tmp.Sync(); err != nil {+ _ = tmp.Close()+ return err+ }+ if err := tmp.Close(); err != nil {+ return err+ }+ return os.Rename(tmpName, path)+}++// ShareSystemLog returns the system log at path to the shared access mode it had before the+// restricted mode existed: the marker goes, the directory is 0755, and the live log and its lock+// are writable by everyone again so hooks can append directly. Archives become 0644: no writer ever+// appends to them. Group ownership is left as it is; it grants nothing the mode does not.+//+// A no-op beyond EnsureSystemLogWritable for a log that was never restricted, whose files already+// carry those modes.+func ShareSystemLog(path string) error {+ wasRestricted := asymptoteobserve.RuntimeLogAccessRestricted(path)+ if err := asymptoteobserve.SetRuntimeLogAccess(path, asymptoteobserve.LogAccessShared); err != nil {+ return fmt.Errorf("remove the restricted mode marker: %w", err)+ }+ if err := EnsureSystemLogWritable(path); err != nil {+ return err+ }+ if !wasRestricted {+ return nil+ }+ return settleLogFamily(path, func(f *os.File) error {+ mode := os.FileMode(0o644)+ if name := filepath.Base(f.Name()); name == filepath.Base(path) || name == filepath.Base(path)+".lock" {+ mode = asymptoteobserve.SharedRuntimeLogFileMode+ }+ return f.Chmod(mode)+ })+}++func ensureRealDirectory(dir string) error {+ info, err := os.Lstat(dir)+ if err != nil {+ return err+ }+ if !info.IsDir() {+ return fmt.Errorf("%s is not a directory", dir)+ }+ return nil+}++// settleLogFamily applies fn to the log at path, its lock and every numbered archive beside it,+// opening each without following a link and skipping anything that is not a regular file with a+// single link -- the same refusals the root writers apply, so the directory cannot steer this into+// changing the owner or mode of some other file.+func settleLogFamily(path string, fn func(*os.File) error) error {+ dir := filepath.Dir(path)+ return forLogFamily(path, func(name string) error {+ return settleOne(filepath.Join(dir, name), fn)+ })+}++// forLogFamily calls fn with the name of the log at path, its lock and every numbered archive in+// its directory. Names listed in last are visited after the others.+func forLogFamily(path string, fn func(name string) error, last ...string) error {+ dir := filepath.Dir(path)+ base := filepath.Base(path)+ entries, err := os.ReadDir(dir)+ if err != nil {+ return err+ }+ var names, deferred []string+ for _, entry := range entries {+ name := entry.Name()+ if !inLogFamily(base, name) {+ continue+ }+ if slices.Contains(last, name) {+ deferred = append(deferred, name)+ continue+ }+ names = append(names, name)+ }+ var errs []error+ for _, name := range append(names, deferred...) {+ if err := fn(name); err != nil {+ errs = append(errs, fmt.Errorf("%s: %w", name, err))+ }+ }+ return errors.Join(errs...)+}++func inLogFamily(base, name string) bool {+ if name == base || name == base+".lock" {+ return true+ }+ suffix, ok := strings.CutPrefix(name, base+".")+ if !ok {+ return false+ }+ n, err := strconv.Atoi(suffix)+ return err == nil && n > 0+}++func settleOne(path string, fn func(*os.File) error) error {+ f, err := os.OpenFile(path, os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)+ if err != nil {+ if errors.Is(err, syscall.ELOOP) {+ return nil // a link where a log file belongs is left alone+ }+ return err+ }+ defer f.Close()+ info, err := f.Stat()+ if err != nil {+ return err+ }+ stat, ok := info.Sys().(*syscall.Stat_t)+ if !info.Mode().IsRegular() || !ok || stat.Nlink != 1 {+ return nil+ }+ return fn(f)+}diff --git a/cli/beacon/internal/endpoint/writer/logaccess_unix_test.go b/cli/beacon/internal/endpoint/writer/logaccess_unix_test.gonew file mode 100644index 00000000..8d98e584--- /dev/null+++ b/cli/beacon/internal/endpoint/writer/logaccess_unix_test.go@@ -0,0 +1,332 @@+//go:build unix++package writer++import (+ "os"+ "path/filepath"+ "strings"+ "syscall"+ "testing"+ "time"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"+)++func ownLogs(t *testing.T) {+ t.Helper()+ prev := logOwnerUID+ logOwnerUID = os.Geteuid()+ t.Cleanup(func() { logOwnerUID = prev })+}++func ownerOf(t *testing.T, path string) (uid, gid int, mode os.FileMode) {+ t.Helper()+ info, err := os.Lstat(path)+ if err != nil {+ t.Fatal(err)+ }+ st := info.Sys().(*syscall.Stat_t)+ return int(st.Uid), int(st.Gid), info.Mode()+}++func systemLogFixture(t *testing.T) string {+ t.Helper()+ testenv.SetHome(t, t.TempDir())+ dir := filepath.Join(t.TempDir(), "beacon-agent")+ if err := os.MkdirAll(dir, 0o755); err != nil {+ t.Fatal(err)+ }+ path := filepath.Join(dir, "runtime.jsonl")+ for _, name := range []string{"runtime.jsonl", "runtime.jsonl.lock", "runtime.jsonl.1", "runtime.jsonl.2", "unrelated.txt", "runtime.jsonl.bak"} {+ target := filepath.Join(dir, name)+ if err := os.WriteFile(target, []byte("x\n"), 0o666); err != nil {+ t.Fatal(err)+ }+ if err := os.Chmod(target, 0o666); err != nil {+ t.Fatal(err)+ }+ }+ return path+}++func TestRestrictSystemLogNarrowsTheDirectoryAndEveryLogFile(t *testing.T) {+ testenv.RequirePOSIXFileModes(t)+ ownLogs(t)+ path := systemLogFixture(t)+ gid := os.Getegid()+ if err := RestrictSystemLog(path, gid); err != nil {+ t.Fatal(err)+ }+ dir := filepath.Dir(path)+ uid, g, mode := ownerOf(t, dir)+ if uid != os.Geteuid() || g != gid || mode.Perm() != 0o750 || mode&os.ModeSetgid == 0 {+ t.Fatalf("dir = uid %d gid %d mode %v; want the readers group, 0750 and setgid", uid, g, mode)+ }+ for _, name := range []string{"runtime.jsonl", "runtime.jsonl.lock", "runtime.jsonl.1", "runtime.jsonl.2"} {+ want := asymptoteobserve.RestrictedRuntimeLogFileMode+ if name == "runtime.jsonl.lock" {+ want = asymptoteobserve.RestrictedRuntimeLogLockMode+ }+ uid, g, mode := ownerOf(t, filepath.Join(dir, name))+ if uid != os.Geteuid() || g != gid || mode.Perm() != want {+ t.Fatalf("%s = uid %d gid %d mode %o", name, uid, g, mode.Perm())+ }+ if data, _ := os.ReadFile(filepath.Join(dir, name)); string(data) != "x\n" {+ t.Fatalf("%s lost its content: %q", name, data)+ }+ }+ // No temporary copies are left behind.+ entries, _ := os.ReadDir(dir)+ for _, e := range entries {+ if strings.Contains(e.Name(), ".restrict-") {+ t.Fatalf("left %s behind", e.Name())+ }+ }+ for _, name := range []string{"unrelated.txt", "runtime.jsonl.bak"} {+ if _, _, mode := ownerOf(t, filepath.Join(dir, name)); mode.Perm() != 0o666 {+ t.Fatalf("%s was changed to %o; only the log's own files may be", name, mode.Perm())+ }+ }+ data, err := os.ReadFile(asymptoteobserve.LogAccessMarkerPath(path))+ if err != nil || string(data) != "restricted\n" {+ t.Fatalf("marker = %q, %v", data, err)+ }+}++func TestRestrictSystemLogCreatesAMissingDirectory(t *testing.T) {+ testenv.RequirePOSIXFileModes(t)+ ownLogs(t)+ testenv.SetHome(t, t.TempDir())+ path := filepath.Join(t.TempDir(), "new", "runtime.jsonl")+ if err := RestrictSystemLog(path, os.Getegid()); err != nil {+ t.Fatal(err)+ }+ if _, _, mode := ownerOf(t, filepath.Dir(path)); mode.Perm() != 0o750 || mode&os.ModeSetgid == 0 {+ t.Fatalf("dir mode = %v", mode)+ }+}++func TestRestrictSystemLogLeavesLinksAlone(t *testing.T) {+ testenv.RequirePOSIXFileModes(t)+ ownLogs(t)+ path := systemLogFixture(t)+ outside := filepath.Join(t.TempDir(), "victim")+ if err := os.WriteFile(outside, nil, 0o666); err != nil {+ t.Fatal(err)+ }+ _ = os.Chmod(outside, 0o666)+ if err := os.Symlink(outside, path+".3"); err != nil {+ t.Fatal(err)+ }+ hard := filepath.Join(t.TempDir(), "hard-victim")+ if err := os.WriteFile(hard, nil, 0o666); err != nil {+ t.Fatal(err)+ }+ _ = os.Chmod(hard, 0o666)+ if err := os.Remove(path + ".2"); err != nil {+ t.Fatal(err)+ }+ if err := os.Link(hard, path+".2"); err != nil {+ t.Skipf("hard link across temp dirs: %v", err)+ }+ if err := RestrictSystemLog(path, os.Getegid()); err != nil {+ t.Fatal(err)+ }+ for _, victim := range []string{outside, hard} {+ if _, _, mode := ownerOf(t, victim); mode.Perm() != 0o666 {+ t.Fatalf("%s was changed through a link to %o", victim, mode.Perm())+ }+ }+}++func TestRestrictSystemLogRefusesALinkedDirectory(t *testing.T) {+ ownLogs(t)+ testenv.SetHome(t, t.TempDir())+ real := t.TempDir()+ link := filepath.Join(t.TempDir(), "beacon-agent")+ if err := os.Symlink(real, link); err != nil {+ t.Fatal(err)+ }+ if err := RestrictSystemLog(filepath.Join(link, "runtime.jsonl"), os.Getegid()); err == nil {+ t.Fatal("restricted a log directory reached through a link")+ }+}++// Back to shared: as root, which is the only account that can trust the marker it removes.+func TestShareSystemLogUndoesTheRestrictedMode(t *testing.T) {+ if os.Geteuid() != 0 {+ t.Skip("the restricted marker is trusted only when root owns it")+ }+ path := systemLogFixture(t)+ if err := RestrictSystemLog(path, 0); err != nil {+ t.Fatal(err)+ }+ if !asymptoteobserve.RuntimeLogAccessRestricted(path) {+ t.Fatal("fixture: not restricted")+ }+ if err := ShareSystemLog(path); err != nil {+ t.Fatal(err)+ }+ if asymptoteobserve.RuntimeLogAccessRestricted(path) {+ t.Fatal("still restricted")+ }+ dir := filepath.Dir(path)+ if _, _, mode := ownerOf(t, dir); mode.Perm() != 0o755 {+ t.Fatalf("dir mode = %v", mode)+ }+ for name, want := range map[string]os.FileMode{+ "runtime.jsonl": 0o666, "runtime.jsonl.lock": 0o666, "runtime.jsonl.1": 0o644, "runtime.jsonl.2": 0o644,+ } {+ if _, _, mode := ownerOf(t, filepath.Join(dir, name)); mode.Perm() != want {+ t.Fatalf("%s mode = %o, want %o", name, mode.Perm(), want)+ }+ }+}++func TestShareSystemLogOnANeverRestrictedLogChangesNoFiles(t *testing.T) {+ testenv.RequirePOSIXFileModes(t)+ path := systemLogFixture(t)+ if err := os.Chmod(path+".1", 0o600); err != nil {+ t.Fatal(err)+ }+ if err := ShareSystemLog(path); err != nil {+ t.Fatal(err)+ }+ if _, _, mode := ownerOf(t, path+".1"); mode.Perm() != 0o600 {+ t.Fatalf("an archive of a log that was never restricted was changed to %o", mode.Perm())+ }+}++func TestInLogFamily(t *testing.T) {+ for name, want := range map[string]bool{+ "runtime.jsonl": true, "runtime.jsonl.lock": true, "runtime.jsonl.1": true, "runtime.jsonl.12": true,+ "runtime.jsonl.0": false, "runtime.jsonl.-1": false, "runtime.jsonl.bak": false, "runtime.json": false,+ ".beacon-log-access": false, "other.jsonl.1": false,+ } {+ if got := inLogFamily("runtime.jsonl", name); got != want {+ t.Errorf("inLogFamily(%q) = %v, want %v", name, got, want)+ }+ }+}++// A descriptor someone opened while the log was 0666 must not keep working on the restricted log:+// not to rewrite it, not to read what is written after, not to hold its lock.+func TestRestrictSystemLogStrandsDescriptorsOpenedWhileShared(t *testing.T) {+ testenv.RequirePOSIXFileModes(t)+ ownLogs(t)+ path := systemLogFixture(t)+ held, err := os.OpenFile(path, os.O_RDWR, 0)+ if err != nil {+ t.Fatal(err)+ }+ defer held.Close()+ heldLock, err := os.OpenFile(path+".lock", os.O_RDWR, 0)+ if err != nil {+ t.Fatal(err)+ }+ defer heldLock.Close()++ if err := RestrictSystemLog(path, os.Getegid()); err != nil {+ t.Fatal(err)+ }+ if _, err := held.WriteAt([]byte("FORGED\n"), 0); err != nil {+ t.Fatal(err)+ }+ if err := held.Truncate(0); err != nil {+ t.Fatal(err)+ }+ if data, _ := os.ReadFile(path); string(data) != "x\n" {+ t.Fatalf("a descriptor opened before the restriction changed the log: %q", data)+ }+ // The collector's next append is not visible through the old descriptor.+ f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0)+ if err != nil {+ t.Fatal(err)+ }+ _, _ = f.WriteString("secret\n")+ _ = f.Close()+ buf := make([]byte, 64)+ n, _ := held.ReadAt(buf, 0)+ if strings.Contains(string(buf[:n]), "secret") {+ t.Fatal("a descriptor opened before the restriction reads new events")+ }+ // The old lock is not the lock any more.+ oldInfo, _ := heldLock.Stat()+ newInfo, err := os.Stat(path + ".lock")+ if err != nil {+ t.Fatal(err)+ }+ if os.SameFile(oldInfo, newInfo) {+ t.Fatal("the lock opened while shared is still the log's lock")+ }+}++func TestRestrictSystemLogLeavesAnAlreadyRestrictedLogInPlace(t *testing.T) {+ if os.Geteuid() != 0 {+ t.Skip("the restricted marker is trusted only when root owns it")+ }+ path := systemLogFixture(t)+ if err := RestrictSystemLog(path, 0); err != nil {+ t.Fatal(err)+ }+ before, _ := os.Stat(path)+ if err := RestrictSystemLog(path, 0); err != nil {+ t.Fatal(err)+ }+ after, _ := os.Stat(path)+ if !os.SameFile(before, after) {+ t.Fatal("a repair of a restricted log copied the log again")+ }+}++// A user who hard-linked the shared log could otherwise keep it out of the restriction: a file+// with another link is copied like any other, and the user's link keeps only its old content.+func TestRestrictSystemLogReplacesAHardLinkedLog(t *testing.T) {+ testenv.RequirePOSIXFileModes(t)+ ownLogs(t)+ path := systemLogFixture(t)+ link := filepath.Join(filepath.Dir(path), "..", "users-link")+ if err := os.Link(path, link); err != nil {+ t.Skipf("hard link: %v", err)+ }+ if err := RestrictSystemLog(path, os.Getegid()); err != nil {+ t.Fatal(err)+ }+ if _, _, mode := ownerOf(t, path); mode.Perm() != asymptoteobserve.RestrictedRuntimeLogFileMode {+ t.Fatalf("log mode = %o", mode.Perm())+ }+ a, _ := os.Stat(path)+ b, _ := os.Stat(link)+ if os.SameFile(a, b) {+ t.Fatal("the restricted log is still the file the user linked")+ }+}++func TestRestrictSystemLogDoesNotHangOnAHeldLock(t *testing.T) {+ testenv.RequirePOSIXFileModes(t)+ ownLogs(t)+ prev := restrictLockWait+ restrictLockWait = 100 * time.Millisecond+ t.Cleanup(func() { restrictLockWait = prev })+ path := systemLogFixture(t)+ f, err := os.OpenFile(path+".lock", os.O_RDWR, 0)+ if err != nil {+ t.Fatal(err)+ }+ defer f.Close()+ if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {+ t.Fatal(err)+ }+ done := make(chan error, 1)+ go func() { done <- RestrictSystemLog(path, os.Getegid()) }()+ select {+ case err := <-done:+ if err != nil {+ t.Fatal(err)+ }+ case <-time.After(5 * time.Second):+ t.Fatal("RestrictSystemLog hung on a lock another process held")+ }+}diff --git a/packaging/linux/install-endpoint.sh b/packaging/linux/install-endpoint.shindex 63c1a705..968c616a 100755--- a/packaging/linux/install-endpoint.sh+++ b/packaging/linux/install-endpoint.sh@@ -27,6 +27,9 @@ BEACON_SPLUNK_SOURCE="${BEACON_SPLUNK_SOURCE:-}"BEACON_SPLUNK_SOURCETYPE="${BEACON_SPLUNK_SOURCETYPE:-}"BEACON_SPLUNK_INSECURE_SKIP_VERIFY="${BEACON_SPLUNK_INSECURE_SKIP_VERIFY:-0}"BEACON_SPLUNK_CA_FILE="${BEACON_SPLUNK_CA_FILE:-}"+# shared or restricted; empty keeps the mode the endpoint already has, so a package upgrade never+# changes it.+BEACON_LOG_ACCESS="${BEACON_LOG_ACCESS:-}"# The packaged collector sits beside the CLI. Auto-detected rather than required, so a manual# invocation without BEACON_COLLECTOR still works.@@ -43,6 +46,7 @@ set -- endpoint install --system \# Empty means auto-detect: systemd when it is PID 1, otherwise a supervised collector.[ -n "$BEACON_SERVICE" ] && set -- "$@" --service "$BEACON_SERVICE"[ "$BEACON_NO_START" = "1" ] && set -- "$@" --no-start+[ -n "$BEACON_LOG_ACCESS" ] && set -- "$@" --log-access "$BEACON_LOG_ACCESS"[ -n "$BEACON_SPLUNK_HEC_ENDPOINT" ] && set -- "$@" --splunk-hec-endpoint "$BEACON_SPLUNK_HEC_ENDPOINT"[ -n "$BEACON_SPLUNK_HEC_TOKEN" ] && set -- "$@" --splunk-hec-token "$BEACON_SPLUNK_HEC_TOKEN"diff --git a/packaging/macos/install-endpoint.sh b/packaging/macos/install-endpoint.shindex ef50be6d..f13def4a 100755--- a/packaging/macos/install-endpoint.sh+++ b/packaging/macos/install-endpoint.sh@@ -28,6 +28,9 @@ BEACON_FALCON_SOURCE="${BEACON_FALCON_SOURCE:-}"BEACON_FALCON_SOURCETYPE="${BEACON_FALCON_SOURCETYPE:-}"BEACON_FALCON_INSECURE_SKIP_VERIFY="${BEACON_FALCON_INSECURE_SKIP_VERIFY:-0}"BEACON_FALCON_CA_FILE="${BEACON_FALCON_CA_FILE:-}"+# shared or restricted; empty keeps the mode the endpoint already has, so a package upgrade never+# changes it.+BEACON_LOG_ACCESS="${BEACON_LOG_ACCESS:-}"if [ -z "$BEACON_COLLECTOR" ] && [ -x "/opt/beacon/bin/beacon-otelcol" ]; thenBEACON_COLLECTOR="/opt/beacon/bin/beacon-otelcol"@@ -43,6 +46,10 @@ if [ -n "$BEACON_COLLECTOR" ]; thenset -- "$@" --collector "$BEACON_COLLECTOR"fi+if [ -n "$BEACON_LOG_ACCESS" ]; then+ set -- "$@" --log-access "$BEACON_LOG_ACCESS"+fi+if [ -n "$BEACON_SPLUNK_HEC_ENDPOINT" ]; thenset -- "$@" --splunk-hec-endpoint "$BEACON_SPLUNK_HEC_ENDPOINT"fidiff --git a/packaging/macos/jamf/claude/falcon/install-forwarder.sh b/packaging/macos/jamf/claude/falcon/install-forwarder.shindex 5fff6963..337afc87 100755--- a/packaging/macos/jamf/claude/falcon/install-forwarder.sh+++ b/packaging/macos/jamf/claude/falcon/install-forwarder.sh@@ -73,10 +73,16 @@ include_array() {*)mkdir -p "$(dirname "$path")"# Hooks run as the console user and append to the runtime log, so a missing log is- # created 0666, the mode Beacon's own writer uses. An existing log keeps its mode.+ # created 0666, the mode Beacon's own writer uses. A log in the restricted access mode,+ # which only the collector writes, is created 0640 instead (its directory's setgid bit+ # gives it the beacon-log group). An existing log keeps its mode.if [ ! -e "$path" ]; thentouch "$path"- chmod 0666 "$path" || true+ if grep -qx restricted "$(dirname "$path")/.beacon-log-access" 2>/dev/null; then+ chmod 0640 "$path" || true+ else+ chmod 0666 "$path" || true+ fifi;;esacdiff --git a/packaging/macos/jamf/claude/gcs/install-forwarder.sh b/packaging/macos/jamf/claude/gcs/install-forwarder.shindex 0597dad1..51c6c64c 100755--- a/packaging/macos/jamf/claude/gcs/install-forwarder.sh+++ b/packaging/macos/jamf/claude/gcs/install-forwarder.sh@@ -126,10 +126,16 @@ include_array() {*)mkdir -p "$(dirname "$path")"# Hooks run as the console user and append to the runtime log, so a missing log is- # created 0666, the mode Beacon's own writer uses. An existing log keeps its mode.+ # created 0666, the mode Beacon's own writer uses. A log in the restricted access mode,+ # which only the collector writes, is created 0640 instead (its directory's setgid bit+ # gives it the beacon-log group). An existing log keeps its mode.if [ ! -e "$path" ]; thentouch "$path"- chmod 0666 "$path" || true+ if grep -qx restricted "$(dirname "$path")/.beacon-log-access" 2>/dev/null; then+ chmod 0640 "$path" || true+ else+ chmod 0666 "$path" || true+ fifi;;esacdiff --git a/packaging/macos/jamf/claude/s3/install-forwarder.sh b/packaging/macos/jamf/claude/s3/install-forwarder.shindex f723dba3..05a1737e 100755--- a/packaging/macos/jamf/claude/s3/install-forwarder.sh+++ b/packaging/macos/jamf/claude/s3/install-forwarder.sh@@ -97,10 +97,16 @@ include_array() {*)mkdir -p "$(dirname "$path")"# Hooks run as the console user and append to the runtime log, so a missing log is- # created 0666, the mode Beacon's own writer uses. An existing log keeps its mode.+ # created 0666, the mode Beacon's own writer uses. A log in the restricted access mode,+ # which only the collector writes, is created 0640 instead (its directory's setgid bit+ # gives it the beacon-log group). An existing log keeps its mode.if [ ! -e "$path" ]; thentouch "$path"- chmod 0666 "$path" || true+ if grep -qx restricted "$(dirname "$path")/.beacon-log-access" 2>/dev/null; then+ chmod 0640 "$path" || true+ else+ chmod 0666 "$path" || true+ fifi;;esacdiff --git a/packaging/macos/scripts/postinstall b/packaging/macos/scripts/postinstallindex a1aaecdb..55e90650 100755--- a/packaging/macos/scripts/postinstall+++ b/packaging/macos/scripts/postinstall@@ -14,6 +14,7 @@ BEACON_SPLUNK_SOURCE="${BEACON_SPLUNK_SOURCE:-}" \BEACON_SPLUNK_SOURCETYPE="${BEACON_SPLUNK_SOURCETYPE:-}" \BEACON_SPLUNK_INSECURE_SKIP_VERIFY="${BEACON_SPLUNK_INSECURE_SKIP_VERIFY:-0}" \BEACON_SPLUNK_CA_FILE="${BEACON_SPLUNK_CA_FILE:-}" \+BEACON_LOG_ACCESS="${BEACON_LOG_ACCESS:-}" \BEACON_NO_START=1 \/opt/beacon/scripts/install-endpoint.shdiff --git a/packaging/macos/test-endpoint-scripts.sh b/packaging/macos/test-endpoint-scripts.shindex 594b3961..d1647b6c 100644--- a/packaging/macos/test-endpoint-scripts.sh+++ b/packaging/macos/test-endpoint-scripts.sh@@ -763,6 +763,25 @@ case "$(ls -l "$TMP_DIR/s3-runtime.jsonl" | awk '{print $1}')" inexit 1;;esac+# A log in the restricted access mode is written only by the collector, so an installer that has+# to create it creates it 0640 instead.+mkdir -p "$TMP_DIR/restricted-logs"+printf 'restricted\n' >"$TMP_DIR/restricted-logs/.beacon-log-access"+BEACON_VECTOR_BIN="$FAKE_VECTOR" \+BEACON_S3_FORWARDER_WRAPPER="$ROOT_DIR/packaging/macos/jamf/claude/s3/run-forwarder.sh" \+BEACON_FORWARDER_BASE_DIR="$S3_FORWARDER_BASE" \+BEACON_LAUNCHDAEMONS_DIR="$S3_LAUNCHDAEMONS_DIR" \+BEACON_RUNTIME_LOG_PATHS="$TMP_DIR/restricted-logs/runtime.jsonl" \+BEACON_NO_START="1" \+AWS_REGION="us-west-2" \+"$ROOT_DIR/packaging/macos/jamf/claude/s3/install-forwarder.sh" _ _ _ "beacon-test-bucket" "us-west-2" "beacon/claude/runtime/" "STANDARD_IA" "end" >/dev/null+case "$(ls -l "$TMP_DIR/restricted-logs/runtime.jsonl" | awk '{print $1}')" in+ -rw-r-----*) ;;+ *)+ echo "S3 forwarder installer should create a missing restricted runtime log as 0640" >&2+ exit 1+ ;;+esacS3_FAKE_REPAIR="$TMP_DIR/fake-s3-repair-fails.sh"S3_FAKE_FORWARDER="$TMP_DIR/fake-s3-forwarder-runs.sh"