Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit f8a6bb0

Browse files
authored
Merge pull request #2444 from thaJeztah/19.03_backport_host_gateway_validation
[19.03 backport] Skip IPAddr validation for "host-gateway" string Upstream-commit: ba0a2ffe2ad3e832a7d01908ba3212ecb841e4ab Component: cli
2 parents c9f1c43 + 62e46c2 commit f8a6bb0

6 files changed

Lines changed: 28 additions & 5 deletions

File tree

components/cli/cli/command/service/update_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,19 @@ func TestUpdateHosts(t *testing.T) {
358358
flags := newUpdateCommand(nil).Flags()
359359
flags.Set("host-add", "example.net:2.2.2.2")
360360
flags.Set("host-add", "ipv6.net:2001:db8:abc8::1")
361+
// adding the special "host-gateway" target should work
362+
flags.Set("host-add", "host.docker.internal:host-gateway")
361363
// remove with ipv6 should work
362364
flags.Set("host-rm", "example.net:2001:db8:abc8::1")
363365
// just hostname should work as well
364366
flags.Set("host-rm", "example.net")
367+
// removing the special "host-gateway" target should work
368+
flags.Set("host-rm", "gateway.docker.internal:host-gateway")
365369
// bad format error
366370
assert.ErrorContains(t, flags.Set("host-add", "$example.com$"), `bad format for add-host: "$example.com$"`)
367371

368-
hosts := []string{"1.2.3.4 example.com", "4.3.2.1 example.org", "2001:db8:abc8::1 example.net"}
369-
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"}
372+
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"}
373+
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"}
370374

371375
err := updateHosts(flags, &hosts)
372376
assert.NilError(t, err)

components/cli/cli/compose/loader/full-example.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ services:
140140
# extra_hosts:
141141
# somehost: "162.242.195.82"
142142
# otherhost: "50.31.209.229"
143+
# host.docker.internal: "host-gateway"
143144
extra_hosts:
144145
- "somehost:162.242.195.82"
145146
- "otherhost:50.31.209.229"
147+
- "host.docker.internal:host-gateway"
146148

147149
hostname: foo
148150

components/cli/cli/compose/loader/full-struct_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
144144
ExtraHosts: []string{
145145
"somehost:162.242.195.82",
146146
"otherhost:50.31.209.229",
147+
"host.docker.internal:host-gateway",
147148
},
148149
Extras: map[string]interface{}{
149150
"x-bar": "baz",
@@ -626,6 +627,7 @@ services:
626627
extra_hosts:
627628
- somehost:162.242.195.82
628629
- otherhost:50.31.209.229
630+
- host.docker.internal:host-gateway
629631
hostname: foo
630632
healthcheck:
631633
test:
@@ -1135,7 +1137,8 @@ func fullExampleJSON(workingDir string) string {
11351137
],
11361138
"extra_hosts": [
11371139
"somehost:162.242.195.82",
1138-
"otherhost:50.31.209.229"
1140+
"otherhost:50.31.209.229",
1141+
"host.docker.internal:host-gateway"
11391142
],
11401143
"hostname": "foo",
11411144
"healthcheck": {

components/cli/cli/compose/loader/loader_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,11 +1276,13 @@ services:
12761276
extra_hosts:
12771277
"zulu": "162.242.195.82"
12781278
"alpha": "50.31.209.229"
1279+
"host.docker.internal": "host-gateway"
12791280
`)
12801281
assert.NilError(t, err)
12811282

12821283
expected := types.HostsList{
12831284
"alpha:50.31.209.229",
1285+
"host.docker.internal:host-gateway",
12841286
"zulu:162.242.195.82",
12851287
}
12861288

@@ -1298,13 +1300,15 @@ services:
12981300
- "zulu:162.242.195.82"
12991301
- "alpha:50.31.209.229"
13001302
- "zulu:ff02::1"
1303+
- "host.docker.internal:host-gateway"
13011304
`)
13021305
assert.NilError(t, err)
13031306

13041307
expected := types.HostsList{
13051308
"zulu:162.242.195.82",
13061309
"alpha:50.31.209.229",
13071310
"zulu:ff02::1",
1311+
"host.docker.internal:host-gateway",
13081312
}
13091313

13101314
assert.Assert(t, is.Len(config.Services, 1))

components/cli/opts/hosts.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ var (
2424
DefaultTLSHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultTLSHTTPPort)
2525
// DefaultNamedPipe defines the default named pipe used by docker on Windows
2626
DefaultNamedPipe = `//./pipe/docker_engine`
27+
// hostGatewayName defines a special string which users can append to --add-host
28+
// to add an extra entry in /etc/hosts that maps host.docker.internal to the host IP
29+
// TODO Consider moving the HostGatewayName constant defined in docker at
30+
// github.com/docker/docker/daemon/network/constants.go outside of the "daemon"
31+
// package, so that the CLI can consume it.
32+
hostGatewayName = "host-gateway"
2733
)
2834

2935
// ValidateHost validates that the specified string is a valid host and returns it.
@@ -160,8 +166,11 @@ func ValidateExtraHost(val string) (string, error) {
160166
if len(arr) != 2 || len(arr[0]) == 0 {
161167
return "", fmt.Errorf("bad format for add-host: %q", val)
162168
}
163-
if _, err := ValidateIPAddress(arr[1]); err != nil {
164-
return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1])
169+
// Skip IPaddr validation for "host-gateway" string
170+
if arr[1] != hostGatewayName {
171+
if _, err := ValidateIPAddress(arr[1]); err != nil {
172+
return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1])
173+
}
165174
}
166175
return val, nil
167176
}

components/cli/opts/hosts_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ func TestValidateExtraHosts(t *testing.T) {
154154
`thathost:10.0.2.1`,
155155
`anipv6host:2003:ab34:e::1`,
156156
`ipv6local:::1`,
157+
`host.docker.internal:host-gateway`,
157158
}
158159

159160
invalid := map[string]string{

0 commit comments

Comments
 (0)