-
Notifications
You must be signed in to change notification settings - Fork 100
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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` | ||
launcherServiceRegistryKeyName = `SYSTEM\CurrentControlSet\Services\LauncherKolideK2Svc` | ||
|
||
// DelayedAutostart is type REG_DWORD, i.e. uint32. We want to turn off delayed autostart. | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slight preference to calling slog.Logger There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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, | ||
) | ||
} | ||
}() | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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