Fix postgresql host-v6 literals (#46876)

Ipv6 literals in postgresql hosts previously worked and were subsequently
broken. This change fixes the parsing and adds additional test cases.
This commit is contained in:
felixdoerre
2022-03-31 12:45:49 +02:00
committed by GitHub
parent 0d87de153a
commit f5a790e291
2 changed files with 53 additions and 8 deletions

View File

@ -122,18 +122,36 @@ func (s *Service) generateConnectionString(dsInfo sqleng.DataSourceInfo) (string
host = dsInfo.URL
logger.Debug("Generating connection string with Unix socket specifier", "socket", host)
} else {
index := strings.LastIndex(dsInfo.URL, ":")
v6Index := strings.Index(dsInfo.URL, "]")
sp := strings.SplitN(dsInfo.URL, ":", 2)
host = sp[0]
if len(sp) > 1 {
var err error
port, err = strconv.Atoi(sp[1])
if err != nil {
return "", errutil.Wrapf(err, "invalid port in host specifier %q", sp[1])
}
if v6Index == -1 {
if len(sp) > 1 {
var err error
port, err = strconv.Atoi(sp[1])
if err != nil {
return "", errutil.Wrapf(err, "invalid port in host specifier %q", sp[1])
}
logger.Debug("Generating connection string with network host/port pair", "host", host, "port", port)
logger.Debug("Generating connection string with network host/port pair", "host", host, "port", port)
} else {
logger.Debug("Generating connection string with network host", "host", host)
}
} else {
logger.Debug("Generating connection string with network host", "host", host)
if index == v6Index+1 {
host = dsInfo.URL[0:index]
var err error
port, err = strconv.Atoi(dsInfo.URL[index+1:])
if err != nil {
return "", errutil.Wrapf(err, "invalid port in host specifier %q", dsInfo.URL[index+1:])
}
logger.Debug("Generating ipv6 connection string with network host/port pair", "host", host, "port", port)
} else {
host = dsInfo.URL
logger.Debug("Generating ipv6 connection string with network host", "host", host)
}
}
}