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

[Agent] Handle abs paths on windows correctly #17461

Merged
merged 5 commits into from
Apr 6, 2020
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
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Fixed tests on windows {pull}16922[16922]
- Fixed installers for SNAPSHOTs and windows {pull}17077[17077]
- Fixed merge of config {pull}17399[17399]
- Handle abs paths on windows correctly {pull}17461[17461]

==== New features

Expand Down
45 changes: 36 additions & 9 deletions x-pack/elastic-agent/pkg/agent/transpiler/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,16 @@ type DeleteFileStep struct {

// Execute executes delete file step.
func (r *DeleteFileStep) Execute(rootDir string) error {
path, isSubpath := joinPaths(rootDir, r.Path)
path, isSubpath, err := joinPaths(rootDir, r.Path)
if err != nil {
return err
}

if !isSubpath {
return fmt.Errorf("invalid path value for operation 'Delete': %s", path)
}

err := os.Remove(path)
err = os.Remove(path)

if os.IsNotExist(err) && r.FailOnMissing {
// is not found and should be reported
Expand Down Expand Up @@ -166,17 +170,25 @@ type MoveFileStep struct {

// Execute executes move file step.
func (r *MoveFileStep) Execute(rootDir string) error {
path, isSubpath := joinPaths(rootDir, r.Path)
path, isSubpath, err := joinPaths(rootDir, r.Path)
if err != nil {
return err
}

if !isSubpath {
return fmt.Errorf("invalid path value for operation 'Move': %s", path)
}

target, isSubpath := joinPaths(rootDir, r.Target)
target, isSubpath, err := joinPaths(rootDir, r.Target)
if err != nil {
return err
}

if !isSubpath {
return fmt.Errorf("invalid target value for operation 'Move': %s", target)
}

err := os.Rename(path, target)
err = os.Rename(path, target)

if os.IsNotExist(err) && r.FailOnMissing {
// is not found and should be reported
Expand All @@ -201,21 +213,36 @@ func MoveFile(path, target string, failOnMissing bool) *MoveFileStep {
}

// joinPaths joins paths and returns true if path is subpath of rootDir
func joinPaths(rootDir, path string) (string, bool) {
func joinPaths(rootDir, path string) (string, bool, error) {
rootDir = filepath.FromSlash(rootDir)
path = filepath.FromSlash(path)

if runtime.GOOS == "windows" {
// if is unix absolute fix to win absolute
if strings.HasPrefix(path, "\\") {
abs, err := filepath.Abs(rootDir) // get current volume
if err != nil {
return "", false, err
}
vol := filepath.VolumeName(abs)
path = filepath.Join(vol, path)
}
}

if !filepath.IsAbs(path) {
path = filepath.Join(rootDir, path)
}

absRoot := filepath.Clean(filepath.FromSlash(rootDir))
absPath := filepath.Clean(filepath.FromSlash(path))
absRoot := filepath.Clean(rootDir)
absPath := filepath.Clean(path)

// path on windows are case insensitive
if !isFsCaseSensitive(rootDir) {
absRoot = strings.ToLower(absRoot)
absPath = strings.ToLower(absPath)
}

return absPath, strings.HasPrefix(absPath, absRoot)
return absPath, strings.HasPrefix(absPath, absRoot), nil
}

func isFsCaseSensitive(rootDir string) bool {
Expand Down
27 changes: 14 additions & 13 deletions x-pack/elastic-agent/pkg/agent/transpiler/steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ func TestIsSubpath(t *testing.T) {
{"/a", "/a/b/c", "/a/b/c", true},
{"/a", "/A/b/c", "/a/b/c", true},
},
// (Windows issue) See issue:
//"windows": {
// {"/", "a", "\\a", true},
// {"/a", "b", "\\a\\b", true},
// {"/a", "b/c", "\\a\\b\\c", true},
// //
// {"/a/b", "/a/c", "\\a\\c", false},
// {"/a/b", "/a/b/../c", "\\a\\c", false},
// {"/a/b", "../c", "\\a\\c", false},
// {"/a", "/a/b/c", "\\a\\b\\c", true},
// {"/a", "/A/b/c", "\\a\\b\\c", true},
//},
"windows": {
{"c:/", "/a", "c:\\a", true},
{"c:/a", "b", "c:\\a\\b", true},
{"c:/a", "b/c", "c:\\a\\b\\c", true},
{"c:/a/b", "/a/c", "c:\\a\\c", false},
{"c:/a/b", "/a/b/../c", "c:\\a\\c", false},
{"c:/a/b", "../c", "c:\\a\\c", false},
{"c:/a", "/a/b/c", "c:\\a\\b\\c", true},
{"c:/a", "/A/b/c", "c:\\a\\b\\c", true},
{"c:/a", "c:/A/b/c", "c:\\a\\b\\c", true},
{"c:/a", "c:/b/c", "c:\\b\\c", false},
},
}

osSpecificTests, found := testCases[runtime.GOOS]
Expand All @@ -62,7 +62,8 @@ func TestIsSubpath(t *testing.T) {

for _, test := range osSpecificTests {
t.Run(fmt.Sprintf("[%s] root:'%s path: %s'", runtime.GOOS, test.root, test.path), func(t *testing.T) {
newPath, result := joinPaths(test.root, test.path)
newPath, result, err := joinPaths(test.root, test.path)
assert.NoError(t, err)
assert.Equal(t, test.resultPath, newPath)
assert.Equal(t, test.isSubpath, result)
})
Expand Down