From 4eb25458f1647b9292570a1c23bd04bde5556fb3 Mon Sep 17 00:00:00 2001 From: Jared Watts Date: Wed, 16 Nov 2016 16:51:11 -0800 Subject: [PATCH] Look for devices using a cleaned mount point --- pkg/util/sys/device.go | 2 ++ pkg/util/sys/device_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/pkg/util/sys/device.go b/pkg/util/sys/device.go index 02f01e7bd..d1faf1f4e 100644 --- a/pkg/util/sys/device.go +++ b/pkg/util/sys/device.go @@ -19,6 +19,7 @@ import ( "fmt" "os" "os/user" + "path/filepath" "strings" "github.com/google/uuid" @@ -103,6 +104,7 @@ func GetDeviceMountPoint(deviceName string, executor exec.Executor) (string, err } func GetDeviceFromMountPoint(mountPoint string, executor exec.Executor) (string, error) { + mountPoint = filepath.Clean(mountPoint) cmd := fmt.Sprintf("get device from mount point %s", mountPoint) device, err := executor.ExecuteCommandPipeline( cmd, diff --git a/pkg/util/sys/device_test.go b/pkg/util/sys/device_test.go index 5c1239f9f..3abe95882 100644 --- a/pkg/util/sys/device_test.go +++ b/pkg/util/sys/device_test.go @@ -16,8 +16,10 @@ limitations under the License. package sys import ( + "strings" "testing" + exectest "github.com/rook/rook/pkg/util/exec/test" "github.com/stretchr/testify/assert" ) @@ -55,3 +57,33 @@ sdc tmpfs` result = parseDFOutput("sdc", output) assert.Equal(t, "", result) } + +func TestGetDeviceFromMountPoint(t *testing.T) { + const device = "/dev/rbd3" + e := &exectest.MockExecutor{ + MockExecuteCommandPipeline: func(actionName string, command string) (string, error) { + switch { + case strings.HasPrefix(actionName, "get device from mount point"): + // verify that the mount path being searched for has been cleaned + assert.Contains(t, command, " /tmp/mymountpath ") + return device, nil + } + return "", nil + }, + } + + // no trailing slash should work OK + d, err := GetDeviceFromMountPoint("/tmp/mymountpath", e) + assert.Nil(t, err) + assert.Equal(t, device, d) + + // a trailing slash should be cleaned and work OK + d, err = GetDeviceFromMountPoint("/tmp/mymountpath/", e) + assert.Nil(t, err) + assert.Equal(t, device, d) + + // a parent directory '..' in the middle of the path should work OK + d, err = GetDeviceFromMountPoint("/tmp/somedir/../mymountpath/", e) + assert.Nil(t, err) + assert.Equal(t, device, d) +}