forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
229 lines (188 loc) · 5.24 KB
/
storage.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
package teams
import (
"fmt"
"log"
"sync"
lru "github.com/hashicorp/golang-lru"
context "golang.org/x/net/context"
"github.com/keybase/client/go/encrypteddb"
"github.com/keybase/client/go/engine"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
)
// Store TeamData's on memory and disk. Threadsafe.
type Storage struct {
libkb.Contextified
sync.Mutex
mem *MemoryStorage
disk *DiskStorage
}
func NewStorage(g *libkb.GlobalContext) *Storage {
return &Storage{
Contextified: libkb.NewContextified(g),
mem: NewMemoryStorage(g),
disk: NewDiskStorage(g),
}
}
func (s *Storage) Put(ctx context.Context, state *keybase1.TeamData) {
s.Lock()
defer s.Unlock()
s.mem.Put(ctx, state)
err := s.disk.Put(ctx, state)
if err != nil {
s.G().Log.CWarningf(ctx, "teams.Storage.Put err: %v", err)
}
}
// Can return nil.
func (s *Storage) Get(ctx context.Context, teamID keybase1.TeamID) *keybase1.TeamData {
s.Lock()
defer s.Unlock()
item := s.mem.Get(ctx, teamID)
if item != nil {
// Mem hit
return item
}
res, found, err := s.disk.Get(ctx, teamID)
if found && err == nil {
// Disk hit
s.mem.Put(ctx, res)
return res
}
if err != nil {
s.G().Log.Debug("teams.Storage#Get disk err: %v", err)
}
return nil
}
func (s *Storage) onLogout() {
s.mem.onLogout()
}
// --------------------------------------------------
// Store TeamData's on disk. Threadsafe.
type DiskStorage struct {
libkb.Contextified
sync.Mutex
encryptedDB *encrypteddb.EncryptedDB
}
// Increment to invalidate the disk cache.
const diskStorageVersion = 3
type DiskStorageItem struct {
Version int `codec:"V"`
State *keybase1.TeamData `codec:"S"`
}
func NewDiskStorage(g *libkb.GlobalContext) *DiskStorage {
keyFn := func(ctx context.Context) ([32]byte, error) {
return getLocalStorageSecretBoxKey(ctx, g)
}
dbFn := func(g *libkb.GlobalContext) *libkb.JSONLocalDb {
return g.LocalDb
}
return &DiskStorage{
Contextified: libkb.NewContextified(g),
encryptedDB: encrypteddb.New(g, dbFn, keyFn),
}
}
func (s *DiskStorage) Put(ctx context.Context, state *keybase1.TeamData) error {
s.Lock()
defer s.Unlock()
key := s.dbKey(ctx, state.Chain.Id)
item := DiskStorageItem{
Version: diskStorageVersion,
State: state,
}
err := s.encryptedDB.Put(ctx, key, item)
if err != nil {
return err
}
return nil
}
// Res is valid if (found && err == nil)
func (s *DiskStorage) Get(ctx context.Context, teamID keybase1.TeamID) (res *keybase1.TeamData, found bool, err error) {
s.Lock()
defer s.Unlock()
key := s.dbKey(ctx, teamID)
var item DiskStorageItem
found, err = s.encryptedDB.Get(ctx, key, &item)
if (err != nil) || !found {
return res, found, err
}
if item.Version != diskStorageVersion {
// Pretend it wasn't found.
return res, false, nil
}
// Sanity check
if len(item.State.Chain.Id) == 0 {
return res, false, fmt.Errorf("decode from disk had empty team id")
}
if !item.State.Chain.Id.Eq(teamID) {
return res, false, fmt.Errorf("decode from disk had wrong team id %v != %v", item.State.Chain.Id, teamID)
}
return item.State, true, nil
}
func (s *DiskStorage) dbKey(ctx context.Context, teamID keybase1.TeamID) libkb.DbKey {
return libkb.DbKey{
Typ: libkb.DBChatInbox,
Key: fmt.Sprintf("tid:%s", teamID),
}
}
// --------------------------------------------------
const MemCacheLRUSize = 50
// Store some TeamSigChainState's in memory. Threadsafe.
type MemoryStorage struct {
libkb.Contextified
lru *lru.Cache
}
func NewMemoryStorage(g *libkb.GlobalContext) *MemoryStorage {
nlru, err := lru.New(MemCacheLRUSize)
if err != nil {
// lru.New only panics if size <= 0
log.Panicf("Could not create lru cache: %v", err)
}
return &MemoryStorage{
Contextified: libkb.NewContextified(g),
lru: nlru,
}
}
func (s *MemoryStorage) Put(ctx context.Context, state *keybase1.TeamData) {
s.lru.Add(state.Chain.Id, state)
}
// Can return nil.
func (s *MemoryStorage) Get(ctx context.Context, teamID keybase1.TeamID) *keybase1.TeamData {
untyped, ok := s.lru.Get(teamID)
if !ok {
return nil
}
state, ok := untyped.(*keybase1.TeamData)
if !ok {
s.G().Log.Warning("Team MemoryStorage got bad type from lru: %T", untyped)
return nil
}
return state
}
func (s *MemoryStorage) onLogout() {
s.lru.Purge()
}
// --------------------------------------------------
func getLocalStorageSecretBoxKey(ctx context.Context, g *libkb.GlobalContext) (fkey [32]byte, err error) {
// Get secret device key
encKey, err := engine.GetMySecretKey(ctx, g, getLameSecretUI, libkb.DeviceEncryptionKeyType,
"encrypt teams storage")
if err != nil {
return fkey, err
}
kp, ok := encKey.(libkb.NaclDHKeyPair)
if !ok || kp.Private == nil {
return fkey, libkb.KeyCannotDecryptError{}
}
// Derive symmetric key from device key
skey, err := encKey.SecretSymmetricKey(libkb.EncryptionReasonTeamsLocalStorage)
if err != nil {
return fkey, err
}
copy(fkey[:], skey[:])
return fkey, nil
}
type LameSecretUI struct{}
func (d LameSecretUI) GetPassphrase(pinentry keybase1.GUIEntryArg, terminal *keybase1.SecretEntryArg) (keybase1.GetPassphraseRes, error) {
return keybase1.GetPassphraseRes{}, fmt.Errorf("no secret UI available")
}
var getLameSecretUI = func() libkb.SecretUI { return LameSecretUI{} }