From 07ad6621311a5a6269dd50e324a0230fe2093097 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 18 Aug 2020 18:48:05 -0400 Subject: [PATCH] Fix panic when decoding 'Connect: null' Surprisingly the json Unmarshal updates the aux pointer to a nil. --- agent/structs/structs.go | 4 +++- agent/structs/structs_test.go | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/agent/structs/structs.go b/agent/structs/structs.go index 48024b6ab3..d6fb989589 100644 --- a/agent/structs/structs.go +++ b/agent/structs/structs.go @@ -1056,10 +1056,12 @@ func (t *ServiceConnect) UnmarshalJSON(data []byte) (err error) { }{ Alias: (*Alias)(t), } + if err = json.Unmarshal(data, &aux); err != nil { return err } - if t.SidecarService == nil { + + if t.SidecarService == nil && aux != nil { t.SidecarService = aux.SidecarServiceSnake } return nil diff --git a/agent/structs/structs_test.go b/agent/structs/structs_test.go index ae42a905a6..2d3bc9b8f7 100644 --- a/agent/structs/structs_test.go +++ b/agent/structs/structs_test.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/consul/agent/cache" "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/types" "github.com/stretchr/testify/assert" @@ -173,6 +174,46 @@ func testServiceNode(t *testing.T) *ServiceNode { } } +func TestRegisterRequest_UnmarshalJSON_WithConnectNilDoesNotPanic(t *testing.T) { + in := ` +{ + "ID": "", + "Node": "k8s-sync", + "Address": "127.0.0.1", + "TaggedAddresses": null, + "NodeMeta": { + "external-source": "kubernetes" + }, + "Datacenter": "", + "Service": { + "Kind": "", + "ID": "test-service-f8fd5f0f4e6c", + "Service": "test-service", + "Tags": [ + "k8s" + ], + "Meta": { + "external-k8s-ns": "", + "external-source": "kubernetes", + "port-stats": "18080" + }, + "Port": 8080, + "Address": "192.0.2.10", + "EnableTagOverride": false, + "CreateIndex": 0, + "ModifyIndex": 0, + "Connect": null + }, + "Check": null, + "SkipNodeUpdate": true +} +` + + var req RegisterRequest + err := lib.DecodeJSON(strings.NewReader(in), &req) + require.NoError(t, err) +} + func TestNode_IsSame(t *testing.T) { id := types.NodeID("e62f3b31-9284-4e26-ab14-2a59dea85b55") node := "mynode1"