diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2020-05-28 13:16:05 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2020-06-02 13:24:16 +0300 |
commit | 20e1bc638e02cca6159bbaf5629f70d9097603df (patch) | |
tree | 5f4423508aef46a0b727ca0d3d8cf2f51f51a030 | |
parent | 5ea5f391d66b03c01685dce7014433c09ad4a1c3 (diff) |
clean configured storage paths
Cleans configured storage paths to ensure the paths are well-
formed.
-rw-r--r-- | changelogs/unreleased/smh-clean-storage-paths.yml | 5 | ||||
-rw-r--r-- | internal/config/config.go | 15 | ||||
-rw-r--r-- | internal/config/config_test.go | 21 |
3 files changed, 32 insertions, 9 deletions
diff --git a/changelogs/unreleased/smh-clean-storage-paths.yml b/changelogs/unreleased/smh-clean-storage-paths.yml new file mode 100644 index 000000000..051a18326 --- /dev/null +++ b/changelogs/unreleased/smh-clean-storage-paths.yml @@ -0,0 +1,5 @@ +--- +title: Clean configured storage paths +merge_request: 2223 +author: +type: fixed diff --git a/internal/config/config.go b/internal/config/config.go index 0b31aaf00..22af64b5a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -119,6 +119,10 @@ func Load(file io.Reader) error { Config.setDefaults() + for i := range Config.Storages { + Config.Storages[i].Path = filepath.Clean(Config.Storages[i].Path) + } + return nil } @@ -277,22 +281,19 @@ func validateStorages() error { return fmt.Errorf("storage %+v path must be a dir", storage) } - stPath := filepath.Clean(storage.Path) - for j := 0; j < i; j++ { - other := Config.Storages[j] + for _, other := range Config.Storages[:i] { if other.Name == storage.Name { return fmt.Errorf("storage %q is defined more than once", storage.Name) } - otherPath := filepath.Clean(other.Path) - if stPath == otherPath { + if storage.Path == other.Path { // This is weird but we allow it for legacy gitlab.com reasons. continue } - if strings.HasPrefix(stPath, otherPath) || strings.HasPrefix(otherPath, stPath) { + if strings.HasPrefix(storage.Path, other.Path) || strings.HasPrefix(other.Path, storage.Path) { // If storages have the same sub directory, that is allowed - if filepath.Dir(stPath) == filepath.Dir(otherPath) { + if filepath.Dir(storage.Path) == filepath.Dir(other.Path) { continue } return fmt.Errorf("storage paths may not nest: %q and %q", storage.Name, other.Name) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 82b916796..5bb3ab46d 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -9,6 +9,7 @@ import ( "os/exec" "path" "path/filepath" + "strings" "testing" "time" @@ -52,7 +53,7 @@ func TestLoadEmptyConfig(t *testing.T) { func TestLoadStorage(t *testing.T) { tmpFile := configFileReader(`[[storage]] name = "default" -path = "/tmp"`) +path = "/tmp/"`) err := Load(tmpFile) assert.NoError(t, err) @@ -69,6 +70,22 @@ path = "/tmp"`) } } +func TestUncleanStoragePaths(t *testing.T) { + require.NoError(t, Load(strings.NewReader(`[[storage]] +name="unclean-path-1" +path="/tmp/repos1//" + +[[storage]] +name="unclean-path-2" +path="/tmp/repos2/subfolder/.." +`))) + + require.Equal(t, []Storage{ + {Name: "unclean-path-1", Path: "/tmp/repos1"}, + {Name: "unclean-path-2", Path: "/tmp/repos2"}, + }, Config.Storages) +} + func TestLoadMultiStorage(t *testing.T) { tmpFile := configFileReader(`[[storage]] name="default" @@ -76,7 +93,7 @@ path="/tmp/repos1" [[storage]] name="other" -path="/tmp/repos2"`) +path="/tmp/repos2/"`) err := Load(tmpFile) assert.NoError(t, err) |