From 9756cf4fb57b0b029430c86ed884b91c90ff928d Mon Sep 17 00:00:00 2001 From: geauxvirtual Date: Thu, 1 Sep 2016 16:01:44 -0700 Subject: [PATCH] Fix #1130: Validate task workflow and schedule when creating task request. --- core/task.go | 19 +++++++++++++++++-- mgmt/rest/client/task.go | 2 +- mgmt/rest/rest_func_test.go | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/core/task.go b/core/task.go index 1a018cb5a..721b5ffd4 100644 --- a/core/task.go +++ b/core/task.go @@ -161,7 +161,7 @@ type TaskCreationRequest struct { Version int `json:"version"` Deadline string `json:"deadline"` Workflow *wmap.WorkflowMap `json:"workflow"` - Schedule Schedule `json:"schedule"` + Schedule *Schedule `json:"schedule"` Start bool `json:"start"` MaxFailures int `json:"max-failures"` } @@ -224,7 +224,11 @@ func CreateTaskFromContent(body io.ReadCloser, return nil, err } - sch, err := makeSchedule(tr.Schedule) + if err := validateTaskRequest(tr); err != nil { + return nil, err + } + + sch, err := makeSchedule(*tr.Schedule) if err != nil { return nil, err } @@ -285,3 +289,14 @@ func UnmarshalBody(in interface{}, body io.ReadCloser) (int, error) { } return 0, nil } + +func validateTaskRequest(tr *TaskCreationRequest) error { + if tr.Schedule == nil || *tr.Schedule == (Schedule{}) { + return fmt.Errorf("Task must include a schedule, and the schedule must not be empty") + } + + if tr.Workflow == nil || *tr.Workflow == (wmap.WorkflowMap{}) { + return fmt.Errorf("Task must include a workflow, and the workflow must not be empty") + } + return nil +} diff --git a/mgmt/rest/client/task.go b/mgmt/rest/client/task.go index 19bb6cd12..e46e9cfce 100644 --- a/mgmt/rest/client/task.go +++ b/mgmt/rest/client/task.go @@ -50,7 +50,7 @@ type Schedule struct { // A ScheduledTask is returned if it succeeds, otherwise an error is returned. func (c *Client) CreateTask(s *Schedule, wf *wmap.WorkflowMap, name string, deadline string, startTask bool, maxFailures int) *CreateTaskResult { t := core.TaskCreationRequest{ - Schedule: core.Schedule{ + Schedule: &core.Schedule{ Type: s.Type, Interval: s.Interval, }, diff --git a/mgmt/rest/rest_func_test.go b/mgmt/rest/rest_func_test.go index 8f6cc1ee6..b519486f2 100644 --- a/mgmt/rest/rest_func_test.go +++ b/mgmt/rest/rest_func_test.go @@ -252,7 +252,7 @@ func createTask(sample, name, interval string, noStart bool, port int) *rbody.AP uri := fmt.Sprintf("http://localhost:%d/v1/tasks", port) t := core.TaskCreationRequest{ - Schedule: core.Schedule{Type: "simple", Interval: interval}, + Schedule: &core.Schedule{Type: "simple", Interval: interval}, Workflow: wf, Name: name, Start: !noStart,