8000 Add request validation on batch APIs by yux0 · Pull Request #3621 · temporalio/temporal · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add request validation on batch APIs #3621

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

Merged
merged 1 commit into from
Nov 18, 2022
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
3 changes: 3 additions & 0 deletions service/frontend/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ var (
errShuttingDown = serviceerror.NewUnavailable("Shutting down")
errUnableDeleteSystemNamespace = serviceerror.NewInvalidArgument("Unable to delete system namespace.")
errBatchJobIDNotSet = serviceerror.NewInvalidArgument("JobId is not set on request.")
errNamespaceNotSet = serviceerror.NewInvalidArgument("Namespace is not set on request.")
errReasonNotSet = serviceerror.NewInvalidArgument("Reason is not set on request.")
errBatchOperationNotSet = serviceerror.NewInvalidArgument("Batch operation is not set on request.")

errPageSizeTooBigMessage = "PageSize is larger than allowed %d."

Expand Down
33 changes: 33 additions & 0 deletions service/frontend/workflow_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3695,6 +3695,18 @@ func (wh *WorkflowHandler) StartBatchOperation(
if len(request.GetJobId()) == 0 {
return nil, errBatchJobIDNotSet
}
if len(request.Namespace) == 0 {
return nil, errNamespaceNotSet
}
if len(request.VisibilityQuery) == 0 {
return nil, errQueryNotSet
}
if len(request.Reason) == 0 {
return nil, errReasonNotSet
}
if request.Operation == nil {
return nil, errBatchOperationNotSet
}

if !wh.config.EnableBatcher(request.Namespace) {
return nil, errBatchAPINotAllowed
Expand Down Expand Up @@ -3804,6 +3816,16 @@ func (wh *WorkflowHandler) StopBatchOperation(
return nil, errRequestNotSet
}

if len(request.GetJobId()) == 0 {
return nil, errBatchJobIDNotSet
}
if len(request.Namespace) == 0 {
return nil, errNamespaceNotSet
}
if len(request.Reason) == 0 {
return nil, errReasonNotSet
}

if !wh.config.EnableBatcher(request.Namespace) {
return nil, errBatchAPINotAllowed
}
Expand Down Expand Up @@ -3841,6 +3863,13 @@ func (wh *WorkflowHandler) DescribeBatchOperation(
return nil, errRequestNotSet
}

if len(request.GetJobId()) == 0 {
return nil, errBatchJobIDNotSet
}
if len(request.Namespace) == 0 {
return nil, errNamespaceNotSet
}

if !wh.config.EnableBatcher(request.Namespace) {
return nil, errBatchAPINotAllowed
}
Expand Down Expand Up @@ -3952,6 +3981,10 @@ func (wh *WorkflowHandler) ListBatchOperations(
return nil, errRequestNotSet
}

if len(request.Namespace) == 0 {
return nil, errNamespaceNotSet
}

if !wh.config.EnableBatcher(request.Namespace) {
return nil, errBatchAPINotAllowed
}
Expand Down
110 changes: 109 additions & 1 deletion service/frontend/workflow_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,7 @@ func (s *workflowHandlerSuite) TestStartBatchOperation_Terminate() {
Namespace: testNamespace.String(),
Reason: inputString,
BatchType: batcher.BatchTypeTerminate,
Query: inputString,
}
inputPayload, err := payloads.Encode(params)
s.NoError(err)
Expand Down Expand Up @@ -1984,6 +1985,7 @@ func (s *workflowHandlerSuite) TestStartBatchOperation_Terminate() {
Identity: inputString,
},
},
VisibilityQuery: inputString,
}

_, err = wh.StartBatchOperation(context.Background(), request)
Expand All @@ -2001,6 +2003,7 @@ func (s *workflowHandlerSuite) TestStartBatchOperation_Cancellation() {
Namespace: testNamespace.String(),
Reason: inputString,
BatchType: batcher.BatchTypeCancel,
Query: inputString,
}
inputPayload, err := payloads.Encode(params)
s.NoError(err)
Expand Down Expand Up @@ -2033,6 +2036,7 @@ func (s *workflowHandlerSuite) TestStartBatchOperation_Cancellation() {
Identity: inputString,
},
},
VisibilityQuery: inputString,
}

_, err = wh.StartBatchOperation(context.Background(), request)
Expand All @@ -2049,6 +2053,8 @@ func (s *workflowHandlerSuite) TestStartBatchOperation_Signal() {
signalPayloads := payloads.EncodeString(signalName)
params := &batcher.BatchParams{
Namespace: testNamespace.String(),
Query: inputString,
Reason: inputString,
BatchType: batcher.BatchTypeSignal,
SignalParams: batcher.SignalParams{
SignalName: signalName,
Expand All @@ -2070,7 +2076,7 @@ func (s *workflowHandlerSuite) TestStartBatchOperation_Signal() {
s.Equal(enumspb.WORKFLOW_ID_REUSE_POLICY_REJECT_DUPLICATE, request.StartRequest.WorkflowIdReusePolicy)
s.Equal(inputString, request.StartRequest.Identity)
s.Equal(payload.EncodeString(batcher.BatchTypeSignal), request.StartRequest.Memo.Fields[batcher.BatchOperationTypeMemo])
s.Equal(payload.EncodeString(""), request.StartRequest.Memo.Fields[batcher.BatchReasonMemo])
s.Equal(payload.EncodeString(inputString), request.StartRequest.Memo.Fields[batcher.BatchReasonMemo])
s.Equal(payload.EncodeString(inputString), request.StartRequest.SearchAttributes.IndexedFields[searchattribute.BatcherUser])
s.Equal(inputPayload, request.StartRequest.Input)
return &historyservice.StartWorkflowExecutionResponse{}, nil
Expand All @@ -2087,12 +2093,60 @@ func (s *workflowHandlerSuite) TestStartBatchOperation_Signal() {
Identity: inputString,
},
},
Reason: inputString,
VisibilityQuery: inputString,
}

_, err = wh.StartBatchOperation(context.Background(), request)
s.NoError(err)
}

func (s *workflowHandlerSuite) TestStartBatchOperation_InvalidRequest() {
request := &workflowservice.StartBatchOperationRequest{
Namespace: "",
JobId: uuid.New(),
Operation: &workflowservice.StartBatchOperationRequest_SignalOperation{
SignalOperation: &batchpb.BatchOperationSignal{
Signal: "signalName",
Identity: "identity",
},
},
Reason: uuid.New(),
VisibilityQuery: uuid.New(),
}

config := s.newConfig()
wh := s.getWorkflowHandler(config)
var invalidArgumentErr *serviceerror.InvalidArgument
_, err := wh.StartBatchOperation(context.Background(), request)
s.ErrorAs(err, &invalidArgumentErr)

request.Namespace = uuid.New()
request.JobId = ""
_, err = wh.StartBatchOperation(context.Background(), request)
s.ErrorAs(err, &invalidArgumentErr)

request.JobId = uuid.New()
request.Operation = nil
_, err = wh.StartBatchOperation(context.Background(), request)
s.ErrorAs(err, &invalidArgumentErr)

request.Operation = &workflowservice.StartBatchOperationRequest_SignalOperation{
SignalOperation: &batchpb.BatchOperationSignal{
Signal: "signalName",
Identity: "identity",
},
}
request.Reason = ""
_, err = wh.StartBatchOperation(context.Background(), request)
s.ErrorAs(err, &invalidArgumentErr)

request.Reason = uuid.New()
request.VisibilityQuery = ""
_, err = wh.StartBatchOperation(context.Background(), request)
s.ErrorAs(err, &invalidArgumentErr)
}

func (s *workflowHandlerSuite) TestStopBatchOperation() {
testNamespace := namespace.Name("test-namespace")
namespaceID := namespace.ID(uuid.New())
Expand All @@ -2116,12 +2170,37 @@ func (s *workflowHandlerSuite) TestStopBatchOperation() {
request := &workflowservice.StopBatchOperationRequest{
Namespace: testNamespace.String(),
JobId: jobID,
Reason: "reason",
}

_, err := wh.StopBatchOperation(context.Background(), request)
s.NoError(err)
}

func (s *workflowHandlerSuite) TestStopBatchOperation_InvalidRequest() {
config := s.newConfig()
wh := s.getWorkflowHandler(config)
request := &workflowservice.StopBatchOperationRequest{
Namespace: "",
JobId: uuid.New(),
Reason: "reason",
}

var invalidArgumentErr *serviceerror.InvalidArgument
_, err := wh.StopBatchOperation(context.Background(), request)
s.ErrorAs(err, &invalidArgumentErr)

request.Namespace = uuid.New()
request.JobId = ""
_, err = wh.StopBatchOperation(context.Background(), request)
s.ErrorAs(err, &invalidArgumentErr)

request.JobId = uuid.New()
request.Reason = ""
_, err = wh.StopBatchOperation(context.Background(), request)
s.ErrorAs(err, &invalidArgumentErr)
}

func (s *workflowHandlerSuite) TestDescribeBatchOperation_CompletedStatus() {
testNamespace := namespace.Name("test-namespace")
namespaceID := namespace.ID(uuid.New())
Expand Down Expand Up @@ -2325,6 +2404,23 @@ func (s *workflowHandlerSuite) TestDescribeBatchOperation_FailedStatus() {
s.Equal(enumspb.BATCH_OPERATION_STATE_FAILED, resp.GetState())
}

func (s *workflowHandlerSuite) TestDescribeBatchOperation_InvalidRequest() {
config := s.newConfig()
wh := s.getWorkflowHandler(config)
request := &workflowservice.DescribeBatchOperationRequest{
Namespace: "",
JobId: uuid.New(),
}
var invalidArgumentErr *serviceerror.InvalidArgument
_, err := wh.DescribeBatchOperation(context.Background(), request)
s.ErrorAs(err, &invalidArgumentErr)

request.Namespace = uuid.New()
request.JobId = ""
_, err = wh.DescribeBatchOperation(context.Background(), request)
s.ErrorAs(err, &invalidArgumentErr)
}

func (s *workflowHandlerSuite) TestListBatchOperations() {
testNamespace := namespace.Name("test-namespace")
namespaceID := namespace.ID(uuid.New())
Expand Down Expand Up @@ -2371,6 +2467,18 @@ func (s *workflowHandlerSuite) TestListBatchOperations() {
s.Equal(enumspb.BATCH_OPERATION_STATE_FAILED, resp.OperationInfo[0].GetState())
}

func (s *workflowHandlerSuite) TestListBatchOperations_InvalidRerquest() {
config := s.newConfig()
wh := s.getWorkflowHandler(config)

request := &workflowservice.ListBatchOperationsRequest{
Namespace: "",
}
var invalidArgumentErr *serviceerror.InvalidArgument
_, err := wh.ListBatchOperations(context.Background(), request)
s.ErrorAs(err, &invalidArgumentErr)
}

func (s *workflowHandlerSuite) newConfig() *Config {
return NewConfig(dc.NewCollection(dc.NewNoopClient(), s.mockResource.GetLogger()), numHistoryShards, "", false)
}
Expand Down
0