From 82a5e6d6fcc7753314f424f30e4ec70a32195676 Mon Sep 17 00:00:00 2001 From: John Cowen Date: Wed, 20 Jan 2021 16:59:02 +0000 Subject: [PATCH] api: Ensure the internal/ui/gateway-service-nodes endpoint responds with an array (#9593) In some circumstances this endpoint will have no results in it (dues to ACLs, Namespaces, filtering or missing configuration). This ensures that the response is at least an empty array (`[]`) rather than `null` --- agent/ui_endpoint.go | 9 ++++++++- agent/ui_endpoint_test.go | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/agent/ui_endpoint.go b/agent/ui_endpoint.go index cf1206327..6be13b384 100644 --- a/agent/ui_endpoint.go +++ b/agent/ui_endpoint.go @@ -261,7 +261,12 @@ RPC: } summaries, _ := summarizeServices(out.Dump, s.agent.config, args.Datacenter) - return prepSummaryOutput(summaries, false), nil + + prepped := prepSummaryOutput(summaries, false) + if prepped == nil { + prepped = make([]*ServiceSummary, 0) + } + return prepped, nil } // UIServiceTopology returns the list of upstreams and downstreams for a Connect enabled service. @@ -449,6 +454,8 @@ func summarizeServices(dump structs.ServiceDump, cfg *config.RuntimeConfig, dc s func prepSummaryOutput(summaries map[structs.ServiceName]*ServiceSummary, excludeSidecars bool) []*ServiceSummary { var resp []*ServiceSummary + // Ensure at least a zero length slice + resp = make([]*ServiceSummary, 0) // Collect and sort resp for display for _, sum := range summaries { diff --git a/agent/ui_endpoint_test.go b/agent/ui_endpoint_test.go index 280c01230..c0bb01dbb 100644 --- a/agent/ui_endpoint_test.go +++ b/agent/ui_endpoint_test.go @@ -629,7 +629,18 @@ func TestUIGatewayServiceNodes_Terminating(t *testing.T) { }, } require.NoError(t, a.RPC("Catalog.Register", &arg, ®Output)) + } + { + // Request without having registered the config-entry, shouldn't respond with null + req, _ := http.NewRequest("GET", "/v1/internal/ui/gateway-services-nodes/terminating-gateway", nil) + resp := httptest.NewRecorder() + obj, err := a.srv.UIGatewayServicesNodes(resp, req) + require.Nil(t, err) + require.NotNil(t, obj) + } + + { // Register terminating-gateway config entry, linking it to db and redis (does not exist) args := &structs.TerminatingGatewayConfigEntry{ Name: "terminating-gateway",