Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 1913977: add validation for user provided Rsync options (#878)
  • Loading branch information
pranavgaikwad committed Jan 8, 2021
1 parent f669d75 commit 2b86f2e
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions pkg/controller/directvolumemigration/rsync.go
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/rsa"
"fmt"
random "math/rand"
"regexp"
"strings"
"text/template"
"time"
Expand Down Expand Up @@ -681,8 +682,20 @@ func (t *Task) getPVCNodeNameMap() (map[string]string, error) {
return nodeNameMap, nil
}

// generates Rsync commands based on custom options provided by the user in MigrationController CR
// TODO: validate user provided extra options
// validates extra Rsync options set by user
// only returns options identified as valid
func (t *Task) filterRsyncExtraOptions(options []string) (validatedOptions []string) {
for _, opt := range options {
if valid, _ := regexp.Match(`^\-{1,2}[\w-]+?\w$`, []byte(opt)); valid {
validatedOptions = append(validatedOptions, opt)
} else {
t.Log.Info(fmt.Sprintf("Invalid Rsync extra option passed: %s", opt))
}
}
return
}

// generates Rsync options based on custom options provided by the user in MigrationController CR
func (t *Task) getRsyncOptions() []string {
var rsyncOpts []string
defaultInfoOpts := "COPY2,DEL2,REMOVE2,SKIP2,FLIST2,PROGRESS2,STATS2"
Expand Down Expand Up @@ -710,15 +723,16 @@ func (t *Task) getRsyncOptions() []string {
if rsyncOptions.Partial {
rsyncOpts = append(rsyncOpts, "--partial")
}
if rsyncOptions.Info != "" {
if valid, _ := regexp.Match(`^\w[\w,]*?\w$`, []byte(rsyncOptions.Info)); valid {
rsyncOpts = append(rsyncOpts,
fmt.Sprintf("--info=%s", rsyncOptions.Info))
} else {
rsyncOpts = append(rsyncOpts,
fmt.Sprintf("--info=%s", defaultInfoOpts))
}
rsyncOpts = append(rsyncOpts, defaultExtraOpts...)
rsyncOpts = append(rsyncOpts, rsyncOptions.Extras...)
rsyncOpts = append(rsyncOpts,
t.filterRsyncExtraOptions(rsyncOptions.Extras)...)
return rsyncOpts
}

Expand Down

0 comments on commit 2b86f2e

Please sign in to comment.