Skip to content

Commit e6778e0

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

File tree

7 files changed

+86
-6
lines changed

7 files changed

+86
-6
lines changed

cmd/antrea-agent/options.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"net"
2020
"os"
21+
"strconv"
2122
"strings"
2223
"time"
2324

@@ -34,6 +35,7 @@ import (
3435
agentconfig "antrea.io/antrea/pkg/config/agent"
3536
"antrea.io/antrea/pkg/features"
3637
"antrea.io/antrea/pkg/ovs/ovsconfig"
38+
"antrea.io/antrea/pkg/util/checks"
3739
"antrea.io/antrea/pkg/util/env"
3840
"antrea.io/antrea/pkg/util/flowexport"
3941
"antrea.io/antrea/pkg/util/ip"
@@ -193,7 +195,7 @@ func (o *Options) setDefaults() {
193195
if o.config.OVSRunDir == "" {
194196
o.config.OVSRunDir = ovsconfig.DefaultOVSRunDir
195197
}
196-
if o.config.APIPort == 0 {
198+
if !checks.IsValidPort(o.config.APIPort) {
197199
o.config.APIPort = apis.AntreaAgentAPIPort
198200
}
199201
if o.config.NodeType == "" {
@@ -430,13 +432,13 @@ func (o *Options) setK8sNodeDefaultOptions() {
430432
if o.config.AntreaProxy.DefaultLoadBalancerMode == "" {
431433
o.config.AntreaProxy.DefaultLoadBalancerMode = config.LoadBalancerModeNAT.String()
432434
}
433-
if o.config.ClusterMembershipPort == 0 {
435+
if !checks.IsValidPort(o.config.ClusterMembershipPort) {
434436
o.config.ClusterMembershipPort = apis.AntreaAgentClusterMembershipPort
435437
}
436438
if o.config.EnablePrometheusMetrics == nil {
437439
o.config.EnablePrometheusMetrics = ptr.To(true)
438440
}
439-
if o.config.WireGuard.Port == 0 {
441+
if !checks.IsValidPort(o.config.WireGuard.Port) {
440442
o.config.WireGuard.Port = apis.WireGuardListenPort
441443
}
442444

@@ -534,6 +536,9 @@ func (o *Options) validateK8sNodeOptions() error {
534536
o.config.TunnelType != ovsconfig.GRETunnel && o.config.TunnelType != ovsconfig.STTTunnel {
535537
return fmt.Errorf("tunnel type %s is invalid", o.config.TunnelType)
536538
}
539+
if !checks.IsValidPort(int(o.config.TunnelPort)) {
540+
return fmt.Errorf("tunnel port %d is invalid", o.config.TunnelPort)
541+
}
537542
ok, encryptionMode := config.GetTrafficEncryptionModeFromStr(o.config.TrafficEncryptionMode)
538543
if !ok {
539544
return fmt.Errorf("TrafficEncryptionMode %s is unknown", o.config.TrafficEncryptionMode)
@@ -605,8 +610,9 @@ func (o *Options) validateK8sNodeOptions() error {
605610

606611
if o.config.DNSServerOverride != "" {
607612
hostPort := ip.AppendPortIfMissing(o.config.DNSServerOverride, "53")
608-
_, _, err := net.SplitHostPort(hostPort)
609-
if err != nil {
613+
_, port, err := net.SplitHostPort(hostPort)
614+
portNum, parseErr := strconv.Atoi(port)
615+
if err != nil || !checks.IsValidPort(portNum) || parseErr != nil {
610616
return fmt.Errorf("dnsServerOverride %s is invalid: %v", o.config.DNSServerOverride, err)
611617
}
612618
o.dnsServerOverride = hostPort
@@ -706,7 +712,7 @@ func (o *Options) setExternalNodeDefaultOptions() {
706712
func (o *Options) setMulticlusterDefaultOptions() {
707713
_, trafficEncryptionModeType := config.GetTrafficEncryptionModeFromStr(o.config.Multicluster.TrafficEncryptionMode)
708714
if trafficEncryptionModeType == config.TrafficEncryptionModeWireGuard {
709-
if o.config.Multicluster.WireGuard.Port == 0 {
715+
if !checks.IsValidPort(o.config.Multicluster.WireGuard.Port) {
710716
o.config.Multicluster.WireGuard.Port = apis.MulticlusterWireGuardListenPort
711717
}
712718
}

cmd/antrea-agent/options_linux_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func TestMulticlusterOptions(t *testing.T) {
8888
FeatureGates: map[string]bool{"Multicluster": tt.featureGate},
8989
TrafficEncapMode: tt.encapMode,
9090
Multicluster: tt.mcConfig,
91+
TunnelPort: 6081,
9192
}
9293
if tt.encryptionMode != "" {
9394
config.TrafficEncryptionMode = tt.encryptionMode

pkg/util/checks/checks.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package checks
2+
3+
// IsValidPort checks if the given port number is within the valid range of 1 to 65535.
4+
func IsValidPort(port int) bool {
5+
if port < 1 || port > 65535 {
6+
return false
7+
}
8+
return true
9+
}

pkg/util/checks/checks_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package checks
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestIsValidPort(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
port int
13+
expected bool
14+
}{
15+
{
16+
name: "invalid port 0",
17+
port: 0,
18+
expected: false,
19+
},
20+
{
21+
name: "invalid port 70000",
22+
port: 70000,
23+
expected: false,
24+
},
25+
{
26+
name: "valid port",
27+
port: 65500,
28+
expected: true,
29+
},
30+
}
31+
32+
for _, tc := range tests {
33+
t.Run(tc.name, func(t *testing.T) {
34+
result := IsValidPort(tc.port)
35+
assert.Equal(t, tc.expected, result)
36+
})
37+
}
38+
}

pkg/util/flowexport/flowexport.go

+6
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ package flowexport
1717
import (
1818
"fmt"
1919
"regexp"
20+
"strconv"
2021
"strings"
2122
"time"
2223

2324
flowaggregatorconfig "antrea.io/antrea/pkg/config/flowaggregator"
25+
"antrea.io/antrea/pkg/util/checks"
2426
)
2527

2628
// ParseFlowCollectorAddr parses the flow collector address input for flow exporter and aggregator
@@ -44,6 +46,10 @@ func ParseFlowCollectorAddr(addr string, defaultPort string, defaultProtocol str
4446
port = defaultPort
4547
} else {
4648
port = strSlice[1]
49+
portNum, err := strconv.Atoi(port)
50+
if !checks.IsValidPort(portNum) || err != nil {
51+
port = defaultPort
52+
}
4753
}
4854
if (strSlice[2] != "tls") && (strSlice[2] != "tcp") && (strSlice[2] != "udp") {
4955
return host, port, proto, fmt.Errorf("connection over %s transport proto is not supported", strSlice[2])

pkg/util/flowexport/flowexport_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ func TestParseFlowCollectorAddr(t *testing.T) {
6767
expectedProto: "tcp",
6868
expectedError: nil,
6969
},
70+
{
71+
addr: "flow-aggregator/flow-aggregator:str:tcp",
72+
expectedHost: "flow-aggregator/flow-aggregator",
73+
expectedPort: defaultFlowCollectorPort,
74+
expectedProto: "tcp",
75+
expectedError: nil,
76+
},
77+
{
78+
addr: "flow-aggregator/flow-aggregator:78900:tcp",
79+
expectedHost: "flow-aggregator/flow-aggregator",
80+
expectedPort: defaultFlowCollectorPort,
81+
expectedProto: "tcp",
82+
expectedError: nil,
83+
},
7084
{
7185
addr: ":abbbsctp::",
7286
expectedHost: "",

pkg/util/k8s/client.go

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"net"
2020
"os"
21+
"strconv"
2122
"strings"
2223

2324
discovery "k8s.io/api/discovery/v1"
@@ -33,6 +34,7 @@ import (
3334

3435
mcclientset "antrea.io/antrea/multicluster/pkg/client/clientset/versioned"
3536
crdclientset "antrea.io/antrea/pkg/client/clientset/versioned"
37+
"antrea.io/antrea/pkg/util/checks"
3638
)
3739

3840
const (
@@ -125,6 +127,10 @@ func OverrideKubeAPIServer(kubeAPIServerOverride string) {
125127
host = hostPort
126128
port = "443"
127129
}
130+
portNum, err := strconv.Atoi(port)
131+
if !checks.IsValidPort(portNum) || err != nil {
132+
port = "443"
133+
}
128134
os.Setenv(kubeServiceHostEnvKey, host)
129135
os.Setenv(kubeServicePortEnvKey, port)
130136
}

0 commit comments

Comments
 (0)