From f4d0e124d601097fd6be94f7dc75e32a8d689411 Mon Sep 17 00:00:00 2001 From: Jerome Degroote Date: Thu, 29 Aug 2024 09:53:03 +0200 Subject: [PATCH] Podman CLI --add-host with multiple host for a single IP Signed-off-by: Jerome degroote --- cmd/podman/parse/net.go | 15 ++++++++++++--- cmd/podman/parse/net_test.go | 6 ++++++ docs/source/locale/ja/LC_MESSAGES/markdown.po | 2 +- docs/source/markdown/options/add-host.md | 3 ++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/cmd/podman/parse/net.go b/cmd/podman/parse/net.go index 9d6ae1947c..a19c00b06f 100644 --- a/cmd/podman/parse/net.go +++ b/cmd/podman/parse/net.go @@ -29,14 +29,23 @@ var ( ) // validateExtraHost validates that the specified string is a valid extrahost and returns it. -// ExtraHost is in the form of name:ip where the ip has to be a valid ip (ipv4 or ipv6) or the special string HostGateway. +// ExtraHost is in the form of name1;name2;name3:ip where the ip has to be a valid ip (ipv4 or ipv6) or the special string HostGateway. // for add-host flag func ValidateExtraHost(val string) (string, error) { // allow for IPv6 addresses in extra hosts by only splitting on first ":" - name, ip, hasIP := strings.Cut(val, ":") - if !hasIP || len(name) == 0 { + names, ip, hasIP := strings.Cut(val, ":") + if !hasIP || len(names) == 0 { return "", fmt.Errorf("bad format for add-host: %q", val) } + + // Split the hostnames by semicolon and validate each one + nameList := strings.Split(names, ";") + for _, name := range nameList { + if len(name) == 0 { + return "", fmt.Errorf("hostname in add-host %q is empty", val) + } + } + if ip == etchosts.HostGateway { return val, nil } diff --git a/cmd/podman/parse/net_test.go b/cmd/podman/parse/net_test.go index c25a664b32..2242173268 100644 --- a/cmd/podman/parse/net_test.go +++ b/cmd/podman/parse/net_test.go @@ -42,17 +42,23 @@ func TestValidateExtraHost(t *testing.T) { }{ // 2001:0db8:85a3:0000:0000:8a2e:0370:7334 {name: "good-ipv4", args: args{val: "foobar:192.168.1.1"}, want: "foobar:192.168.1.1", wantErr: false}, + {name: "good-multiple-ipv4", args: args{val: "host1;host2;host3:192.168.1.1"}, want: "host1;host2;host3:192.168.1.1", wantErr: false}, {name: "bad-ipv4", args: args{val: "foobar:999.999.999.99"}, want: "", wantErr: true}, {name: "bad-ipv4", args: args{val: "foobar:999.999.999"}, want: "", wantErr: true}, + {name: "bad-multiple-ipv4", args: args{val: "host1;host2;host3:999.999.999"}, want: "", wantErr: true}, {name: "noname-ipv4", args: args{val: "192.168.1.1"}, want: "", wantErr: true}, {name: "noname-ipv4", args: args{val: ":192.168.1.1"}, want: "", wantErr: true}, + {name: "noname-multiple-ipv4", args: args{val: "host1;;host3:192.168.1.1"}, want: "", wantErr: true}, {name: "noip", args: args{val: "foobar:"}, want: "", wantErr: true}, {name: "noip", args: args{val: "foobar"}, want: "", wantErr: true}, {name: "good-ipv6", args: args{val: "foobar:2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, want: "foobar:2001:0db8:85a3:0000:0000:8a2e:0370:7334", wantErr: false}, + {name: "good-multiple-ipv6", args: args{val: "host1;host2;host3:2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, want: "host1;host2;host3:2001:0db8:85a3:0000:0000:8a2e:0370:7334", wantErr: false}, {name: "bad-ipv6", args: args{val: "foobar:0db8:85a3:0000:0000:8a2e:0370:7334"}, want: "", wantErr: true}, {name: "bad-ipv6", args: args{val: "foobar:0db8:85a3:0000:0000:8a2e:0370:7334.0000.0000.000"}, want: "", wantErr: true}, + {name: "bad-multiple-ipv6", args: args{val: "host1;host2;host3:0db8:85a3:0000:0000:8a2e:0370:7334.0000.0000.000"}, want: "", wantErr: true}, {name: "noname-ipv6", args: args{val: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, want: "", wantErr: true}, {name: "noname-ipv6", args: args{val: ":2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, want: "", wantErr: true}, + {name: "noname-multiple-ipv6", args: args{val: "host1;;host3:2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, want: "", wantErr: true}, {name: "host-gateway", args: args{val: "foobar:host-gateway"}, want: fmt.Sprintf("foobar:%s", etchosts.HostGateway), wantErr: false}, } for _, tt := range tests { diff --git a/docs/source/locale/ja/LC_MESSAGES/markdown.po b/docs/source/locale/ja/LC_MESSAGES/markdown.po index daba666e6d..f60f9df7e8 100644 --- a/docs/source/locale/ja/LC_MESSAGES/markdown.po +++ b/docs/source/locale/ja/LC_MESSAGES/markdown.po @@ -1629,7 +1629,7 @@ msgstr "" #: ../../source/markdown/podman-pod-create.1.md:39 #: ../../source/markdown/podman-run.1.md:94 msgid "" -"Add a line to /etc/hosts. The format is hostname:ip. The **--add-host** " +"Add a line to /etc/hosts. The format is hostname1;hostname2;hostname3:ip. Multiple hostnames for the same IP can be separated by semicolons. The **--add-host** " "option can be set multiple times. Conflicts with the **--no-hosts** " "option." msgstr "" diff --git a/docs/source/markdown/options/add-host.md b/docs/source/markdown/options/add-host.md index 6e9f598ca0..649bccf7b3 100644 --- a/docs/source/markdown/options/add-host.md +++ b/docs/source/markdown/options/add-host.md @@ -5,6 +5,7 @@ #### **--add-host**=*host:ip* Add a custom host-to-IP mapping (host:ip) +Multiple hostnames for the same IP can be separated by semicolons. -Add a line to /etc/hosts. The format is hostname:ip. The **--add-host** +Add a line to /etc/hosts. The format is hostname:ip or hostname1;hostname2;hostname3:ip if you want to map multiple hostnames to the same ip without duplicating the --add-host parameter. The **--add-host** option can be set multiple times. Conflicts with the **--no-hosts** option.