forked from keybase/client
/
upak_loader.go
707 lines (602 loc) · 22.6 KB
/
upak_loader.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
package libkb
import (
"errors"
"fmt"
"time"
lru "github.com/hashicorp/golang-lru"
"github.com/keybase/client/go/protocol/keybase1"
"golang.org/x/net/context"
)
// UPAK Loader is a loader for UserPlusKeysV2AllIncarnations. It's a thin user object that is
// almost as good for many purposes, but can be safely copied and serialized.
type UPAKLoader interface {
ClearMemory()
Load(arg LoadUserArg) (ret *keybase1.UserPlusAllKeys, user *User, err error)
LoadV2(arg LoadUserArg) (ret *keybase1.UserPlusKeysV2AllIncarnations, user *User, err error)
CheckKIDForUID(ctx context.Context, uid keybase1.UID, kid keybase1.KID) (found bool, revokedAt *keybase1.KeybaseTime, deleted bool, err error)
LoadUserPlusKeys(ctx context.Context, uid keybase1.UID, pollForKID keybase1.KID) (keybase1.UserPlusKeys, error)
LoadKeyV2(ctx context.Context, uid keybase1.UID, kid keybase1.KID) (*keybase1.UserPlusKeysV2, *keybase1.PublicKeyV2NaCl, map[keybase1.Seqno]keybase1.LinkID, error)
Invalidate(ctx context.Context, uid keybase1.UID)
LoadDeviceKey(ctx context.Context, uid keybase1.UID, deviceID keybase1.DeviceID) (upak *keybase1.UserPlusAllKeys, deviceKey *keybase1.PublicKey, revoked *keybase1.RevokedKey, err error)
LookupUsername(ctx context.Context, uid keybase1.UID) (NormalizedUsername, error)
LookupUsernameUPAK(ctx context.Context, uid keybase1.UID) (NormalizedUsername, error)
LookupUID(ctx context.Context, un NormalizedUsername) (keybase1.UID, error)
LookupUsernameAndDevice(ctx context.Context, uid keybase1.UID, did keybase1.DeviceID) (username NormalizedUsername, deviceName string, deviceType string, err error)
ListFollowedUIDs(uid keybase1.UID) ([]keybase1.UID, error)
PutUserToCache(ctx context.Context, user *User) error
}
// CachedUPAKLoader is a UPAKLoader implementation that can cache results both
// in memory and on disk.
type CachedUPAKLoader struct {
Contextified
cache *lru.Cache
locktab LockTable
Freshness time.Duration
noCache bool
TestDeadlocker func()
}
// NewCachedUPAKLoader constructs a new CachedUPAKLoader
func NewCachedUPAKLoader(g *GlobalContext, f time.Duration) *CachedUPAKLoader {
c, err := lru.New(g.Env.GetUPAKCacheSize())
if err != nil {
panic(fmt.Sprintf("could not create lru cache (size = %d)", g.Env.GetUPAKCacheSize()))
}
return &CachedUPAKLoader{
Contextified: NewContextified(g),
Freshness: f,
cache: c,
noCache: false,
}
}
// NewUncachedUPAKLoader creates a UPAK loader that doesn't do any caching.
// It uses the implementation of CachedUPAKLoader but disables all caching.
func NewUncachedUPAKLoader(g *GlobalContext) UPAKLoader {
return &CachedUPAKLoader{
Contextified: NewContextified(g),
Freshness: time.Duration(0),
noCache: true,
}
}
func culDBKeyV1(uid keybase1.UID) DbKey {
return DbKeyUID(DBUserPlusAllKeysV1, uid)
}
func culDBKeyVersioned(version int, uid keybase1.UID) DbKey {
return DbKey{
Typ: DBUserPlusKeysVersioned,
Key: fmt.Sprintf("%d:%s", version, uid.String()),
}
}
func culDBKeyV2(uid keybase1.UID) DbKey {
return culDBKeyVersioned(2, uid)
}
func (u *CachedUPAKLoader) ClearMemory() {
if u.noCache {
return
}
u.purgeMemCache()
}
const UPK2MinorVersionCurrent = keybase1.UPK2MinorVersion_V5
func (u *CachedUPAKLoader) getCachedUPAK(ctx context.Context, uid keybase1.UID, info *CachedUserLoadInfo) (*keybase1.UserPlusKeysV2AllIncarnations, bool) {
if u.Freshness == time.Duration(0) || u.noCache {
u.G().VDL.CLogf(ctx, VLog0, "| cache miss since cache disabled")
return nil, false
}
upak := u.getMemCache(ctx, uid)
// Try loading from persistent storage if we missed memory cache.
if upak != nil {
// Note that below we check the minor version and then discard the cached object if it's
// stale. But no need in memory, since we'll never have the old version in memory.
u.G().VDL.CLogf(ctx, VLog0, "| hit memory cache")
if info != nil {
info.InCache = true
}
} else {
var tmp keybase1.UserPlusKeysV2AllIncarnations
found, err := u.G().LocalDb.GetInto(&tmp, culDBKeyV2(uid))
if err != nil {
u.G().Log.CWarningf(ctx, "trouble accessing UserPlusKeysV2AllIncarnations cache: %s", err)
} else if !found {
u.G().VDL.CLogf(ctx, VLog0, "| missed disk cache")
} else if tmp.MinorVersion != UPK2MinorVersionCurrent {
u.G().VDL.CLogf(ctx, VLog0, "| found old minor version %d, but wanted %d; will overwrite with fresh UPAK", tmp.MinorVersion, UPK2MinorVersionCurrent)
} else {
u.G().VDL.CLogf(ctx, VLog0, "| hit disk cache")
upak = &tmp
if info != nil {
info.InDiskCache = true
}
// Insert disk object into memory.
u.putMemCache(ctx, uid, tmp)
}
}
if upak == nil {
u.G().VDL.CLogf(ctx, VLog0, "| missed cache")
return nil, true
}
diff := u.G().Clock().Now().Sub(keybase1.FromTime(upak.Uvv.CachedAt))
fresh := (diff <= u.Freshness)
if fresh {
u.G().VDL.CLogf(ctx, VLog0, "| cache hit was fresh (cached %s ago)", diff)
} else {
u.G().VDL.CLogf(ctx, VLog0, "| cache hit was stale (by %s)", u.Freshness-diff)
}
return upak, fresh
}
type CachedUserLoadInfo struct {
InCache bool
InDiskCache bool
TimedOut bool
StaleVersion bool
LoadedLeaf bool
LoadedUser bool
}
func (u *CachedUPAKLoader) Disable() {
u.Freshness = time.Duration(0)
}
func culDebug(u keybase1.UID) string {
return fmt.Sprintf("CachedUPAKLoader#Load(%s)", u)
}
func (u *CachedUPAKLoader) extractDeviceKey(upak keybase1.UserPlusAllKeys, deviceID keybase1.DeviceID) (deviceKey *keybase1.PublicKey, revoked *keybase1.RevokedKey, err error) {
for i := range upak.Base.RevokedDeviceKeys {
r := &upak.Base.RevokedDeviceKeys[i]
pk := &r.Key
if pk.DeviceID == deviceID {
deviceKey = pk
revoked = r
}
}
for i := range upak.Base.DeviceKeys {
pk := &upak.Base.DeviceKeys[i]
if pk.DeviceID == deviceID {
deviceKey = pk
revoked = nil
}
}
if deviceKey == nil {
dkey := fmt.Sprintf("%s:%s", upak.Base.Uid, deviceID)
return nil, nil, fmt.Errorf("device not found for %s", dkey)
}
return deviceKey, revoked, nil
}
func (u *CachedUPAKLoader) putUPAKToCache(ctx context.Context, obj *keybase1.UserPlusKeysV2AllIncarnations) error {
if u.noCache {
u.G().VDL.CLogf(ctx, VLog0, "| no cache enabled, so not putting UPAK")
return nil
}
uid := obj.Current.Uid
u.G().VDL.CLogf(ctx, VLog0, "| Caching UPAK for %s", uid)
stale := false
existing := u.getMemCache(ctx, uid)
if existing != nil {
if obj.IsOlderThan(*existing) {
stale = true
} else {
u.putMemCache(ctx, uid, *obj)
}
} else {
u.putMemCache(ctx, uid, *obj)
}
if stale {
u.G().VDL.CLogf(ctx, VLog0, "| CachedUpakLoader#putUPAKToCache: Refusing to overwrite with stale object")
return errors.New("stale object rejected")
}
err := u.G().LocalDb.PutObj(culDBKeyV2(uid), nil, *obj)
if err != nil {
u.G().Log.CWarningf(ctx, "Error in writing UPAK for %s: %s", uid, err)
}
u.deleteV1UPAK(uid)
return err
}
func (u *CachedUPAKLoader) PutUserToCache(ctx context.Context, user *User) error {
lock := u.locktab.AcquireOnName(ctx, u.G(), user.GetUID().String())
defer lock.Release(ctx)
upak, err := user.ExportToUPKV2AllIncarnations()
if err != nil {
return err
}
upak.Uvv.CachedAt = keybase1.ToTime(u.G().Clock().Now())
err = u.putUPAKToCache(ctx, upak)
return err
}
// loadWithInfo loads a user from the CachedUPAKLoader object. The 'info'
// object contains information about how the request was handled, but otherwise,
// this method behaves like (and implements) the public CachedUPAKLoader#Load
// method below. If `accessor` is nil, then a deep copy of the UPAK is returned.
// In some cases, that deep copy can be expensive, so as for users who have lots of
// followees. So if you provide accessor, the UPAK won't be deep-copied, but you'll
// be able to access it from inside the accessor with exclusion.
func (u *CachedUPAKLoader) loadWithInfo(arg LoadUserArg, info *CachedUserLoadInfo, accessor func(k *keybase1.UserPlusKeysV2AllIncarnations) error, shouldReturnFullUser bool) (ret *keybase1.UserPlusKeysV2AllIncarnations, user *User, err error) {
// Shorthand
g := u.G()
// Add a LU= tax to this context, for all subsequent debugging
ctx := arg.WithLogTag()
defer g.CVTrace(ctx, VLog0, culDebug(arg.uid), func() error { return err })()
if arg.uid.IsNil() {
if len(arg.name) == 0 {
return nil, nil, errors.New("need a UID or username to load UPAK from loader")
}
// Modifies the load arg, setting a UID
arg.uid, err = u.LookupUID(ctx, NewNormalizedUsername(arg.name))
if err != nil {
return nil, nil, err
}
}
lock := u.locktab.AcquireOnName(ctx, g, arg.uid.String())
defer func() {
lock.Release(ctx)
if !shouldReturnFullUser {
user = nil
}
if user != nil && err == nil {
// Update the full-self cacher after the lock is released, to avoid
// any circular locking.
if fs := u.G().GetFullSelfer(); fs != nil && arg.self {
fs.Update(ctx, user)
}
}
}()
returnUPAK := func(upak *keybase1.UserPlusKeysV2AllIncarnations, needCopy bool) (*keybase1.UserPlusKeysV2AllIncarnations, *User, error) {
if accessor != nil {
err := accessor(upak)
if err != nil {
return nil, nil, err
}
return nil, user, err
}
if needCopy {
tmp := upak.DeepCopy()
upak = &tmp
}
return upak, user, nil
}
var upak *keybase1.UserPlusKeysV2AllIncarnations
var fresh bool
if !arg.forceReload {
upak, fresh = u.getCachedUPAK(ctx, arg.uid, info)
}
if arg.forcePoll {
g.VDL.CLogf(ctx, VLog0, "%s: force-poll required us to repoll (fresh=%v)", culDebug(arg.uid), fresh)
fresh = false
}
if upak != nil {
g.VDL.CLogf(ctx, VLog0, "%s: cache-hit; fresh=%v", culDebug(arg.uid), fresh)
if fresh || arg.staleOK {
return returnUPAK(upak, true)
}
if info != nil {
info.TimedOut = true
}
var sigHints *SigHints
var leaf *MerkleUserLeaf
sigHints, leaf, err = lookupSigHintsAndMerkleLeaf(ctx, u.G(), arg.uid, true)
if err != nil {
return nil, nil, err
}
if info != nil {
info.LoadedLeaf = true
}
if leaf.eldest == "" {
g.VDL.CLogf(ctx, VLog0, "%s: cache-hit; but user is nuked, evicting", culDebug(arg.uid))
// Our cached user turned out to be in reset state (without
// current sigchain), remove from cache, and then fall
// through. LoadUser shall return an error, which we will
// return to the caller.
u.removeMemCache(ctx, arg.uid)
err := u.G().LocalDb.Delete(culDBKeyV2(arg.uid))
if err != nil {
u.G().Log.Warning("Failed to remove %s from disk cache: %s", arg.uid, err)
}
u.deleteV1UPAK(arg.uid)
} else if leaf.public != nil && leaf.public.Seqno == keybase1.Seqno(upak.Uvv.SigChain) {
g.VDL.CLogf(ctx, VLog0, "%s: cache-hit; fresh after poll", culDebug(arg.uid))
upak.Uvv.CachedAt = keybase1.ToTime(g.Clock().Now())
// This is only necessary to update the levelDB representation,
// since the previous line updates the in-memory cache satisfactorially.
if err := u.putUPAKToCache(ctx, upak); err != nil {
u.G().Log.CDebugf(ctx, "continuing past error in putUPAKToCache: %s", err)
}
return returnUPAK(upak, true)
}
if info != nil {
info.StaleVersion = true
}
arg.sigHints = sigHints
arg.merkleLeaf = leaf
} else if arg.cachedOnly {
return nil, nil, UserNotFoundError{UID: arg.uid, Msg: "no cached user found"}
}
g.VDL.CLogf(ctx, VLog0, "%s: LoadUser", culDebug(arg.uid))
user, err = LoadUser(arg)
if info != nil {
info.LoadedUser = true
}
if user != nil {
// The `err` value might be non-nil above! Don't overwrite it.
var exportErr error
ret, exportErr = user.ExportToUPKV2AllIncarnations()
if exportErr != nil {
return nil, nil, exportErr
}
ret.Uvv.CachedAt = keybase1.ToTime(g.Clock().Now())
}
// In some cases, it's OK to have a user object and an error. This comes up in
// Identify2 when identifying users who don't have a sigchain. Note that we'll never
// hit the cache in this case (for now...)
if err != nil {
return ret, user, err
}
if user == nil {
return nil, nil, UserNotFoundError{UID: arg.uid, Msg: "LoadUser failed"}
}
if err := u.putUPAKToCache(ctx, ret); err != nil {
u.G().Log.CDebugf(ctx, "continuing past error in putUPAKToCache: %s", err)
}
if u.TestDeadlocker != nil {
u.TestDeadlocker()
}
return returnUPAK(ret, false)
}
// Load a UserPlusKeysV2AllIncarnations from the local cache, falls back to
// LoadUser, and cache the user. Can only perform lookups by UID. Will return a
// non-nil UserPlusKeysV2AllIncarnations, or a non-nil error, but never both
// non-nil, nor never both nil. If we had to do a full LoadUser as part of the
// request, it's returned too. Convert to UserPlusAllKeys on the way out, for
// backwards compatibility.
func (u *CachedUPAKLoader) Load(arg LoadUserArg) (*keybase1.UserPlusAllKeys, *User, error) {
ret, user, err := u.loadWithInfo(arg, nil, nil, true)
// NOTE -- it's OK to return an error and a user, since certain code paths
// want both (see note in loadWithInfo).
var converted *keybase1.UserPlusAllKeys
if ret != nil {
tmp := keybase1.UPAKFromUPKV2AI(*ret)
converted = &tmp
}
return converted, user, err
}
// Load a UserPlusKeysV2AllIncarnations from the local cache, falls back to
// LoadUser, and cache the user. Can only perform lookups by UID. Will return a
// non-nil UserPlusKeysV2AllIncarnations, or a non-nil error, but never both
// non-nil, nor never both nil. If we had to do a full LoadUser as part of the
// request, it's returned too.
func (u *CachedUPAKLoader) LoadV2(arg LoadUserArg) (*keybase1.UserPlusKeysV2AllIncarnations, *User, error) {
return u.loadWithInfo(arg, nil, nil, true)
}
func (u *CachedUPAKLoader) CheckKIDForUID(ctx context.Context, uid keybase1.UID, kid keybase1.KID) (found bool, revokedAt *keybase1.KeybaseTime, deleted bool, err error) {
var info CachedUserLoadInfo
larg := NewLoadUserByUIDArg(ctx, u.G(), uid).WithPublicKeyOptional()
upak, _, err := u.loadWithInfo(larg, &info, nil, false)
if err != nil {
return false, nil, false, err
}
found, revokedAt, deleted = CheckKID(upak, kid)
if found || info.LoadedLeaf || info.LoadedUser {
return found, revokedAt, deleted, nil
}
larg = larg.WithForceReload()
upak, _, err = u.loadWithInfo(larg, nil, nil, false)
if err != nil {
return false, nil, false, err
}
found, revokedAt, deleted = CheckKID(upak, kid)
return found, revokedAt, deleted, nil
}
func (u *CachedUPAKLoader) LoadUserPlusKeys(ctx context.Context, uid keybase1.UID, pollForKID keybase1.KID) (keybase1.UserPlusKeys, error) {
var up keybase1.UserPlusKeys
if uid.IsNil() {
return up, NoUIDError{}
}
arg := NewLoadUserArg(u.G()).WithUID(uid).WithPublicKeyOptional().WithNetContext(ctx)
forcePollValues := []bool{false, true}
for _, fp := range forcePollValues {
arg = arg.WithForcePoll(fp)
upak, _, err := u.Load(arg)
if err != nil {
return up, err
}
if upak == nil {
return up, fmt.Errorf("Nil user, nil error from LoadUser")
}
up = upak.Base
if pollForKID.IsNil() || up.FindKID(pollForKID) != nil {
break
}
}
return up, nil
}
// LoadKeyV2 looks through all incarnations for the user and returns the incarnation with the given
// KID, as well as the Key data associated with that KID. It picks the latest such
// incarnation if there are multiple.
func (u *CachedUPAKLoader) LoadKeyV2(ctx context.Context, uid keybase1.UID, kid keybase1.KID) (ret *keybase1.UserPlusKeysV2, key *keybase1.PublicKeyV2NaCl, linkMap map[keybase1.Seqno]keybase1.LinkID, err error) {
defer u.G().CVTrace(ctx, VLog0, fmt.Sprintf("LoadKeyV2 uid:%s,kid:%s", uid, kid), func() error { return err })()
if uid.IsNil() {
return nil, nil, nil, NoUIDError{}
}
argBase := NewLoadUserArg(u.G()).WithUID(uid).WithPublicKeyOptional().WithNetContext(ctx)
// Make the retry mechanism increasingly aggresive. See CORE-8851.
// It should be that a ForcePoll is good enough, but in some rare cases,
// people have cached values for previous pre-reset user incarnations that
// were incorrect. So clobber over that if it comes to it.
attempts := []LoadUserArg{
argBase,
argBase.WithForcePoll(true),
argBase.WithForceReload(),
}
for _, arg := range attempts {
u.G().VDL.CLogf(ctx, VLog0, "| reloading with arg: %s", arg.String())
upak, _, err := u.LoadV2(arg)
if err != nil {
return nil, nil, nil, err
}
if upak == nil {
return nil, nil, nil, fmt.Errorf("Nil user, nil error from LoadUser")
}
linkMap = upak.SeqnoLinkIDs
ret, key = upak.FindKID(kid)
if key != nil {
u.G().VDL.CLogf(ctx, VLog0, "- found kid in UPAK: %v", ret.Uid)
return ret, key, linkMap, nil
}
ret = nil
}
return nil, nil, nil, NotFoundError{Msg: "Not found: User"}
}
func (u *CachedUPAKLoader) Invalidate(ctx context.Context, uid keybase1.UID) {
u.G().VDL.CLogf(ctx, VLog0, "| CachedUPAKLoader#Invalidate(%s)", uid)
if u.noCache {
return
}
lock := u.locktab.AcquireOnName(ctx, u.G(), uid.String())
defer lock.Release(ctx)
u.removeMemCache(ctx, uid)
err := u.G().LocalDb.Delete(culDBKeyV2(uid))
if err != nil {
u.G().Log.CWarningf(ctx, "Failed to remove %s from disk cache: %s", uid, err)
}
u.deleteV1UPAK(uid)
}
// Load the PublicKey for a user's device from the local cache, falling back to LoadUser, and cache the user.
// If the user exists but the device doesn't, will force a load in case the device is very new.
func (u *CachedUPAKLoader) LoadDeviceKey(ctx context.Context, uid keybase1.UID, deviceID keybase1.DeviceID) (upakv1 *keybase1.UserPlusAllKeys, deviceKey *keybase1.PublicKey, revoked *keybase1.RevokedKey, err error) {
var info CachedUserLoadInfo
larg := NewLoadUserByUIDArg(ctx, u.G(), uid)
upakV2, _, err := u.loadWithInfo(larg, &info, nil, false)
if err != nil {
return nil, nil, nil, err
}
upakV1 := keybase1.UPAKFromUPKV2AI(*upakV2)
deviceKey, revoked, err = u.extractDeviceKey(upakV1, deviceID)
if err == nil {
// Early success, return
return &upakV1, deviceKey, revoked, err
}
// Try again with a forced load in case the device is very new.
larg = larg.WithForcePoll(true)
upakV2, _, err = u.loadWithInfo(larg, nil, nil, false)
if err != nil {
return nil, nil, nil, err
}
upakV1 = keybase1.UPAKFromUPKV2AI(*upakV2)
deviceKey, revoked, err = u.extractDeviceKey(upakV1, deviceID)
return &upakV1, deviceKey, revoked, err
}
// LookupUsername uses the UIDMapper to find a username for uid.
func (u *CachedUPAKLoader) LookupUsername(ctx context.Context, uid keybase1.UID) (NormalizedUsername, error) {
var empty NormalizedUsername
uids := []keybase1.UID{uid}
namePkgs, err := u.G().UIDMapper.MapUIDsToUsernamePackages(ctx, u.G(), uids, 0, 0, false)
if err != nil {
return empty, err
}
if len(namePkgs) == 0 {
return empty, UserNotFoundError{UID: uid, Msg: "in CachedUPAKLoader"}
}
if u.TestDeadlocker != nil {
u.TestDeadlocker()
}
return namePkgs[0].NormalizedUsername, nil
}
// LookupUsernameUPAK uses the upak loader to find a username for uid.
func (u *CachedUPAKLoader) LookupUsernameUPAK(ctx context.Context, uid keybase1.UID) (NormalizedUsername, error) {
var info CachedUserLoadInfo
arg := NewLoadUserByUIDArg(ctx, u.G(), uid).WithStaleOK(true).WithPublicKeyOptional()
var ret NormalizedUsername
_, _, err := u.loadWithInfo(arg, &info, func(upak *keybase1.UserPlusKeysV2AllIncarnations) error {
if upak == nil {
return UserNotFoundError{UID: uid, Msg: "in CachedUPAKLoader"}
}
ret = NewNormalizedUsername(upak.Current.Username)
return nil
}, false)
return ret, err
}
// LookupUID is a verified map of username -> UID. IT calls into the resolver, which gives un untrusted
// UID, but verifies with the UPAK loader that the mapping UID -> username is correct.
func (u *CachedUPAKLoader) LookupUID(ctx context.Context, un NormalizedUsername) (keybase1.UID, error) {
rres := u.G().Resolver.Resolve(un.String())
if err := rres.GetError(); err != nil {
return keybase1.UID(""), err
}
un2, err := u.LookupUsername(ctx, rres.GetUID())
if err != nil {
return keybase1.UID(""), err
}
if !un.Eq(un2) {
u.G().Log.CWarningf(ctx, "Unexpected mismatched usernames (uid=%s): %s != %s", rres.GetUID(), un.String(), un2.String())
return keybase1.UID(""), NewBadUsernameError(un.String())
}
return rres.GetUID(), nil
}
func (u *CachedUPAKLoader) lookupUsernameAndDeviceWithInfo(ctx context.Context, uid keybase1.UID, did keybase1.DeviceID, info *CachedUserLoadInfo) (username NormalizedUsername, deviceName string, deviceType string, err error) {
arg := NewLoadUserByUIDArg(ctx, u.G(), uid)
// First iteration through, say it's OK to load a stale user. Note that the
// mappings of UID to Username and DeviceID to DeviceName are immutable, so this
// data can never be stale. However, our user might be out of date and lack the
// mappings, so the second time through, we request a fresh object.
staleOK := []bool{true, false}
for _, b := range staleOK {
arg = arg.WithStaleOK(b)
found := false
u.loadWithInfo(arg, info, func(upak *keybase1.UserPlusKeysV2AllIncarnations) error {
if upak == nil {
return nil
}
if pk := upak.FindDevice(did); pk != nil {
username = NewNormalizedUsername(upak.Current.Username)
deviceName = pk.DeviceDescription
deviceType = pk.DeviceType
found = true
}
return nil
}, false)
if found {
return username, deviceName, deviceType, nil
}
}
if err == nil {
err = NotFoundError{fmt.Sprintf("UID/Device pair %s/%s not found", uid, did)}
}
return NormalizedUsername(""), "", "", err
}
func (u *CachedUPAKLoader) LookupUsernameAndDevice(ctx context.Context, uid keybase1.UID, did keybase1.DeviceID) (username NormalizedUsername, deviceName string, deviceType string, err error) {
return u.lookupUsernameAndDeviceWithInfo(ctx, uid, did, nil)
}
func (u *CachedUPAKLoader) ListFollowedUIDs(uid keybase1.UID) ([]keybase1.UID, error) {
arg := NewLoadUserByUIDArg(nil, u.G(), uid)
upak, _, err := u.Load(arg)
if err != nil {
return nil, err
}
var ret []keybase1.UID
for _, t := range upak.RemoteTracks {
ret = append(ret, t.Uid)
}
return ret, nil
}
// v1 UPAKs are all legacy and need to be gradually cleaned from cache.
func (u *CachedUPAKLoader) deleteV1UPAK(uid keybase1.UID) {
err := u.G().LocalDb.Delete(culDBKeyV1(uid))
if err != nil {
u.G().Log.Warning("Failed to remove %s v1 object from disk cache: %s", uid, err)
}
}
func (u *CachedUPAKLoader) getMemCache(ctx context.Context, uid keybase1.UID) *keybase1.UserPlusKeysV2AllIncarnations {
val, ok := u.cache.Get(uid)
if !ok {
return nil
}
upak, ok := val.(keybase1.UserPlusKeysV2AllIncarnations)
if !ok {
u.G().Log.CWarningf(ctx, "invalid type in upak cache: %T", val)
return nil
}
return &upak
}
func (u *CachedUPAKLoader) putMemCache(ctx context.Context, uid keybase1.UID, upak keybase1.UserPlusKeysV2AllIncarnations) {
u.cache.Add(uid, upak)
}
func (u *CachedUPAKLoader) removeMemCache(ctx context.Context, uid keybase1.UID) {
u.cache.Remove(uid)
}
func (u *CachedUPAKLoader) purgeMemCache() {
u.cache.Purge()
}