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

Custom error for homebrew table not found #1872

Merged
merged 7 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 5 additions & 2 deletions ee/allowedcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"os/exec"
"path/filepath"
"errors"
RebeccaMahany marked this conversation as resolved.
Show resolved Hide resolved
)

type AllowedCommand func(ctx context.Context, arg ...string) (*exec.Cmd, error)
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
2 changes: 1 addition & 1 deletion ee/allowedcmd/cmd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Apt(ctx context.Context, arg ...string) (*exec.Cmd, error) {
func Brew(ctx context.Context, arg ...string) (*exec.Cmd, error) {
validatedCmd, err := validatedCommand(ctx, "/home/linuxbrew/.linuxbrew/bin/brew", arg...)
if err != nil {
return nil, err
return nil, nil
RebeccaMahany marked this conversation as resolved.
Show resolved Hide resolved
}

validatedCmd.Env = append(validatedCmd.Environ(), "HOMEBREW_NO_AUTO_UPDATE=1")
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 @@ -12,6 +12,7 @@ import (
"strconv"
"strings"
"syscall"
"errors"

"github.com/kolide/launcher/ee/allowedcmd"
"github.com/kolide/launcher/ee/dataflatten"
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)

RebeccaMahany marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
if errors.Is(err, allowedcmd.ErrCommandNotFound) {
// No data, no error
return nil, err
Dharma-09 marked this conversation as resolved.
Show resolved Hide resolved
}
return nil, fmt.Errorf("failure allocating allowedcmd.Brew: %w", err)
}

Expand Down
Loading