Skip to content

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

loggroup: read net.exe's error number only from its own output

Claude03d258c30ae6
diff --git a/cli/beacon/internal/endpoint/loggroup/loggroup.go b/cli/beacon/internal/endpoint/loggroup/loggroup.go
index cd98e610..17963fdf 100644
--- a/cli/beacon/internal/endpoint/loggroup/loggroup.go
+++ b/cli/beacon/internal/endpoint/loggroup/loggroup.go
@@ -16,6 +16,7 @@ import (
"errors"
"fmt"
"os/exec"
+ "regexp"
"strconv"
"strings"
"time"
@@ -32,6 +33,24 @@ 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")
+// toolError is a system tool that failed. Stderr is what the tool itself printed, kept apart from
+// the command line in Error(), so a caller that recognizes a tool's message by its text (net.exe's
+// system error number, say) never matches an account name or path it was given.
+type toolError struct {
+ command string
+ err error
+ Stderr string
+}
+
+func (e *toolError) Error() string {
+ if e.Stderr != "" {
+ return fmt.Sprintf("%s: %v: %s", e.command, e.err, e.Stderr)
+ }
+ return fmt.Sprintf("%s: %v", e.command, e.err)
+}
+
+func (e *toolError) Unwrap() error { return e.err }
+
// 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) {
@@ -40,11 +59,12 @@ var run = func(path string, args ...string) (string, error) {
cmd := exec.CommandContext(ctx, path, args...)
out, err := cmd.Output()
if err != nil {
+ failure := &toolError{command: path + " " + strings.Join(args, " "), err: err}
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)))
+ if errors.As(err, &exitErr) {
+ failure.Stderr = strings.TrimSpace(string(exitErr.Stderr))
}
- return string(out), fmt.Errorf("%s %s: %w", path, strings.Join(args, " "), err)
+ return string(out), failure
}
return string(out), nil
}
@@ -164,3 +184,38 @@ func parseGID(field string) (int, error) {
}
return gid, nil
}
+
+// errMemberInAlias is ERROR_MEMBER_IN_ALIAS: the account is already a member of the local group.
+const errMemberInAlias = 1378
+
+// netErrorNumber matches the number in net.exe's "System error N has occurred." In other display
+// languages the sentence is translated but the number is the same, so only the number is read.
+var netErrorNumber = regexp.MustCompile(`(?:^|[^0-9])([0-9]{1,5})(?:[^0-9]|$)`)
+
+// netSystemError returns the system error number net.exe printed for a failed command, or 0. It is
+// used on Windows and lives here so every platform runs its test. It reads only what net.exe wrote
+// to standard error, never the command line (which holds the account name and the path), and
+// ignores the account name wherever net.exe echoes it, so a name such as svc1378 can never pass for
+// an error number.
+func netSystemError(err error, account string) int {
+ var failure *toolError
+ if !errors.As(err, &failure) || failure.Stderr == "" {
+ return 0
+ }
+ stderr := failure.Stderr
+ if account != "" {
+ stderr = strings.ReplaceAll(stderr, account, " ")
+ if _, name, ok := strings.Cut(account, `\`); ok && name != "" {
+ stderr = strings.ReplaceAll(stderr, name, " ")
+ }
+ }
+ match := netErrorNumber.FindStringSubmatch(stderr)
+ if match == nil {
+ return 0
+ }
+ n, convErr := strconv.Atoi(match[1])
+ if convErr != nil {
+ return 0
+ }
+ return n
+}
diff --git a/cli/beacon/internal/endpoint/loggroup/loggroup_account_test.go b/cli/beacon/internal/endpoint/loggroup/loggroup_account_test.go
index bef188f1..328e739e 100644
--- a/cli/beacon/internal/endpoint/loggroup/loggroup_account_test.go
+++ b/cli/beacon/internal/endpoint/loggroup/loggroup_account_test.go
@@ -1,6 +1,7 @@
package loggroup
import (
+ "errors"
"strings"
"testing"
)
@@ -33,3 +34,31 @@ func TestValidAccount(t *testing.T) {
}
}
}
+
+// net.exe's error number is read only from what it printed, never from the command line or the
+// account name, so `svc1378` failing with another error is never taken for "already a member".
+func TestNetSystemError(t *testing.T) {
+ failure := func(stderr string) error {
+ return &toolError{command: `net.exe localgroup beacon-log svc1378 /add`, err: errors.New("exit status 2"), Stderr: stderr}
+ }
+ cases := []struct {
+ err error
+ account string
+ want int
+ }{
+ {failure("System error 1378 has occurred.\r\n\r\nThe specified account name is already a member of the group."), "alice", 1378},
+ {failure("Systemfehler 1378 aufgetreten."), "alice", 1378},
+ {failure("System error 1332 has occurred."), "svc1378", 1332},
+ {failure("There is no such global user or group: svc1378.\r\n\r\nMore help is available by typing NET HELPMSG 3783."), `CORP\svc1378`, 3783},
+ {failure("The account app1378 could not be added."), "app1378", 0},
+ {failure("System error 5 has occurred."), "alice", 5},
+ {failure("error 113780"), "alice", 0},
+ {failure(""), "svc1378", 0},
+ {errors.New("net.exe localgroup beacon-log svc1378 /add: 1378"), "svc1378", 0},
+ }
+ for _, c := range cases {
+ if got := netSystemError(c.err, c.account); got != c.want {
+ t.Errorf("netSystemError(%q, %q) = %d, want %d", c.err, c.account, got, c.want)
+ }
+ }
+}
diff --git a/cli/beacon/internal/endpoint/loggroup/loggroup_windows.go b/cli/beacon/internal/endpoint/loggroup/loggroup_windows.go
index 91c953a3..3412960e 100644
--- a/cli/beacon/internal/endpoint/loggroup/loggroup_windows.go
+++ b/cli/beacon/internal/endpoint/loggroup/loggroup_windows.go
@@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"path/filepath"
- "strings"
"golang.org/x/sys/windows"
)
@@ -70,7 +69,7 @@ func addMember(group, account string) error {
return fmt.Errorf("find the system directory: %w", err)
}
if _, err := run(filepath.Join(system, "net.exe"), "localgroup", group, account, "/add"); err != nil {
- if strings.Contains(err.Error(), "1378") {
+ if netSystemError(err, account) == errMemberInAlias {
return nil
}
return err
diff --git a/cli/beacon/internal/endpoint/loggroup/loggroup_windows_test.go b/cli/beacon/internal/endpoint/loggroup/loggroup_windows_test.go
index c5dad37c..7e111211 100644
--- a/cli/beacon/internal/endpoint/loggroup/loggroup_windows_test.go
+++ b/cli/beacon/internal/endpoint/loggroup/loggroup_windows_test.go
@@ -3,6 +3,7 @@
package loggroup
import (
+ "errors"
"fmt"
"os"
"path/filepath"
@@ -119,10 +120,52 @@ func TestAddMemberAcceptsTheNamesWindowsGivesLocalAccounts(t *testing.T) {
func TestAddMemberTreatsAnExistingMemberAsSuccess(t *testing.T) {
fakeAccounts(t, map[string]string{Name: "S-1-5-21-1-2-3-1500"})
- run = func(string, ...string) (string, error) {
- return "", fmt.Errorf("net.exe: exit status 2: System error 1378 has occurred. The specified account name is already a member of the group.")
+ run = func(path string, args ...string) (string, error) {
+ return "", &toolError{command: path + " " + strings.Join(args, " "), err: errors.New("exit status 2"),
+ Stderr: "System error 1378 has occurred.\r\n\r\nThe specified account name is already a member of the group."}
}
if err := AddMember(Name, "alice"); err != nil {
t.Fatalf("AddMember = %v, want success for an existing member", err)
}
+ // In another display language the sentence is translated but the number is not.
+ run = func(path string, args ...string) (string, error) {
+ return "", &toolError{command: path + " " + strings.Join(args, " "), err: errors.New("exit status 2"),
+ Stderr: "Systemfehler 1378 aufgetreten.\r\n\r\nDer angegebene Kontoname ist bereits ein Mitglied der Gruppe."}
+ }
+ if err := AddMember(Name, "alice"); err != nil {
+ t.Fatalf("AddMember (German net.exe) = %v, want success for an existing member", err)
+ }
+}
+
+// Only net.exe's own error number counts as "already a member": never a 1378 in the account name,
+// the command line, or another error's text. Otherwise an account that does not exist would be
+// reported as added, and the operator told a log shipper can read the log when it cannot.
+func TestAddMemberReportsOtherFailuresEvenWith1378InTheName(t *testing.T) {
+ fakeAccounts(t, map[string]string{Name: "S-1-5-21-1-2-3-1500"})
+ cases := []struct{ account, stderr string }{
+ // The name is on the command line, which Error() includes.
+ {"svc1378", "System error 1332 has occurred.\r\n\r\nNo mapping between account names and security IDs was done."},
+ {`CORP\svc1378`, "System error 1332 has occurred."},
+ // net.exe echoing the name, with or without its domain.
+ {`CORP\svc1378`, "There is no such global user or group: svc1378.\r\n\r\nMore help is available by typing NET HELPMSG 3783."},
+ {"app1378", "The account app1378 could not be added."},
+ {"alice", "System error 5 has occurred.\r\n\r\nAccess is denied."},
+ {"alice", ""},
+ }
+ for _, c := range cases {
+ stderr := c.stderr
+ run = func(path string, args ...string) (string, error) {
+ return "", &toolError{command: path + " " + strings.Join(args, " "), err: errors.New("exit status 2"), Stderr: stderr}
+ }
+ if err := AddMember(Name, c.account); err == nil {
+ t.Errorf("AddMember(%q) with net.exe saying %q = nil, want the failure reported", c.account, c.stderr)
+ }
+ }
+ // A plain error from run (one that is not a toolError) is never read as a net.exe error number.
+ run = func(string, ...string) (string, error) {
+ return "", fmt.Errorf("net.exe localgroup %s svc1378 /add: 1378", Name)
+ }
+ if err := AddMember(Name, "svc1378"); err == nil {
+ t.Error("AddMember with an untyped error mentioning 1378 = nil, want the failure reported")
+ }
}