Skip to content

Commit

Permalink
Register the desktop app path with launch services
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany committed May 31, 2024
1 parent 8e5c472 commit b07f594
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ee/allowedcmd/cmd_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func Lsof(ctx context.Context, arg ...string) (*exec.Cmd, error) {
return validatedCommand(ctx, "/usr/sbin/lsof", arg...)
}

func Lsregister(ctx context.Context, arg ...string) (*exec.Cmd, error) {
return validatedCommand(ctx, "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister", arg...)
}

func Mdfind(ctx context.Context, arg ...string) (*exec.Cmd, error) {
return validatedCommand(ctx, "/usr/bin/mdfind", arg...)
}
Expand Down
39 changes: 39 additions & 0 deletions ee/desktop/user/universallink/handler_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"log/slog"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"

"github.com/kolide/launcher/ee/allowedcmd"
"github.com/kolide/launcher/ee/gowrapper"
"github.com/kolide/launcher/ee/localserver"
)
Expand Down Expand Up @@ -41,6 +43,13 @@ func NewUniversalLinkHandler(slogger *slog.Logger) (*universalLinkHandler, chan
}

func (u *universalLinkHandler) Execute() error {
// Register self
if err := register(); err != nil {
u.slogger.Log(context.TODO(), slog.LevelWarn,
"could not register desktop app with Launch Services on startup",
"err", err,
)
}
for {
select {
case i := <-u.urlInput:
Expand Down Expand Up @@ -74,6 +83,36 @@ func (u *universalLinkHandler) Interrupt(_ error) {
close(u.urlInput)
}

func register() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

currentExecutable, err := os.Executable()
if err != nil {
return fmt.Errorf("getting current executable: %w", err)
}

// If we're running the originally-installed version of launcher, no need to register
if strings.HasPrefix(currentExecutable, "/usr/local") {
return nil
}

// Point to `Kolide.app`
currentExecutable = strings.TrimSuffix(currentExecutable, "/Contents/MacOS/launcher")

// Run lsregister against this path
lsregisterCmd, err := allowedcmd.Lsregister(ctx, currentExecutable)
if err != nil {
return fmt.Errorf("creating lsregister %s command: %w", currentExecutable, err)
}

if out, err := lsregisterCmd.CombinedOutput(); err != nil {
return fmt.Errorf("running lsregister %s: output `%s`, err: %w", currentExecutable, string(out), err)
}

return nil
}

// handleUniversalLinkRequest receives requests, validates them, and forwards them
// to launcher root's localserver.
func (u *universalLinkHandler) handleUniversalLinkRequest(requestUrl string) error {
Expand Down

0 comments on commit b07f594

Please sign in to comment.