mirror of
https://github.com/garethgeorge/backrest.git
synced 2025-12-13 01:05:48 +00:00
feat: validate plan ID and repo ID
This commit is contained in:
@@ -2,7 +2,15 @@ package stringutil
|
|||||||
|
|
||||||
import "regexp"
|
import "regexp"
|
||||||
|
|
||||||
|
var (
|
||||||
|
sanitizeIDRegex = regexp.MustCompile(`[^a-zA-Z0-9_\-\.]+`) // matches invalid characters in an ID
|
||||||
|
idRegex = regexp.MustCompile(`[a-zA-Z0-9_\-\.]*`) // matches a valid ID (including empty string)
|
||||||
|
)
|
||||||
|
|
||||||
func SanitizeID(id string) string {
|
func SanitizeID(id string) string {
|
||||||
reg := regexp.MustCompile(`[^a-zA-Z0-9_\-\.]+`)
|
return sanitizeIDRegex.ReplaceAllString(id, "_")
|
||||||
return reg.ReplaceAllString(id, "_")
|
}
|
||||||
|
|
||||||
|
func ValidateID(id string) bool {
|
||||||
|
return idRegex.MatchString(id)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,8 +63,9 @@ func ValidateConfig(c *v1.Config) error {
|
|||||||
|
|
||||||
func validateRepo(repo *v1.Repo) error {
|
func validateRepo(repo *v1.Repo) error {
|
||||||
var err error
|
var err error
|
||||||
if repo.Id == "" {
|
|
||||||
err = multierror.Append(err, errors.New("id is required"))
|
if repo.Id == "" || !stringutil.ValidateID(repo.Id) {
|
||||||
|
err = multierror.Append(err, fmt.Errorf("id %q contains invalid characters (or empty)", repo.Id))
|
||||||
}
|
}
|
||||||
|
|
||||||
if repo.Uri == "" {
|
if repo.Uri == "" {
|
||||||
@@ -88,6 +89,10 @@ func validatePlan(plan *v1.Plan, repos map[string]*v1.Repo) error {
|
|||||||
err = multierror.Append(err, fmt.Errorf("path is required"))
|
err = multierror.Append(err, fmt.Errorf("path is required"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if plan.Id == "" || !stringutil.ValidateID(plan.Id) {
|
||||||
|
err = multierror.Append(err, fmt.Errorf("id %q contains invalid characters (or empty)", plan.Id))
|
||||||
|
}
|
||||||
|
|
||||||
for idx, p := range plan.Paths {
|
for idx, p := range plan.Paths {
|
||||||
if p == "" {
|
if p == "" {
|
||||||
err = multierror.Append(err, fmt.Errorf("path[%d] cannot be empty", idx))
|
err = multierror.Append(err, fmt.Errorf("path[%d] cannot be empty", idx))
|
||||||
|
|||||||
@@ -35,8 +35,10 @@ func NewRepoOrchestrator(config *v1.Config, repoConfig *v1.Repo, resticPath stri
|
|||||||
opts = append(opts, restic.WithEnviron())
|
opts = append(opts, restic.WithEnviron())
|
||||||
opts = append(opts, restic.WithEnv("RESTIC_PROGRESS_FPS=0.5"))
|
opts = append(opts, restic.WithEnv("RESTIC_PROGRESS_FPS=0.5"))
|
||||||
|
|
||||||
if len(repoConfig.GetEnv()) > 0 {
|
if env := repoConfig.GetEnv(); len(env) != 0 {
|
||||||
opts = append(opts, restic.WithEnv(repoConfig.GetEnv()...))
|
for _, e := range env {
|
||||||
|
opts = append(opts, restic.WithEnv(ExpandEnv(e)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if p := repoConfig.GetPassword(); p != "" {
|
if p := repoConfig.GetPassword(); p != "" {
|
||||||
@@ -58,12 +60,6 @@ func NewRepoOrchestrator(config *v1.Config, repoConfig *v1.Repo, resticPath stri
|
|||||||
opts = append(opts, restic.WithFlags("-o", "sftp.args=-oBatchMode=yes"))
|
opts = append(opts, restic.WithFlags("-o", "sftp.args=-oBatchMode=yes"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if env := repoConfig.GetEnv(); len(env) != 0 {
|
|
||||||
for _, e := range env {
|
|
||||||
opts = append(opts, restic.WithEnv(ExpandEnv(e)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
repo := restic.NewRepo(resticPath, repoConfig.GetUri(), opts...)
|
repo := restic.NewRepo(resticPath, repoConfig.GetUri(), opts...)
|
||||||
|
|
||||||
return &RepoOrchestrator{
|
return &RepoOrchestrator{
|
||||||
|
|||||||
Reference in New Issue
Block a user