86 lines
2.3 KiB
Diff
86 lines
2.3 KiB
Diff
From 7766b079c8fd004fee9d5eba19cb98cac568773b Mon Sep 17 00:00:00 2001
|
|
From: Anatol Pomozov <anatol.pomozov@gmail.com>
|
|
Date: Thu, 23 Mar 2023 18:49:14 -0700
|
|
Subject: [PATCH] generator: Do not rely on user's lookup order for binaries
|
|
|
|
Until now we used `exec.LookPath` golang function to lookup binaries.
|
|
This function depends on user's $PATH envvar and the order directories
|
|
specified there. This could mess with our image symlink tree.
|
|
|
|
What we want is to make sure that we lookup/add files at non-symlinked
|
|
directories first (i.e. /usr/bin).
|
|
|
|
Closes #213
|
|
---
|
|
generator/generator.go | 28 ++++++++++++++++++++++++++--
|
|
generator/generator_test.go | 6 ++++++
|
|
2 files changed, 32 insertions(+), 2 deletions(-)
|
|
|
|
diff --git a/generator/generator.go b/generator/generator.go
|
|
index 0ab135c..8f0a72f 100644
|
|
--- a/generator/generator.go
|
|
+++ b/generator/generator.go
|
|
@@ -5,7 +5,6 @@ import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
- "os/exec"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
@@ -290,7 +289,7 @@ func (img *Image) appendExtraFiles(binaries ...string) error {
|
|
// If the given name is not an absolute path, assume that it refers
|
|
// to an executable and lookup the path to the executable using $PATH.
|
|
var err error
|
|
- f, err = exec.LookPath(f)
|
|
+ f, err = lookupPath(f)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
@@ -303,6 +302,31 @@ func (img *Image) appendExtraFiles(binaries ...string) error {
|
|
return nil
|
|
}
|
|
|
|
+func lookupPath(binary string) (string, error) {
|
|
+ paths := []string{
|
|
+ "/usr/bin",
|
|
+ "/usr/sbin",
|
|
+ "/bin",
|
|
+ "/sbin",
|
|
+ "/usr/local/bin",
|
|
+ "/usr/local/sbin",
|
|
+ }
|
|
+
|
|
+ for _, p := range paths {
|
|
+ f := filepath.Join(p, binary)
|
|
+ _, err := os.Stat(f)
|
|
+ if os.IsNotExist(err) {
|
|
+ continue
|
|
+ }
|
|
+ if err != nil {
|
|
+ return "", err
|
|
+ }
|
|
+ return f, nil
|
|
+ }
|
|
+
|
|
+ return "", os.ErrNotExist
|
|
+}
|
|
+
|
|
func findFwFile(fw string) (string, error) {
|
|
supportedFwExt := []string{
|
|
"",
|
|
diff --git a/generator/generator_test.go b/generator/generator_test.go
|
|
index 57b7cce..5938cbc 100644
|
|
--- a/generator/generator_test.go
|
|
+++ b/generator/generator_test.go
|
|
@@ -550,3 +550,9 @@ func TestModprobeOptions(t *testing.T) {
|
|
}
|
|
require.Equal(t, expect, cfg.ModprobeOptions)
|
|
}
|
|
+
|
|
+func TestLookupFile(t *testing.T) {
|
|
+ path, err := lookupPath("echo")
|
|
+ require.NoError(t, err)
|
|
+ require.Equal(t, "/usr/bin/echo", path)
|
|
+}
|