From 1417e17909121b4247665d30dc1a0b3f9c7ba9cd Mon Sep 17 00:00:00 2001 From: Todd Date: Tue, 16 Nov 2021 17:36:09 -0700 Subject: [PATCH] Fix nil pointer error when listing by non existing host catalog. (#1720) * Fix nil pointer error when listing hosts from nonexisting host catalog. --- .../servers/controller/handlers/hosts/host_service.go | 10 ++++++---- .../controller/handlers/hosts/host_service_test.go | 10 ++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/internal/servers/controller/handlers/hosts/host_service.go b/internal/servers/controller/handlers/hosts/host_service.go index 1e59df85b..0d588bf64 100644 --- a/internal/servers/controller/handlers/hosts/host_service.go +++ b/internal/servers/controller/handlers/hosts/host_service.go @@ -493,25 +493,27 @@ func (s Service) parentAndAuthResult(ctx context.Context, id string, a action.Ty var cat host.Catalog switch host.SubtypeFromId(id) { case static.Subtype: - cat, err = staticRepo.LookupCatalog(ctx, parentId) + stcat, err := staticRepo.LookupCatalog(ctx, parentId) if err != nil { res.Error = err return nil, res } - if cat == nil { + if stcat == nil { res.Error = handlers.NotFoundError() return nil, res } + cat = stcat case plugin.Subtype: - cat, _, err = pluginRepo.LookupCatalog(ctx, parentId) + plcat, _, err := pluginRepo.LookupCatalog(ctx, parentId) if err != nil { res.Error = err return nil, res } - if cat == nil { + if plcat == nil { res.Error = handlers.NotFoundError() return nil, res } + cat = plcat } opts = append(opts, auth.WithScopeId(cat.GetScopeId()), auth.WithPin(parentId)) return cat, auth.Verify(ctx, opts...) diff --git a/internal/servers/controller/handlers/hosts/host_service_test.go b/internal/servers/controller/handlers/hosts/host_service_test.go index c5d43d2c7..cfffab5c0 100644 --- a/internal/servers/controller/handlers/hosts/host_service_test.go +++ b/internal/servers/controller/handlers/hosts/host_service_test.go @@ -280,6 +280,11 @@ func TestList_Static(t *testing.T) { req: &pbs.ListHostsRequest{HostCatalogId: hc.GetPublicId()}, res: &pbs.ListHostsResponse{Items: wantHs}, }, + { + name: "List Non Existing Hosts", + req: &pbs.ListHostsRequest{HostCatalogId: "hcst_doesntexist"}, + err: handlers.NotFoundError(), + }, { name: "List No Hosts", req: &pbs.ListHostsRequest{HostCatalogId: hcNoHosts.GetPublicId()}, @@ -396,6 +401,11 @@ func TestList_Plugin(t *testing.T) { req: &pbs.ListHostsRequest{HostCatalogId: hc.GetPublicId()}, res: &pbs.ListHostsResponse{Items: wantHs}, }, + { + name: "List Non Existing Hosts", + req: &pbs.ListHostsRequest{HostCatalogId: "hc_doesntexist"}, + err: handlers.NotFoundError(), + }, { name: "List No Hosts", req: &pbs.ListHostsRequest{HostCatalogId: hcNoHosts.GetPublicId()},