Skip to content

Commit

Permalink
fix(api/schedule): make validateEntry more strict and set updated_by …
Browse files Browse the repository at this point in the history
…using claims (#901)

* fix(api/schedule): make validateEntry more strict and set updated_by using claims

* rm docker compose update

* add test cases to validateEntry
  • Loading branch information
ecrupper authored Jul 7, 2023
1 parent 900608b commit 71a484d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
36 changes: 23 additions & 13 deletions api/schedule/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,31 @@ func validateEntry(minimum time.Duration, entry string) error {
return fmt.Errorf("invalid entry of %s", entry)
}

// check the previous occurrence of the entry
prevTime, err := gronx.PrevTick(entry, true)
if err != nil {
return err
}
// iterate 5 times through ticks in an effort to catch scalene entries
tickForward := 5

// check the next occurrence of the entry
nextTime, err := gronx.NextTick(entry, true)
if err != nil {
return err
}
// start with now
t := time.Now().UTC()

for i := 0; i < tickForward; i++ {
// check the previous occurrence of the entry
prevTime, err := gronx.PrevTickBefore(entry, t, true)
if err != nil {
return err
}

// check the next occurrence of the entry
nextTime, err := gronx.NextTickAfter(entry, t, false)
if err != nil {
return err
}

// ensure the time between previous and next schedule exceeds the minimum duration
if nextTime.Sub(prevTime) < minimum {
return fmt.Errorf("entry needs to occur less frequently than every %s", minimum)
}

// ensure the time between previous and next schedule exceeds the minimum duration
if nextTime.Sub(prevTime) < minimum {
return fmt.Errorf("entry needs to occur less frequently than every %s", minimum)
t = nextTime
}

return nil
Expand Down
16 changes: 16 additions & 0 deletions api/schedule/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ func Test_validateEntry(t *testing.T) {
},
wantErr: true,
},
{
name: "exceeds minimum frequency with scalene entry pattern",
args: args{
minimum: 30 * time.Minute,
entry: "1,2,45 * * * *",
},
wantErr: true,
},
{
name: "meets minimum frequency",
args: args{
Expand All @@ -51,6 +59,14 @@ func Test_validateEntry(t *testing.T) {
},
wantErr: false,
},
{
name: "meets minimum frequency with comma entry pattern",
args: args{
minimum: 15 * time.Minute,
entry: "0,15,30,45 * * * *",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions api/schedule/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/schedule"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -73,6 +74,7 @@ func UpdateSchedule(c *gin.Context) {
// capture middleware values
r := repo.Retrieve(c)
s := schedule.Retrieve(c)
u := user.Retrieve(c)
scheduleName := util.PathParameter(c, "schedule")
minimumFrequency := c.Value("scheduleminimumfrequency").(time.Duration)

Expand Down Expand Up @@ -122,6 +124,9 @@ func UpdateSchedule(c *gin.Context) {
s.SetEntry(input.GetEntry())
}

// set the updated by field using claims
s.SetUpdatedBy(u.GetName())

// update the schedule within the database
err = database.FromContext(c).UpdateSchedule(s, true)
if err != nil {
Expand Down

0 comments on commit 71a484d

Please sign in to comment.