Skip to content

Commit

Permalink
Custom error for homebrew table not found (#1872)
Browse files Browse the repository at this point in the history
Co-authored-by: seph <seph@kolide.co>
  • Loading branch information
Dharma-09 and directionless authored Sep 24, 2024
1 parent fb6d91a commit 1ec19cd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ee/allowedcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package allowedcmd

import (
"context"
"errors"
"fmt"
"os"
"os/exec"
Expand All @@ -20,6 +21,8 @@ func newCmd(ctx context.Context, fullPathToCmd string, arg ...string) *exec.Cmd
return exec.CommandContext(ctx, fullPathToCmd, arg...) //nolint:forbidigo // This is our approved usage of exec.CommandContext
}

var ErrCommandNotFound = errors.New("command not found")

func validatedCommand(ctx context.Context, knownPath string, arg ...string) (*exec.Cmd, error) {
knownPath = filepath.Clean(knownPath)

Expand All @@ -31,15 +34,15 @@ func validatedCommand(ctx context.Context, knownPath string, arg ...string) (*ex
// We expect to know the exact location for allowlisted commands on all
// OSes except for a few Linux distros.
if !allowSearchPath() {
return nil, fmt.Errorf("not found: %s", knownPath)
return nil, fmt.Errorf("%w: %s", ErrCommandNotFound, knownPath)
}

cmdName := filepath.Base(knownPath)
if foundPath, err := exec.LookPath(cmdName); err == nil {
return newCmd(ctx, foundPath, arg...), nil
}

return nil, fmt.Errorf("%s not found at %s and could not be located elsewhere", cmdName, knownPath)
return nil, fmt.Errorf("%w: not found at %s and could not be located elsewhere", ErrCommandNotFound, knownPath)
}

func allowSearchPath() bool {
Expand Down
6 changes: 6 additions & 0 deletions ee/tables/homebrew/upgradeable.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package brew_upgradeable
import (
"bytes"
"context"
"errors"
"fmt"
"log/slog"
"os"
Expand Down Expand Up @@ -43,7 +44,12 @@ func (t *Table) generate(ctx context.Context, queryContext table.QueryContext) (
// that user. To reduce duplicating the WithUid table helper, we can find the owner of the binary,
// and pass the said owner to the WIthUid method to handle setting the appropriate env vars.
cmd, err := allowedcmd.Brew(ctx)

if err != nil {
if errors.Is(err, allowedcmd.ErrCommandNotFound) {
// No data, no error
return nil, nil
}
return nil, fmt.Errorf("failure allocating allowedcmd.Brew: %w", err)
}

Expand Down

0 comments on commit 1ec19cd

Please sign in to comment.