fix: don't attempt to launch fleet desktop until the user is logged into GUI (#16090)

> 📜 Related issue: https://github.com/fleetdm/fleet/issues/14698

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

<!-- Note that API documentation changes are now addressed by the
product design team. -->

- [x] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Manual QA for all new/changed functionality
  - For Orbit and Fleet Desktop changes:
- [x] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.
- [x] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).
This commit is contained in:
Jahziel Villasana-Espinoza
2024-01-17 10:00:28 -05:00
committed by GitHub
parent 29b7c8e44e
commit d7d55f0e56
4 changed files with 57 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
- Fixed issue on MacOS with starting Fleet Desktop for the first time. MacOS would return an error
if a user is not logged in via the GUI.
+14
View File
@@ -33,6 +33,7 @@ import (
"github.com/fleetdm/fleet/v4/orbit/pkg/token"
"github.com/fleetdm/fleet/v4/orbit/pkg/update"
"github.com/fleetdm/fleet/v4/orbit/pkg/update/filestore"
"github.com/fleetdm/fleet/v4/orbit/pkg/user"
"github.com/fleetdm/fleet/v4/pkg/certificate"
"github.com/fleetdm/fleet/v4/pkg/file"
retrypkg "github.com/fleetdm/fleet/v4/pkg/retry"
@@ -1307,6 +1308,19 @@ func (d *desktopRunner) execute() error {
for {
// First retry logic to start fleet-desktop.
if done := retry(30*time.Second, false, d.interruptCh, func() bool {
// On MacOS, if we attempt to run Fleet Desktop while the user is not logged in through
// the GUI, MacOS returns an error. See https://github.com/fleetdm/fleet/issues/14698
// for more details.
loggedInGui, err := user.IsUserLoggedInViaGui()
if err != nil {
log.Debug().Err(err).Msg("desktop.IsUserLoggedInGui")
return true
}
if !loggedInGui {
return true
}
// Orbit runs as root user on Unix and as SYSTEM (Windows Service) user on Windows.
// To be able to run the desktop application (mostly to register the icon in the system tray)
// we need to run the application as the login user.
+30
View File
@@ -0,0 +1,30 @@
//go:build darwin
// +build darwin
package user
import (
"errors"
"os/exec"
"strings"
)
// IsUserLoggedInViaGui returns whether or not a user is logged into the machine via the GUI.
func IsUserLoggedInViaGui() (bool, error) {
output, err := exec.Command("/usr/bin/stat", "-f", "%Su", "/dev/console").Output()
if err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) && len(ee.Stderr) > 0 {
return false, errors.Join(err, errors.New(string(ee.Stderr)))
}
return false, err
}
// If no user is logged in via GUI, the command line returns "root".
if strings.TrimSpace(string(output)) == "root" {
return false, nil
}
return true, nil
}
+11
View File
@@ -0,0 +1,11 @@
//go:build !darwin
// +build !darwin
package user
// IsUserLoggedInViaGui returns whether or not a user is logged into the machine via the GUI. This
// function is only relevant on MacOS, where it is used to prevent errors when launching Fleet
// Desktop. We assume yes (effectively a no-op) on all other platforms.
func IsUserLoggedInViaGui() (bool, error) {
return true, nil
}