diff --git a/internal/configconverter/config_server.go b/internal/configconverter/config_server.go index ed81250931..eb1bff05b8 100644 --- a/internal/configconverter/config_server.go +++ b/internal/configconverter/config_server.go @@ -37,12 +37,13 @@ import ( ) const ( - configServerEnabledEnvVar = "SPLUNK_DEBUG_CONFIG_SERVER" - configServerPortEnvVar = "SPLUNK_DEBUG_CONFIG_SERVER_PORT" - defaultConfigServerPort = "55554" - defaultConfigServerEndpoint = "localhost:" + defaultConfigServerPort - effectivePath = "/debug/configz/effective" - initialPath = "/debug/configz/initial" + configServerEnabledEnvVar = "SPLUNK_DEBUG_CONFIG_SERVER" + configServerPortEnvVar = "SPLUNK_DEBUG_CONFIG_SERVER_PORT" + configServerNetworkInterfaceEnvVar = "SPLUNK_DEBUG_CONFIG_SERVER_NETWORK_INTERFACE" + defaultConfigServerPort = "55554" + defaultConfigServerNetworkInterface = "localhost" + effectivePath = "/debug/configz/effective" + initialPath = "/debug/configz/initial" ) type ConfigType int @@ -138,16 +139,26 @@ func (cs *ConfigServer) start() { cs.once.Do( func() { - endpoint := defaultConfigServerEndpoint + port := defaultConfigServerPort if portOverride, ok := os.LookupEnv(configServerPortEnvVar); ok { if portOverride == "" { // If explicitly set to empty do not start the server. return } - endpoint = "localhost:" + portOverride + port = portOverride } + networkInterface := defaultConfigServerNetworkInterface + if networkInterfaceOverride, ok := os.LookupEnv(configServerNetworkInterfaceEnvVar); ok { + if networkInterfaceOverride != "" { + networkInterface = networkInterfaceOverride + } + } + + endpoint := net.JoinHostPort(networkInterface, port) + log.Printf("Starting config server at %q\n", endpoint) + listener, err := net.Listen("tcp", endpoint) if err != nil { if errors.Is(err, syscall.EADDRINUSE) { diff --git a/internal/configconverter/config_server_test.go b/internal/configconverter/config_server_test.go index 184e3be64c..20f563594d 100644 --- a/internal/configconverter/config_server_test.go +++ b/internal/configconverter/config_server_test.go @@ -51,23 +51,22 @@ func TestConfigServer_RequireEnvVar(t *testing.T) { client := &http.Client{} path := "/debug/configz/initial" - _, err := client.Get("http://" + defaultConfigServerEndpoint + path) + _, err := client.Get("http://localhost:55554" + path) assert.Error(t, err) } func TestConfigServer_EnvVar(t *testing.T) { alternativePort := strconv.FormatUint(uint64(testutils.GetAvailablePort(t)), 10) - require.NoError(t, os.Setenv(configServerEnabledEnvVar, "true")) - t.Cleanup(func() { - assert.NoError(t, os.Unsetenv(configServerEnabledEnvVar)) - }) + alternativeNetworkInterface := "0.0.0.0" + t.Setenv(configServerEnabledEnvVar, "true") tests := []struct { - name string - portEnvVar string - endpoint string - setPortEnvVar bool - serverDown bool + name string + portEnvVar string + networkInterfaceEnvVar string + endpoint string + setPortEnvVar bool + serverDown bool }{ { name: "default", @@ -82,6 +81,11 @@ func TestConfigServer_EnvVar(t *testing.T) { portEnvVar: alternativePort, endpoint: "http://localhost:" + alternativePort, }, + { + name: "change_networkInterface", + networkInterfaceEnvVar: alternativeNetworkInterface, + endpoint: "http://0.0.0.0:55554", + }, } for _, tt := range tests { @@ -97,10 +101,10 @@ func TestConfigServer_EnvVar(t *testing.T) { } if tt.portEnvVar != "" || tt.setPortEnvVar { - require.NoError(t, os.Setenv(configServerPortEnvVar, tt.portEnvVar)) - defer func() { - assert.NoError(t, os.Unsetenv(configServerPortEnvVar)) - }() + t.Setenv(configServerPortEnvVar, tt.portEnvVar) + } + if tt.portEnvVar != "" || tt.setPortEnvVar { + t.Setenv(configServerNetworkInterfaceEnvVar, tt.networkInterfaceEnvVar) } cs := NewConfigServer() @@ -115,7 +119,7 @@ func TestConfigServer_EnvVar(t *testing.T) { endpoint := tt.endpoint if endpoint == "" { - endpoint = "http://" + defaultConfigServerEndpoint + endpoint = "http://localhost:55554" } path := "/debug/configz/initial" @@ -180,7 +184,7 @@ func TestConfigServer_Serve(t *testing.T) { } func assertValidYAMLPages(t *testing.T, expected map[string]any, path string) { - url := "http://" + defaultConfigServerEndpoint + path + url := "http://localhost:55554" + path client := &http.Client{}