Skip to content

Commit

Permalink
Add port validator to ensure configurable ports are valid
Browse files Browse the repository at this point in the history
Signed-off-by: Lan Luo <lan.luo@broadcom.com>
  • Loading branch information
luolanzone committed Mar 4, 2025
1 parent c33622c commit e6778e0
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 6 deletions.
18 changes: 12 additions & 6 deletions cmd/antrea-agent/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"net"
"os"
"strconv"
"strings"
"time"

Expand All @@ -34,6 +35,7 @@ import (
agentconfig "antrea.io/antrea/pkg/config/agent"
"antrea.io/antrea/pkg/features"
"antrea.io/antrea/pkg/ovs/ovsconfig"
"antrea.io/antrea/pkg/util/checks"
"antrea.io/antrea/pkg/util/env"
"antrea.io/antrea/pkg/util/flowexport"
"antrea.io/antrea/pkg/util/ip"
Expand Down Expand Up @@ -193,7 +195,7 @@ func (o *Options) setDefaults() {
if o.config.OVSRunDir == "" {
o.config.OVSRunDir = ovsconfig.DefaultOVSRunDir
}
if o.config.APIPort == 0 {
if !checks.IsValidPort(o.config.APIPort) {
o.config.APIPort = apis.AntreaAgentAPIPort
}
if o.config.NodeType == "" {
Expand Down Expand Up @@ -430,13 +432,13 @@ func (o *Options) setK8sNodeDefaultOptions() {
if o.config.AntreaProxy.DefaultLoadBalancerMode == "" {
o.config.AntreaProxy.DefaultLoadBalancerMode = config.LoadBalancerModeNAT.String()
}
if o.config.ClusterMembershipPort == 0 {
if !checks.IsValidPort(o.config.ClusterMembershipPort) {
o.config.ClusterMembershipPort = apis.AntreaAgentClusterMembershipPort
}
if o.config.EnablePrometheusMetrics == nil {
o.config.EnablePrometheusMetrics = ptr.To(true)
}
if o.config.WireGuard.Port == 0 {
if !checks.IsValidPort(o.config.WireGuard.Port) {
o.config.WireGuard.Port = apis.WireGuardListenPort
}

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

if o.config.DNSServerOverride != "" {
hostPort := ip.AppendPortIfMissing(o.config.DNSServerOverride, "53")
_, _, err := net.SplitHostPort(hostPort)
if err != nil {
_, port, err := net.SplitHostPort(hostPort)
portNum, parseErr := strconv.Atoi(port)
if err != nil || !checks.IsValidPort(portNum) || parseErr != nil {
return fmt.Errorf("dnsServerOverride %s is invalid: %v", o.config.DNSServerOverride, err)
}
o.dnsServerOverride = hostPort
Expand Down Expand Up @@ -706,7 +712,7 @@ func (o *Options) setExternalNodeDefaultOptions() {
func (o *Options) setMulticlusterDefaultOptions() {
_, trafficEncryptionModeType := config.GetTrafficEncryptionModeFromStr(o.config.Multicluster.TrafficEncryptionMode)
if trafficEncryptionModeType == config.TrafficEncryptionModeWireGuard {
if o.config.Multicluster.WireGuard.Port == 0 {
if !checks.IsValidPort(o.config.Multicluster.WireGuard.Port) {
o.config.Multicluster.WireGuard.Port = apis.MulticlusterWireGuardListenPort
}
}
Expand Down
1 change: 1 addition & 0 deletions cmd/antrea-agent/options_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestMulticlusterOptions(t *testing.T) {
FeatureGates: map[string]bool{"Multicluster": tt.featureGate},
TrafficEncapMode: tt.encapMode,
Multicluster: tt.mcConfig,
TunnelPort: 6081,
}
if tt.encryptionMode != "" {
config.TrafficEncryptionMode = tt.encryptionMode
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/checks/checks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package checks

// IsValidPort checks if the given port number is within the valid range of 1 to 65535.
func IsValidPort(port int) bool {
if port < 1 || port > 65535 {
return false
}
return true
}
38 changes: 38 additions & 0 deletions pkg/util/checks/checks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package checks

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsValidPort(t *testing.T) {
tests := []struct {
name string
port int
expected bool
}{
{
name: "invalid port 0",
port: 0,
expected: false,
},
{
name: "invalid port 70000",
port: 70000,
expected: false,
},
{
name: "valid port",
port: 65500,
expected: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := IsValidPort(tc.port)
assert.Equal(t, tc.expected, result)
})
}
}
6 changes: 6 additions & 0 deletions pkg/util/flowexport/flowexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ package flowexport
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"

flowaggregatorconfig "antrea.io/antrea/pkg/config/flowaggregator"
"antrea.io/antrea/pkg/util/checks"
)

// ParseFlowCollectorAddr parses the flow collector address input for flow exporter and aggregator
Expand All @@ -44,6 +46,10 @@ func ParseFlowCollectorAddr(addr string, defaultPort string, defaultProtocol str
port = defaultPort
} else {
port = strSlice[1]
portNum, err := strconv.Atoi(port)
if !checks.IsValidPort(portNum) || err != nil {
port = defaultPort
}
}
if (strSlice[2] != "tls") && (strSlice[2] != "tcp") && (strSlice[2] != "udp") {
return host, port, proto, fmt.Errorf("connection over %s transport proto is not supported", strSlice[2])
Expand Down
14 changes: 14 additions & 0 deletions pkg/util/flowexport/flowexport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ func TestParseFlowCollectorAddr(t *testing.T) {
expectedProto: "tcp",
expectedError: nil,
},
{
addr: "flow-aggregator/flow-aggregator:str:tcp",
expectedHost: "flow-aggregator/flow-aggregator",
expectedPort: defaultFlowCollectorPort,
expectedProto: "tcp",
expectedError: nil,
},
{
addr: "flow-aggregator/flow-aggregator:78900:tcp",
expectedHost: "flow-aggregator/flow-aggregator",
expectedPort: defaultFlowCollectorPort,
expectedProto: "tcp",
expectedError: nil,
},
{
addr: ":abbbsctp::",
expectedHost: "",
Expand Down
6 changes: 6 additions & 0 deletions pkg/util/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"net"
"os"
"strconv"
"strings"

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

mcclientset "antrea.io/antrea/multicluster/pkg/client/clientset/versioned"
crdclientset "antrea.io/antrea/pkg/client/clientset/versioned"
"antrea.io/antrea/pkg/util/checks"
)

const (
Expand Down Expand Up @@ -125,6 +127,10 @@ func OverrideKubeAPIServer(kubeAPIServerOverride string) {
host = hostPort
port = "443"
}
portNum, err := strconv.Atoi(port)
if !checks.IsValidPort(portNum) || err != nil {
port = "443"
}
os.Setenv(kubeServiceHostEnvKey, host)
os.Setenv(kubeServicePortEnvKey, port)
}
Expand Down

0 comments on commit e6778e0

Please sign in to comment.