Merge pull request #15397 from stonezdj/21aug4_fix_ldap_ipv6

Support IPv6 when connecting to LDAP server
This commit is contained in:
stonezdj(Daojun Zhang) 2021-08-09 12:47:20 +08:00 committed by GitHub
commit d7c8c722f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 {