mirror of https://github.com/docker/cli.git
Improve validation for volume specs
The current validation only checked for the number of elements in the volume-spec, however, did not validate if the elements were empty. Because of this, an empty volume-spec (""), or volume spec only containing separators ("::") would not be invalidated. This adds a simple check for empty elements in the volume-spec, and returns an error if the spec is invalid. A unit-test is also added to verify the behavior. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
bc4590fd7d
commit
1f57f07070
|
@ -31,6 +31,12 @@ func convertVolumeToMount(volumeSpec string, stackVolumes volumes, namespace Nam
|
||||||
// TODO: split Windows path mappings properly
|
// TODO: split Windows path mappings properly
|
||||||
parts := strings.SplitN(volumeSpec, ":", 3)
|
parts := strings.SplitN(volumeSpec, ":", 3)
|
||||||
|
|
||||||
|
for _, part := range parts {
|
||||||
|
if strings.TrimSpace(part) == "" {
|
||||||
|
return mount.Mount{}, fmt.Errorf("invalid volume: %s", volumeSpec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch len(parts) {
|
switch len(parts) {
|
||||||
case 3:
|
case 3:
|
||||||
source = parts[0]
|
source = parts[0]
|
||||||
|
@ -41,8 +47,6 @@ func convertVolumeToMount(volumeSpec string, stackVolumes volumes, namespace Nam
|
||||||
target = parts[1]
|
target = parts[1]
|
||||||
case 1:
|
case 1:
|
||||||
target = parts[0]
|
target = parts[0]
|
||||||
default:
|
|
||||||
return mount.Mount{}, fmt.Errorf("invalid volume: %s", volumeSpec)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if source == "" {
|
if source == "" {
|
||||||
|
|
|
@ -46,6 +46,15 @@ func TestConvertVolumeToMountAnonymousVolume(t *testing.T) {
|
||||||
assert.DeepEqual(t, mount, expected)
|
assert.DeepEqual(t, mount, expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConvertVolumeToMountInvalidFormat(t *testing.T) {
|
||||||
|
namespace := NewNamespace("foo")
|
||||||
|
invalids := []string{"::", "::cc", ":bb:", "aa::", "aa::cc", "aa:bb:", " : : ", " : :cc", " :bb: ", "aa: : ", "aa: :cc", "aa:bb: "}
|
||||||
|
for _, vol := range invalids {
|
||||||
|
_, err := convertVolumeToMount(vol, volumes{}, namespace)
|
||||||
|
assert.Error(t, err, "invalid volume: "+vol)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestConvertVolumeToMountNamedVolume(t *testing.T) {
|
func TestConvertVolumeToMountNamedVolume(t *testing.T) {
|
||||||
stackVolumes := volumes{
|
stackVolumes := volumes{
|
||||||
"normal": composetypes.VolumeConfig{
|
"normal": composetypes.VolumeConfig{
|
||||||
|
|
Loading…
Reference in New Issue