validate.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package container // import "github.com/docker/docker/daemon/cluster/executor/container"
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "github.com/moby/swarmkit/v2/api"
  7. )
  8. func validateMounts(mounts []api.Mount) error {
  9. for _, mount := range mounts {
  10. // Target must always be absolute
  11. // except if target is Windows named pipe
  12. if !filepath.IsAbs(mount.Target) && mount.Type != api.MountTypeNamedPipe {
  13. return fmt.Errorf("invalid mount target, must be an absolute path: %s", mount.Target)
  14. }
  15. switch mount.Type {
  16. // The checks on abs paths are required due to the container API confusing
  17. // volume mounts as bind mounts when the source is absolute (and vice-versa)
  18. // See #25253
  19. // TODO: This is probably not necessary once #22373 is merged
  20. case api.MountTypeBind:
  21. if !filepath.IsAbs(mount.Source) {
  22. return fmt.Errorf("invalid bind mount source, must be an absolute path: %s", mount.Source)
  23. }
  24. case api.MountTypeVolume:
  25. if filepath.IsAbs(mount.Source) {
  26. return fmt.Errorf("invalid volume mount source, must not be an absolute path: %s", mount.Source)
  27. }
  28. case api.MountTypeTmpfs:
  29. if mount.Source != "" {
  30. return errors.New("invalid tmpfs source, source must be empty")
  31. }
  32. case api.MountTypeNamedPipe:
  33. if mount.Source == "" {
  34. return errors.New("invalid npipe source, source must not be empty")
  35. }
  36. case api.MountTypeCluster:
  37. // nothing to do here.
  38. default:
  39. return fmt.Errorf("invalid mount type: %s", mount.Type)
  40. }
  41. }
  42. return nil
  43. }