From 9d93afac450ec222b561bbf60ee0410504de1b06 Mon Sep 17 00:00:00 2001 From: Gregor Eichelberger Date: Mon, 17 Jul 2023 15:02:41 +0200 Subject: [PATCH] Return error on missing host-gateway IP Signed-off-by: Gregor Eichelberger --- libnetwork/etchosts/hosts.go | 3 +++ libnetwork/etchosts/hosts_test.go | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/libnetwork/etchosts/hosts.go b/libnetwork/etchosts/hosts.go index e6e53cb74..2d4b73a9b 100644 --- a/libnetwork/etchosts/hosts.go +++ b/libnetwork/etchosts/hosts.go @@ -246,6 +246,9 @@ func parseExtraHosts(extraHosts []string, hostContainersInternalIP string) (Host } ip := values[1] if values[1] == HostGateway { + if hostContainersInternalIP == "" { + return nil, fmt.Errorf("unable to replace %q of host entry %q: host containers internal IP address is empty", HostGateway, entry) + } ip = hostContainersInternalIP } e := HostEntry{IP: ip, Names: []string{values[0]}} diff --git a/libnetwork/etchosts/hosts_test.go b/libnetwork/etchosts/hosts_test.go index a5093930e..69e83b5c7 100644 --- a/libnetwork/etchosts/hosts_test.go +++ b/libnetwork/etchosts/hosts_test.go @@ -274,6 +274,12 @@ func TestNew(t *testing.T) { extraHosts: []string{"name"}, wantErrString: "unable to parse host entry \"name\": incorrect format", }, + { + name: "invalid host-gateway", + baseFileContent: baseFileContent1Spaces, + extraHosts: []string{"gatewayname:host-gateway"}, + wantErrString: "host containers internal IP address is empty", + }, } for _, tt := range tests {