-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
join.go
406 lines (384 loc) · 14 KB
/
join.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
/*
Copyright 2023 Avi Zimmerman <avi.zimmerman@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package node
import (
"net"
"net/netip"
"strconv"
"time"
v1 "github.com/webmeshproj/api/v1"
"golang.org/x/exp/slog"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/webmeshproj/webmesh/pkg/context"
"github.com/webmeshproj/webmesh/pkg/meshdb/peers"
"github.com/webmeshproj/webmesh/pkg/net/mesh"
"github.com/webmeshproj/webmesh/pkg/services/leaderproxy"
"github.com/webmeshproj/webmesh/pkg/services/rbac"
)
var canVoteAction = &rbac.Action{
Verb: v1.RuleVerb_VERB_PUT,
Resource: v1.RuleResource_RESOURCE_VOTES,
}
var canPutRouteAction = &rbac.Action{
Verb: v1.RuleVerb_VERB_PUT,
Resource: v1.RuleResource_RESOURCE_ROUTES,
}
var canPutEdgeAction = &rbac.Action{
Verb: v1.RuleVerb_VERB_PUT,
Resource: v1.RuleResource_RESOURCE_EDGES,
}
func (s *Server) Join(ctx context.Context, req *v1.JoinRequest) (*v1.JoinResponse, error) {
if !s.store.Raft().IsLeader() {
return nil, status.Errorf(codes.FailedPrecondition, "not leader")
}
s.mu.Lock()
defer s.mu.Unlock()
log := s.log.With("op", "join", "id", req.GetId())
ctx = context.WithLogger(ctx, log)
log.Info("join request received", slog.Any("request", req))
// Check if we haven't loaded the mesh domain and prefixes into memory yet
err := s.loadMeshState(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to load mesh state: %v", err)
}
// Validate inputs
if req.GetId() == "" {
return nil, status.Error(codes.InvalidArgument, "node id required")
} else if !peers.IsValidID(req.GetId()) {
return nil, status.Error(codes.InvalidArgument, "node id is invalid")
}
if len(req.GetRoutes()) > 0 {
for _, route := range req.GetRoutes() {
route, err := netip.ParsePrefix(route)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid route %q: %v", route, err)
}
// Make sure the route does not overlap with a mesh reserved prefix
if route.Contains(s.ipv4Prefix.Addr()) || route.Contains(s.ipv6Prefix.Addr()) {
return nil, status.Errorf(codes.InvalidArgument, "route %q overlaps with mesh prefix", route)
}
}
}
publicKey, err := wgtypes.ParseKey(req.GetPublicKey())
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid public key: %v", err)
}
// Check that the node is indeed who they say they are
if !s.insecure {
if !nodeIDMatchesContext(ctx, req.GetId()) {
return nil, status.Errorf(codes.PermissionDenied, "node id %s does not match authenticated caller", req.GetId())
}
// We can go ahead and check here if the node is allowed to do what
// they want.
var actions rbac.Actions
if req.GetAsVoter() {
actions = append(actions, canVoteAction)
}
if len(req.GetRoutes()) > 0 {
actions = append(actions, canPutRouteAction)
}
if len(req.GetDirectPeers()) > 0 {
for _, peer := range req.GetDirectPeers() {
actions = append(actions, canPutEdgeAction.For(peer))
actions = append(actions, canNegDataChannelAction.For(peer)...)
}
}
if len(actions) > 0 {
allowed, err := s.rbacEval.Evaluate(ctx, actions)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to evaluate permissions: %v", err)
}
if !allowed {
s.log.Warn("Node not allowed to perform requested actions",
slog.String("id", req.GetId()),
slog.Any("actions", actions))
return nil, status.Error(codes.PermissionDenied, "not allowed")
}
}
}
// Issue a barrier to the raft cluster to ensure all nodes are
// fully caught up before we start assigning it addresses
log.Debug("sending barrier to raft cluster")
timeout := time.Second * 10 // TODO: Make this configurable
err = s.store.Raft().Raft().Barrier(timeout).Error()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to send barrier: %v", err)
}
log.Debug("barrier complete, all nodes caught up")
// Start building a list of clean up functions to run if we fail
cleanFuncs := make([]func(), 0)
handleErr := func(cause error) error {
for _, f := range cleanFuncs {
f()
}
return cause
}
// Handle any new routes
if len(req.GetRoutes()) > 0 {
created, err := s.ensurePeerRoutes(ctx, req.GetId(), req.GetRoutes())
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to ensure peer routes: %v", err))
} else if created {
cleanFuncs = append(cleanFuncs, func() {
err := s.networking.DeleteRoute(ctx, nodeAutoRoute(req.GetId()))
if err != nil {
log.Warn("failed to delete route", slog.String("error", err.Error()))
}
})
}
}
var leasev4, leasev6 netip.Prefix
// We always try to generate an IPv6 address for the peer, even if they choose not to
// use it. This helps enforce an upper bound on the umber of peers we can have in the network
// (ULA/48 with /64 prefixes == 65536 peers same as a /16 class B and the limit for direct WireGuard
// peers an interface can hold).
log.Debug("assigning IPv6 address to peer")
leasev6, err = s.store.Plugins().AllocateIP(ctx, &v1.AllocateIPRequest{
NodeId: req.GetId(),
Subnet: s.ipv6Prefix.String(),
Version: v1.AllocateIPRequest_IP_VERSION_6,
})
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to allocate IPv6 address: %v", err))
}
log.Debug("assigned IPv6 address to peer", slog.String("ipv6", leasev6.String()))
// Acquire an IPv4 address for the peer only if requested
if req.GetAssignIpv4() {
log.Debug("assigning IPv4 address to peer")
leasev4, err = s.store.Plugins().AllocateIP(ctx, &v1.AllocateIPRequest{
NodeId: req.GetId(),
Subnet: s.ipv4Prefix.String(),
Version: v1.AllocateIPRequest_IP_VERSION_4,
})
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to allocate IPv4 address: %v", err))
}
log.Debug("assigned IPv4 address to peer", slog.String("ipv4", leasev4.String()))
}
// Write the peer to the database
err = s.peers.Put(ctx, peers.Node{
ID: req.GetId(),
PublicKey: publicKey,
PrimaryEndpoint: req.GetPrimaryEndpoint(),
WireGuardEndpoints: req.GetWireguardEndpoints(),
ZoneAwarenessID: req.GetZoneAwarenessId(),
GRPCPort: int(req.GetGrpcPort()),
RaftPort: int(req.GetRaftPort()),
DNSPort: int(req.GetMeshdnsPort()),
Features: req.GetFeatures(),
PrivateIPv4: leasev4,
PrivateIPv6: leasev6,
})
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to persist peer details to raft log: %v", err))
}
cleanFuncs = append(cleanFuncs, func() {
err := s.peers.Delete(ctx, req.GetId())
if err != nil {
log.Warn("failed to delete peer", slog.String("error", err.Error()))
}
})
// At this point we want to
// Add an edge from the joining server to the caller
joiningServer := string(s.store.ID())
if proxiedFrom, ok := leaderproxy.ProxiedFrom(ctx); ok {
joiningServer = proxiedFrom
}
log.Debug("adding edge between caller and joining server", slog.String("joining_server", joiningServer))
err = s.peers.PutEdge(ctx, peers.Edge{
From: joiningServer,
To: req.GetId(),
Weight: 1,
})
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to add edge: %v", err))
}
if req.GetPrimaryEndpoint() != "" {
// Add an edge between the caller and all other nodes with public endpoints
// TODO: This should be done according to network policy and batched
allPeers, err := s.peers.ListPublicNodes(ctx)
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to list peers: %v", err))
}
for _, peer := range allPeers {
if peer.ID != req.GetId() && peer.PrimaryEndpoint != "" {
log.Debug("adding edge from public peer to public caller", slog.String("peer", peer.ID))
err = s.peers.PutEdge(ctx, peers.Edge{
From: peer.ID,
To: req.GetId(),
Weight: 99,
})
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to add edge: %v", err))
}
}
}
}
if req.GetZoneAwarenessId() != "" {
// Add an edge between the caller and all other nodes in the same zone
// with public endpoints.
// TODO: Same as above - this should be done according to network policy and batched
zonePeers, err := s.peers.ListByZoneID(ctx, req.GetZoneAwarenessId())
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to list peers: %v", err))
}
for _, peer := range zonePeers {
if peer.ID == req.GetId() || peer.PrimaryEndpoint == "" {
continue
}
log.Debug("adding edges to peer in the same zone", slog.String("peer", peer.ID))
if peer.ID != req.GetId() {
err = s.peers.PutEdge(ctx, peers.Edge{
From: peer.ID,
To: req.GetId(),
Weight: 1,
})
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to add edge: %v", err))
}
}
}
}
if len(req.GetDirectPeers()) > 0 {
// Put an ICE edge between the caller and all direct peers
for _, peer := range req.GetDirectPeers() {
// Check if the peer exists
_, err := s.peers.Get(ctx, peer)
if err != nil {
if err != peers.ErrNodeNotFound {
return nil, handleErr(status.Errorf(codes.Internal, "failed to get peer: %v", err))
}
// The peer doesn't exist, so create a placeholder for it
log.Debug("registering empty peer", slog.String("peer", peer))
err = s.peers.Put(ctx, peers.Node{
ID: peer,
})
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to register peer: %v", err))
}
}
log.Debug("adding ICE edge to peer", slog.String("peer", peer))
err = s.peers.PutEdge(ctx, peers.Edge{
From: peer,
To: req.GetId(),
Weight: 1,
Attrs: map[string]string{
v1.EdgeAttributes_EDGE_ATTRIBUTE_ICE.String(): "true",
},
})
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to add edge: %v", err))
}
}
}
// Add peer to the raft cluster
var raftAddress string
if req.GetAssignIpv4() && !req.GetPreferRaftIpv6() {
// Prefer IPv4 for raft
raftAddress = net.JoinHostPort(leasev4.Addr().String(), strconv.Itoa(int(req.GetRaftPort())))
} else {
// Use IPv6
// TODO: Doesn't work when we are IPv4 only. Need to fix this.
// Basically if a single node is IPv4 only, we need to use IPv4 for raft.
// We may as well use IPv4 for everything in that case. Leave it for now,
// but need to document these requirements fully for dual-stack setups.
// Another option is to create another role of being neither an observer or voter,
// in which case another subscription method needs to be exposed for receiving updates.
raftAddress = net.JoinHostPort(leasev6.Addr().String(), strconv.Itoa(int(req.GetRaftPort())))
}
if req.GetAsVoter() {
log.Info("adding candidate to cluster", slog.String("raft_address", raftAddress))
if err := s.store.Raft().AddVoter(ctx, req.GetId(), raftAddress); err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to add voter: %v", err))
}
} else {
log.Info("adding non-voter to cluster", slog.String("raft_address", raftAddress))
if err := s.store.Raft().AddNonVoter(ctx, req.GetId(), raftAddress); err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to add non-voter: %v", err))
}
}
cleanFuncs = append(cleanFuncs, func() {
err := s.store.Raft().RemoveServer(ctx, req.GetId(), false)
if err != nil {
log.Warn("failed to remove voter", slog.String("error", err.Error()))
}
})
// Start building the response
resp := &v1.JoinResponse{
MeshDomain: s.meshDomain,
NetworkIpv4: s.ipv4Prefix.String(),
NetworkIpv6: s.ipv6Prefix.String(),
AddressIpv6: leasev6.String(),
AddressIpv4: func() string {
if leasev4.IsValid() {
return leasev4.String()
}
return ""
}(),
}
dnsServers, err := s.peers.ListByFeature(ctx, v1.Feature_MESH_DNS)
if err != nil {
log.Warn("could not lookup DNS servers", slog.String("error", err.Error()))
} else {
for _, peer := range dnsServers {
if peer.ID == req.GetId() {
continue
}
switch {
// Prefer the IPv4 address
case peer.PrivateDNSAddrV4().IsValid():
resp.DnsServers = append(resp.DnsServers, peer.PrivateDNSAddrV4().String())
case peer.PrivateDNSAddrV6().IsValid():
resp.DnsServers = append(resp.DnsServers, peer.PrivateDNSAddrV6().String())
}
}
}
peers, err := mesh.WireGuardPeersFor(ctx, s.store.Storage(), req.GetId())
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to get wireguard peers: %v", err))
}
var requiresICE bool
for _, peer := range peers {
if peer.PrimaryEndpoint == "" || peer.Ice {
requiresICE = true
break
}
}
resp.Peers = peers
// If the caller needs ICE servers, find all the eligible peers and return them
if requiresICE {
peers, err := s.peers.ListByFeature(ctx, v1.Feature_ICE_NEGOTIATION)
if err != nil {
return nil, handleErr(status.Errorf(codes.Internal, "failed to list peers by ICE feature: %v", err))
}
for _, peer := range peers {
if peer.ID == req.GetId() {
continue
}
// We only return peers that are publicly accessible for now.
// This should be configurable in the future.
publicAddr := peer.PublicRPCAddr()
if publicAddr.IsValid() {
resp.IceServers = append(resp.IceServers, publicAddr.String())
}
}
if len(resp.IceServers) == 0 {
log.Warn("no peers with ICE negotiation feature found, rejecting join request")
return nil, handleErr(status.Errorf(codes.FailedPrecondition, "no peers with ICE negotiation feature found"))
}
}
log.Info("sending join response", slog.Any("response", resp))
return resp, nil
}