1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
package client
import (
"context"
"fmt"
"net"
"net/url"
"time"
gitaly_x509 "gitlab.com/gitlab-org/gitaly/internal/x509"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
)
// 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
)
func DialContext(ctx context.Context, rawAddress string, connOpts []grpc.DialOption) (*grpc.ClientConn, error) {
var canonicalAddress string
var err error
switch getConnectionType(rawAddress) {
case invalidConnection:
return nil, fmt.Errorf("invalid connection string: %q", rawAddress)
case tlsConnection:
canonicalAddress, err = extractHostFromRemoteURL(rawAddress) // Ensure the form: "host:port" ...
if err != nil {
return nil, fmt.Errorf("failed to extract host for 'tls' connection: %w", err)
}
certPool, err := gitaly_x509.SystemCertPool()
if err != nil {
return nil, fmt.Errorf("failed to get system certificat pool for 'tls' connection: %w", err)
}
creds := credentials.NewClientTLSFromCert(certPool, "")
connOpts = append(connOpts, grpc.WithTransportCredentials(creds))
case tcpConnection:
canonicalAddress, err = extractHostFromRemoteURL(rawAddress) // Ensure the form: "host:port" ...
if err != nil {
return nil, fmt.Errorf("failed to extract host for 'tcp' connection: %w", err)
}
connOpts = append(connOpts, grpc.WithInsecure())
case unixConnection:
canonicalAddress = rawAddress // This will be overridden by the custom dialer...
connOpts = append(
connOpts,
grpc.WithInsecure(),
// Use a custom dialer to ensure that we don't experience
// issues in environments that have proxy configurations
// https://gitlab.com/gitlab-org/gitaly/merge_requests/1072#note_140408512
grpc.WithContextDialer(func(ctx context.Context, addr string) (conn net.Conn, err error) {
path, err := extractPathFromSocketURL(addr)
if err != nil {
return nil, fmt.Errorf("failed to extract host for 'unix' connection: %w", err)
}
d := net.Dialer{}
return d.DialContext(ctx, "unix", path)
}),
)
}
// grpc.KeepaliveParams must be specified at least as large as what is allowed by the
// server-side grpc.KeepaliveEnforcementPolicy
connOpts = append(connOpts, grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 20 * time.Second,
PermitWithoutStream: true,
}))
conn, err := grpc.DialContext(ctx, canonicalAddress, connOpts...)
if err != nil {
return nil, fmt.Errorf("failed to dial %q connection: %w", canonicalAddress, err)
}
return conn, nil
}
func Dial(rawAddress string, connOpts []grpc.DialOption) (*grpc.ClientConn, error) {
return DialContext(context.Background(), rawAddress, connOpts)
}
func getConnectionType(rawAddress string) connectionType {
u, err := url.Parse(rawAddress)
if err != nil {
return invalidConnection
}
switch u.Scheme {
case "tls":
return tlsConnection
case "unix":
return unixConnection
case "tcp":
return tcpConnection
default:
return invalidConnection
}
}
|