Skip to content

Commit

Permalink
Merge pull request #23802 from jerome59/main
Browse files Browse the repository at this point in the history
Podman CLI --add-host with multiple host for a single IP
  • Loading branch information
openshift-merge-bot[bot] authored Sep 17, 2024
2 parents 214e64e + f4d0e12 commit 3f0483f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
15 changes: 12 additions & 3 deletions cmd/podman/parse/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/podman/parse/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion docs/source/locale/ja/LC_MESSAGES/markdown.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
3 changes: 2 additions & 1 deletion docs/source/markdown/options/add-host.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

1 comment on commit 3f0483f

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.