Fix intention of support for endpoints without protocols

When we receive an endpoint address without a protocol, our code states we
don't support them and that the format is deprecated.

In reality it was not the case, e.g:

When we received an address in the form of `127.0.0.1`, we'd attempt to
parse the scheme from it, we'd realise is does have one (would be
equivalent to "") and our function `parseEndpoint` would return `"", "", fmt.Error`.

Then, our `parseEndpointWithFallbackProtocol` would use the
fallback protocol (unix) and attempt to connect to `unix://127.0.0.1`.

This meant two things:

1. The error returned from `parseEndpoint` would never be thrown
2. We would connect anyways since the address is valid

This commit changes the assertion logic to match the intention of using
a fallback protocol when one is not supplied.
This commit is contained in:
gotjosh
2018-11-01 20:21:54 +00:00
parent beeb27810c
commit 3faf109b8f

View File

@@ -18,26 +18,32 @@ func dial(addr string, timeout time.Duration) (net.Conn, error) {
}
func getAddressAndDialer(endpoint string) (string, func(addr string, timeout time.Duration) (net.Conn, error), error) {
protocol, addr, err := parseEndpointWithFallbackProtocol(endpoint, unixProtocol)
addr, err := parseEndpointWithFallbackProtocol(endpoint, unixProtocol)
if err != nil {
return "", nil, err
}
if protocol != unixProtocol {
return "", nil, fmt.Errorf("endpoint was not unix socket %v", protocol)
}
return addr, dial, nil
}
func parseEndpointWithFallbackProtocol(endpoint string, fallbackProtocol string) (protocol string, addr string, err error) {
if protocol, addr, err = parseEndpoint(endpoint); err != nil && protocol == "" {
func parseEndpointWithFallbackProtocol(endpoint string, fallbackProtocol string) (addr string, err error) {
var protocol string
protocol, addr, err = parseEndpoint(endpoint)
if err != nil {
return "", err
}
if protocol == "" {
fallbackEndpoint := fallbackProtocol + "://" + endpoint
protocol, addr, err = parseEndpoint(fallbackEndpoint)
_, addr, err = parseEndpoint(fallbackEndpoint)
if err != nil {
return "", "", err
return "", err
}
}
return
return addr, err
}
func parseEndpoint(endpoint string) (string, string, error) {
@@ -47,11 +53,11 @@ func parseEndpoint(endpoint string) (string, string, error) {
}
if u.Scheme == "tcp" {
return "tcp", u.Host, nil
return "tcp", u.Host, fmt.Errorf("endpoint was not unix socket %v", u.Scheme)
} else if u.Scheme == "unix" {
return "unix", u.Path, nil
} else if u.Scheme == "" {
return "", "", fmt.Errorf("Using %q as endpoint is deprecated, please consider using full url format", endpoint)
return "", "", nil
} else {
return u.Scheme, "", fmt.Errorf("protocol %q not supported", u.Scheme)
}