-
Notifications
You must be signed in to change notification settings - Fork 19
/
manager.go
592 lines (488 loc) · 19.3 KB
/
manager.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package connections
import (
"context"
"errors"
"fmt"
"sort"
"sync"
"time"
vgcrypto "code.vegaprotocol.io/vega/libs/crypto"
"code.vegaprotocol.io/vega/libs/jsonrpc"
"code.vegaprotocol.io/vega/wallet/api"
"code.vegaprotocol.io/vega/wallet/wallet"
)
// Manager holds the opened connections between the third-party
// applications and the wallets.
type Manager struct {
// tokenToConnection maps the token to the connection. It is the base
// registry for all opened connections.
tokenToConnection map[Token]*walletConnection
// sessionFingerprintToToken maps the session fingerprint (wallet + hostname
// on a session connection). This is used to determine whether a session
// connection already exists for a given wallet and hostname.
// It only holds sessions fingerprints.
// Long-living connections are not tracked.
sessionFingerprintToToken map[string]Token
// timeService is used to resolve the current time to update the last activity
// time on the token, and figure out their expiration.
timeService TimeService
walletStore WalletStore
sessionStore SessionStore
tokenStore TokenStore
interactor api.Interactor
mu sync.Mutex
}
type walletConnection struct {
// connectedWallet is the projection of the wallet through the permissions
// and authentication system. On a regular wallet, there is no restriction
// on what we can call, which doesn't fit the model of having allowed
// access, so we wrap the "regular wallet" behind the "connected wallet".
connectedWallet api.ConnectedWallet
policy connectionPolicy
}
// StartSession initializes a connection between a wallet and a third-party
// application.
// If a connection already exists, it's disconnected and a new token is
// generated.
func (m *Manager) StartSession(hostname string, w wallet.Wallet) (Token, error) {
m.mu.Lock()
defer m.mu.Unlock()
cw, err := api.NewConnectedWallet(hostname, w)
if err != nil {
return "", fmt.Errorf("could not instantiate the connected wallet for a session connection: %w", err)
}
newToken := m.generateToken()
sessionFingerprint := asSessionFingerprint(hostname, w.Name())
if previousToken, sessionAlreadyExists := m.sessionFingerprintToToken[sessionFingerprint]; sessionAlreadyExists {
m.destroySessionToken(previousToken)
}
m.sessionFingerprintToToken[sessionFingerprint] = newToken
now := m.timeService.Now()
m.tokenToConnection[newToken] = &walletConnection{
connectedWallet: cw,
policy: &sessionPolicy{
expiryDate: now.Add(1 * time.Hour),
},
}
// We ignore this error as tracking the session a nice-to-have feature to
// ease reconnection after a software reboot. We don't want to prevent the
// connection because an error on that layer.
_ = m.sessionStore.TrackSession(Session{
Token: newToken,
Hostname: hostname,
Wallet: w.Name(),
})
return newToken, nil
}
func (m *Manager) EndSessionConnectionWithToken(token Token) {
m.mu.Lock()
defer m.mu.Unlock()
m.destroySessionToken(token)
}
func (m *Manager) EndSessionConnection(hostname, walletName string) {
m.mu.Lock()
defer m.mu.Unlock()
fingerprint := asSessionFingerprint(hostname, walletName)
token, exists := m.sessionFingerprintToToken[fingerprint]
if !exists {
return
}
m.destroySessionToken(token)
}
func (m *Manager) EndAllSessionConnections() {
m.mu.Lock()
defer m.mu.Unlock()
for token := range m.tokenToConnection {
m.destroySessionToken(token)
}
}
// ConnectedWallet retrieves the wallet associated to the specified token.
func (m *Manager) ConnectedWallet(ctx context.Context, hostname string, token Token) (api.ConnectedWallet, *jsonrpc.ErrorDetails) {
m.mu.Lock()
defer m.mu.Unlock()
connection, exists := m.tokenToConnection[token]
if !exists {
return api.ConnectedWallet{}, serverErrorAuthenticationFailure(ErrNoConnectionAssociatedThisAuthenticationToken)
}
now := m.timeService.Now()
hasExpired := connection.policy.HasConnectionExpired(now)
if connection.policy.IsLongLivingConnection() {
if hasExpired {
return api.ConnectedWallet{}, serverErrorAuthenticationFailure(ErrTokenHasExpired)
}
} else {
traceID := jsonrpc.TraceIDFromContext(ctx)
if connection.connectedWallet.Hostname() != hostname {
return api.ConnectedWallet{}, serverErrorAuthenticationFailure(ErrHostnamesMismatchForThisToken)
}
isClosed := connection.policy.IsClosed()
if hasExpired || isClosed {
if err := m.interactor.NotifyInteractionSessionBegan(ctx, traceID, api.WalletUnlockingWorkflow, 2); err != nil {
return api.ConnectedWallet{}, api.RequestNotPermittedError(err)
}
defer m.interactor.NotifyInteractionSessionEnded(ctx, traceID)
for {
if ctx.Err() != nil {
m.interactor.NotifyError(ctx, traceID, api.ApplicationErrorType, api.ErrRequestInterrupted)
return api.ConnectedWallet{}, api.RequestInterruptedError(api.ErrRequestInterrupted)
}
unlockingReason := fmt.Sprintf("The third-party application %q is attempting access to the locked wallet %q. To unlock this wallet and allow access to all connected apps associated to it, enter its passphrase.", hostname, connection.connectedWallet.Name())
walletPassphrase, err := m.interactor.RequestPassphrase(ctx, traceID, 1, connection.connectedWallet.Name(), unlockingReason)
if err != nil {
if errDetails := api.HandleRequestFlowError(ctx, traceID, m.interactor, err); errDetails != nil {
return api.ConnectedWallet{}, errDetails
}
m.interactor.NotifyError(ctx, traceID, api.InternalErrorType, fmt.Errorf("requesting the wallet passphrase failed: %w", err))
return api.ConnectedWallet{}, api.InternalError(api.ErrCouldNotConnectToWallet)
}
if err := m.walletStore.UnlockWallet(ctx, connection.connectedWallet.Name(), walletPassphrase); err != nil {
if errors.Is(err, wallet.ErrWrongPassphrase) {
m.interactor.NotifyError(ctx, traceID, api.UserErrorType, wallet.ErrWrongPassphrase)
continue
}
if errDetails := api.HandleRequestFlowError(ctx, traceID, m.interactor, err); errDetails != nil {
return api.ConnectedWallet{}, errDetails
}
m.interactor.NotifyError(ctx, traceID, api.InternalErrorType, fmt.Errorf("could not unlock the wallet: %w", err))
return api.ConnectedWallet{}, api.InternalError(api.ErrCouldNotConnectToWallet)
}
break
}
w, err := m.walletStore.GetWallet(ctx, connection.connectedWallet.Name())
if err != nil {
m.interactor.NotifyError(ctx, traceID, api.InternalErrorType, fmt.Errorf("could not retrieve the wallet: %w", err))
return api.ConnectedWallet{}, api.InternalError(api.ErrCouldNotConnectToWallet)
}
// Update the connected wallet for all connections referencing the
// newly unlocked wallet.
for _, otherConnection := range m.tokenToConnection {
if w.Name() != otherConnection.connectedWallet.Name() {
continue
}
cw, err := api.NewConnectedWallet(otherConnection.connectedWallet.Hostname(), w)
if err != nil {
m.interactor.NotifyError(ctx, traceID, api.InternalErrorType, fmt.Errorf("could not instantiate the connected wallet for a session connection: %w", err))
return api.ConnectedWallet{}, api.InternalError(api.ErrCouldNotConnectToWallet)
}
otherConnection.connectedWallet = cw
otherConnection.policy = &sessionPolicy{
expiryDate: now.Add(1 * time.Hour),
closed: false,
}
}
m.interactor.NotifySuccessfulRequest(ctx, traceID, 2, fmt.Sprintf("The wallet %q has been successfully unlocked.", w.Name()))
}
}
return connection.connectedWallet, nil
}
// ListSessionConnections lists all the session connections as a list of pairs of
// hostname/wallet name.
// The list is sorted, first, by hostname, and, then, by wallet name.
func (m *Manager) ListSessionConnections() []api.Connection {
m.mu.Lock()
defer m.mu.Unlock()
connections := make([]api.Connection, 0, len(m.tokenToConnection))
connectionsCount := 0
for _, connection := range m.tokenToConnection {
if connection.policy.IsLongLivingConnection() {
continue
}
connections = append(connections, api.Connection{
Hostname: connection.connectedWallet.Hostname(),
Wallet: connection.connectedWallet.Name(),
})
connectionsCount++
}
connections = connections[0:connectionsCount]
sort.SliceStable(connections, func(i, j int) bool {
if connections[i].Hostname == connections[j].Hostname {
return connections[i].Wallet < connections[j].Wallet
}
return connections[i].Hostname < connections[j].Hostname
})
return connections
}
// generateToken generates a new token and ensure it is not already in use to
// avoid collisions.
func (m *Manager) generateToken() Token {
for {
token := GenerateToken()
if _, alreadyExistingToken := m.tokenToConnection[token]; !alreadyExistingToken {
return token
}
}
}
func (m *Manager) destroySessionToken(tokenToDestroy Token) {
connection, exists := m.tokenToConnection[tokenToDestroy]
if !exists || connection.policy.IsLongLivingConnection() {
return
}
// Remove the session fingerprint associated to the session token.
for sessionFingerprint, t := range m.sessionFingerprintToToken {
if t == tokenToDestroy {
delete(m.sessionFingerprintToToken, sessionFingerprint)
break
}
}
// Break the link between a token and its associated wallet.
m.tokenToConnection[tokenToDestroy] = nil
delete(m.tokenToConnection, tokenToDestroy)
}
func (m *Manager) loadLongLivingConnections(ctx context.Context) error {
tokenSummaries, err := m.tokenStore.ListTokens()
if err != nil {
return err
}
for _, tokenSummary := range tokenSummaries {
tokenDescription, err := m.tokenStore.DescribeToken(tokenSummary.Token)
if err != nil {
return fmt.Errorf("could not get information associated to the token %q: %w", tokenDescription.Token.Short(), err)
}
if err := m.loadLongLivingConnection(ctx, tokenDescription); err != nil {
return err
}
}
return nil
}
func (m *Manager) loadLongLivingConnection(ctx context.Context, tokenDescription TokenDescription) error {
if err := m.walletStore.UnlockWallet(ctx, tokenDescription.Wallet.Name, tokenDescription.Wallet.Passphrase); err != nil {
// We don't properly handle wallets renaming, nor wallets passphrase
// update in the token file automatically. We only support a direct
// update of the token file.
return fmt.Errorf("could not unlock the wallet %q associated to the token %q: %w",
tokenDescription.Wallet.Name,
tokenDescription.Token.Short(),
err)
}
w, err := m.walletStore.GetWallet(ctx, tokenDescription.Wallet.Name)
if err != nil {
// This should not happen because we just unlocked the wallet.
return fmt.Errorf("could not get the information for the wallet %q associated to the token %q: %w",
tokenDescription.Wallet.Name,
tokenDescription.Token.Short(),
err)
}
m.tokenToConnection[tokenDescription.Token] = &walletConnection{
connectedWallet: api.NewLongLivingConnectedWallet(w),
policy: &longLivingConnectionPolicy{
expirationDate: tokenDescription.ExpirationDate,
},
}
return nil
}
func (m *Manager) loadPreviousSessionConnections(ctx context.Context) error {
sessions, err := m.sessionStore.ListSessions(ctx)
if err != nil {
return fmt.Errorf("could not list the sessions: %w", err)
}
now := m.timeService.Now()
for _, session := range sessions {
sessionFingerprint := asSessionFingerprint(session.Hostname, session.Wallet)
m.sessionFingerprintToToken[sessionFingerprint] = session.Token
// If the wallet in the session store doesn't exist, we destroy that session
// and move onto the next.
walletExists, err := m.walletStore.WalletExists(ctx, session.Wallet)
if err != nil {
return fmt.Errorf("could not verify if the wallet %q exists: %w", session.Wallet, err)
}
if !walletExists {
err := m.sessionStore.DeleteSession(ctx, session.Token)
if err != nil {
return fmt.Errorf("could not delete the session with token %q: %w", session.Token, err)
}
continue
}
// If the wallet is already unlocked, we fully restore the connection.
isAlreadyUnlocked, err := m.walletStore.IsWalletAlreadyUnlocked(ctx, session.Wallet)
if err != nil {
return fmt.Errorf("could not verify wether the wallet %q is locked or not: %w", session.Wallet, err)
}
var connectedWallet api.ConnectedWallet
isClosed := true
if isAlreadyUnlocked {
w, err := m.walletStore.GetWallet(ctx, session.Wallet)
if err != nil {
return fmt.Errorf("could not retrieve the wallet %q: %w", session.Wallet, err)
}
cw, err := api.NewConnectedWallet(session.Hostname, w)
if err != nil {
return fmt.Errorf("could not instantiate the connected wallet for a session connection: %w", err)
}
connectedWallet = cw
isClosed = false
} else {
connectedWallet = api.NewDisconnectedWallet(session.Hostname, session.Wallet)
}
m.tokenToConnection[session.Token] = &walletConnection{
connectedWallet: connectedWallet,
policy: &sessionPolicy{
// Since this session is being reloaded, we consider it to be
// expired.
expiryDate: now,
closed: isClosed,
},
}
}
return nil
}
// refreshConnections is called when the wallet store notices a change in
// the wallets. This way the connection manager is able to reload the connected
// wallets.
func (m *Manager) refreshConnections(_ context.Context, event wallet.Event) {
m.mu.Lock()
defer m.mu.Unlock()
switch event.Type {
case wallet.WalletRemovedEventType:
m.destroyConnectionsUsingThisWallet(event.Data.(wallet.WalletRemovedEventData).Name)
case wallet.UnlockedWalletUpdatedEventType:
m.updateConnectionsUsingThisWallet(event.Data.(wallet.UnlockedWalletUpdatedEventData).UpdatedWallet)
case wallet.WalletHasBeenLockedEventType:
m.closeConnectionsUsingThisWallet(event.Data.(wallet.WalletHasBeenLockedEventData).Name)
case wallet.WalletRenamedEventType:
data := event.Data.(wallet.WalletRenamedEventData)
m.updateConnectionsUsingThisRenamedWallet(data.PreviousName, data.NewName)
}
}
// destroyConnectionUsingThisWallet close the connection, dereference it, and
// remove it from the session store.
func (m *Manager) destroyConnectionsUsingThisWallet(walletName string) {
ctx := context.Background()
for token, connection := range m.tokenToConnection {
if connection.connectedWallet.Name() != walletName {
continue
}
connection.policy.SetAsForcefullyClose()
delete(m.tokenToConnection, token)
// We ignore the error in a best-effort to have the session store clean
// up.
_ = m.sessionStore.DeleteSession(ctx, token)
}
}
func (m *Manager) updateConnectionsUsingThisWallet(w wallet.Wallet) {
for _, connection := range m.tokenToConnection {
if connection.connectedWallet.Name() != w.Name() {
continue
}
var updatedConnectedWallet api.ConnectedWallet
if connection.policy.IsLongLivingConnection() {
updatedConnectedWallet = api.NewLongLivingConnectedWallet(w)
} else {
updatedConnectedWallet, _ = api.NewConnectedWallet(connection.connectedWallet.Hostname(), w)
}
connection.connectedWallet = updatedConnectedWallet
}
}
func (m *Manager) refreshLongLivingTokens(ctx context.Context, activeTokensDescriptions ...TokenDescription) {
m.mu.Lock()
defer m.mu.Unlock()
// We need to find the new tokens among the active ones, so, we build a
// registry with all the active tokens. Then, we remove the tracked token
// when found. We will end up with the new tokens, only.
activeTokens := make(map[Token]TokenDescription, len(activeTokensDescriptions))
for _, tokenDescription := range activeTokensDescriptions {
activeTokens[tokenDescription.Token] = tokenDescription
}
isActiveToken := func(token Token) (TokenDescription, bool) {
activeTokenDescription, isTracked := activeTokens[token]
if isTracked {
delete(activeTokens, token)
}
return activeTokenDescription, isTracked
}
// First, we update of the tokens we already track.
for token, connection := range m.tokenToConnection {
if !connection.policy.IsLongLivingConnection() {
continue
}
activeToken, isActive := isActiveToken(token)
if !isActive {
// If the token could not be found in the active tokens, this means
// the token has been deleted from the token store. Thus, we close the
// connection.
delete(m.tokenToConnection, token)
continue
}
_ = m.loadLongLivingConnection(ctx, activeToken)
}
// Then, we load the new tokens.
for _, tokenDescription := range activeTokens {
_ = m.loadLongLivingConnection(ctx, tokenDescription)
}
}
// closeConnectionsUsingThisWallet defines the connection as closed. It keeps track of it
// but next time it will be used the application will request for the passphrase
// to reinstate it.
func (m *Manager) closeConnectionsUsingThisWallet(walletName string) {
for _, connection := range m.tokenToConnection {
if connection.connectedWallet.Name() != walletName {
continue
}
connection.policy.SetAsForcefullyClose()
}
}
func (m *Manager) updateConnectionsUsingThisRenamedWallet(previousWalletName, newWalletName string) {
var _updatedWallet wallet.Wallet
// This acts as a cached getter, to avoid multiple or useless fetch.
getUpdatedWallet := func() wallet.Wallet {
if _updatedWallet == nil {
w, _ := m.walletStore.GetWallet(context.Background(), newWalletName)
_updatedWallet = w
}
return _updatedWallet
}
for _, connection := range m.tokenToConnection {
if connection.connectedWallet.Name() != previousWalletName {
continue
}
if connection.policy.IsLongLivingConnection() {
connection.connectedWallet = api.NewLongLivingConnectedWallet(
getUpdatedWallet(),
)
}
connection.connectedWallet, _ = api.NewConnectedWallet(
connection.connectedWallet.Hostname(),
getUpdatedWallet(),
)
}
}
func NewManager(timeService TimeService, walletStore WalletStore, tokenStore TokenStore, sessionStore SessionStore, interactor api.Interactor) (*Manager, error) {
m := &Manager{
tokenToConnection: map[Token]*walletConnection{},
sessionFingerprintToToken: map[string]Token{},
timeService: timeService,
walletStore: walletStore,
sessionStore: sessionStore,
tokenStore: tokenStore,
interactor: interactor,
}
ctx := context.Background()
if err := m.loadLongLivingConnections(ctx); err != nil {
return nil, fmt.Errorf("could not load the long-living connections: %w", err)
}
if err := m.loadPreviousSessionConnections(ctx); err != nil {
return nil, fmt.Errorf("could not load the previous session connections: %w", err)
}
walletStore.OnUpdate(m.refreshConnections)
tokenStore.OnUpdate(m.refreshLongLivingTokens)
return m, nil
}
func asSessionFingerprint(hostname string, walletName string) string {
return vgcrypto.HashStrToHex(hostname + "::" + walletName)
}
func serverErrorAuthenticationFailure(err error) *jsonrpc.ErrorDetails {
return jsonrpc.NewServerError(api.ErrorCodeAuthenticationFailure, err)
}