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

Adjust paths in checkups to accommodate NixOS #1558

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
41 changes: 40 additions & 1 deletion ee/debug/checkups/binary_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"

"github.com/kolide/launcher/ee/allowedcmd"
)

type BinaryDirectory struct {
Expand All @@ -21,7 +24,7 @@ func (c *BinaryDirectory) Name() string {
func (c *BinaryDirectory) Run(_ context.Context, extraFH io.Writer) error {
bindir := getBinDir()
if bindir == "" {
return errors.New("No default bin directory")
return errors.New("no default bin directory")
}

// Note that we're recursing `/usr/local/kolide-k2` and not .../bin. So the counts may not be what
Expand Down Expand Up @@ -68,10 +71,46 @@ func getBinDir() string {
case "darwin":
return "/usr/local/kolide-k2"
case "linux":
if allowedcmd.IsNixOS() {
Comment on lines 71 to +74
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
case "darwin":
return "/usr/local/kolide-k2"
case "linux":
if allowedcmd.IsNixOS() {
case "darwin", "linux":
if allowedcmd.IsNixOS() {

I think?

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 don't think so -- IsNixOS is an OS check, not if the package manager is in use. I kept it pretty narrowly scoped to start.

I am not sure how much of the changes I've made so far adequately support Nix on Darwin right now -- could be fine, but I haven't tested it at all yet.

return getBinDirOnNixOS()
}
return "/usr/local/kolide-k2"
case "windows":
return "C:\\Program Files\\Kolide\\Launcher-kolide-k2\\bin"
}

return ""
}

// getBinDirOnNixOS returns the most likely binary directory for NixOS,
// looking through the nix store to find it.
func getBinDirOnNixOS() string {
// The binary directory on NixOS will look like, e.g.,:
// `/nix/store/w8k7h0s9v64gqj60as756gg81c788zkp-kolide-launcher-1.4.4-6-g33c6fd9/bin`
matches, err := filepath.Glob("/nix/store/*-kolide-launcher-*/bin")
if err != nil || len(matches) == 0 {
return ""
}

if len(matches) == 1 {
return matches[0]
}

// In this case, launcher has been installed multiple times. We will pick
// the most recent installation.
mostRecentDir := ""
mostRecentDirTimestamp := int64(0)
for _, m := range matches {
dirInfo, err := os.Stat(m)
if err != nil {
continue
}

if dirInfo.ModTime().Unix() > mostRecentDirTimestamp {
mostRecentDir = m
mostRecentDirTimestamp = dirInfo.ModTime().Unix()
}
}

return mostRecentDir
}
14 changes: 5 additions & 9 deletions ee/debug/checkups/osquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -61,19 +61,15 @@ func (o *osqueryCheckup) version(ctx context.Context) (string, error) {
}

func (o *osqueryCheckup) interactive(ctx context.Context) error {
var launcherPath string
switch runtime.GOOS {
case "linux", "darwin":
launcherPath = "/usr/local/kolide-k2/bin/launcher"
case "windows":
launcherPath = `C:\Program Files\Kolide\Launcher-kolide-k2\bin\launcher.exe`
launcherPath, err := os.Executable()
if err != nil {
return fmt.Errorf("getting current running executable: %w", err)
}

cmdCtx, cmdCancel := context.WithTimeout(ctx, 20*time.Second)
defer cmdCancel()

// We trust the autoupdate library to find the correct path
cmd := exec.CommandContext(cmdCtx, launcherPath, "interactive") //nolint:forbidigo // We trust the autoupdate library to find the correct path
cmd := exec.CommandContext(cmdCtx, launcherPath, "interactive") //nolint:forbidigo // It is safe to exec the current running executable
hideWindow(cmd)
cmd.Stdin = strings.NewReader(`select * from osquery_info;`)

Expand Down
Loading