package migration import ( "encoding/json" "errors" "fmt" "io" "os" "path/filepath" "strings" ) // Endpoint describes how to reach one side of a migration: the Ceph // client identity and configuration, the subvolume and, for the rsync // backend, the local mount of the CephFS filesystem root. type Endpoint struct { User string `json:"user,omitempty"` Config string `json:"config"` Keyring string `json:"keyring"` Filesystem string `json:"filesystem"` Group string `json:"group"` Subvolume string `json:"subvolume"` Mount string `json:"mount,omitempty"` } // Config is the JSON migration configuration read by ReadConfig. // Backend selects "native" (libcephfs) or "rsync" (the default). type Config struct { Backend string `json:"backend,omitempty"` Workers int `json:"workers,omitempty"` Source Endpoint `json:"source"` Target Endpoint `json:"target"` BandwidthKiB int64 `json:"bandwidth_kib,omitempty"` // FullScan disables the rctime shortcut of the native backend, so every // round reads all metadata. finalize always does. FullScan bool `json:"full_scan,omitempty"` } // ReadConfig reads exactly one JSON object from the regular file at path, // rejects unknown fields and returns the validated configuration. func ReadConfig(path string) (Config, error) { var c Config f, err := os.Open(path) if err != nil { return c, err } defer f.Close() st, err := f.Stat() if err != nil { return c, err } if !st.Mode().IsRegular() || st.Size() > 1<<20 { return c, errors.New("config must be a regular file of at most 1 MiB") } dec := json.NewDecoder(io.LimitReader(f, 1<<20)) dec.DisallowUnknownFields() if err = dec.Decode(&c); err != nil { return c, fmt.Errorf("config: %w", err) } if err = dec.Decode(new(any)); !errors.Is(err, io.EOF) { return c, errors.New("config must contain exactly one JSON object") } return c, c.Validate() } func validName(s string) bool { return s != "" && s != "." && s != ".." && !strings.HasPrefix(s, "-") && !strings.ContainsAny(s, "/\x00\n\r") } // Validate checks names, paths, worker and bandwidth limits and reports // the first problem found. func (c Config) Validate() error { if c.Backend != "" && c.Backend != "rsync" && c.Backend != "native" { return errors.New("backend must be native or rsync") } if c.Workers < 0 || c.Workers > 64 { return errors.New("workers must be between 0 and 64 (0 selects the default)") } for _, p := range []struct { name string e Endpoint }{{"source", c.Source}, {"target", c.Target}} { if p.e.User != "" && (!strings.HasPrefix(p.e.User, "client.") || !validName(p.e.User)) { return fmt.Errorf("%s: user must be a client identity", p.name) } for _, s := range []string{p.e.Filesystem, p.e.Subvolume} { if !validName(s) { return fmt.Errorf("%s: invalid filesystem or subvolume name", p.name) } } if p.e.Group != "" && !validName(p.e.Group) { return fmt.Errorf("%s: invalid group", p.name) } paths := []string{p.e.Config, p.e.Keyring} if c.Backend != "native" || p.e.Mount != "" { paths = append(paths, p.e.Mount) } for _, path := range paths { if !filepath.IsAbs(path) || filepath.Clean(path) != path { return fmt.Errorf("%s: paths must be absolute and clean: %q", p.name, path) } } } if c.BandwidthKiB < 0 || c.BandwidthKiB > (1<<63-1)/1024 { return errors.New("bandwidth_kib must be nonnegative and fit in bytes per second") } if c.Backend != "native" && overlaps(c.Source.Mount, c.Target.Mount) { return errors.New("source and target mounts must not overlap") } return nil } func beneath(root, path string) bool { rel, err := filepath.Rel(root, path) return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) } func overlaps(a, b string) bool { return beneath(a, b) || beneath(b, a) } // dataPath resolves cephPath to a real directory below the filesystem mount // root. No symlink component is permitted, including in the mount path. func dataPath(root, cephPath string) (string, error) { if !filepath.IsAbs(cephPath) || filepath.Clean(cephPath) != cephPath || cephPath == "/" { return "", fmt.Errorf("invalid Ceph path %q", cephPath) } p := filepath.Join(root, strings.TrimPrefix(cephPath, "/")) resolved, err := filepath.EvalSymlinks(p) if err != nil { return "", err } if resolved != p || !beneath(root, p) { return "", fmt.Errorf("path escapes mount or contains symlinks: %q", p) } st, err := os.Stat(p) if err != nil { return "", err } if !st.IsDir() { return "", fmt.Errorf("not a directory: %s", p) } return p, nil }