From e6d3feea5611f96891cc5e47e41f18f92cd3dfb9 Mon Sep 17 00:00:00 2001 From: Rebecca Mahany-Horton Date: Mon, 21 Oct 2024 16:26:02 -0400 Subject: [PATCH] Set osquery version in packaging context (#1901) --- pkg/packagekit/context.go | 2 +- pkg/packagekit/context_test.go | 2 +- pkg/packagekit/package_fpm.go | 2 +- pkg/packagekit/package_pkg.go | 4 +-- pkg/packagekit/package_wix.go | 2 +- pkg/packaging/helpers.go | 47 ++++++++++++++++++++++++++++++++++ pkg/packaging/packaging.go | 4 ++- 7 files changed, 56 insertions(+), 7 deletions(-) diff --git a/pkg/packagekit/context.go b/pkg/packagekit/context.go index f2e0f5d67..b21433c67 100644 --- a/pkg/packagekit/context.go +++ b/pkg/packagekit/context.go @@ -32,7 +32,7 @@ func InitContext(ctx context.Context) context.Context { return ctx } -func setInContext(ctx context.Context, key contextKey, val string) { +func SetInContext(ctx context.Context, key contextKey, val string) { // If there's no pointer, then there's no point in setting // this. It won't get back to the caller. ptr, ok := ctx.Value(key).(*string) diff --git a/pkg/packagekit/context_test.go b/pkg/packagekit/context_test.go index 3c464d1fe..7ca0d1d61 100644 --- a/pkg/packagekit/context_test.go +++ b/pkg/packagekit/context_test.go @@ -54,7 +54,7 @@ func TestContext(t *testing.T) { } for _, pair := range contextPairs { - setInContext(ctx, pair.key, pair.val) + SetInContext(ctx, pair.key, pair.val) } for _, pair := range contextPairs { diff --git a/pkg/packagekit/package_fpm.go b/pkg/packagekit/package_fpm.go index be518390e..be60fdc4c 100644 --- a/pkg/packagekit/package_fpm.go +++ b/pkg/packagekit/package_fpm.go @@ -178,7 +178,7 @@ func PackageFPM(ctx context.Context, w io.Writer, po *PackageOptions, fpmOpts .. return fmt.Errorf("copying output: %w", err) } - setInContext(ctx, ContextLauncherVersionKey, po.Version) + SetInContext(ctx, ContextLauncherVersionKey, po.Version) return nil } diff --git a/pkg/packagekit/package_pkg.go b/pkg/packagekit/package_pkg.go index 82ffad135..ef0a76090 100644 --- a/pkg/packagekit/package_pkg.go +++ b/pkg/packagekit/package_pkg.go @@ -61,7 +61,7 @@ func PackagePkg(ctx context.Context, w io.Writer, po *PackageOptions, arch strin return fmt.Errorf("copying output: %w", err) } - setInContext(ctx, ContextLauncherVersionKey, po.Version) + SetInContext(ctx, ContextLauncherVersionKey, po.Version) return nil } @@ -89,7 +89,7 @@ func runNotarize(ctx context.Context, file string, po *PackageOptions) error { "uuid", uuid, ) - setInContext(ctx, ContextNotarizationUuidKey, uuid) + SetInContext(ctx, ContextNotarizationUuidKey, uuid) return nil } diff --git a/pkg/packagekit/package_wix.go b/pkg/packagekit/package_wix.go index 4e8a8204a..01fb5d1d4 100644 --- a/pkg/packagekit/package_wix.go +++ b/pkg/packagekit/package_wix.go @@ -175,7 +175,7 @@ func PackageWixMSI(ctx context.Context, w io.Writer, po *PackageOptions, include return fmt.Errorf("copying output: %w", err) } - setInContext(ctx, ContextLauncherVersionKey, po.Version) + SetInContext(ctx, ContextLauncherVersionKey, po.Version) return nil } diff --git a/pkg/packaging/helpers.go b/pkg/packaging/helpers.go index 80e6490ac..82ffd1a91 100644 --- a/pkg/packaging/helpers.go +++ b/pkg/packaging/helpers.go @@ -1,7 +1,15 @@ package packaging import ( + "context" + "os" + "path/filepath" "strings" + + "github.com/go-kit/kit/log" + "github.com/go-kit/kit/log/level" + "github.com/kolide/launcher/pkg/contexts/ctxlog" + "github.com/kolide/launcher/pkg/packagekit" ) // sanitizeHostname will replace any ":" characters in a given hostname with "-" @@ -9,3 +17,42 @@ import ( func sanitizeHostname(hostname string) string { return strings.Replace(hostname, ":", "-", -1) } + +// setOsqueryVersionInCtx retrieves the osquery version (by running the binary) and +// sets it in the context. +func (p *PackageOptions) setOsqueryVersionInCtx(ctx context.Context) { + logger := log.With(ctxlog.FromContext(ctx), "library", "setOsqueryVersionInCtx") + + osqueryPath := p.osqueryLocation() + stdout, err := p.execOut(ctx, osqueryPath, "-version") + if err != nil { + level.Warn(logger).Log( + "msg", "could not run osqueryd -version", + "err", err, + ) + return + } + + osquerydVersionTrimmed := strings.TrimPrefix(strings.TrimSpace(stdout), "osqueryd version ") + + packagekit.SetInContext(ctx, packagekit.ContextOsqueryVersionKey, osquerydVersionTrimmed) +} + +// osqueryLocation returns the location of the osquery binary within `binDir`. For darwin, +// it may be in an app bundle -- we check to see if the binary exists there first, and then +// fall back to the common location if it doesn't. +func (p *PackageOptions) osqueryLocation() string { + if p.target.Platform == Darwin { + // We want /usr/local/osquery.app, not /usr/local/bin/Kolide.app, so we use Dir to strip out `bin` + appBundleBinaryPath := filepath.Join(p.packageRoot, filepath.Dir(p.binDir), "osquery.app", "Contents", "MacOS", "launcher") + if info, err := os.Stat(appBundleBinaryPath); err == nil && !info.IsDir() { + return appBundleBinaryPath + } + } + + if p.target.Platform == Windows { + return filepath.Join(p.packageRoot, p.binDir, string(p.target.Arch), p.target.PlatformBinaryName("osqueryd")) + } + + return filepath.Join(p.packageRoot, p.binDir, p.target.PlatformBinaryName("osqueryd")) +} diff --git a/pkg/packaging/packaging.go b/pkg/packaging/packaging.go index a250243bc..db9c6be2c 100644 --- a/pkg/packaging/packaging.go +++ b/pkg/packaging/packaging.go @@ -93,7 +93,6 @@ func NewPackager() *PackageOptions { } func (p *PackageOptions) Build(ctx context.Context, packageWriter io.Writer, target Target) error { - p.target = target p.packageWriter = packageWriter @@ -284,6 +283,9 @@ func (p *PackageOptions) Build(ctx context.Context, packageWriter io.Writer, tar } } + // Record the osquery version, now that we've downloaded it + p.setOsqueryVersionInCtx(ctx) + p.initOptions = &packagekit.InitOptions{ Name: "launcher", Description: "The Kolide Launcher",