Newer
Older
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
package proxy
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"time"
"git.tu-berlin.de/mcc-fred/fred/proto/client"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
)
type APIProxy struct {
p *Proxy
port int
conn map[string]client.ClientClient
opts grpc.DialOption
roots *x509.CertPool
}
func StartAPIProxy(p *Proxy, port int, cert string, key string, caCert string) (*grpc.Server, error) {
// Load server's certificate and private key
serverCert, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
return nil, err
}
// Create a new cert pool and add our own CA certificate
rootCAs := x509.NewCertPool()
loaded, err := ioutil.ReadFile(caCert)
if err != nil {
return nil, err
}
rootCAs.AppendCertsFromPEM(loaded)
// Create the credentials and return it
config := &tls.Config{
Certificates: []tls.Certificate{serverCert},
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: rootCAs,
RootCAs: rootCAs,
MinVersion: tls.VersionTLS12,
}
a := &APIProxy{
p: p,
port: port,
conn: make(map[string]client.ClientClient),
opts: grpc.WithTransportCredentials(credentials.NewTLS(config)),
roots: rootCAs,
}
s := grpc.NewServer(grpc.Creds(credentials.NewTLS(config)))
client.RegisterClientServer(s, a)
return s, nil
}
func (a *APIProxy) getConn(keygroup string) (client.ClientClient, error) {
host := a.p.getHost(keygroup)
if c, ok := a.conn[host]; ok {
return c, nil
}
conn, err := grpc.Dial(fmt.Sprintf("%s:%d", host, a.port), a.opts)
if err != nil {
return nil, err
}
c := client.NewClientClient(conn)
a.conn[host] = c
return c, nil
}
func (a *APIProxy) getAny() (client.ClientClient, error) {
host := a.p.getAny()
if c, ok := a.conn[host]; ok {
return c, nil
}
conn, err := grpc.Dial(fmt.Sprintf("%s:%d", host, a.port), a.opts)
if err != nil {
return nil, err
}
c := client.NewClientClient(conn)
a.conn[host] = c
return c, nil
}
func (a *APIProxy) addUserHeader(ctx context.Context) (context.Context, error) {
// since we pass each request on to the backend with our proxy, we lose user information
// we add that back on by adding the "user" header to the context
// get peer information
p, ok := peer.FromContext(ctx)
if !ok {
return ctx, fmt.Errorf("no peer found")
}
// get TLS credential information for this connection
tlsAuth, ok := p.AuthInfo.(credentials.TLSInfo)
if !ok {
return ctx, fmt.Errorf("unexpected peer transport credentials")
}
// check that the certificate exists
if len(tlsAuth.State.VerifiedChains) == 0 || len(tlsAuth.State.VerifiedChains[0]) == 0 {
return ctx, fmt.Errorf("could not verify peer certificate: %v", tlsAuth.State)
}
host, _, err := net.SplitHostPort(p.Addr.String())
if err != nil {
return ctx, err
}
// verify the certificate:
// IF we are not proxied and communicate with the client directly:
// 1) it should be issued by a CA in our root CA pool
// 2) any intermediates are valid for us
// 3) the certificate should be valid for client authentication
// 4) the certificate should have the clients address as a SAN
_, err = tlsAuth.State.VerifiedChains[0][0].Verify(x509.VerifyOptions{
Roots: a.roots,
CurrentTime: time.Now(),
Intermediates: x509.NewCertPool(),
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
DNSName: host,
})
if err != nil {
return ctx, err
}
// Check subject common name exists and return it as the user name for that client
name := tlsAuth.State.VerifiedChains[0][0].Subject.CommonName
if name == "" {
return ctx, fmt.Errorf("invalid subject common name")
}
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return ctx, fmt.Errorf("no metadata could be found for proxied request")
}
newMD := md.Copy()
newMD.Set("user", name)
return metadata.NewOutgoingContext(ctx, newMD), nil
}
// CreateKeygroup calls this method on the exthandler
func (a *APIProxy) CreateKeygroup(ctx context.Context, req *client.CreateKeygroupRequest) (*client.Empty, error) {
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.CreateKeygroup(ctx, req)
}
// DeleteKeygroup calls this method on the exthandler
func (a *APIProxy) DeleteKeygroup(ctx context.Context, req *client.DeleteKeygroupRequest) (*client.Empty, error) {
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.DeleteKeygroup(ctx, req)
}
// Read calls this method on the exthandler
func (a *APIProxy) Read(ctx context.Context, req *client.ReadRequest) (*client.ReadResponse, error) {
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.Read(ctx, req)
}
// Scan calls this method on the exthandler
func (a *APIProxy) Scan(ctx context.Context, req *client.ScanRequest) (*client.ScanResponse, error) {
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.Scan(ctx, req)
}
// Append calls this method on the exthandler
func (a *APIProxy) Append(ctx context.Context, req *client.AppendRequest) (*client.AppendResponse, error) {
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.Append(ctx, req)
}
// Update calls this method on the exthandler
func (a *APIProxy) Update(ctx context.Context, req *client.UpdateRequest) (*client.UpdateResponse, error) {
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.Update(ctx, req)
}
// Delete calls this method on the exthandler
func (a *APIProxy) Delete(ctx context.Context, req *client.DeleteRequest) (*client.DeleteResponse, error) {
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.Delete(ctx, req)
}
// AddReplica calls this method on the exthandler
func (a *APIProxy) AddReplica(ctx context.Context, req *client.AddReplicaRequest) (*client.Empty, error) {
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.AddReplica(ctx, req)
}
// GetKeygroupReplica calls this method on the exthandler
func (a *APIProxy) GetKeygroupReplica(ctx context.Context, req *client.GetKeygroupReplicaRequest) (*client.GetKeygroupReplicaResponse, error) {
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.GetKeygroupReplica(ctx, req)
}
// RemoveReplica calls this method on the exthandler
func (a *APIProxy) RemoveReplica(ctx context.Context, req *client.RemoveReplicaRequest) (*client.Empty, error) {
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.RemoveReplica(ctx, req)
}
// GetReplica calls this method on the exthandler
func (a *APIProxy) GetReplica(ctx context.Context, req *client.GetReplicaRequest) (*client.GetReplicaResponse, error) {
c, err := a.getAny()
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.GetReplica(ctx, req)
}
// GetAllReplica calls this method on the exthandler
func (a *APIProxy) GetAllReplica(ctx context.Context, req *client.Empty) (*client.GetAllReplicaResponse, error) {
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
c, err := a.getAny()
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.GetAllReplica(ctx, req)
}
// GetKeygroupTriggers calls this method on the exthandler
func (a *APIProxy) GetKeygroupTriggers(ctx context.Context, req *client.GetKeygroupTriggerRequest) (*client.GetKeygroupTriggerResponse, error) {
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.GetKeygroupTriggers(ctx, req)
}
// AddTrigger calls this method on the exthandler
func (a *APIProxy) AddTrigger(ctx context.Context, req *client.AddTriggerRequest) (*client.Empty, error) {
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.AddTrigger(ctx, req)
}
// RemoveTrigger calls this method on the exthandler
func (a *APIProxy) RemoveTrigger(ctx context.Context, req *client.RemoveTriggerRequest) (*client.Empty, error) {
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.RemoveTrigger(ctx, req)
}
// AddUser calls this method on the exthandler
func (a *APIProxy) AddUser(ctx context.Context, req *client.AddUserRequest) (*client.Empty, error) {
c, err := a.getConn(req.Keygroup)
if err != nil {
return nil, err
}
ctx, err = a.addUserHeader(ctx)
if err != nil {
return nil, err
}
return c.AddUser(ctx, req)
}
// RemoveUser calls this method on the exthandler
func (a *APIProxy) RemoveUser(ctx context.Context, req *client.RemoveUserRequest) (*client.Empty, error) {