Skip to content

Commit

Permalink
fix: validate cron expression while parsing it
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Zeng <[email protected]>
  • Loading branch information
knight42 committed Jan 6, 2024
1 parent f1807fb commit b76d4a7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions pkg/model/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ type StringMap map[string]string

// Repo represents a Repository.
type Repo struct {
Name string `gorm:"primaryKey" json:"name" validate:"required"`
Cron string `json:"cron" validate:"required,cron"`
Name string `gorm:"primaryKey" json:"name" validate:"required"`
// NOTE: the cron validator does not support */number syntax
Cron string `json:"cron" validate:"required"`
Image string `json:"image" validate:"required"`
StorageDir string `json:"storageDir" validate:"required,dir"`
User string `json:"user"`
Expand Down
11 changes: 7 additions & 4 deletions pkg/server/repo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,14 @@ func (s *Server) loadRepo(c echo.Context, logger *slog.Logger, dirs []string, fi
return nil, newHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid config: %q: %v", file, err))
}

schedule, err := cron.ParseStandard(repo.Cron)
if err != nil {
return nil, newHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid cron: %q: %v", repo.Cron, err))
}
s.repoSchedules.Set(repo.Name, schedule)

logDir := filepath.Join(s.config.RepoLogsDir, repo.Name)
err := os.MkdirAll(logDir, 0o755)
err = os.MkdirAll(logDir, 0o755)
if err != nil {
return nil, newHTTPError(http.StatusInternalServerError, fmt.Sprintf("Fail to create log dir: %q", logDir))
}
Expand All @@ -138,9 +144,6 @@ func (s *Server) loadRepo(c echo.Context, logger *slog.Logger, dirs []string, fi
return nil, newHTTPError(http.StatusInternalServerError, msg)
}

schedule, _ := cron.ParseStandard(repo.Cron)
s.repoSchedules.Set(repo.Name, schedule)

upstream := getUpstream(repo.Image, repo.Envs)
nextRun := schedule.Next(time.Now()).Unix()
err = db.
Expand Down

0 comments on commit b76d4a7

Please sign in to comment.