Skip to content

Commit b5d90ff

Browse files
authored
hubtest: resolve relative path for 'cscli', 'crowdsec' (#3651)
1 parent c7b12e9 commit b5d90ff

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

pkg/hubtest/hubtest.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package hubtest
22

33
import (
4+
"errors"
45
"fmt"
6+
"io/fs"
57
"os"
68
"os/exec"
79
"path/filepath"
10+
"strings"
811

912
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
1013
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
@@ -58,6 +61,45 @@ http:
5861
`
5962
)
6063

64+
// resolveBinaryPath returns the absolute path to an executable.
65+
// - If the `binary` argument is relative, it is resolved as absolute.
66+
// - If it's not a path, it's looked up in $PATH.
67+
// In all cases it returns an error if the file doesn’t exist (after following links) or is a directory.
68+
func resolveBinaryPath(binary string) (string, error) {
69+
var resolved string
70+
71+
switch {
72+
case filepath.IsAbs(binary):
73+
resolved = binary
74+
case strings.Contains(binary, string(os.PathSeparator)):
75+
absPath, err := filepath.Abs(binary)
76+
if err != nil {
77+
return "", err
78+
}
79+
resolved = absPath
80+
default:
81+
p, err := exec.LookPath(binary)
82+
if err != nil {
83+
return "", err
84+
}
85+
resolved = p
86+
}
87+
88+
f, err := os.Stat(resolved)
89+
switch {
90+
case errors.Is(err, fs.ErrNotExist):
91+
return "", err
92+
case err != nil:
93+
return "", err
94+
}
95+
96+
if f.IsDir() {
97+
return "", fmt.Errorf("%q is a directory, not a file", resolved)
98+
}
99+
100+
return resolved, nil
101+
}
102+
61103
func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecTest bool) (HubTest, error) {
62104
hubPath, err := filepath.Abs(hubPath)
63105
if err != nil {
@@ -74,17 +116,13 @@ func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecT
74116
return HubTest{}, fmt.Errorf("path to hub '%s' doesn't exist, can't run", hubPath)
75117
}
76118
// we can't use hubtest without crowdsec binary
77-
if _, err = exec.LookPath(crowdsecPath); err != nil {
78-
if _, err = os.Stat(crowdsecPath); os.IsNotExist(err) {
79-
return HubTest{}, fmt.Errorf("path to crowdsec binary '%s' doesn't exist or is not in $PATH, can't run", crowdsecPath)
80-
}
119+
if crowdsecPath, err = resolveBinaryPath(crowdsecPath); err != nil {
120+
return HubTest{}, fmt.Errorf("can't find crowdsec binary: %w", err)
81121
}
82122

83123
// we can't use hubtest without cscli binary
84-
if _, err = exec.LookPath(cscliPath); err != nil {
85-
if _, err = os.Stat(cscliPath); os.IsNotExist(err) {
86-
return HubTest{}, fmt.Errorf("path to cscli binary '%s' doesn't exist or is not in $PATH, can't run", cscliPath)
87-
}
124+
if cscliPath, err = resolveBinaryPath(cscliPath); err != nil {
125+
return HubTest{}, fmt.Errorf("can't find cscli binary: %w", err)
88126
}
89127

90128
if isAppsecTest {

0 commit comments

Comments
 (0)