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

Find correct update directory for osqueryd #1349

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions cmd/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
12 changes: 7 additions & 5 deletions pkg/autoupdate/findnew.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const defaultBuildTimestamp = "0"
const updateDirSuffix = "-updates"

const (
LegacyAutoupdatePathEnvVar = "LAUNCHER_LEGACY_AUTOUPDATE_PATH"
LegacyLauncherAutoupdatePathEnvVar = "LAUNCHER_LEGACY_AUTOUPDATE_PATH"
)

type newestSettings struct {
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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
Expand Down
36 changes: 28 additions & 8 deletions pkg/autoupdate/findnew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand Down
Loading