-
Notifications
You must be signed in to change notification settings - Fork 63
/
peering.go
410 lines (355 loc) · 10.1 KB
/
peering.go
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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
328
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
360
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package peering
import (
"context"
"crypto/ecdsa"
"crypto/x509/pkix"
"database/sql"
"fmt"
"math/rand"
"regexp"
"time"
"github.com/dgraph-io/ristretto"
"github.com/paralus/paralus/pkg/log"
"github.com/paralus/paralus/pkg/query"
"github.com/paralus/paralus/pkg/sentry/cryptoutil"
"github.com/paralus/paralus/pkg/sentry/register"
"github.com/paralus/paralus/pkg/service"
sentryrpc "github.com/paralus/paralus/proto/rpc/sentry"
"github.com/paralus/paralus/proto/types/sentry"
"github.com/rs/xid"
)
var (
_log = log.GetLogger()
)
// RelayClusterConnectionInfo relay conn info
type RelayClusterConnectionInfo struct {
Relayuuid string
Relayip string
}
type OnEvict = func(item *ristretto.Item)
// InitPeerCache initialize the cache to store dialin cluster-connection
// information of peers. When a dialin miss happens look into this cache
// to find the peer IP address to forward the user connection.
func InitPeerCache(evict OnEvict) (*ristretto.Cache, error) {
return ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // Num keys to track frequency of (10M).
MaxCost: 1 << 30, // Maximum cost of cache (1GB).
BufferItems: 64, // Number of keys per Get buffer.
OnEvict: evict,
})
}
// InsertPeerCache inserts the value to cache
func InsertPeerCache(cache *ristretto.Cache, expiry time.Duration, key, value interface{}) bool {
return cache.SetWithTTL(key, value, 100, expiry)
}
// GetPeerCache get value from cache and if more than 1
// rnadomly select the peer
func GetPeerCache(cache *ristretto.Cache, key interface{}) (string, bool) {
value, found := cache.Get(key)
if found {
if value == nil {
return "", false
}
cacheItems := value.([]RelayClusterConnectionInfo)
cnt := len(cacheItems)
if cnt > 1 {
rand.Seed(time.Now().UnixNano())
min := 0
max := cnt - 1
indx := rand.Intn(max-min+1) + min
return cacheItems[indx].Relayip, found
}
return cacheItems[0].Relayip, found
}
return "", found
}
// sends periodic heartbeats to core service
func helloRPCSend(ctx context.Context, stream sentryrpc.RelayPeerService_RelayPeerHelloRPCClient, interval time.Duration, relayUUID string, ip func() string) {
_log.Infow("send first hello")
msg := &sentryrpc.PeerHelloRequest{
Relayuuid: relayUUID,
Relayip: ip(),
}
// send first hello message
err := stream.Send(msg)
if err != nil {
_log.Errorw("failed to send hello message", "error", err)
return
}
tick := time.NewTicker(interval)
defer tick.Stop()
helloRPCSendLoop:
for {
select {
case <-ctx.Done():
stream.CloseSend()
break helloRPCSendLoop
case <-tick.C:
err := stream.Send(msg)
if err != nil {
_log.Errorw("failed to send hello message", err)
break helloRPCSendLoop
}
}
}
_log.Debugw("Exit: helloRPCSendLoop")
}
// ClientHelloRPC will handle periodic heartbeat messages between relay and the core service.
func ClientHelloRPC(ctx context.Context, stream sentryrpc.RelayPeerService_RelayPeerHelloRPCClient, interval time.Duration, relayUUID string, ip func() string) {
go helloRPCSend(ctx, stream, interval, relayUUID, ip)
for {
in, err := stream.Recv()
if err != nil {
_log.Errorw("helloRPC stream recv error", err)
stream.CloseSend()
break
}
_log.Debugw("recvd hello resp from service",
"serviceip", in.GetServiceip(),
"serviceuuid", in.GetServiceuuid(),
)
}
_log.Debugw("stopping helloRPC routine")
}
// send loop of probe rpc. recvs clustersni from PeerProbeChanel and sends to core service
func probeRPCSend(ctx context.Context, stream sentryrpc.RelayPeerService_RelayPeerProbeRPCClient, relayUUID string, peerProbeChanel chan string) {
probeRPCSendLoop:
for {
select {
case clustersni := <-peerProbeChanel:
_log.Debugw("probeRPCSend", "clustersni", clustersni)
msg := &sentryrpc.PeerProbeRequest{
Relayuuid: relayUUID,
Clustersni: clustersni,
}
err := stream.Send(msg)
if err != nil {
_log.Errorw(
"failed to send probe message for ",
"clustersni", clustersni,
"error", err,
)
stream.CloseSend()
break probeRPCSendLoop
}
case <-ctx.Done():
stream.CloseSend()
break probeRPCSendLoop
}
}
_log.Debugw("exit: probeRPCSendLoop")
}
// ClientProbeRPC will manage the probes.
// When a relay neeed to probe for connections for a cluster
// it will add a probe message to probe channel. The probe rpc
// will send that message to probe core service. When a probe response
// get inserted to peerCache.
func ClientProbeRPC(ctx context.Context, stream sentryrpc.RelayPeerService_RelayPeerProbeRPCClient, pcache *ristretto.Cache, relayUUID string, expiry time.Duration, peerProbeChanel chan string, ip func() string) {
//send message with empty cluster sni to init the chanl
msg := &sentryrpc.PeerProbeRequest{
Relayuuid: relayUUID,
Clustersni: "",
}
err := stream.Send(msg)
if err != nil {
stream.CloseSend()
return
}
_log.Debugw("probeRPC send first", "msg", msg)
go probeRPCSend(ctx, stream, relayUUID, peerProbeChanel)
//probe response recv loop will process the response
//and push it into peer cache
for {
resp, err := stream.Recv()
if err != nil {
stream.CloseSend()
break
}
clustersni := resp.GetClustersni()
items := resp.GetItems()
if clustersni != "" && items != nil && len(items) > 0 {
cachevalue := []RelayClusterConnectionInfo{}
//prepare cachevalue
for _, item := range items {
matched, err := regexp.Match(relayUUID, []byte(item.Relayuuid))
if err == nil && matched {
_log.Errorw("skip duplicate probe resp",
"relayuuid", relayUUID,
"recvd-relayuuid", item.Relayuuid,
)
//uuid is same as this relay skip this entry
continue
}
ipAddr := ip()
if ipAddr != "" && ipAddr == item.Relayip {
_log.Errorw("skip duplicate probe resp", "ip address", item.Relayip)
//ip is same as this relay skip this entry
continue
}
v := RelayClusterConnectionInfo{item.Relayuuid, item.Relayip}
cachevalue = append(cachevalue, v)
}
_log.Infow(
"cache probeRPC response",
"key", clustersni,
"value", cachevalue,
)
//insert to peer cache
if !InsertPeerCache(pcache, expiry, clustersni, cachevalue) {
_log.Errorw(
"failed cache probeRPC response",
"key", clustersni,
"value", cachevalue,
)
}
} else {
_log.Errorw(
"prob response with empty items for ", clustersni,
)
}
}
_log.Debug(
"stopping probeRPC routine",
)
}
// ClientSurveyRPC will handle the survey RPC.
// When a relay neeed to probe for connections for a cluster
// it will message to probe core service. The service then sends
// survey to all the connected relays (survey-request).
// On survey request, the relay will lookup its local-dialin-map
// and reply to core if connection from the given cluster is available.
func ClientSurveyRPC(ctx context.Context, stream sentryrpc.RelayPeerService_RelayPeerSurveyRPCClient, relayUUID string, ip func() string, dialinlookup func(string) int) {
var relayIP string
relayIP = ip()
//Send a empty clustersni meesage to init the channel
msg := &sentryrpc.PeerSurveyResponse{
Relayuuid: relayUUID,
Relayip: relayIP,
Clustersni: "",
}
err := stream.Send(msg)
if err != nil {
stream.CloseSend()
return
}
_log.Debugw("surveyRPC send first", "msg", msg)
go func() {
surveyRPCStremWatch:
for {
select {
case <-ctx.Done():
stream.CloseSend()
break surveyRPCStremWatch
}
}
}()
//survey request recv loop will process the survey requests
for {
surveyReq, err := stream.Recv()
if err != nil {
stream.CloseSend()
break
}
clustersni := surveyReq.GetClustersni()
if clustersni == "" {
_log.Errorw(
"prob response with empty items for ",
"clustersni", clustersni,
)
continue
}
//lookup the local dialin table for connections\
cnt := dialinlookup(clustersni)
_log.Infow(
"survey lookup",
"key", clustersni,
"count", cnt,
)
if relayIP == "" {
_log.Errorw(
"survey failed to get relay ip",
"key", clustersni,
)
continue
}
if cnt > 0 {
msg := &sentryrpc.PeerSurveyResponse{
Relayuuid: relayUUID,
Relayip: relayIP,
Clustersni: clustersni,
}
err = stream.Send(msg)
if err != nil {
_log.Errorw(
"survey send response failed",
"key", clustersni,
"error", err,
)
stream.CloseSend()
break
}
}
}
_log.Debug(
"stopping probeRPC routine",
)
}
// GetPeeringServerCreds returns peering cert, key, ca
func GetPeeringServerCreds(ctx context.Context, bs service.BootstrapService, rpcPort int, host string) (cert, key, ca []byte, err error) {
nctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
var template *sentry.BootstrapAgentTemplate
template, err = bs.GetBootstrapAgentTemplate(nctx, "paralus-sentry-peering-server")
if err != nil {
return
}
config := ®ister.Config{
TemplateName: "paralus-sentry-peering-server",
Addr: fmt.Sprintf("localhost:%d", rpcPort),
Name: "paralus-sentry-peering-server",
Scheme: "grpc",
Mode: "server",
}
var privKey *ecdsa.PrivateKey
privKey, err = cryptoutil.GenerateECDSAPrivateKey()
if err != nil {
return
}
config.PrivateKey, err = cryptoutil.EncodePrivateKey(privKey, cryptoutil.NoPassword)
if err != nil {
return
}
var csr []byte
csr, err = cryptoutil.CreateCSR(pkix.Name{
CommonName: host,
Country: []string{"USA"},
Organization: []string{"Paralus"},
OrganizationalUnit: []string{"Paralus Sentry Peering Server"},
Province: []string{"California"},
Locality: []string{"Sunnyvale"},
}, privKey)
if err != nil {
return
}
config.CSR = csr
var agent *sentry.BootstrapAgent
agent, err = bs.GetBootstrapAgent(nctx, template.Metadata.Name, query.WithName("paralus-sentry-peering-server"), query.WithGlobalScope())
if err != nil {
if err != sql.ErrNoRows {
return
}
}
if agent != nil {
config.ClientID = agent.Spec.Token
} else {
config.ClientID = xid.New().String()
}
err = register.Register(nctx, config)
if err != nil {
return
}
cert = config.Certificate
key = config.PrivateKey
ca = config.CACertificate
return
}