mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
fix util for splitting host and port
Now you can provide both a default host and a default port
This commit is contained in:
parent
eb8dfefb23
commit
d433ca7d40
@ -242,10 +242,7 @@ func (ss *SqlStore) buildConnectionString() (string, error) {
|
||||
|
||||
cnnstr += ss.buildExtraConnectionString('&')
|
||||
case migrator.POSTGRES:
|
||||
host, port, err := util.SplitIPPort(ss.dbCfg.Host, "5432")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
host, port := util.SplitHostPortDefault(ss.dbCfg.Host, "127.0.0.1", "5432")
|
||||
if ss.dbCfg.Pwd == "" {
|
||||
ss.dbCfg.Pwd = "''"
|
||||
}
|
||||
|
@ -49,10 +49,7 @@ func generateConnectionString(datasource *models.DataSource) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
server, port, err := util.SplitIPPort(datasource.Url, "1433")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
server, port := util.SplitHostPortDefault(datasource.Url, "localhost", "1433")
|
||||
|
||||
encrypt := datasource.JsonData.Get("encrypt").MustString("false")
|
||||
connStr := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s;",
|
||||
|
@ -1,25 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
// SplitIPPort splits the ip string and port.
|
||||
func SplitIPPort(ipStr string, portDefault string) (ip string, port string, err error) {
|
||||
ipAddr := net.ParseIP(ipStr)
|
||||
|
||||
if ipAddr == nil {
|
||||
// Port was included
|
||||
ip, port, err = net.SplitHostPort(ipStr)
|
||||
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
} else {
|
||||
// No port was included
|
||||
ip = ipAddr.String()
|
||||
port = portDefault
|
||||
}
|
||||
|
||||
return ip, port, nil
|
||||
}
|
@ -7,23 +7,48 @@ import (
|
||||
|
||||
// ParseIPAddress parses an IP address and removes port and/or IPV6 format
|
||||
func ParseIPAddress(input string) string {
|
||||
s := input
|
||||
lastIndex := strings.LastIndex(input, ":")
|
||||
host, _ := SplitHostPort(input)
|
||||
|
||||
if lastIndex != -1 {
|
||||
if lastIndex > 0 && input[lastIndex-1:lastIndex] != ":" {
|
||||
s = input[:lastIndex]
|
||||
}
|
||||
ip := net.ParseIP(host)
|
||||
|
||||
if ip == nil {
|
||||
return host
|
||||
}
|
||||
|
||||
s = strings.Replace(s, "[", "", -1)
|
||||
s = strings.Replace(s, "]", "", -1)
|
||||
|
||||
ip := net.ParseIP(s)
|
||||
|
||||
if ip.IsLoopback() {
|
||||
return "127.0.0.1"
|
||||
}
|
||||
|
||||
return ip.String()
|
||||
}
|
||||
|
||||
// SplitHostPortDefault splits ip address/hostname string by host and port. Defaults used if no match found
|
||||
func SplitHostPortDefault(input, defaultHost, defaultPort string) (host string, port string) {
|
||||
port = defaultPort
|
||||
s := input
|
||||
lastIndex := strings.LastIndex(input, ":")
|
||||
|
||||
if lastIndex != -1 {
|
||||
if lastIndex > 0 && input[lastIndex-1:lastIndex] != ":" {
|
||||
s = input[:lastIndex]
|
||||
port = input[lastIndex+1:]
|
||||
} else if lastIndex == 0 {
|
||||
s = defaultHost
|
||||
port = input[lastIndex+1:]
|
||||
}
|
||||
} else {
|
||||
port = defaultPort
|
||||
}
|
||||
|
||||
s = strings.Replace(s, "[", "", -1)
|
||||
s = strings.Replace(s, "]", "", -1)
|
||||
port = strings.Replace(port, "[", "", -1)
|
||||
port = strings.Replace(port, "]", "", -1)
|
||||
|
||||
return s, port
|
||||
}
|
||||
|
||||
// SplitHostPort splits ip address/hostname string by host and port
|
||||
func SplitHostPort(input string) (host string, port string) {
|
||||
return SplitHostPortDefault(input, "", "")
|
||||
}
|
||||
|
@ -9,8 +9,90 @@ import (
|
||||
func TestParseIPAddress(t *testing.T) {
|
||||
Convey("Test parse ip address", t, func() {
|
||||
So(ParseIPAddress("192.168.0.140:456"), ShouldEqual, "192.168.0.140")
|
||||
So(ParseIPAddress("192.168.0.140"), ShouldEqual, "192.168.0.140")
|
||||
So(ParseIPAddress("[::1:456]"), ShouldEqual, "127.0.0.1")
|
||||
So(ParseIPAddress("[::1]"), ShouldEqual, "127.0.0.1")
|
||||
So(ParseIPAddress("192.168.0.140"), ShouldEqual, "192.168.0.140")
|
||||
So(ParseIPAddress("::1"), ShouldEqual, "127.0.0.1")
|
||||
So(ParseIPAddress("::1:123"), ShouldEqual, "127.0.0.1")
|
||||
})
|
||||
}
|
||||
|
||||
func TestSplitHostPortDefault(t *testing.T) {
|
||||
Convey("Test split ip address to host and port", t, func() {
|
||||
host, port := SplitHostPortDefault("192.168.0.140:456", "", "")
|
||||
So(host, ShouldEqual, "192.168.0.140")
|
||||
So(port, ShouldEqual, "456")
|
||||
|
||||
host, port = SplitHostPortDefault("192.168.0.140", "", "123")
|
||||
So(host, ShouldEqual, "192.168.0.140")
|
||||
So(port, ShouldEqual, "123")
|
||||
|
||||
host, port = SplitHostPortDefault("[::1:456]", "", "")
|
||||
So(host, ShouldEqual, "::1")
|
||||
So(port, ShouldEqual, "456")
|
||||
|
||||
host, port = SplitHostPortDefault("[::1]", "", "123")
|
||||
So(host, ShouldEqual, "::1")
|
||||
So(port, ShouldEqual, "123")
|
||||
|
||||
host, port = SplitHostPortDefault("::1:123", "", "")
|
||||
So(host, ShouldEqual, "::1")
|
||||
So(port, ShouldEqual, "123")
|
||||
|
||||
host, port = SplitHostPortDefault("::1", "", "123")
|
||||
So(host, ShouldEqual, "::1")
|
||||
So(port, ShouldEqual, "123")
|
||||
|
||||
host, port = SplitHostPortDefault(":456", "1.2.3.4", "")
|
||||
So(host, ShouldEqual, "1.2.3.4")
|
||||
So(port, ShouldEqual, "456")
|
||||
|
||||
host, port = SplitHostPortDefault("xyz.rds.amazonaws.com", "", "123")
|
||||
So(host, ShouldEqual, "xyz.rds.amazonaws.com")
|
||||
So(port, ShouldEqual, "123")
|
||||
|
||||
host, port = SplitHostPortDefault("xyz.rds.amazonaws.com:123", "", "")
|
||||
So(host, ShouldEqual, "xyz.rds.amazonaws.com")
|
||||
So(port, ShouldEqual, "123")
|
||||
})
|
||||
}
|
||||
|
||||
func TestSplitHostPort(t *testing.T) {
|
||||
Convey("Test split ip address to host and port", t, func() {
|
||||
host, port := SplitHostPort("192.168.0.140:456")
|
||||
So(host, ShouldEqual, "192.168.0.140")
|
||||
So(port, ShouldEqual, "456")
|
||||
|
||||
host, port = SplitHostPort("192.168.0.140")
|
||||
So(host, ShouldEqual, "192.168.0.140")
|
||||
So(port, ShouldEqual, "")
|
||||
|
||||
host, port = SplitHostPort("[::1:456]")
|
||||
So(host, ShouldEqual, "::1")
|
||||
So(port, ShouldEqual, "456")
|
||||
|
||||
host, port = SplitHostPort("[::1]")
|
||||
So(host, ShouldEqual, "::1")
|
||||
So(port, ShouldEqual, "")
|
||||
|
||||
host, port = SplitHostPort("::1:123")
|
||||
So(host, ShouldEqual, "::1")
|
||||
So(port, ShouldEqual, "123")
|
||||
|
||||
host, port = SplitHostPort("::1")
|
||||
So(host, ShouldEqual, "::1")
|
||||
So(port, ShouldEqual, "")
|
||||
|
||||
host, port = SplitHostPort(":456")
|
||||
So(host, ShouldEqual, "")
|
||||
So(port, ShouldEqual, "456")
|
||||
|
||||
host, port = SplitHostPort("xyz.rds.amazonaws.com")
|
||||
So(host, ShouldEqual, "xyz.rds.amazonaws.com")
|
||||
So(port, ShouldEqual, "")
|
||||
|
||||
host, port = SplitHostPort("xyz.rds.amazonaws.com:123")
|
||||
So(host, ShouldEqual, "xyz.rds.amazonaws.com")
|
||||
So(port, ShouldEqual, "123")
|
||||
})
|
||||
}
|
||||
|
@ -1,43 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestSplitIPPort(t *testing.T) {
|
||||
|
||||
Convey("When parsing an IPv4 without explicit port", t, func() {
|
||||
ip, port, err := SplitIPPort("1.2.3.4", "5678")
|
||||
|
||||
So(err, ShouldEqual, nil)
|
||||
So(ip, ShouldEqual, "1.2.3.4")
|
||||
So(port, ShouldEqual, "5678")
|
||||
})
|
||||
|
||||
Convey("When parsing an IPv6 without explicit port", t, func() {
|
||||
ip, port, err := SplitIPPort("::1", "5678")
|
||||
|
||||
So(err, ShouldEqual, nil)
|
||||
So(ip, ShouldEqual, "::1")
|
||||
So(port, ShouldEqual, "5678")
|
||||
})
|
||||
|
||||
Convey("When parsing an IPv4 with explicit port", t, func() {
|
||||
ip, port, err := SplitIPPort("1.2.3.4:56", "78")
|
||||
|
||||
So(err, ShouldEqual, nil)
|
||||
So(ip, ShouldEqual, "1.2.3.4")
|
||||
So(port, ShouldEqual, "56")
|
||||
})
|
||||
|
||||
Convey("When parsing an IPv6 with explicit port", t, func() {
|
||||
ip, port, err := SplitIPPort("[::1]:56", "78")
|
||||
|
||||
So(err, ShouldEqual, nil)
|
||||
So(ip, ShouldEqual, "::1")
|
||||
So(port, ShouldEqual, "56")
|
||||
})
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user