Skip to content

Commit

Permalink
Set osquery version in packaging context (#1901)
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany authored Oct 21, 2024
1 parent 63230d3 commit e6d3fee
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/packagekit/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/packagekit/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/packagekit/package_fpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions pkg/packagekit/package_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/packagekit/package_wix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
47 changes: 47 additions & 0 deletions pkg/packaging/helpers.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
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 "-"
// This is useful because ":" is not a valid character for file paths.
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"))
}
4 changes: 3 additions & 1 deletion pkg/packaging/packaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit e6d3fee

Please sign in to comment.