From 28a640ac5f060621e91d5b017767e1fb08899deb Mon Sep 17 00:00:00 2001 From: Rebecca Mahany-Horton Date: Tue, 12 Sep 2023 15:39:12 -0400 Subject: [PATCH] Find correct update directory for osqueryd --- cmd/launcher/main.go | 4 ++-- pkg/autoupdate/findnew.go | 12 +++++++----- pkg/autoupdate/findnew_test.go | 36 ++++++++++++++++++++++++++-------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/cmd/launcher/main.go b/cmd/launcher/main.go index 437a0212c..2587a371e 100644 --- a/cmd/launcher/main.go +++ b/cmd/launcher/main.go @@ -150,10 +150,10 @@ func runNewerLauncherIfAvailable(ctx context.Context, logger log.Logger) { // If the legacy autoupdate path variable isn't already set, set it to help // the legacy autoupdater find its update directory even when the newer binary // runs out of a different directory. - if _, ok := os.LookupEnv(autoupdate.LegacyAutoupdatePathEnvVar); !ok { + if _, ok := os.LookupEnv(autoupdate.LegacyLauncherAutoupdatePathEnvVar); !ok { currentPath, err := os.Executable() if err == nil { - os.Setenv(autoupdate.LegacyAutoupdatePathEnvVar, currentPath) + os.Setenv(autoupdate.LegacyLauncherAutoupdatePathEnvVar, currentPath) } } diff --git a/pkg/autoupdate/findnew.go b/pkg/autoupdate/findnew.go index 7f9e22306..37e6d33fb 100644 --- a/pkg/autoupdate/findnew.go +++ b/pkg/autoupdate/findnew.go @@ -29,7 +29,7 @@ const defaultBuildTimestamp = "0" const updateDirSuffix = "-updates" const ( - LegacyAutoupdatePathEnvVar = "LAUNCHER_LEGACY_AUTOUPDATE_PATH" + LegacyLauncherAutoupdatePathEnvVar = "LAUNCHER_LEGACY_AUTOUPDATE_PATH" ) type newestSettings struct { @@ -251,8 +251,9 @@ func FindNewest(ctx context.Context, fullBinaryPath string, opts ...newestOption // // It makes some string assumptions about how things are named. func getUpdateDir(fullBinaryPath string) string { - if installedPath := os.Getenv(LegacyAutoupdatePathEnvVar); installedPath != "" { - fullBinaryPath = installedPath + if installedPath := os.Getenv(LegacyLauncherAutoupdatePathEnvVar); installedPath != "" { + binaryName := filepath.Base(fullBinaryPath) + fullBinaryPath = filepath.Join(filepath.Dir(installedPath), binaryName) } if strings.Contains(fullBinaryPath, ".app") { @@ -331,8 +332,9 @@ func FindBaseDir(path string) string { return "" } - if installedPath := os.Getenv(LegacyAutoupdatePathEnvVar); installedPath != "" { - path = installedPath + if installedPath := os.Getenv(LegacyLauncherAutoupdatePathEnvVar); installedPath != "" { + binaryName := filepath.Base(path) + path = filepath.Join(filepath.Dir(installedPath), binaryName) } // If this is an app bundle installation, we need to adjust the directory -- otherwise we end up with a library diff --git a/pkg/autoupdate/findnew_test.go b/pkg/autoupdate/findnew_test.go index c9b6c61ce..629b1a587 100644 --- a/pkg/autoupdate/findnew_test.go +++ b/pkg/autoupdate/findnew_test.go @@ -93,7 +93,7 @@ func TestGetUpdateDir_WithEnvVar(t *testing.T) { //nolint:paralleltest // same time as other tests. t.Cleanup(func() { - os.Setenv(LegacyAutoupdatePathEnvVar, "") + os.Setenv(LegacyLauncherAutoupdatePathEnvVar, "") }) var tests = []struct { @@ -103,28 +103,48 @@ func TestGetUpdateDir_WithEnvVar(t *testing.T) { //nolint:paralleltest }{ { currentPath: "/a/path/var/id/hostname/updates/binary/1.2.3/binary", - installPath: filepath.Clean("/a/bin/binary"), + installPath: filepath.Clean("/a/bin/launcher"), out: filepath.Clean("/a/bin/binary-updates"), }, { currentPath: "/a/path/var/id/hostname/updates/binary/1.2.3/binary", - installPath: filepath.Clean("/a/Test.app/Contents/MacOS/binary"), + installPath: filepath.Clean("/a/Test.app/Contents/MacOS/launcher"), out: filepath.Clean("/a/bin/binary-updates"), }, { currentPath: "/a/path/var/id/hostname/updates/binary/1.2.3/Test.app/Contents/MacOS/binary", - installPath: filepath.Clean("/a/bin/binary"), + installPath: filepath.Clean("/a/bin/launcher"), out: filepath.Clean("/a/bin/binary-updates"), }, { currentPath: "/a/path/var/id/hostname/updates/binary/1.2.3/Test.app/Contents/MacOS/binary", - installPath: filepath.Clean("/a/Test.app/Contents/MacOS/binary"), + installPath: filepath.Clean("/a/Test.app/Contents/MacOS/launcher"), out: filepath.Clean("/a/bin/binary-updates"), }, + { + currentPath: "/a/path/var/id/hostname/updates/launcher/1.2.3/launcher", + installPath: filepath.Clean("/a/bin/launcher"), + out: filepath.Clean("/a/bin/launcher-updates"), + }, + { + currentPath: "/a/path/var/id/hostname/updates/launcher/1.2.3/launcher", + installPath: filepath.Clean("/a/Test.app/Contents/MacOS/launcher"), + out: filepath.Clean("/a/bin/launcher-updates"), + }, + { + currentPath: "/a/path/var/id/hostname/updates/launcher/1.2.3/Test.app/Contents/MacOS/launcher", + installPath: filepath.Clean("/a/bin/launcher"), + out: filepath.Clean("/a/bin/launcher-updates"), + }, + { + currentPath: "/a/path/var/id/hostname/updates/launcher/1.2.3/Test.app/Contents/MacOS/launcher", + installPath: filepath.Clean("/a/Test.app/Contents/MacOS/launcher"), + out: filepath.Clean("/a/bin/launcher-updates"), + }, } for _, tt := range tests { - os.Setenv(LegacyAutoupdatePathEnvVar, tt.installPath) + os.Setenv(LegacyLauncherAutoupdatePathEnvVar, tt.installPath) require.Equal(t, tt.out, getUpdateDir(tt.currentPath), "input: install path %s, current path %s", tt.installPath, tt.currentPath) } } @@ -156,7 +176,7 @@ func TestFindBaseDir_WithEnvVar(t *testing.T) { //nolint:paralleltest // same time as other tests. t.Cleanup(func() { - os.Setenv(LegacyAutoupdatePathEnvVar, "") + os.Setenv(LegacyLauncherAutoupdatePathEnvVar, "") }) var tests = []struct { @@ -187,7 +207,7 @@ func TestFindBaseDir_WithEnvVar(t *testing.T) { //nolint:paralleltest } for _, tt := range tests { - os.Setenv(LegacyAutoupdatePathEnvVar, tt.installPath) + os.Setenv(LegacyLauncherAutoupdatePathEnvVar, tt.installPath) require.Equal(t, tt.out, FindBaseDir(tt.currentPath), "input: install path %s, current path %s", tt.installPath, tt.currentPath) } }