Skip to content

Commit

Permalink
fix(CSI-226): support IPv6 in APIclient
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyberezansky committed Jul 29, 2024
1 parent 59acf13 commit 05baecc
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions pkg/wekafs/apiclient/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -112,17 +113,50 @@ func NewApiClient(ctx context.Context, credentials Credentials, allowInsecureHtt
return a, nil
}

func isValidIPv6Address(ipStr string) bool {
ip := net.ParseIP(ipStr)
return ip != nil && ip.To4() == nil && ip.To16() != nil
}

func isValidIPv4Address(ipStr string) bool {
ip := net.ParseIP(ipStr)
return ip != nil && ip.To4() != nil
}

func isValidHostname(hostname string) bool {
if len(hostname) > 253 {
return false
}

// Regex to match the general structure of a hostname.
// Each label must start and end with an alphanumeric character,
// may contain hyphens, and be 1 to 63 characters long.
hostnameRegex := regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]$`)

return hostnameRegex.MatchString(hostname)
}

func (a *ApiClient) resetDefaultEndpoints(ctx context.Context) {
actualEndPoints := make(map[string]*ApiEndPoint)
for _, e := range a.Credentials.Endpoints {

split := strings.Split(e, ":")
ip := split[0]
ip := ""
port := "14000" // default port

// if there is a port number in the endpoint, use it
if len(split) > 1 {
port = split[1]
port = split[len(split)-1]
ip = strings.Join(split[:len(split)-1], ":")
} else {
ip = split[0]
}

if !isValidIPv4Address(ip) && !isValidIPv6Address(ip) && !isValidHostname(ip) {
log.Ctx(ctx).Error().Str("ip", ip).Msg("Cannot determine a valid hostname, IPv4 or IPv6 address, skipping endpoint")
continue
}

portNum, err := strconv.Atoi(port)
if err != nil {
log.Ctx(ctx).Error().Err(err).Str("port", port).Msg("Failed to parse port number, using default")
Expand Down

0 comments on commit 05baecc

Please sign in to comment.