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

[TUF autoupdater] Use new autoupdate code for internal testing #1203

Closed
wants to merge 4 commits into from
Closed
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
67 changes: 43 additions & 24 deletions cmd/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"text/tabwriter"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/kit/env"
"github.com/kolide/kit/logutil"
"github.com/kolide/kit/version"
"github.com/kolide/launcher/pkg/autoupdate"
"github.com/kolide/launcher/pkg/autoupdate/tuf"
"github.com/kolide/launcher/pkg/contexts/ctxlog"
"github.com/kolide/launcher/pkg/execwrapper"
"github.com/kolide/launcher/pkg/log/locallogger"
Expand All @@ -39,9 +40,6 @@ func main() {
ctx = ctxlog.NewContext(ctx, logger)

// If there's a newer version of launcher on disk, use it.
// This does not call DeleteOldUpdates, on the theory that
// it's better left to the service to handle cleanup. This is
// a straight forward exec.
//
// launcher is _also_ called when we're checking update
// validity (with autoupdate.checkExecutable). This is
Expand All @@ -51,26 +49,7 @@ func main() {
// skip exec'ing an update. This helps prevent launcher from
// fork-bombing itself. This is an ENV, because there's no
// good way to pass it through the flags.
if !env.Bool("LAUNCHER_SKIP_UPDATES", false) {
newerBinary, err := autoupdate.FindNewestSelf(ctx)
if err != nil {
logutil.Fatal(logger, err, "checking for updated version")
}

if newerBinary != "" {
level.Debug(logger).Log(
"msg", "preparing to exec new binary",
"oldVersion", version.Version().Version,
"newBinary", newerBinary,
)
if err := execwrapper.Exec(ctx, newerBinary, os.Args, os.Environ()); err != nil {
logutil.Fatal(logger, err, "exec")
}
panic("how")
} else {
level.Debug(logger).Log("msg", "Nothing new")
}
}
runLatestLauncher(ctx, logger)

// if the launcher is being ran with a positional argument,
// handle that argument. Fall-back to running launcher
Expand Down Expand Up @@ -114,6 +93,46 @@ func main() {
}
}

func runLatestLauncher(ctx context.Context, logger log.Logger) {
if env.Bool("LAUNCHER_SKIP_UPDATES", false) {
return
}

// Parse startup options -- just the couple that we need to check out the latest version of launcher
opts, err := parseStartupOptions(os.Args[1:])
if err != nil {
level.Error(logger).Log("msg", "could not parse startup options", "err", err)
return
}

newerBinary, err := tuf.CheckOutLatest("launcher", opts.RootDirectory, opts.UpdateDirectory, opts.UpdateChannel.String(), logger)
if err != nil {
level.Error(logger).Log(
"msg", "could not check out latest launcher, will continue running current",
"err", err,
)
return
}

if newerBinary.Version == version.Version().Version {
level.Error(logger).Log("msg", "already running latest launcher")
return
}

level.Error(logger).Log(
"msg", "preparing to exec new launcher binary",
"old_version", version.Version().Version,
"new_binary_version", newerBinary.Version,
"new_binary_path", newerBinary.Path,
)

if err := execwrapper.Exec(ctx, newerBinary.Path, os.Args, os.Environ()); err != nil {
logutil.Fatal(logger, err, "exec")
}

panic("how")
}

func runSubcommands() error {
var run func([]string) error
switch os.Args[1] {
Expand Down
73 changes: 73 additions & 0 deletions cmd/launcher/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,79 @@ func (i *arrayFlags) Set(value string) error {
return nil
}

func parseStartupOptions(args []string) (*launcher.Options, error) {
configFilePath := getConfigFilePathFromArgs(args)
if len(configFilePath) == 0 {
var err error
configFilePath, err = getConfigFilePathFromWellKnownLocation()
if err != nil {
return nil, fmt.Errorf("config file not provided and its location could not be determined: %w", err)
}
}

flagset := flag.NewFlagSet("launcher", flag.ExitOnError)
flagset.Usage = func() { usage(flagset) }

var (
flRootDirectory = flagset.String("root_directory", defaultRootDirectoryPath(), "The location of the local database, pidfiles, etc.")
flUpdateChannel = flagset.String("update_channel", "stable", "The channel to pull updates from (options: stable, beta, nightly)")
flUpdateDirectory = flagset.String("update_directory", "", "Local directory to hold updates for osqueryd and launcher")
)

ffOpts := []ff.Option{
ff.WithConfigFile(configFilePath),
ff.WithConfigFileParser(ff.PlainParser),
ff.WithIgnoreUndefined(true),
}

ff.Parse(flagset, []string{}, ffOpts...)

return &launcher.Options{
RootDirectory: *flRootDirectory,
UpdateChannel: autoupdate.UpdateChannel(*flUpdateChannel),
UpdateDirectory: *flUpdateDirectory,
}, nil
}

func getConfigFilePathFromArgs(args []string) string {
for i, arg := range args {
if arg == "--config" || arg == "-config" {
return strings.Trim(args[i+1], `"'`)
}
}

return ""
}

func getConfigFilePathFromWellKnownLocation() (string, error) {
var configFilePath string
switch runtime.GOOS {
case "darwin", "linux":
configFilePath = "/etc/kolide-k2/launcher.flags"
case "windows":
configFilePath = `C:\Program Files\Kolide\Launcher-kolide-k2\conf\launcher.flags`
default:
return "", fmt.Errorf("cannot find config file for unsupported OS %s", runtime.GOOS)
}

if _, err := os.Stat(configFilePath); err != nil && os.IsNotExist(err) {
return "", fmt.Errorf("could not read config file because it does not exist at %s: %w", configFilePath, err)
}

return configFilePath, nil
}

func defaultRootDirectoryPath() string {
switch runtime.GOOS {
case "darwin", "linux":
return "/var/kolide-k2/k2device.kolide.com"
case "windows":
return `C:\Program Files\Kolide\Launcher-kolide-k2\data`
default:
return ""
}
}

// parseOptions parses the options that may be configured via command-line flags
// and/or environment variables, determines order of precedence and returns a
// typed struct of options for further application use
Expand Down