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

set RecoveryActionsOnNonCrashFailures flag in svc_config_windows #1670

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 86 additions & 10 deletions cmd/launcher/svc_config_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
package main

import (
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"context"
"log/slog"

"github.com/kolide/launcher/pkg/launcher"

"golang.org/x/sys/windows/registry"
"golang.org/x/sys/windows/svc/mgr"
)

const (
launcherServiceName = `LauncherKolideK2Svc`
Copy link
Contributor

Choose a reason for hiding this comment

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

Hrm. This is fine for now, but this is an interesting thread....

The ServiceName is tied to the identifier the package was built with. I don't recall if we have it available anywhere at runtime. If we did, using that makes more sense.

If we don't, we should consider a longer arc of making something more human friendly, and where we store manage it. (Constant in agent?) But that's all kinda future handwaving

launcherServiceRegistryKeyName = `SYSTEM\CurrentControlSet\Services\LauncherKolideK2Svc`

// DelayedAutostart is type REG_DWORD, i.e. uint32. We want to turn off delayed autostart.
Expand All @@ -25,7 +28,7 @@ const (
notFoundInRegistryError = "The system cannot find the file specified."
)

func checkServiceConfiguration(logger log.Logger, opts *launcher.Options) {
func checkServiceConfiguration(logger *slog.Logger, opts *launcher.Options) {
RebeccaMahany marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Slight preference to calling slog.Logger slogger, but 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 I will update this in the second set of changes!

// If this isn't a Kolide installation, do not update the configuration
if opts.KolideServerURL != "k2device.kolide.com" && opts.KolideServerURL != "k2device-preprod.kolide.com" {
return
Expand All @@ -34,14 +37,23 @@ func checkServiceConfiguration(logger log.Logger, opts *launcher.Options) {
// Get launcher service key
launcherServiceKey, err := registry.OpenKey(registry.LOCAL_MACHINE, launcherServiceRegistryKeyName, registry.ALL_ACCESS)
if err != nil {
level.Error(logger).Log("msg", "could not open registry key", "key_name", launcherServiceRegistryKeyName, "err", err)
logger.Log(context.TODO(), slog.LevelError,
"could not open registry key",
"key_name", launcherServiceRegistryKeyName,
"err", err,
)

return
}

// Close it once we're done
defer func() {
if err := launcherServiceKey.Close(); err != nil {
level.Error(logger).Log("msg", "could not close registry key", "key_name", launcherServiceRegistryKeyName, "err", err)
logger.Log(context.TODO(), slog.LevelError,
"could not close registry key",
"key_name", launcherServiceRegistryKeyName,
"err", err,
)
}
}()

Expand All @@ -50,11 +62,13 @@ func checkServiceConfiguration(logger log.Logger, opts *launcher.Options) {

// Check to see if we need to update the service to depend on Dnscache
checkDependOnService(launcherServiceKey, logger)

checkRestartActions(logger)
}

// checkDelayedAutostart checks the current value of `DelayedAutostart` (whether to wait ~2 minutes
// before starting the launcher service) and updates it if necessary.
func checkDelayedAutostart(launcherServiceKey registry.Key, logger log.Logger) {
func checkDelayedAutostart(launcherServiceKey registry.Key, logger *slog.Logger) {
currentDelayedAutostart, _, getDelayedAutostartErr := launcherServiceKey.GetIntegerValue(delayedAutostartName)

// Can't determine current value, don't update
Expand All @@ -69,20 +83,26 @@ func checkDelayedAutostart(launcherServiceKey registry.Key, logger log.Logger) {

// Turn off delayed autostart
if err := launcherServiceKey.SetDWordValue(delayedAutostartName, delayedAutostartDisabled); err != nil {
level.Error(logger).Log("msg", "could not turn off DelayedAutostart", "err", err)
logger.Log(context.TODO(), slog.LevelError,
"could not turn off DelayedAutostart",
"err", err,
)
}
}

// checkDependOnService checks the current value of `DependOnService` (the list of services that must
// start before launcher can) and updates it if necessary.
func checkDependOnService(launcherServiceKey registry.Key, logger log.Logger) {
func checkDependOnService(launcherServiceKey registry.Key, logger *slog.Logger) {
serviceList, _, getServiceListErr := launcherServiceKey.GetStringsValue(dependOnServiceName)

if getServiceListErr != nil {
if getServiceListErr.Error() == notFoundInRegistryError {
// `DependOnService` does not exist for this service yet -- we can safely set it to include the Dnscache service.
if err := launcherServiceKey.SetStringsValue(dependOnServiceName, []string{dnscacheService}); err != nil {
level.Error(logger).Log("msg", "could not set strings value for DependOnService", "err", err)
logger.Log(context.TODO(), slog.LevelError,
"could not set strings value for DependOnService",
"err", err,
)
}
return
}
Expand All @@ -103,6 +123,62 @@ func checkDependOnService(launcherServiceKey registry.Key, logger log.Logger) {
// Set service to depend on Dnscache
serviceList = append(serviceList, dnscacheService)
if err := launcherServiceKey.SetStringsValue(dependOnServiceName, serviceList); err != nil {
level.Error(logger).Log("msg", "could not set strings value for DependOnService", "err", err)
logger.Log(context.TODO(), slog.LevelError,
"could not set strings value for DependOnService",
"err", err,
)
}
}

// checkRestartActions checks the current value of our `SERVICE_FAILURE_ACTIONS_FLAG` and
// sets it to true if required. See https://learn.microsoft.com/en-us/windows/win32/api/winsvc/ns-winsvc-service_failure_actions_flag
func checkRestartActions(logger *slog.Logger) {
RebeccaMahany marked this conversation as resolved.
Show resolved Hide resolved
sman, err := mgr.Connect()
if err != nil {
logger.Log(context.TODO(), slog.LevelError,
"connecting to service control manager",
"err", err,
)

return
}

defer sman.Disconnect()

launcherService, err := sman.OpenService(launcherServiceName)
if err != nil {
logger.Log(context.TODO(), slog.LevelError,
"opening the launcher service from control manager",
"err", err,
)

return
}

defer launcherService.Close()

curFlag, err := launcherService.RecoveryActionsOnNonCrashFailures()
if err != nil {
logger.Log(context.TODO(), slog.LevelError,
"querying for current RecoveryActionsOnNonCrashFailures flag",
"err", err,
)

return
}

if curFlag { // nothing to do, the flag was already set correctly
return
}

if err = launcherService.SetRecoveryActionsOnNonCrashFailures(true); err != nil {
logger.Log(context.TODO(), slog.LevelError,
"setting RecoveryActionsOnNonCrashFailures flag",
"err", err,
)

return
}

logger.Log(context.TODO(), slog.LevelInfo, "successfully set RecoveryActionsOnNonCrashFailures flag")
}
2 changes: 1 addition & 1 deletion cmd/launcher/svc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func runWindowsSvc(args []string) error {
}()

// Confirm that service configuration is up-to-date
checkServiceConfiguration(logger, opts)
checkServiceConfiguration(systemSlogger.Logger, opts)

level.Info(logger).Log(
"msg", "launching service",
Expand Down
Loading