Skip to content

Commit

Permalink
Validate resourcekey to avoid apiserver being panic for invalid inputs (
Browse files Browse the repository at this point in the history
  • Loading branch information
lilida authored and Bobgy committed Jul 2, 2020
1 parent 1930103 commit 6ced6ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions backend/src/apiserver/server/pipeline_server.go
Expand Up @@ -166,6 +166,11 @@ func (s *PipelineServer) ListPipelineVersions(ctx context.Context, request *api.
return nil, util.Wrap(err, "Failed to create list options")
}

//Ensure resourceKey has been set
if request.ResourceKey == nil {
return nil, util.NewInvalidInputError("ResourceKey must be set in the input")
}

pipelineVersions, total_size, nextPageToken, err :=
s.resourceManager.ListPipelineVersions(request.ResourceKey.Id, opts)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions backend/src/apiserver/server/pipeline_server_test.go
Expand Up @@ -244,6 +244,24 @@ func TestCreatePipelineVersion_InvalidURL(t *testing.T) {
assert.Equal(t, codes.Internal, err.(*util.UserError).ExternalStatusCode())
}

func TestListPipelineVersion_NoResourceKey(t *testing.T){
httpServer := getMockServer(t)
// Close the server when test finishes
defer httpServer.Close()

clientManager := resource.NewFakeClientManagerOrFatal(util.NewFakeTimeForEpoch())
resourceManager := resource.NewResourceManager(clientManager)

pipelineServer := PipelineServer{resourceManager: resourceManager, httpClient: httpServer.Client()}


_, err := pipelineServer.ListPipelineVersions(context.Background(), &api.ListPipelineVersionsRequest{
ResourceKey: nil,
PageSize: 20,
})
assert.Equal(t, "Invalid input error: ResourceKey must be set in the input", err.Error())
}

func getMockServer(t *testing.T) *httptest.Server {
httpServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// Send response to be tested
Expand Down

0 comments on commit 6ced6ea

Please sign in to comment.