diff options
author | Andrew Newdigate <andrew@gitlab.com> | 2019-01-09 23:07:29 +0300 |
---|---|---|
committer | Andrew Newdigate <andrew@gitlab.com> | 2019-01-14 14:39:32 +0300 |
commit | 7596a62637a23caf2e9e9451e6fc1d6cf12b6792 (patch) | |
tree | edf7b14db6fa510cea0b252d9669bc4bbfd6c039 /client/dial.go | |
parent | e8bb2b1482860ed18b6a42ab48bc882c1089df2f (diff) |
Reintroduce a specific dialler for unix sockets
61f6c92779a70d577727e7eefa337409effd69ef removed the Dialer for unix
socket. This was done because a change to the GRPC library caused the
Dialer to stop working, and because the default implementation works as
expected for users not using a proxy.
Unfortunately this led to a regression for users with HTTP or
HTTPS proxy configurations exposed via the `http_proxy` or `https_proxy`
environment variables.
For this reason, we reintroduce the dialer for Unix socket connections.
Diffstat (limited to 'client/dial.go')
-rw-r--r-- | client/dial.go | 70 |
1 files changed, 62 insertions, 8 deletions
diff --git a/client/dial.go b/client/dial.go index d0a51c0c1..fe4a3e683 100644 --- a/client/dial.go +++ b/client/dial.go @@ -1,6 +1,10 @@ package client import ( + "fmt" + "net" + "time" + "google.golang.org/grpc/credentials" "net/url" @@ -11,14 +15,30 @@ import ( // DefaultDialOpts hold the default DialOptions for connection to Gitaly over UNIX-socket var DefaultDialOpts = []grpc.DialOption{} +type connectionType int + +const ( + invalidConnection connectionType = iota + tcpConnection + tlsConnection + unixConnection +) + // Dial gitaly func Dial(rawAddress string, connOpts []grpc.DialOption) (*grpc.ClientConn, error) { - canonicalAddress, err := parseAddress(rawAddress) - if err != nil { - return nil, err - } + var canonicalAddress string + var err error + + switch getConnectionType(rawAddress) { + case invalidConnection: + return nil, fmt.Errorf("invalid connection string: %s", rawAddress) + + case tlsConnection: + canonicalAddress, err = extractHostFromRemoteURL(rawAddress) // Ensure the form: "host:port" ... + if err != nil { + return nil, err + } - if isTLS(rawAddress) { certPool, err := systemCertPool() if err != nil { return nil, err @@ -26,8 +46,29 @@ func Dial(rawAddress string, connOpts []grpc.DialOption) (*grpc.ClientConn, erro creds := credentials.NewClientTLSFromCert(certPool, "") connOpts = append(connOpts, grpc.WithTransportCredentials(creds)) - } else { + + case tcpConnection: + canonicalAddress, err = extractHostFromRemoteURL(rawAddress) // Ensure the form: "host:port" ... + if err != nil { + return nil, err + } connOpts = append(connOpts, grpc.WithInsecure()) + + case unixConnection: + canonicalAddress = rawAddress // This will be overriden by the custom dialer... + connOpts = append( + connOpts, + grpc.WithInsecure(), + grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) { + path, err := extractPathFromSocketURL(addr) + if err != nil { + return nil, err + } + + return net.DialTimeout("unix", path, timeout) + }), + ) + } conn, err := grpc.Dial(canonicalAddress, connOpts...) @@ -38,7 +79,20 @@ func Dial(rawAddress string, connOpts []grpc.DialOption) (*grpc.ClientConn, erro return conn, nil } -func isTLS(rawAddress string) bool { +func getConnectionType(rawAddress string) connectionType { u, err := url.Parse(rawAddress) - return err == nil && u.Scheme == "tls" + if err != nil { + return invalidConnection + } + + switch u.Scheme { + case "tls": + return tlsConnection + case "unix": + return unixConnection + case "tcp": + return tcpConnection + default: + return invalidConnection + } } |