Exemple #1
0
func validatePath(val string, validateMountMode bool) (string, error) {
	var containerPath string
	var mode string

	if strings.Count(val, ":") > 2 {
		return val, fmt.Errorf("bad format for volumes: %s", val)
	}

	splited := strings.SplitN(val, ":", 3)
	if splited[0] == "" {
		return val, fmt.Errorf("bad format for volumes: %s", val)
	}
	switch len(splited) {
	case 1:
		containerPath = splited[0]
		val = path.Clean(containerPath)
	case 2:
		if isValid, _ := volume.ValidateMountMode(splited[1]); validateMountMode && isValid {
			containerPath = splited[0]
			mode = splited[1]
			val = fmt.Sprintf("%s:%s", path.Clean(containerPath), mode)
		} else {
			containerPath = splited[1]
			val = fmt.Sprintf("%s:%s", splited[0], path.Clean(containerPath))
		}
	case 3:
		containerPath = splited[1]
		mode = splited[2]
		if isValid, _ := volume.ValidateMountMode(splited[2]); validateMountMode && !isValid {
			return val, fmt.Errorf("bad mount mode specified : %s", mode)
		}
		val = fmt.Sprintf("%s:%s:%s", splited[0], containerPath, mode)
	}

	if !path.IsAbs(containerPath) {
		return val, fmt.Errorf("%s is not an absolute path", containerPath)
	}
	return val, nil
}
Exemple #2
0
// parseVolumesFrom ensure that the supplied volumes-from is valid.
func parseVolumesFrom(spec string) (string, string, error) {
	if len(spec) == 0 {
		return "", "", fmt.Errorf("malformed volumes-from specification: %s", spec)
	}

	specParts := strings.SplitN(spec, ":", 2)
	id := specParts[0]
	mode := "rw"

	if len(specParts) == 2 {
		mode = specParts[1]
		if isValid, _ := volume.ValidateMountMode(mode); !isValid {
			return "", "", fmt.Errorf("invalid mode for volumes-from: %s", mode)
		}
	}
	return id, mode, nil
}
Exemple #3
0
// parseBindMount validates the configuration of mount information in runconfig is valid.
func parseBindMount(spec string, mountLabel string, config *runconfig.Config) (*mountPoint, error) {
	bind := &mountPoint{
		RW: true,
	}
	arr := strings.Split(spec, ":")

	switch len(arr) {
	case 2:
		bind.Destination = arr[1]
	case 3:
		bind.Destination = arr[1]
		mode := arr[2]
		isValid, isRw := volume.ValidateMountMode(mode)
		if !isValid {
			return nil, fmt.Errorf("invalid mode for volumes-from: %s", mode)
		}
		bind.RW = isRw
		// Mode field is used by SELinux to decide whether to apply label
		bind.Mode = mode
	default:
		return nil, fmt.Errorf("Invalid volume specification: %s", spec)
	}

	name, source, err := parseVolumeSource(arr[0])
	if err != nil {
		return nil, err
	}

	if len(source) == 0 {
		bind.Driver = config.VolumeDriver
		if len(bind.Driver) == 0 {
			bind.Driver = volume.DefaultDriverName
		}
	} else {
		bind.Source = filepath.Clean(source)
	}

	bind.Name = name
	bind.Destination = filepath.Clean(bind.Destination)
	return bind, nil
}