Skip to content

Commit 41087bf

Browse files
authored
Merge pull request #660 from rancher-sandbox/guest-bind-all
Add portForwards.guestIPMustBeZero property
2 parents f7dbc08 + 050b4eb commit 41087bf

File tree

6 files changed

+39
-12
lines changed

6 files changed

+39
-12
lines changed

hack/test-port-forwarding.pl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,15 @@ sub JoinHostPort {
292292
# forward: 0.0.0.0 4033 → ipv4 4033
293293
# forward: :: 4034 → ipv4 4034
294294
# forward: ::1 4035 → ipv4 4035
295+
296+
- guestIPMustBeZero: true
297+
guestPortRange: [4040, 4049]
298+
299+
- guestIP: "0.0.0.0"
300+
guestPortRange: [4040, 4049]
301+
ignore: true
302+
303+
# forward: 0.0.0.0 4040 → 127.0.0.1 4040
304+
# forward: :: 4041 → 127.0.0.1 4041
305+
# ignore: 127.0.0.1 4043 → 127.0.0.1 4043
306+
# ignore: 192.168.5.15 4044 → 127.0.0.1 4044

pkg/hostagent/port.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ func (pf *portForwarder) forwardingAddresses(guest api.IPPort) (string, string)
5252
case guest.IP.IsUnspecified():
5353
case guest.IP.Equal(rule.GuestIP):
5454
case guest.IP.Equal(net.IPv6loopback) && rule.GuestIP.Equal(api.IPv4loopback1):
55-
case rule.GuestIP.IsUnspecified():
55+
case rule.GuestIP.IsUnspecified() && !rule.GuestIPMustBeZero:
56+
// When GuestIPMustBeZero is true, then 0.0.0.0 must be an exact match, which is already
57+
// handled above by the guest.IP.IsUnspecified() condition.
5658
default:
5759
continue
5860
}

pkg/limayaml/default.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ networks:
209209
# - guestPort: 8888
210210
# ignore: true (don't forward this port)
211211
#
212+
# - guestPort: 7443
213+
# guestIP: "0.0.0.0" # Will match *any* interface
214+
# guestIPMustBeZero: true # Restrict matching to 0.0.0.0 binds only
215+
# hostIP: "0.0.0.0" # Forwards to 0.0.0.0, exposing it externally
216+
#
212217
# - guestSocket: "/run/user/{{.UID}}/my.sock"
213218
# hostSocket: mysocket
214219
# # "guestSocket" can include these template variables: {{.Home}}, {{.UID}}, and {{.User}}.

pkg/limayaml/defaults.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,11 @@ func FillPortForwardDefaults(rule *PortForward, instDir string) {
402402
rule.Proto = TCP
403403
}
404404
if rule.GuestIP == nil {
405-
rule.GuestIP = api.IPv4loopback1
405+
if rule.GuestIPMustBeZero {
406+
rule.GuestIP = net.IPv4zero
407+
} else {
408+
rule.GuestIP = api.IPv4loopback1
409+
}
406410
}
407411
if rule.HostIP == nil {
408412
rule.HostIP = api.IPv4loopback1

pkg/limayaml/limayaml.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,17 @@ const (
112112
)
113113

114114
type PortForward struct {
115-
GuestIP net.IP `yaml:"guestIP,omitempty" json:"guestIP,omitempty"`
116-
GuestPort int `yaml:"guestPort,omitempty" json:"guestPort,omitempty"`
117-
GuestPortRange [2]int `yaml:"guestPortRange,omitempty" json:"guestPortRange,omitempty"`
118-
GuestSocket string `yaml:"guestSocket,omitempty" json:"guestSocket,omitempty"`
119-
HostIP net.IP `yaml:"hostIP,omitempty" json:"hostIP,omitempty"`
120-
HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"`
121-
HostPortRange [2]int `yaml:"hostPortRange,omitempty" json:"hostPortRange,omitempty"`
122-
HostSocket string `yaml:"hostSocket,omitempty" json:"hostSocket,omitempty"`
123-
Proto Proto `yaml:"proto,omitempty" json:"proto,omitempty"`
124-
Ignore bool `yaml:"ignore,omitempty" json:"ignore,omitempty"`
115+
GuestIPMustBeZero bool `yaml:"guestIPMustBeZero,omitempty" json:"guestIPMustBeZero,omitempty"`
116+
GuestIP net.IP `yaml:"guestIP,omitempty" json:"guestIP,omitempty"`
117+
GuestPort int `yaml:"guestPort,omitempty" json:"guestPort,omitempty"`
118+
GuestPortRange [2]int `yaml:"guestPortRange,omitempty" json:"guestPortRange,omitempty"`
119+
GuestSocket string `yaml:"guestSocket,omitempty" json:"guestSocket,omitempty"`
120+
HostIP net.IP `yaml:"hostIP,omitempty" json:"hostIP,omitempty"`
121+
HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"`
122+
HostPortRange [2]int `yaml:"hostPortRange,omitempty" json:"hostPortRange,omitempty"`
123+
HostSocket string `yaml:"hostSocket,omitempty" json:"hostSocket,omitempty"`
124+
Proto Proto `yaml:"proto,omitempty" json:"proto,omitempty"`
125+
Ignore bool `yaml:"ignore,omitempty" json:"ignore,omitempty"`
125126
}
126127

127128
type Network struct {

pkg/limayaml/validate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ func Validate(y LimaYAML, warn bool) error {
134134
}
135135
for i, rule := range y.PortForwards {
136136
field := fmt.Sprintf("portForwards[%d]", i)
137+
if rule.GuestIPMustBeZero && !rule.GuestIP.Equal(net.IPv4zero) {
138+
return fmt.Errorf("field `%s.guestIPMustBeZero` can only be true when field `%s.guestIP` is 0.0.0.0", field, field)
139+
}
137140
if rule.GuestPort != 0 {
138141
if rule.GuestSocket != "" {
139142
return fmt.Errorf("field `%s.guestPort` must be 0 when field `%s.guestSocket` is set", field, field)

0 commit comments

Comments
 (0)