diff --git a/cmd/skpr/rsync/command.go b/cmd/skpr/rsync/command.go index 6d9bfc6..bcd261c 100644 --- a/cmd/skpr/rsync/command.go +++ b/cmd/skpr/rsync/command.go @@ -8,11 +8,22 @@ import ( ) var ( - cmdLong = "Sync files from remote and local directories" + cmdLong = `Sync files from remote and local directories. + +Private files (/mnt/private) may contain personal information and are not downloaded by default. +Syncing from a parent directory, such as /mnt/, excludes them automatically. Syncing from the +private filesystem itself fails unless --include-private is set. Only download private files to +investigate a specific issue, with approval.` cmdExample = ` # Sync from local to a remote environment - skpr rsync ./foo/ dev:/mnt/temporary/foo/` + skpr rsync ./foo/ dev:/mnt/temporary/foo/ + + # Download public files from a remote environment + skpr rsync dev:/mnt/public/ ./files/ + + # Download private files from a remote environment (asks for confirmation) + skpr rsync --include-private dev:/mnt/private/ ./private/` ) // NewCommand creates a new cobra.Command for 'rsync' sub command @@ -37,6 +48,8 @@ func NewCommand() *cobra.Command { cmd.Flags().StringSliceVar(&command.Excludes, "exclude", []string{}, "Exclude files matching PATTERN.") cmd.Flags().StringVar(&command.ExcludeFrom, "exclude-from", "", "Exclude files matching patterns in FILE.") cmd.Flags().BoolVar(&command.DryRun, "dry-run", false, "Skpr will not request a confirmation for configuration changes.") + cmd.Flags().BoolVar(&command.IncludePrivate, "include-private", false, "Download private files ("+v1rsync.PrivateFilesPath+"). Asks for confirmation.") + cmd.Flags().BoolVarP(&command.Force, "force", "f", false, "Skip the confirmation prompt when downloading private files.") return cmd } diff --git a/internal/command/rsync/command.go b/internal/command/rsync/command.go index 23ebe2c..8ca85dd 100644 --- a/internal/command/rsync/command.go +++ b/internal/command/rsync/command.go @@ -7,38 +7,111 @@ import ( "os/exec" "github.com/pkg/errors" + + "github.com/skpr/cli/internal/confirmation" ) +// ErrPrivateFilesNotIncluded is returned when a download includes private files without --include-private. +var ErrPrivateFilesNotIncluded = errors.New("this sync would download private files, which may contain personal information. " + + "Only download private files to investigate a specific issue, with approval. " + + "Use --include-private to download them") + // Command to rsync files. type Command struct { - Source string - Destination string - Excludes []string - ExcludeFrom string - DryRun bool + Source string + Destination string + Excludes []string + ExcludeFrom string + DryRun bool + IncludePrivate bool + Force bool + + // confirm defaults to confirmation.Confirm. + confirm func(force bool, message string) bool } // Run the command. func (c *Command) Run(_ context.Context) error { + plan, err := c.planPrivateFiles() + if err != nil { + return err + } + + if plan.confirm { + confirm := c.confirm + if confirm == nil { + confirm = confirmation.Confirm + } + + msg := fmt.Sprintf("You are about to download private files from %s. Private files may contain personal information. "+ + "Only download them to investigate a specific issue, with approval. Continue? [yes/no]", plan.environment) + if ok := confirm(c.Force, msg); !ok { + fmt.Fprintln(os.Stderr, "Download cancelled.") + return nil + } + } + executable, err := os.Executable() if err != nil { return errors.Wrap(err, "failed to lookup binary path") } - args := c.generateArgs(executable) + if plan.exclude != "" { + fmt.Fprintf(os.Stderr, "Excluding private files (%s). Use --include-private to download them.\n", PrivateFilesPath) + } + + args := c.generateArgs(executable, plan.exclude) cmd := exec.Command("rsync", args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr return cmd.Run() } +// privateFilesPlan describes how a sync handles private files. +type privateFilesPlan struct { + environment string + confirm bool + // exclude is an rsync pattern that skips private files. + exclude string +} + +// planPrivateFiles decides how the sync handles private files. Dry runs skip confirmation. +func (c *Command) planPrivateFiles() (privateFilesPlan, error) { + env, remotePath, ok := splitRemote(c.Source) + if !ok { + return privateFilesPlan{}, nil + } + + plan := privateFilesPlan{environment: env} + + switch classifyRemotePath(remotePath) { + case privateScopeRequiresFlag: + if !c.IncludePrivate { + return plan, ErrPrivateFilesNotIncluded + } + + plan.confirm = !c.DryRun + case privateScopeParent: + if c.IncludePrivate { + plan.confirm = !c.DryRun + } else { + plan.exclude = privateExcludePattern(remotePath) + } + } + + return plan, nil +} + // generateArgs generates the args for the rsync command. -func (c *Command) generateArgs(executable string) []string { +func (c *Command) generateArgs(executable, privateExclude string) []string { args := []string{ "-avz", "--progress", "-e", fmt.Sprintf("%s-rsh", executable), } + if privateExclude != "" { + args = append(args, "--exclude", privateExclude) + } for _, ex := range c.Excludes { args = append(args, "--exclude", ex) } diff --git a/internal/command/rsync/command_test.go b/internal/command/rsync/command_test.go index 65c503c..3c2d405 100644 --- a/internal/command/rsync/command_test.go +++ b/internal/command/rsync/command_test.go @@ -1,83 +1,135 @@ package rsync import ( + "context" "testing" "github.com/stretchr/testify/assert" ) func TestGenerateArgs(t *testing.T) { - c := Command{ - Source: "source", - Destination: "destination", + tests := []struct { + name string + command Command + privateExclude string + want []string + }{ + { + name: "defaults", + command: Command{Source: "source", Destination: "destination"}, + want: []string{"-avz", "--progress", "-e", "skpr-rsh", "source", "destination"}, + }, + { + name: "all options", + command: Command{ + Source: "dev:/mnt/", + Destination: "destination", + Excludes: []string{"js/*", "css/*"}, + ExcludeFrom: "foo.txt", + DryRun: true, + }, + privateExclude: "/private/", + // The private exclude comes first so user patterns can't override it. + want: []string{ + "-avz", "--progress", "-e", "skpr-rsh", + "--exclude", "/private/", + "--exclude", "js/*", + "--exclude", "css/*", + "--exclude-from", "foo.txt", + "--dry-run", + "dev:/mnt/", "destination", + }, + }, } - args := c.generateArgs("skpr") - - assert.Equal(t, []string{ - "-avz", - "--progress", - "-e", "skpr-rsh", - "source", - "destination", - }, args) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, tt.command.generateArgs("skpr", tt.privateExclude)) + }) + } } -func TestGenerateArgsExcludes(t *testing.T) { - c := Command{ - Source: "source", - Destination: "destination", - Excludes: []string{ - "js/*", - "css/*", - "styles/*", - }, +func TestPlanPrivateFiles(t *testing.T) { + plan := func(c Command) (privateFilesPlan, error) { + c.Destination = "destination" + return c.planPrivateFiles() } - args := c.generateArgs("skpr") + t.Run("not private", func(t *testing.T) { + for source, env := range map[string]string{ + "./private/": "", + "./a:b/": "", + "dev:/mnt/public/": "dev", + "dev:files/": "dev", + "dev:/mnt/private-old/": "dev", + "dev:/mnt/pub*/": "dev", + "dev:/var/*": "dev", + } { + got, err := plan(Command{Source: source}) + assert.NoError(t, err, source) + assert.Equal(t, privateFilesPlan{environment: env}, got, source) + } + }) - assert.Equal(t, []string{ - "-avz", - "--progress", - "-e", "skpr-rsh", - "--exclude", "js/*", - "--exclude", "css/*", - "--exclude", "styles/*", - "source", - "destination", - }, args) -} + t.Run("requires flag", func(t *testing.T) { + for _, source := range []string{ + "dev:/mnt/private/", + "dev:/mnt/private/webform", + "dev:/mnt/public/../private", + "dev:/mnt/.", + "dev:/mnt/*", + "dev:/mnt/priv*/", + "dev:/mnt/private/*.pdf", + "dev:/m*", + "dev:/var/*/../mnt/private", + } { + _, err := plan(Command{Source: source}) + assert.ErrorIs(t, err, ErrPrivateFilesNotIncluded, source) -func TestGenerateArgsExcludeFrom(t *testing.T) { - c := Command{ - Source: "source", - Destination: "destination", - ExcludeFrom: "foo.txt", - } - args := c.generateArgs("skpr") - assert.Equal(t, []string{ - "-avz", - "--progress", - "-e", "skpr-rsh", - "--exclude-from", "foo.txt", - "source", - "destination", - }, args) + _, err = plan(Command{Source: source, DryRun: true}) + assert.ErrorIs(t, err, ErrPrivateFilesNotIncluded, source) + + got, err := plan(Command{Source: source, IncludePrivate: true}) + assert.NoError(t, err, source) + assert.Equal(t, privateFilesPlan{environment: "dev", confirm: true}, got, source) + + got, err = plan(Command{Source: source, IncludePrivate: true, DryRun: true}) + assert.NoError(t, err, source) + assert.Equal(t, privateFilesPlan{environment: "dev"}, got, source) + } + }) + + t.Run("parent", func(t *testing.T) { + for source, exclude := range map[string]string{ + "dev:/mnt/": "/private/", + "dev:/mnt": "/mnt/private/", + "dev:/": "/mnt/private/", + } { + got, err := plan(Command{Source: source}) + assert.NoError(t, err, source) + assert.Equal(t, privateFilesPlan{environment: "dev", exclude: exclude}, got, source) + + got, err = plan(Command{Source: source, IncludePrivate: true}) + assert.NoError(t, err, source) + assert.Equal(t, privateFilesPlan{environment: "dev", confirm: true}, got, source) + } + }) } -func TestGenerateDryRun(t *testing.T) { +func TestRunPrivateDeclined(t *testing.T) { + var prompted bool c := Command{ - Source: "source", - Destination: "destination", - DryRun: true, + Source: "dev:/mnt/private/", + Destination: "destination", + IncludePrivate: true, + confirm: func(force bool, message string) bool { + prompted = true + assert.Contains(t, message, "dev") + return false + }, } - args := c.generateArgs("skpr") - assert.Equal(t, []string{ - "-avz", - "--progress", - "-e", "skpr-rsh", - "--dry-run", - "source", - "destination", - }, args) + + // Declining returns before rsync runs. + assert.NoError(t, c.Run(context.Background())) + assert.True(t, prompted) } diff --git a/internal/command/rsync/private.go b/internal/command/rsync/private.go new file mode 100644 index 0000000..18217b2 --- /dev/null +++ b/internal/command/rsync/private.go @@ -0,0 +1,120 @@ +package rsync + +import ( + "path" + "strings" +) + +// PrivateFilesPath is where the private filesystem is mounted. +const PrivateFilesPath = "/mnt/private" + +// privateScope describes how a remote rsync source relates to the private filesystem. +type privateScope int + +const ( + privateScopeNone privateScope = iota + // privateScopeRequiresFlag means the source is, or may expand to, private files. + privateScopeRequiresFlag + // privateScopeParent means the source contains the private filesystem, which can be excluded. + privateScopeParent +) + +// privateFilesElements are the path elements of PrivateFilesPath. +var privateFilesElements = strings.Split(strings.TrimPrefix(PrivateFilesPath, "/"), "/") + +// splitRemote splits "env:path" into its parts. The boolean is false for local paths. +func splitRemote(location string) (string, string, bool) { + env, remotePath, found := strings.Cut(location, ":") + if !found || env == "" || strings.Contains(env, "/") { + return "", "", false + } + + return env, remotePath, true +} + +// classifyRemotePath reports how a remote path relates to the private filesystem. +// Relative paths can't be resolved locally, so they aren't checked. +func classifyRemotePath(remotePath string) privateScope { + if !path.IsAbs(remotePath) { + return privateScopeNone + } + + // Globs expand remotely, so an exclude can't be anchored for them. + if strings.ContainsAny(remotePath, "*?[") { + if globMayReachPrivate(remotePath) { + return privateScopeRequiresFlag + } + + return privateScopeNone + } + + cleaned := path.Clean(remotePath) + + switch { + case isWithin(cleaned, PrivateFilesPath): + return privateScopeRequiresFlag + case isWithin(PrivateFilesPath, cleaned): + // A trailing "." or ".." makes the transfer root, and so the exclude, unpredictable. + if trimmed := strings.TrimRight(remotePath, "/"); trimmed != "" { + if base := path.Base(trimmed); base == "." || base == ".." { + return privateScopeRequiresFlag + } + } + + return privateScopeParent + default: + return privateScopeNone + } +} + +// globMayReachPrivate reports whether a glob could expand to the private filesystem, a path +// within it, or a parent. Globs don't cross "/", so elements are matched depth by depth. +func globMayReachPrivate(glob string) bool { + var elements []string + for _, e := range strings.Split(glob, "/") { + switch e { + case "": + continue + case ".", "..": + return true + } + elements = append(elements, e) + } + + for i := 0; i < len(elements) && i < len(privateFilesElements); i++ { + matched, err := path.Match(elements[i], privateFilesElements[i]) + if err != nil { + return true + } + if !matched { + return false + } + } + + return true +} + +// isWithin reports whether child is parent or below it. +func isWithin(child, parent string) bool { + if child == parent || parent == "/" { + return true + } + + return strings.HasPrefix(child, parent+"/") +} + +// privateExcludePattern returns an exclude for the private filesystem, anchored to the +// transfer root: "/mnt/" syncs from /mnt, "/mnt" syncs from /. +func privateExcludePattern(remotePath string) string { + root := path.Clean(remotePath) + if !strings.HasSuffix(remotePath, "/") { + root = path.Dir(root) + } + + rel := strings.TrimPrefix(PrivateFilesPath, root) + if !strings.HasPrefix(rel, "/") { + rel = "/" + rel + } + + return rel + "/" +} diff --git a/internal/confirmation/confirmation.go b/internal/confirmation/confirmation.go index cb9f194..378d72b 100644 --- a/internal/confirmation/confirmation.go +++ b/internal/confirmation/confirmation.go @@ -2,6 +2,8 @@ package confirmation import ( "fmt" + "io" + "os" "github.com/skpr/cli/internal/slice" ) @@ -19,6 +21,11 @@ var ( // use with caution. This confirmation prompt is here to // protect you. func Confirm(force bool, message string) bool { + return confirm(os.Stdin, os.Stdout, force, message) +} + +// confirm prompts on out and reads from in. An empty or unreadable answer is "no". +func confirm(in io.Reader, out io.Writer, force bool, message string) bool { if force { return true } @@ -26,13 +33,9 @@ func Confirm(force bool, message string) bool { message = "Are you sure? [yes/no]" } var response string - fmt.Printf("%s: ", message) - _, err := fmt.Scanln(&response) - if err != nil { - panic(err) - } - if slice.Contains(okayResponses, response) { - return true + fmt.Fprintf(out, "%s: ", message) + if _, err := fmt.Fscanln(in, &response); err != nil { + return false } - return false + return slice.Contains(okayResponses, response) } diff --git a/internal/confirmation/confirmation_test.go b/internal/confirmation/confirmation_test.go new file mode 100644 index 0000000..e99fd98 --- /dev/null +++ b/internal/confirmation/confirmation_test.go @@ -0,0 +1,31 @@ +package confirmation + +import ( + "io" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestConfirm(t *testing.T) { + tests := []struct { + name string + input string + force bool + want bool + }{ + {name: "yes", input: "yes\n", want: true}, + {name: "short yes", input: "y\n", want: true}, + {name: "no", input: "no\n", want: false}, + {name: "empty line", input: "\n", want: false}, + {name: "no input", input: "", want: false}, + {name: "force", input: "", force: true, want: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, confirm(strings.NewReader(tt.input), io.Discard, tt.force, "")) + }) + } +}