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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
package metadata
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net"
"net/url"
"gitlab.com/gitlab-org/gitaly/internal/backchannel"
"gitlab.com/gitlab-org/gitaly/internal/bootstrap/starter"
"gitlab.com/gitlab-org/gitaly/internal/praefect/config"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
)
const (
// PraefectMetadataKey is the key used to store Praefect server
// information in the gRPC metadata.
PraefectMetadataKey = "gitaly-praefect-server"
)
var (
// ErrPraefectServerNotFound indicates the Praefect server metadata
// could not be found
ErrPraefectServerNotFound = errors.New("metadata for Praefect server not found")
)
// PraefectServer stores parameters required to connect to a Praefect server
type PraefectServer struct {
// BackchannelID identifies the backchannel that corresponds to the Praefect server
// that sent the request and should receive the vote. This field is actually filled
// in by the Gitaly.
BackchannelID backchannel.ID `json:"backchannel_id,omitempty"`
// ListenAddr is the TCP listen address of the Praefect server
ListenAddr string `json:"listen_addr"`
// TLSListenAddr is the TCP listen address of the Praefect server with TLS support
TLSListenAddr string `json:"tls_listen_addr"`
// SocketPath is the Unix socket path of the Praefect server
SocketPath string `json:"socket_path"`
// Token is the token required to authenticate with the Praefect server
Token string `json:"token"`
}
// PraefectFromConfig creates a Praefect server for a given configuration.
func PraefectFromConfig(conf config.Config) (*PraefectServer, error) {
praefectServer := PraefectServer{Token: conf.Auth.Token}
addrBySchema := map[string]*string{
starter.TCP: &praefectServer.ListenAddr,
starter.TLS: &praefectServer.TLSListenAddr,
starter.Unix: &praefectServer.SocketPath,
}
for _, endpoint := range []struct {
schema string
addr string
}{
{schema: starter.TCP, addr: conf.ListenAddr},
{schema: starter.TLS, addr: conf.TLSListenAddr},
{schema: starter.Unix, addr: conf.SocketPath},
} {
if endpoint.addr == "" {
continue
}
parsed, err := starter.ParseEndpoint(endpoint.addr)
if err != nil {
if !errors.Is(err, starter.ErrEmptySchema) {
return nil, err
}
parsed = starter.Config{Name: endpoint.schema, Addr: endpoint.addr}
}
addr, err := parsed.Endpoint()
if err != nil {
return nil, fmt.Errorf("processing of %s: %w", endpoint.schema, err)
}
*addrBySchema[endpoint.schema] = addr
}
return &praefectServer, nil
}
// Inject injects Praefect connection metadata into an incoming context
func (p *PraefectServer) Inject(ctx context.Context) (context.Context, error) {
serialized, err := p.serialize()
if err != nil {
return nil, err
}
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
md = metadata.New(map[string]string{})
} else {
md = md.Copy()
}
md.Set(PraefectMetadataKey, serialized)
return metadata.NewIncomingContext(ctx, md), nil
}
// Resolve Praefect address based on its peer information. Depending on how
// Praefect reached out to us, we'll adjust the PraefectServer to contain
// either its Unix or TCP address.
func (p *PraefectServer) resolvePraefectAddress(peer *peer.Peer) error {
switch addr := peer.Addr.(type) {
case *net.UnixAddr:
if p.SocketPath == "" {
return errors.New("resolvePraefectAddress: got Unix peer but no socket path")
}
p.ListenAddr = ""
p.TLSListenAddr = ""
return nil
case *net.TCPAddr:
var authType string
if peer.AuthInfo != nil {
authType = peer.AuthInfo.AuthType()
}
switch authType {
case "", backchannel.Insecure().Info().SecurityProtocol:
// no transport security being used
addr, err := substituteListeningWithIP(p.ListenAddr, addr.IP.String())
if err != nil {
return fmt.Errorf("resolvePraefectAddress: for ListenAddr: %w", err)
}
p.ListenAddr = addr
p.TLSListenAddr = ""
p.SocketPath = ""
return nil
default:
authType := peer.AuthInfo.AuthType()
if authType != (credentials.TLSInfo{}).AuthType() {
return fmt.Errorf("resolvePraefectAddress: got TCP peer but with unknown transport security type %q", authType)
}
addr, err := substituteListeningWithIP(p.TLSListenAddr, addr.IP.String())
if err != nil {
return fmt.Errorf("resolvePraefectAddress: for TLSListenAddr: %w", err)
}
p.TLSListenAddr = addr
p.ListenAddr = ""
p.SocketPath = ""
return nil
}
default:
return fmt.Errorf("resolvePraefectAddress: unknown peer address scheme: %s", peer.Addr.Network())
}
}
func substituteListeningWithIP(listenAddr, ip string) (string, error) {
if listenAddr == "" {
return "", errors.New("listening address is empty")
}
// We need to replace Praefect's IP address with the peer's
// address as the value we have is from Praefect's configuration,
// which may be a wildcard IP address ("0.0.0.0").
listenURL, err := url.Parse(listenAddr)
if err != nil {
return "", fmt.Errorf("parse listening address %q: %w", listenAddr, err)
}
listenURL.Host = net.JoinHostPort(ip, listenURL.Port())
return listenURL.String(), nil
}
// PraefectFromContext extracts `PraefectServer` from an incoming context. In
// case the metadata key is not set, the function will return `ErrPraefectServerNotFound`.
func PraefectFromContext(ctx context.Context) (*PraefectServer, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, ErrPraefectServerNotFound
}
serialized := md[PraefectMetadataKey]
if len(serialized) == 0 {
return nil, ErrPraefectServerNotFound
}
praefect, err := praefectFromSerialized(serialized[0])
if err != nil {
return nil, err
}
peer, ok := peer.FromContext(ctx)
if !ok {
return nil, fmt.Errorf("PraefectFromContext: could not get peer")
}
if err := praefect.resolvePraefectAddress(peer); err != nil {
return nil, err
}
praefect.BackchannelID, err = backchannel.GetPeerID(ctx)
if err != nil && !errors.Is(err, backchannel.ErrNonMultiplexedConnection) {
return nil, fmt.Errorf("get peer id: %w", err)
}
return praefect, nil
}
func (p *PraefectServer) serialize() (string, error) {
marshalled, err := json.Marshal(p)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(marshalled), nil
}
// praefectFromSerialized creates a Praefect server from a `serialize()`d string.
func praefectFromSerialized(serialized string) (*PraefectServer, error) {
decoded, err := base64.StdEncoding.DecodeString(serialized)
if err != nil {
return nil, err
}
var server PraefectServer
if err := json.Unmarshal(decoded, &server); err != nil {
return nil, err
}
return &server, nil
}
func (p *PraefectServer) Address() (string, error) {
for _, addr := range []string{p.SocketPath, p.TLSListenAddr, p.ListenAddr} {
if addr != "" {
return addr, nil
}
}
return "", errors.New("no address configured")
}
|