Skip to content

Commit 6f62953

Browse files
committed
Add port validator to ensure configurable ports are valid
Signed-off-by: Lan Luo <[email protected]>
1 parent c33622c commit 6f62953

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

cmd/antrea-agent/options.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -430,13 +430,13 @@ func (o *Options) setK8sNodeDefaultOptions() {
430430
if o.config.AntreaProxy.DefaultLoadBalancerMode == "" {
431431
o.config.AntreaProxy.DefaultLoadBalancerMode = config.LoadBalancerModeNAT.String()
432432
}
433-
if o.config.ClusterMembershipPort == 0 {
433+
if !isValidPort(o.config.ClusterMembershipPort) {
434434
o.config.ClusterMembershipPort = apis.AntreaAgentClusterMembershipPort
435435
}
436436
if o.config.EnablePrometheusMetrics == nil {
437437
o.config.EnablePrometheusMetrics = ptr.To(true)
438438
}
439-
if o.config.WireGuard.Port == 0 {
439+
if !isValidPort(o.config.WireGuard.Port) {
440440
o.config.WireGuard.Port = apis.WireGuardListenPort
441441
}
442442

@@ -534,6 +534,9 @@ func (o *Options) validateK8sNodeOptions() error {
534534
o.config.TunnelType != ovsconfig.GRETunnel && o.config.TunnelType != ovsconfig.STTTunnel {
535535
return fmt.Errorf("tunnel type %s is invalid", o.config.TunnelType)
536536
}
537+
if !isValidPort(int(o.config.TunnelPort)) {
538+
return fmt.Errorf("tunnel port %s is invalid", o.config.TunnelPort)
539+
}
537540
ok, encryptionMode := config.GetTrafficEncryptionModeFromStr(o.config.TrafficEncryptionMode)
538541
if !ok {
539542
return fmt.Errorf("TrafficEncryptionMode %s is unknown", o.config.TrafficEncryptionMode)

cmd/antrea-agent/util.go

+8
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,11 @@ func parsePortRange(portRangeStr string) (start, end int, err error) {
7979

8080
return start, end, nil
8181
}
82+
83+
// isValidPort checks if the given port number is within the valid range of 1 to 65535.
84+
func isValidPort(port int) bool {
85+
if port < 1 || port > 65535 {
86+
return false
87+
}
88+
return true
89+
}

cmd/antrea-agent/util_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,34 @@ func TestParsePortRange(t *testing.T) {
122122
})
123123
}
124124
}
125+
126+
func TestIsValidPort(t *testing.T) {
127+
tests := []struct {
128+
name string
129+
port int
130+
expected bool
131+
}{
132+
{
133+
name: "invalid port 0",
134+
port: 0,
135+
expected: false,
136+
},
137+
{
138+
name: "invalid port 70000",
139+
port: 70000,
140+
expected: false,
141+
},
142+
{
143+
name: "valid port",
144+
port: 65500,
145+
expected: true,
146+
},
147+
}
148+
149+
for _, tc := range tests {
150+
t.Run(tc.name, func(t *testing.T) {
151+
result := isValidPort(tc.port)
152+
assert.Equal(t, tc.expected, result)
153+
})
154+
}
155+
}

0 commit comments

Comments
 (0)