Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade watchdog service to scheduled task #1951

Merged
merged 11 commits into from
Nov 12, 2024
31 changes: 8 additions & 23 deletions ee/watchdog/controller_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -217,22 +215,6 @@ func (wc *WatchdogController) ServiceEnabledChanged(enabled bool) {
wc.slogger.Log(ctx, slog.LevelInfo, "completed watchdog scheduled task installation")
}

func getExecutablePath(identifier string) (string, error) {
if strings.TrimSpace(identifier) == "" {
identifier = launcher.DefaultLauncherIdentifier
}

binDirBase := fmt.Sprintf(`C:\Program Files\Kolide\Launcher-%s\bin`, identifier)
launcherBin := filepath.Join(binDirBase, "launcher.exe")
// do some basic sanity checking to prevent installation from a bad path
_, err := os.Stat(launcherBin)
if err != nil {
return "", err
}

return launcherBin, nil
}

// installWatchdogTask registers our watchdog subcommand as a scheduled task.
// see inline comments for details on various settings, but here is a general overview:
// Triggers:
Expand All @@ -246,7 +228,8 @@ func installWatchdogTask(identifier, configFilePath string) error {
}

taskName := launcher.TaskName(identifier, watchdogTaskType)
// init COM
// init COM - we discard the error returned by CoInitialize because it
// harmlessly returns S_FALSE if we call it more than once
ole.CoInitialize(0)
defer ole.CoUninitialize()

Expand Down Expand Up @@ -299,7 +282,7 @@ func installWatchdogTask(identifier, configFilePath string) error {
regInfo := regInfoProp.ToIDispatch()
defer regInfo.Release()

if _, err = oleutil.PutProperty(regInfo, "Description", "Kolide agent waker"); err != nil {
if _, err = oleutil.PutProperty(regInfo, "Description", "Kolide agent restarter"); err != nil {
return fmt.Errorf("setting reginfo description: %w", err)
}

Expand Down Expand Up @@ -481,7 +464,7 @@ func installWatchdogTask(identifier, configFilePath string) error {
execAction := execActionTemplate.ToIDispatch()
defer execAction.Release()

installedExePath, err := getExecutablePath(identifier)
installedExePath, err := launcher.GetOriginalLauncherExecutablePath(identifier)
if err != nil {
return fmt.Errorf("determining watchdog executable path: %w", err)
}
Expand Down Expand Up @@ -521,7 +504,8 @@ func RemoveWatchdogTask(identifier string) error {
}

taskName := launcher.TaskName(identifier, watchdogTaskType)
// init COM
// init COM - we discard the error returned by CoInitialize because it
// harmlessly returns S_FALSE if we call it more than once
ole.CoInitialize(0)
RebeccaMahany marked this conversation as resolved.
Show resolved Hide resolved
defer ole.CoUninitialize()

Expand Down Expand Up @@ -571,7 +555,8 @@ func watchdogTaskExists(identifier string) (bool, error) {
}

taskName := launcher.TaskName(identifier, watchdogTaskType)
// init COM
// init COM - we discard the error returned by CoInitialize because it
// harmlessly returns S_FALSE if we call it more than once
ole.CoInitialize(0)
defer ole.CoUninitialize()

Expand Down
30 changes: 30 additions & 0 deletions pkg/launcher/paths.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package launcher

import (
"fmt"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -156,3 +157,32 @@ func nonEmptyFileExists(path string) (bool, error) {

return fileInfo.Size() > 0, nil
}

// GetOriginalLauncherExecutablePath is a convenience function to determine and verify the location of
// the originally installed launcher executable. it uses the identifier to generate the expected path and
// verifies file presence before returning the path
func GetOriginalLauncherExecutablePath(identifier string) (string, error) {
if strings.TrimSpace(identifier) == "" {
identifier = DefaultLauncherIdentifier
}

var binDirBase string
var launcherExeName string

switch runtime.GOOS {
case "windows":
binDirBase = fmt.Sprintf(`C:\Program Files\Kolide\Launcher-%s\bin`, identifier)
launcherExeName = "launcher.exe"
default:
binDirBase = fmt.Sprintf(`/usr/local/%s/bin`, identifier)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh can you throw a quick comment here or in the docblock that this won't work for NixOS, just so we don't forget if we try to use it for Linux in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh yep! or do you think it make more sense to just add this to pkg_utils_windows.go? it is only in use for these windows tasks currently

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a comment but happy to move this over if you think that's more appropriate

launcherExeName = "launcher"
}

launcherBin := filepath.Join(binDirBase, launcherExeName)
// do some basic sanity checking to prevent installation from a bad path
if exists, err := nonEmptyFileExists(launcherBin); err != nil || !exists {
return "", err
}

return launcherBin, nil
}
Loading