From 1b5023cb69408fdaa86003088e24a487b6f4b9c5 Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" Date: Thu, 21 May 2020 16:08:39 -0500 Subject: [PATCH] connect: ensure proxy-defaults protocol is used for upstreams (#7938) --- agent/consul/config_endpoint.go | 7 +---- agent/consul/config_endpoint_test.go | 47 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/agent/consul/config_endpoint.go b/agent/consul/config_endpoint.go index 0100207917..f3c3177ba3 100644 --- a/agent/consul/config_endpoint.go +++ b/agent/consul/config_endpoint.go @@ -360,14 +360,9 @@ func (c *ConfigEntry) ResolveServiceConfig(args *structs.ServiceConfigRequest, r } } - // No upstream found; skip. - if upstreamConf == nil { - continue - } - // Fallback to proxyConf global protocol. protocol := proxyConfGlobalProtocol - if upstreamConf.Protocol != "" { + if upstreamConf != nil && upstreamConf.Protocol != "" { protocol = upstreamConf.Protocol } diff --git a/agent/consul/config_endpoint_test.go b/agent/consul/config_endpoint_test.go index 94722d10a4..6f984f38d5 100644 --- a/agent/consul/config_endpoint_test.go +++ b/agent/consul/config_endpoint_test.go @@ -1019,6 +1019,9 @@ func TestConfigEntry_ResolveServiceConfig_UpstreamProxyDefaultsProtocol(t *testi "other": map[string]interface{}{ "protocol": "http", }, + "dne": map[string]interface{}{ + "protocol": "http", + }, "alreadyprotocol": map[string]interface{}{ "protocol": "grpc", }, @@ -1029,6 +1032,50 @@ func TestConfigEntry_ResolveServiceConfig_UpstreamProxyDefaultsProtocol(t *testi require.Equal(expected, out) } +func TestConfigEntry_ResolveServiceConfig_ProxyDefaultsProtocol_UsedForAllUpstreams(t *testing.T) { + t.Parallel() + + require := require.New(t) + + dir1, s1 := testServer(t) + defer os.RemoveAll(dir1) + defer s1.Shutdown() + codec := rpcClient(t, s1) + defer codec.Close() + + // Create a dummy proxy/service config in the state store to look up. + state := s1.fsm.State() + require.NoError(state.EnsureConfigEntry(1, &structs.ProxyConfigEntry{ + Kind: structs.ProxyDefaults, + Name: structs.ProxyConfigGlobal, + Config: map[string]interface{}{ + "protocol": "http", + }, + }, nil)) + + args := structs.ServiceConfigRequest{ + Name: "foo", + Datacenter: s1.config.Datacenter, + Upstreams: []string{"bar"}, + } + var out structs.ServiceConfigResponse + require.NoError(msgpackrpc.CallWithCodec(codec, "ConfigEntry.ResolveServiceConfig", &args, &out)) + + expected := structs.ServiceConfigResponse{ + ProxyConfig: map[string]interface{}{ + "protocol": "http", + }, + UpstreamConfigs: map[string]map[string]interface{}{ + "bar": map[string]interface{}{ + "protocol": "http", + }, + }, + // Don't know what this is deterministically + QueryMeta: out.QueryMeta, + } + require.Equal(expected, out) +} + func TestConfigEntry_ResolveServiceConfigNoConfig(t *testing.T) { t.Parallel()