diff --git a/agent/config/runtime.go b/agent/config/runtime.go index 1114e738fc..0ee46aa61f 100644 --- a/agent/config/runtime.go +++ b/agent/config/runtime.go @@ -1203,12 +1203,12 @@ func (c *RuntimeConfig) apiAddresses(maxPerType int) (unixAddrs, httpAddrs, http http_count := 0 for _, addr := range c.HTTPAddrs { net := addr.Network() - if net == "unix" && unix_count < maxPerType { - unixAddrs = append(unixAddrs, addr.String()) - unix_count += 1 - } else if net != "unix" && http_count < maxPerType { + if net == "tcp" && http_count < maxPerType { httpAddrs = append(httpAddrs, addr.String()) http_count += 1 + } else if net != "tcp" && unix_count < maxPerType { + unixAddrs = append(unixAddrs, addr.String()) + unix_count += 1 } } } diff --git a/agent/config/runtime_test.go b/agent/config/runtime_test.go index 9592b49674..1c117511dd 100644 --- a/agent/config/runtime_test.go +++ b/agent/config/runtime_test.go @@ -4507,6 +4507,103 @@ func TestSanitize(t *testing.T) { require.JSONEq(t, rtJSON, string(b)) } +func TestRuntime_apiAddresses(t *testing.T) { + rt := RuntimeConfig{ + HTTPAddrs: []net.Addr{ + &net.TCPAddr{IP: net.ParseIP("198.18.0.1"), Port: 5678}, + &net.UnixAddr{Name: "/var/run/foo"}, + }, + HTTPSAddrs: []net.Addr{ + &net.TCPAddr{IP: net.ParseIP("198.18.0.2"), Port: 5678}, + }} + + unixAddrs, httpAddrs, httpsAddrs := rt.apiAddresses(1) + + require.Len(t, unixAddrs, 1) + require.Len(t, httpAddrs, 1) + require.Len(t, httpsAddrs, 1) + + require.Equal(t, "/var/run/foo", unixAddrs[0]) + require.Equal(t, "198.18.0.1:5678", httpAddrs[0]) + require.Equal(t, "198.18.0.2:5678", httpsAddrs[0]) +} + +func TestRuntime_APIConfigHTTPS(t *testing.T) { + rt := RuntimeConfig{ + HTTPAddrs: []net.Addr{ + &net.TCPAddr{IP: net.ParseIP("198.18.0.1"), Port: 5678}, + &net.UnixAddr{Name: "/var/run/foo"}, + }, + HTTPSAddrs: []net.Addr{ + &net.TCPAddr{IP: net.ParseIP("198.18.0.2"), Port: 5678}, + }, + Datacenter: "dc-test", + CAFile: "/etc/consul/ca.crt", + CAPath: "/etc/consul/ca.dir", + CertFile: "/etc/consul/server.crt", + KeyFile: "/etc/consul/ssl/server.key", + } + + cfg, err := rt.APIConfig(false) + require.NoError(t, err) + require.Equal(t, "198.18.0.2:5678", cfg.Address) + require.Equal(t, "https", cfg.Scheme) + require.Equal(t, rt.CAFile, cfg.TLSConfig.CAFile) + require.Equal(t, rt.CAPath, cfg.TLSConfig.CAPath) + require.Equal(t, "", cfg.TLSConfig.CertFile) + require.Equal(t, "", cfg.TLSConfig.KeyFile) + require.Equal(t, rt.Datacenter, cfg.Datacenter) + + cfg, err = rt.APIConfig(true) + require.NoError(t, err) + require.Equal(t, "198.18.0.2:5678", cfg.Address) + require.Equal(t, "https", cfg.Scheme) + require.Equal(t, rt.CAFile, cfg.TLSConfig.CAFile) + require.Equal(t, rt.CAPath, cfg.TLSConfig.CAPath) + require.Equal(t, rt.CertFile, cfg.TLSConfig.CertFile) + require.Equal(t, rt.KeyFile, cfg.TLSConfig.KeyFile) + require.Equal(t, rt.Datacenter, cfg.Datacenter) +} + +func TestRuntime_APIConfigHTTP(t *testing.T) { + rt := RuntimeConfig{ + HTTPAddrs: []net.Addr{ + &net.UnixAddr{Name: "/var/run/foo"}, + &net.TCPAddr{IP: net.ParseIP("198.18.0.1"), Port: 5678}, + }, + Datacenter: "dc-test", + } + + cfg, err := rt.APIConfig(false) + require.NoError(t, err) + require.Equal(t, rt.Datacenter, cfg.Datacenter) + require.Equal(t, "198.18.0.1:5678", cfg.Address) + require.Equal(t, "http", cfg.Scheme) + require.Equal(t, "", cfg.TLSConfig.CAFile) + require.Equal(t, "", cfg.TLSConfig.CAPath) + require.Equal(t, "", cfg.TLSConfig.CertFile) + require.Equal(t, "", cfg.TLSConfig.KeyFile) +} + +func TestRuntime_APIConfigUNIX(t *testing.T) { + rt := RuntimeConfig{ + HTTPAddrs: []net.Addr{ + &net.UnixAddr{Name: "/var/run/foo"}, + }, + Datacenter: "dc-test", + } + + cfg, err := rt.APIConfig(false) + require.NoError(t, err) + require.Equal(t, rt.Datacenter, cfg.Datacenter) + require.Equal(t, "unix:///var/run/foo", cfg.Address) + require.Equal(t, "http", cfg.Scheme) + require.Equal(t, "", cfg.TLSConfig.CAFile) + require.Equal(t, "", cfg.TLSConfig.CAPath) + require.Equal(t, "", cfg.TLSConfig.CertFile) + require.Equal(t, "", cfg.TLSConfig.KeyFile) +} + func splitIPPort(hostport string) (net.IP, int) { h, p, err := net.SplitHostPort(hostport) if err != nil {