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(admin): validate cron expression in launch plan schedule #5951

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions flyteadmin/pkg/manager/impl/validation/launch_plan_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package validation
import (
"context"

"github.com/robfig/cron/v3"
"google.golang.org/grpc/codes"

"github.com/flyteorg/flyte/flyteadmin/pkg/common"
Expand Down Expand Up @@ -91,6 +92,19 @@ func validateSchedule(request *admin.LaunchPlanCreateRequest, expectedInputs *co
"KickoffTimeInputArg must reference a datetime input. [%v] is a [%v]", schedule.GetKickoffTimeInputArg(), param.GetVar().GetType())
}
}

// validate cron expression
var cronExpression string
if schedule.GetCronExpression() != "" {
cronExpression = schedule.GetCronExpression()
} else if schedule.GetCronSchedule() != nil {
cronExpression = schedule.GetCronSchedule().GetSchedule()
}
if cronExpression != "" {
if _, err := cron.ParseStandard(cronExpression); err != nil {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "Invalid cron expression: %v", err)
}
}
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func TestValidateSchedule_ArgNotFixed(t *testing.T) {
},
}
t.Run("with deprecated cron expression", func(t *testing.T) {
request := testutils.GetLaunchPlanRequestWithDeprecatedCronSchedule("* * * * * *")
request := testutils.GetLaunchPlanRequestWithDeprecatedCronSchedule("* * * * *")

err := validateSchedule(request, inputMap)
assert.NotNil(t, err)
Expand All @@ -370,15 +370,15 @@ func TestValidateSchedule_ArgNotFixed(t *testing.T) {
assert.NotNil(t, err)
})
t.Run("with cron schedule", func(t *testing.T) {
request := testutils.GetLaunchPlanRequestWithCronSchedule("* * * * * *")
request := testutils.GetLaunchPlanRequestWithCronSchedule("* * * * *")

err := validateSchedule(request, inputMap)
assert.NotNil(t, err)
})
}

func TestValidateSchedule_KickoffTimeArgDoesNotExist(t *testing.T) {
request := testutils.GetLaunchPlanRequestWithDeprecatedCronSchedule("* * * * * *")
request := testutils.GetLaunchPlanRequestWithDeprecatedCronSchedule("* * * * *")
inputMap := &core.ParameterMap{
Parameters: map[string]*core.Parameter{},
}
Expand All @@ -389,7 +389,7 @@ func TestValidateSchedule_KickoffTimeArgDoesNotExist(t *testing.T) {
}

func TestValidateSchedule_KickoffTimeArgPointsAtWrongType(t *testing.T) {
request := testutils.GetLaunchPlanRequestWithDeprecatedCronSchedule("* * * * * *")
request := testutils.GetLaunchPlanRequestWithDeprecatedCronSchedule("* * * * *")
inputMap := &core.ParameterMap{
Parameters: map[string]*core.Parameter{
foo: {
Expand All @@ -409,7 +409,7 @@ func TestValidateSchedule_KickoffTimeArgPointsAtWrongType(t *testing.T) {
}

func TestValidateSchedule_NoRequired(t *testing.T) {
request := testutils.GetLaunchPlanRequestWithDeprecatedCronSchedule("* * * * * *")
request := testutils.GetLaunchPlanRequestWithDeprecatedCronSchedule("* * * * *")
inputMap := &core.ParameterMap{
Parameters: map[string]*core.Parameter{
foo: {
Expand All @@ -428,7 +428,7 @@ func TestValidateSchedule_NoRequired(t *testing.T) {
}

func TestValidateSchedule_KickoffTimeBound(t *testing.T) {
request := testutils.GetLaunchPlanRequestWithDeprecatedCronSchedule("* * * * * *")
request := testutils.GetLaunchPlanRequestWithDeprecatedCronSchedule("* * * * *")
inputMap := &core.ParameterMap{
Parameters: map[string]*core.Parameter{
foo: {
Expand All @@ -446,3 +446,34 @@ func TestValidateSchedule_KickoffTimeBound(t *testing.T) {
err := validateSchedule(request, inputMap)
assert.Nil(t, err)
}

func TestValidateSchedule_InvalidCronExpression(t *testing.T) {
inputMap := &core.ParameterMap{
Parameters: map[string]*core.Parameter{
foo: {
Var: &core.Variable{
Type: &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_DATETIME}},
},
Behavior: &core.Parameter_Required{
Required: true,
},
},
},
}

t.Run("with unsupported cron special characters on deprecated cron schedule: #", func(t *testing.T) {
request := testutils.GetLaunchPlanRequestWithDeprecatedCronSchedule("* * * * MON#1")
request.Spec.EntityMetadata.Schedule.KickoffTimeInputArg = foo

err := validateSchedule(request, inputMap)
assert.NotNil(t, err)
})

t.Run("with unsupported cron special characters: #", func(t *testing.T) {
request := testutils.GetLaunchPlanRequestWithCronSchedule("* * * * MON#1")
request.Spec.EntityMetadata.Schedule.KickoffTimeInputArg = foo

err := validateSchedule(request, inputMap)
assert.NotNil(t, err)
})
}
Loading