Skip to content

Commit

Permalink
refactor(config): drops localhost rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Jan 16, 2025
1 parent 7b43ee0 commit 545742f
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 148 deletions.
2 changes: 1 addition & 1 deletion cmd/sentry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func (s *contributoor) initBeaconNode() error {
b, err := ethereum.NewBeaconNode(
s.log,
&ethereum.Config{
BeaconNodeAddress: s.config.NodeAddress(),
BeaconNodeAddress: s.config.BeaconNodeAddress,
OverrideNetworkName: s.name,
},
s.name,
Expand Down
51 changes: 0 additions & 51 deletions pkg/config/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 == "" {
Expand Down
96 changes: 0 additions & 96 deletions pkg/config/v1/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 545742f

Please sign in to comment.