-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
fix: cron string validation #19071
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,18 @@ type Trigger struct { | |
} `json:"trigger_setting,omitempty"` | ||
} | ||
|
||
// 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. duplicate validation in the length of cron string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is because we need to skip |
||
if err := utils.ValidateCronString(s.Trigger.Settings.Cron); err != nil { | ||
return errors.New(nil).WithCode(errors.BadRequestCode). | ||
WithMessage("invalid cron string for scheduled preheat: %s, error: %v", s.Trigger.Settings.Cron, err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Valid the policy | ||
func (s *Schema) Valid(v *validation.Validation) { | ||
if len(s.Name) == 0 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ package policy | |
import ( | ||
"github.com/beego/beego/v2/core/validation" | ||
|
||
"github.com/goharbor/harbor/src/common/utils" | ||
"github.com/goharbor/harbor/src/lib/errors" | ||
"github.com/goharbor/harbor/src/lib/selector/selectors/doublestar" | ||
"github.com/goharbor/harbor/src/pkg/retention/policy/rule" | ||
"github.com/goharbor/harbor/src/pkg/retention/policy/rule/index" | ||
|
@@ -58,6 +60,23 @@ type Metadata struct { | |
Scope *Scope `json:"scope" valid:"Required"` | ||
} | ||
|
||
// ValidateRetentionPolicy validate the retention policy | ||
func (m *Metadata) ValidateRetentionPolicy() error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. This is because we need to skip |
||
// currently only validate the cron string of retention policy | ||
if m.Trigger != nil { | ||
if m.Trigger.Kind == TriggerKindSchedule && m.Trigger.Settings != nil { | ||
cronItem, ok := m.Trigger.Settings[TriggerSettingsCron] | ||
if ok && len(cronItem.(string)) > 0 { | ||
if err := utils.ValidateCronString(cronItem.(string)); err != nil { | ||
return errors.New(nil).WithCode(errors.BadRequestCode). | ||
WithMessage("invalid cron string for scheduled tag retention: %s, error: %v", cronItem.(string), err) | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Valid Valid | ||
func (m *Metadata) Valid(v *validation.Validation) { | ||
if m.Trigger == nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 *
, thet.Second
is not0
.