Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cron string validation #19071

Merged
merged 2 commits into from Aug 9, 2023
Merged

fix: cron string validation #19071

merged 2 commits into from Aug 9, 2023

Conversation

zyyw
Copy link
Contributor

@zyyw zyyw commented Aug 1, 2023

Thank you for contributing to Harbor!

Comprehensive Summary of your change

Issue being fixed

Fixes #18091
task 2

Please indicate you've done the following:

  • Well Written Title and Summary of the PR
  • Label the PR as needed. "release-note/ignore-for-release, release-note/new-feature, release-note/update, release-note/enhancement, release-note/community, release-note/breaking-change, release-note/docs, release-note/infra, release-note/deprecation"
  • Accepted the DCO. Commits without the DCO will delay acceptance.
  • Made sure tests are passing and test coverage is added if needed.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed in website repository.

@codecov
Copy link

codecov bot commented Aug 1, 2023

Codecov Report

Merging #19071 (5897782) into main (90de909) will increase coverage by 0.00%.
The diff coverage is 60.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #19071   +/-   ##
=======================================
  Coverage   67.40%   67.41%           
=======================================
  Files         993      993           
  Lines      108812   108864   +52     
  Branches     2751     2751           
=======================================
+ Hits        73347    73392   +45     
- Misses      31526    31527    +1     
- Partials     3939     3945    +6     
Flag Coverage Δ
unittests 67.41% <60.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
src/server/v2.0/handler/gc.go 2.40% <0.00%> (ø)
src/server/v2.0/handler/purge.go 9.92% <0.00%> (ø)
src/server/v2.0/handler/scan_all.go 86.19% <0.00%> (-1.68%) ⬇️
src/controller/p2p/preheat/controller.go 58.80% <25.00%> (-1.12%) ⬇️
src/controller/retention/controller.go 46.80% <25.00%> (-0.73%) ⬇️
src/common/utils/utils.go 77.93% <100.00%> (+1.43%) ⬆️
src/pkg/p2p/preheat/models/policy/policy.go 75.00% <100.00%> (+1.72%) ⬆️
src/pkg/retention/policy/models.go 28.57% <100.00%> (+11.90%) ⬆️

... and 4 files with indirect coverage changes

Copy link
Contributor

@AllForNothing AllForNothing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

if err != nil {
return false, err
}
cronParts := strings.Split(cron, " ")
Copy link
Contributor

@wy65701436 wy65701436 Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s, err := CronParser().Parse(cron)
if err != nil {
   return false, err
}

t, ok := s.(*cronlib.SpecSchedule)
if ok {
    if t.Second == 0 {
	    return false, err
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried setting cron as 0 0 0 1 1 *, the t.Second is not 0.

// ValidatePreheatPolicy validate preheat policy
func (s *Schema) ValidatePreheatPolicy() error {
// currently only validate cron string of preheat policy
if s.Trigger != nil && s.Trigger.Type == TriggerTypeScheduled && len(s.Trigger.Settings.Cron) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate validation in the length of cron string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because we need to skip utils.ValidateCronString() validation for cron with empty string "". In preheat and tag retention, it uses empty string cron to unschedule an existing job.

// currently only validate cron string of preheat policy
if s.Trigger != nil && s.Trigger.Type == TriggerTypeScheduled && len(s.Trigger.Settings.Cron) > 0 {
isValid, err := utils.ValidateCronString(s.Trigger.Settings.Cron)
if !isValid || err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err != nil {
 return err --- this is 500 error
}
if !isValid {
 return BadRequest -- this is 400 error
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually,when the first field of cron is not 0, like "1 0 0 1 1 *", utils.ValidateCronString will return false and with error message. This is a BadRequest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe for utils.ValidateCronString() we should only return error, instead of return (bool, error)

@@ -58,6 +60,24 @@ type Metadata struct {
Scope *Scope `json:"scope" valid:"Required"`
}

// ValidateRetentionPolicy validate the retention policy
func (m *Metadata) ValidateRetentionPolicy() error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. This is because we need to skip utils.ValidateCronString() validation for cron with empty string "". In preheat and tag retention, it uses empty string cron to unschedule an existing job.

… when there are 6 fields)

Signed-off-by: Shengwen Yu <yshengwen@vmware.com>
@zyyw zyyw merged commit 88c6018 into goharbor:main Aug 9, 2023
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validators for Harbor APIs
6 participants