Support IPv6 when connecting to LDAP server

Use net.SplitHostPort instead of strings.Split
 Fixes #15354

Signed-off-by: stonezdj <stonezdj@gmail.com>
This commit is contained in:
stonezdj 2021-08-04 12:51:10 +08:00
parent 9e11753949
commit 409039b502
2 changed files with 10 additions and 9 deletions

View File

@ -20,8 +20,8 @@ import (
"fmt"
"github.com/goharbor/harbor/src/lib/config/models"
"github.com/goharbor/harbor/src/pkg/ldap/model"
"net"
"net/url"
"strconv"
"strings"
"time"
@ -74,7 +74,6 @@ func NewSession(basicCfg models.LdapConf, groupCfg models.GroupConf) *Session {
func formatURL(ldapURL string) (string, error) {
var protocol, hostport string
_, err := url.Parse(ldapURL)
if err != nil {
return "", fmt.Errorf("parse Ldap Host ERR: %s", err)
@ -92,12 +91,11 @@ func formatURL(ldapURL string) (string, error) {
}
if strings.Contains(hostport, ":") {
splitHostPort := strings.Split(hostport, ":")
port, err := strconv.Atoi(splitHostPort[1])
_, port, err := net.SplitHostPort(hostport)
if err != nil {
return "", fmt.Errorf("illegal url port")
return "", fmt.Errorf("illegal ldap url, error: %v", err)
}
if port == 636 {
if port == "636" {
protocol = "ldaps"
}
@ -198,9 +196,12 @@ func (s *Session) Open() error {
return err
}
splitLdapURL := strings.Split(ldapURL, "://")
protocol, hostport := splitLdapURL[0], splitLdapURL[1]
host := strings.Split(hostport, ":")[0]
protocol, hostport := splitLdapURL[0], splitLdapURL[1]
host, _, err := net.SplitHostPort(hostport)
if err != nil {
return err
}
connectionTimeout := s.basicCfg.ConnectionTimeout
goldap.DefaultTimeout = time.Duration(connectionTimeout) * time.Second

View File

@ -102,7 +102,7 @@ func TestFormatURL(t *testing.T) {
{"ldaps://127.0.0.1:389", "ldaps://127.0.0.1:389"},
{"ldap://127.0.0.1:636", "ldaps://127.0.0.1:636"},
{"112.122.122.122", "ldap://112.122.122.122:389"},
{"ldap:\\wrong url", ""},
{"ldap://[2001:db8::1]:389", "ldap://[2001:db8::1]:389"},
}
for _, u := range urls {