Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(CSI-226): support IPv6 in APIclient #287

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading