From 545742f6525aec49cffe09d2bf8a67cd5013391c Mon Sep 17 00:00:00 2001 From: Matty Evans Date: Fri, 17 Jan 2025 08:05:24 +1000 Subject: [PATCH] refactor(config): drops localhost rewrite --- cmd/sentry/main.go | 2 +- pkg/config/v1/config.go | 51 ------------------- pkg/config/v1/config_test.go | 96 ------------------------------------ 3 files changed, 1 insertion(+), 148 deletions(-) diff --git a/cmd/sentry/main.go b/cmd/sentry/main.go index df0a40f..9c3dc12 100644 --- a/cmd/sentry/main.go +++ b/cmd/sentry/main.go @@ -422,7 +422,7 @@ func (s *contributoor) initBeaconNode() error { b, err := ethereum.NewBeaconNode( s.log, ðereum.Config{ - BeaconNodeAddress: s.config.NodeAddress(), + BeaconNodeAddress: s.config.BeaconNodeAddress, OverrideNetworkName: s.name, }, s.name, diff --git a/pkg/config/v1/config.go b/pkg/config/v1/config.go index 1679bd7..ba30cf5 100644 --- a/pkg/config/v1/config.go +++ b/pkg/config/v1/config.go @@ -19,28 +19,6 @@ const ( defaultPprofPort = "6060" ) -var localHostnames = map[string]bool{ - "localhost": true, - "127.0.0.1": true, - "0.0.0.0": true, -} - -// isRunningInDocker checks if we're actually running inside a Docker container -// by looking for container-specific files and environment variables. -var isRunningInDocker = func() bool { - // Check for .dockerenv file. - if _, err := os.Stat("/.dockerenv"); err == nil { - return true - } - - // Check for docker-specific cgroup. - if data, err := os.ReadFile("/proc/1/cgroup"); err == nil { - return strings.Contains(string(data), "docker") - } - - return false -} - // NewConfigFromPath loads a config from a YAML file and validates it. func NewConfigFromPath(path string) (*Config, error) { data, err := os.ReadFile(path) @@ -173,35 +151,6 @@ func (c *Config) GetPprofHostPort() (host, port string) { return ParseAddress(c.PprofAddress, defaultPprofHost, defaultPprofPort) } -// NodeAddress returns the beacon node address, rewriting local addresses -// to use host.docker.internal when running in Docker. -// Docker containers can't directly access the host via localhost/127.0.0.1. -// We rewrite these to host.docker.internal which resolves differently per platform: -// - macOS: Built-in DNS name that points to the Docker Desktop VM's gateway -// - Linux: Maps to host-gateway via extra_hosts in docker-compose.yml -// This provides a consistent way to access the host machine across platforms. -func (c *Config) NodeAddress() string { - // Only rewrite if: - // 1. We have a beacon node address. - // 2. Docker is configured as the run method. - // 3. We're actually running inside a Docker container. - if c.BeaconNodeAddress == "" || - c.RunMethod != RunMethod_RUN_METHOD_DOCKER || - !isRunningInDocker() { - return c.BeaconNodeAddress - } - - // Check if URL points to a local address. - for hostname := range localHostnames { - if strings.Contains(c.BeaconNodeAddress, hostname) { - // Replace the local hostname with host.docker.internal. - return strings.Replace(c.BeaconNodeAddress, hostname, "host.docker.internal", 1) - } - } - - return c.BeaconNodeAddress -} - // SetNetwork sets the network name. func (c *Config) SetNetwork(network string) { if network == "" { diff --git a/pkg/config/v1/config_test.go b/pkg/config/v1/config_test.go index 76b9593..25dc106 100644 --- a/pkg/config/v1/config_test.go +++ b/pkg/config/v1/config_test.go @@ -244,99 +244,3 @@ func TestConfig_GetPprofHostPort(t *testing.T) { }) } } - -func TestConfig_NodeAddress(t *testing.T) { - // Mock isRunningInDocker for testing - originalIsRunningInDocker := isRunningInDocker - defer func() { isRunningInDocker = originalIsRunningInDocker }() - - tests := []struct { - name string - config *Config - inDocker bool - expectedAddress string - }{ - { - name: "docker mode + in docker container + local url", - config: &Config{ - BeaconNodeAddress: "http://localhost:5052", - RunMethod: RunMethod_RUN_METHOD_DOCKER, - }, - inDocker: true, - expectedAddress: "http://host.docker.internal:5052", - }, - { - name: "docker mode + in docker container + 127.0.0.1", - config: &Config{ - BeaconNodeAddress: "http://127.0.0.1:5052", - RunMethod: RunMethod_RUN_METHOD_DOCKER, - }, - inDocker: true, - expectedAddress: "http://host.docker.internal:5052", - }, - { - name: "docker mode + in docker container + 0.0.0.0", - config: &Config{ - BeaconNodeAddress: "http://0.0.0.0:5052", - RunMethod: RunMethod_RUN_METHOD_DOCKER, - }, - inDocker: true, - expectedAddress: "http://host.docker.internal:5052", - }, - { - name: "docker mode + in docker container + remote url", - config: &Config{ - BeaconNodeAddress: "http://example.com:5052", - RunMethod: RunMethod_RUN_METHOD_DOCKER, - }, - inDocker: true, - expectedAddress: "http://example.com:5052", - }, - { - name: "docker mode + NOT in docker + local url", - config: &Config{ - BeaconNodeAddress: "http://localhost:5052", - RunMethod: RunMethod_RUN_METHOD_DOCKER, - }, - inDocker: false, - expectedAddress: "http://localhost:5052", - }, - { - name: "non-docker mode + in docker + local url", - config: &Config{ - BeaconNodeAddress: "http://localhost:5052", - RunMethod: RunMethod_RUN_METHOD_BINARY, - }, - inDocker: true, - expectedAddress: "http://localhost:5052", - }, - { - name: "docker mode + in docker + local url with path", - config: &Config{ - BeaconNodeAddress: "http://localhost:5052/eth/v1/node/syncing", - RunMethod: RunMethod_RUN_METHOD_DOCKER, - }, - inDocker: true, - expectedAddress: "http://host.docker.internal:5052/eth/v1/node/syncing", - }, - { - name: "empty address", - config: &Config{ - BeaconNodeAddress: "", - RunMethod: RunMethod_RUN_METHOD_DOCKER, - }, - inDocker: true, - expectedAddress: "", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - // Mock isRunningInDocker for this test - isRunningInDocker = func() bool { return tt.inDocker } - - got := tt.config.NodeAddress() - assert.Equal(t, tt.expectedAddress, got) - }) - } -}