From 365118982638094138bb0f88ab23f438aafb7331 Mon Sep 17 00:00:00 2001 From: Arko Dasgupta Date: Tue, 11 Feb 2020 12:00:06 -0800 Subject: [PATCH 1/2] Skip IPAddr validation for "host-gateway" string Relates to - moby/moby 40007 The above PR added support in moby, that detects if a special string "host-gateway" is added to the IP section of --add-host, and if true, replaces it with a special IP value (value of --host-gateway-ip Daemon flag which defaults to the IP of the default bridge). This PR is needed to skip the validation for the above feature Signed-off-by: Arko Dasgupta Signed-off-by: Sebastiaan van Stijn (cherry picked from commit 67ebcd6dcf82c00f133b886a9a343b67124e58ec) Signed-off-by: Sebastiaan van Stijn --- opts/hosts.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/opts/hosts.go b/opts/hosts.go index 408bc24a06..f0e392e1e5 100644 --- a/opts/hosts.go +++ b/opts/hosts.go @@ -24,6 +24,12 @@ var ( DefaultTLSHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultTLSHTTPPort) // DefaultNamedPipe defines the default named pipe used by docker on Windows DefaultNamedPipe = `//./pipe/docker_engine` + // hostGatewayName defines a special string which users can append to --add-host + // to add an extra entry in /etc/hosts that maps host.docker.internal to the host IP + // TODO Consider moving the HostGatewayName constant defined in docker at + // github.com/docker/docker/daemon/network/constants.go outside of the "daemon" + // package, so that the CLI can consume it. + hostGatewayName = "host-gateway" ) // ValidateHost validates that the specified string is a valid host and returns it. @@ -160,8 +166,11 @@ func ValidateExtraHost(val string) (string, error) { if len(arr) != 2 || len(arr[0]) == 0 { return "", fmt.Errorf("bad format for add-host: %q", val) } - if _, err := ValidateIPAddress(arr[1]); err != nil { - return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1]) + // Skip IPaddr validation for "host-gateway" string + if arr[1] != hostGatewayName { + if _, err := ValidateIPAddress(arr[1]); err != nil { + return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1]) + } } return val, nil } From fd9eedce3c1e06f3cbdd0f10dece3ffd73aefc4a Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 15 Apr 2020 09:13:11 +0200 Subject: [PATCH 2/2] Add "host-gateway" to tests for extra_hosts / --add-host 67ebcd6dcf82c00f133b886a9a343b67124e58ec added an exception for the "host-gateway" magic value to the validation rules, but didn't add thise value to any of the tests. This patch adds the magic value to tests, to verify the validation is skipped for this magic value. Note that validation on the client side is "optional" and mostly done to provide a more user-friendly error message for regular values (IP-addresses). Signed-off-by: Sebastiaan van Stijn (cherry picked from commit f88ae74135fc46227d3f62e4308a79a92a76bd76) Signed-off-by: Sebastiaan van Stijn --- cli/command/service/update_test.go | 8 ++++++-- cli/compose/loader/full-example.yml | 2 ++ cli/compose/loader/full-struct_test.go | 5 ++++- cli/compose/loader/loader_test.go | 4 ++++ opts/hosts_test.go | 1 + 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cli/command/service/update_test.go b/cli/command/service/update_test.go index fbabdf2bbf..9ea293e516 100644 --- a/cli/command/service/update_test.go +++ b/cli/command/service/update_test.go @@ -358,15 +358,19 @@ func TestUpdateHosts(t *testing.T) { flags := newUpdateCommand(nil).Flags() flags.Set("host-add", "example.net:2.2.2.2") flags.Set("host-add", "ipv6.net:2001:db8:abc8::1") + // adding the special "host-gateway" target should work + flags.Set("host-add", "host.docker.internal:host-gateway") // remove with ipv6 should work flags.Set("host-rm", "example.net:2001:db8:abc8::1") // just hostname should work as well flags.Set("host-rm", "example.net") + // removing the special "host-gateway" target should work + flags.Set("host-rm", "gateway.docker.internal:host-gateway") // bad format error assert.ErrorContains(t, flags.Set("host-add", "$example.com$"), `bad format for add-host: "$example.com$"`) - hosts := []string{"1.2.3.4 example.com", "4.3.2.1 example.org", "2001:db8:abc8::1 example.net"} - expected := []string{"1.2.3.4 example.com", "4.3.2.1 example.org", "2.2.2.2 example.net", "2001:db8:abc8::1 ipv6.net"} + hosts := []string{"1.2.3.4 example.com", "4.3.2.1 example.org", "2001:db8:abc8::1 example.net", "gateway.docker.internal:host-gateway"} + expected := []string{"1.2.3.4 example.com", "4.3.2.1 example.org", "2.2.2.2 example.net", "2001:db8:abc8::1 ipv6.net", "host-gateway host.docker.internal"} err := updateHosts(flags, &hosts) assert.NilError(t, err) diff --git a/cli/compose/loader/full-example.yml b/cli/compose/loader/full-example.yml index f452812a69..908da1f338 100644 --- a/cli/compose/loader/full-example.yml +++ b/cli/compose/loader/full-example.yml @@ -140,9 +140,11 @@ services: # extra_hosts: # somehost: "162.242.195.82" # otherhost: "50.31.209.229" + # host.docker.internal: "host-gateway" extra_hosts: - "somehost:162.242.195.82" - "otherhost:50.31.209.229" + - "host.docker.internal:host-gateway" hostname: foo diff --git a/cli/compose/loader/full-struct_test.go b/cli/compose/loader/full-struct_test.go index 427f2e8e06..37033c3641 100644 --- a/cli/compose/loader/full-struct_test.go +++ b/cli/compose/loader/full-struct_test.go @@ -144,6 +144,7 @@ func services(workingDir, homeDir string) []types.ServiceConfig { ExtraHosts: []string{ "somehost:162.242.195.82", "otherhost:50.31.209.229", + "host.docker.internal:host-gateway", }, Extras: map[string]interface{}{ "x-bar": "baz", @@ -626,6 +627,7 @@ services: extra_hosts: - somehost:162.242.195.82 - otherhost:50.31.209.229 + - host.docker.internal:host-gateway hostname: foo healthcheck: test: @@ -1135,7 +1137,8 @@ func fullExampleJSON(workingDir string) string { ], "extra_hosts": [ "somehost:162.242.195.82", - "otherhost:50.31.209.229" + "otherhost:50.31.209.229", + "host.docker.internal:host-gateway" ], "hostname": "foo", "healthcheck": { diff --git a/cli/compose/loader/loader_test.go b/cli/compose/loader/loader_test.go index 4ba26a6464..09c98fa93c 100644 --- a/cli/compose/loader/loader_test.go +++ b/cli/compose/loader/loader_test.go @@ -1276,11 +1276,13 @@ services: extra_hosts: "zulu": "162.242.195.82" "alpha": "50.31.209.229" + "host.docker.internal": "host-gateway" `) assert.NilError(t, err) expected := types.HostsList{ "alpha:50.31.209.229", + "host.docker.internal:host-gateway", "zulu:162.242.195.82", } @@ -1298,6 +1300,7 @@ services: - "zulu:162.242.195.82" - "alpha:50.31.209.229" - "zulu:ff02::1" + - "host.docker.internal:host-gateway" `) assert.NilError(t, err) @@ -1305,6 +1308,7 @@ services: "zulu:162.242.195.82", "alpha:50.31.209.229", "zulu:ff02::1", + "host.docker.internal:host-gateway", } assert.Assert(t, is.Len(config.Services, 1)) diff --git a/opts/hosts_test.go b/opts/hosts_test.go index 3e6e49703f..9f65b491c2 100644 --- a/opts/hosts_test.go +++ b/opts/hosts_test.go @@ -154,6 +154,7 @@ func TestValidateExtraHosts(t *testing.T) { `thathost:10.0.2.1`, `anipv6host:2003:ab34:e::1`, `ipv6local:::1`, + `host.docker.internal:host-gateway`, } invalid := map[string]string{