fleet/orbit/pkg/execuser/execuser.go
Scott Gress 866d8bcc00
Ensure MacOS desktop app launched as correct user (#27296)
For #25924  

This PR attempts to fix the issue where the Fleet desktop icon sometimes
fails to appear on MacOS hosts until the hosts are rebooted. Anecdotal
evidence points to this being an issue when system setup is happening,
leading to the theory that Orbit is attempting to launch the app as
`_mbsetupuser` rather than the real logged-in user. The fix here is to
use a different command to get the name of the logged-in user (ignoring
`_mbsetupuser` if it appears), and to launch the desktop app as that
user using `sudo`.

I have tested this on MacOS and Ubuntu hosts, and verified that the
desktop app launches as expected on both.

We don't have a solid reproduction scenario for the issue, but we do
have [some ways to look for relevant
errors](https://github.com/fleetdm/fleet/issues/19172#issuecomment-2627812786),
so we can try this out and see if those errors cease.
2025-03-20 09:49:23 -05:00

80 lines
2 KiB
Go

// Package execuser is used to run applications from a high privilege user (root on Unix,
// SYSTEM service on Windows) as the current login user.
package execuser
import (
"io"
"time"
)
type eopts struct {
env [][2]string
args [][2]string
stderrPath string //nolint:structcheck,unused
timeout time.Duration
user string
}
// Option allows configuring the application.
type Option func(*eopts)
// WithEnv sets environment variables for the application.
func WithEnv(name, value string) Option {
return func(a *eopts) {
a.env = append(a.env, [2]string{name, value})
}
}
// WithArg sets command line arguments for the application.
func WithArg(name, value string) Option {
return func(a *eopts) {
a.args = append(a.args, [2]string{name, value})
}
}
// WithTimeout sets the timeout for the application. Currently only supported on Linux.
func WithTimeout(duration time.Duration) Option {
return func(a *eopts) {
a.timeout = duration
}
}
// WithUser sets the user to run the application as. Currently only supported on MacOS.
func WithUser(user string) Option {
return func(a *eopts) {
a.user = user
}
}
// Run runs an application as the current login user.
// It assumes the caller is running with high privileges (root on Unix, SYSTEM on Windows).
//
// It returns after starting the child process.
func Run(path string, opts ...Option) (lastLogs string, err error) {
var o eopts
for _, fn := range opts {
fn(&o)
}
return run(path, o)
}
// RunWithOutput runs an application as the current login user and returns its output.
// It assumes the caller is running with high privileges (root on UNIX).
//
// It blocks until the child process exits.
// Non ExitError errors return with a -1 exitCode.
func RunWithOutput(path string, opts ...Option) (output []byte, exitCode int, err error) {
var o eopts
for _, fn := range opts {
fn(&o)
}
return runWithOutput(path, o)
}
func RunWithStdin(path string, opts ...Option) (io.WriteCloser, error) {
var o eopts
for _, fn := range opts {
fn(&o)
}
return runWithStdin(path, o)
}