Skip to content

Commit

Permalink
Replace the net.ParseIP with netip.ParseAddr for IP address parsing. (e…
Browse files Browse the repository at this point in the history
…nvoyproxy#2447)

replace the net.ParseIP with netip.ParseAddr for IP address parsing.
Signed-off-by: zhlsunshine <[email protected]>
  • Loading branch information
zhlsunshine authored Jan 16, 2024
1 parent e2c0157 commit 9ed8b20
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions api/v1alpha1/validation/envoyproxy_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package validation
import (
"errors"
"fmt"
"net"
"net/netip"
"reflect"

bootstrapv3 "github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v3"
Expand Down Expand Up @@ -103,7 +103,7 @@ func validateService(spec *egv1a1.EnvoyProxySpec) []error {
errs = append(errs, fmt.Errorf("loadBalancerIP can only be set for %v type", egv1a1.ServiceTypeLoadBalancer))
}

if ip := net.ParseIP(*serviceLoadBalancerIP); ip == nil || ip.To4() == nil {
if ip, err := netip.ParseAddr(*serviceLoadBalancerIP); err != nil || !ip.Unmap().Is4() {
errs = append(errs, fmt.Errorf("loadBalancerIP:%s is an invalid IPv4 address", *serviceLoadBalancerIP))
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/gatewayapi/securitypolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ package gatewayapi
import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/netip"
"net/url"
"sort"
"strconv"
Expand Down Expand Up @@ -578,8 +578,8 @@ func validateTokenEndpoint(tokenEndpoint string) error {
return fmt.Errorf("token endpoint URL scheme must be https: %s", tokenEndpoint)
}

if ip := net.ParseIP(parsedURL.Hostname()); ip != nil {
if v4 := ip.To4(); v4 != nil {
if ip, err := netip.ParseAddr(parsedURL.Hostname()); err == nil {
if ip.Unmap().Is4() {
return fmt.Errorf("token endpoint URL must be a domain name: %s", tokenEndpoint)
}
}
Expand Down
12 changes: 6 additions & 6 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ package ir
import (
"cmp"
"errors"
"net"
"net/http"
"net/netip"
"reflect"

"github.com/tetratelabs/multierror"
Expand Down Expand Up @@ -217,7 +217,7 @@ func (h HTTPListener) Validate() error {
if h.Name == "" {
errs = multierror.Append(errs, ErrListenerNameEmpty)
}
if ip := net.ParseIP(h.Address); ip == nil {
if _, err := netip.ParseAddr(h.Address); err != nil {
errs = multierror.Append(errs, ErrListenerAddressInvalid)
}
if h.Port == 0 {
Expand Down Expand Up @@ -714,9 +714,9 @@ func (d DestinationEndpoint) Validate() error {
var errs error

err := validation.IsDNS1123Subdomain(d.Host)
ip := net.ParseIP(d.Host)
_, pErr := netip.ParseAddr(d.Host)

if err != nil && ip == nil {
if err != nil && pErr != nil {
errs = multierror.Append(errs, ErrDestEndpointHostInvalid)
}

Expand Down Expand Up @@ -941,7 +941,7 @@ func (h TCPListener) Validate() error {
if h.Name == "" {
errs = multierror.Append(errs, ErrListenerNameEmpty)
}
if ip := net.ParseIP(h.Address); ip == nil {
if _, err := netip.ParseAddr(h.Address); err != nil {
errs = multierror.Append(errs, ErrListenerAddressInvalid)
}
if h.Port == 0 {
Expand Down Expand Up @@ -1005,7 +1005,7 @@ func (h UDPListener) Validate() error {
if h.Name == "" {
errs = multierror.Append(errs, ErrListenerNameEmpty)
}
if ip := net.ParseIP(h.Address); ip == nil {
if _, err := netip.ParseAddr(h.Address); err != nil {
errs = multierror.Append(errs, ErrListenerAddressInvalid)
}
if h.Port == 0 {
Expand Down
6 changes: 3 additions & 3 deletions internal/xds/translator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package translator
import (
"errors"
"fmt"
"net"
"net/netip"
"net/url"
"strconv"
"strings"
Expand Down Expand Up @@ -63,8 +63,8 @@ func url2Cluster(strURL string, secure bool) (*urlCluster, error) {

name := fmt.Sprintf("%s_%d", strings.ReplaceAll(u.Hostname(), ".", "_"), port)

if ip := net.ParseIP(u.Hostname()); ip != nil {
if v4 := ip.To4(); v4 != nil {
if ip, err := netip.ParseAddr(u.Hostname()); err == nil {
if ip.Unmap().Is4() {
epType = EndpointTypeStatic
}
}
Expand Down

0 comments on commit 9ed8b20

Please sign in to comment.