forked from git-lfs/lfs-test-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeta_store.go
337 lines (275 loc) · 6.75 KB
/
meta_store.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
package main
import (
"bytes"
"encoding/base64"
"encoding/gob"
"errors"
"fmt"
"strings"
"time"
"github.com/boltdb/bolt"
"github.com/dgrijalva/jwt-go"
)
// MetaStore implements a metadata storage. It stores user credentials and Meta information
// for objects. The storage is handled by boltdb.
type MetaStore struct {
db *bolt.DB
}
var (
errNoBucket = errors.New("Bucket not found")
errObjectNotFound = errors.New("Object not found")
)
var (
usersBucket = []byte("users")
objectsBucket = []byte("objects")
)
// NewMetaStore creates a new MetaStore using the boltdb database at dbFile.
func NewMetaStore(dbFile string) (*MetaStore, error) {
db, err := bolt.Open(dbFile, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return nil, err
}
db.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucketIfNotExists(usersBucket); err != nil {
return err
}
if _, err := tx.CreateBucketIfNotExists(objectsBucket); err != nil {
return err
}
return nil
})
return &MetaStore{db: db}, nil
}
// Get retrieves the Meta information for an object given information in
// RequestVars
func (s *MetaStore) Get(v *RequestVars) (*MetaObject, error) {
if !s.authenticate(v.Authorization, false) {
return nil, newAuthError()
}
meta, error := s.UnsafeGet(v)
return meta, error
}
// Get retrieves the Meta information for an object given information in
// RequestVars
// DO NOT CHECK authentication, as it is supposed to have been done before
func (s *MetaStore) UnsafeGet(v *RequestVars) (*MetaObject, error) {
var meta MetaObject
err := s.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(objectsBucket)
if bucket == nil {
return errNoBucket
}
value := bucket.Get([]byte(v.Oid))
if len(value) == 0 {
return errObjectNotFound
}
dec := gob.NewDecoder(bytes.NewBuffer(value))
return dec.Decode(&meta)
})
if err != nil {
return nil, err
}
return &meta, nil
}
// Put writes meta information from RequestVars to the store.
func (s *MetaStore) Put(v *RequestVars) (*MetaObject, error) {
if !s.authenticate(v.Authorization, true) {
return nil, newAuthError()
}
// Check if it exists first
if meta, err := s.Get(v); err == nil {
meta.Existing = true
return meta, nil
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
meta := MetaObject{Oid: v.Oid, Size: v.Size}
err := enc.Encode(meta)
if err != nil {
return nil, err
}
err = s.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(objectsBucket)
if bucket == nil {
return errNoBucket
}
err = bucket.Put([]byte(v.Oid), buf.Bytes())
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return &meta, nil
}
// Delete removes the meta information from RequestVars to the store.
func (s *MetaStore) Delete(v *RequestVars) error {
if !s.authenticate(v.Authorization, true) {
return newAuthError()
}
err := s.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(objectsBucket)
if bucket == nil {
return errNoBucket
}
err := bucket.Delete([]byte(v.Oid))
if err != nil {
return err
}
return nil
})
return err
}
// Close closes the underlying boltdb.
func (s *MetaStore) Close() {
s.db.Close()
}
// AddUser adds user credentials to the meta store.
func (s *MetaStore) AddUser(user, pass string) error {
err := s.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(usersBucket)
if bucket == nil {
return errNoBucket
}
err := bucket.Put([]byte(user), []byte(pass))
if err != nil {
return err
}
return nil
})
return err
}
// DeleteUser removes user credentials from the meta store.
func (s *MetaStore) DeleteUser(user string) error {
err := s.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(usersBucket)
if bucket == nil {
return errNoBucket
}
err := bucket.Delete([]byte(user))
return err
})
return err
}
// MetaUser encapsulates information about a meta store user
type MetaUser struct {
Name string
}
// Users returns all MetaUsers in the meta store
func (s *MetaStore) Users() ([]*MetaUser, error) {
var users []*MetaUser
err := s.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(usersBucket)
if bucket == nil {
return errNoBucket
}
bucket.ForEach(func(k, v []byte) error {
users = append(users, &MetaUser{string(k)})
return nil
})
return nil
})
return users, err
}
// Objects returns all MetaObjects in the meta store
func (s *MetaStore) Objects() ([]*MetaObject, error) {
var objects []*MetaObject
err := s.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(objectsBucket)
if bucket == nil {
return errNoBucket
}
bucket.ForEach(func(k, v []byte) error {
var meta MetaObject
dec := gob.NewDecoder(bytes.NewBuffer(v))
err := dec.Decode(&meta)
if err != nil {
return err
}
objects = append(objects, &meta)
return nil
})
return nil
})
return objects, err
}
// authenticate uses the authorization string to determine whether
// or not to proceed. This server assumes an HTTP Basic auth format.
func (s *MetaStore) authenticate(authorization string, requireWrite bool) bool {
if Config.IsPublic() {
return true
}
if authorization == "" {
return false
}
if s.authenticateToken(authorization, requireWrite) {
return true
}
if !strings.HasPrefix(authorization, "Basic ") {
return false
}
c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(authorization, "Basic "))
if err != nil {
return false
}
cs := string(c)
i := strings.IndexByte(cs, ':')
if i < 0 {
return false
}
user, password := cs[:i], cs[i+1:]
// check Basic Authentication (admin)
ok := checkBasicAuth(user, password, true)
if ok {
return true
}
value := ""
s.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(usersBucket)
if bucket == nil {
return errNoBucket
}
value = string(bucket.Get([]byte(user)))
return nil
})
if value != "" && value == password {
return true
}
return false
}
func (s *MetaStore) authenticateToken(authorization string, requireWrite bool) bool {
if !strings.HasPrefix(authorization, "Bearer ") {
return false
}
token, err := jwt.Parse(authorization[7:], func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
}
return Config.DecodeSecret()
})
if err != nil {
return false
}
claims, claimsOk := token.Claims.(jwt.MapClaims)
if !token.Valid || !claimsOk {
return false
}
opstr, ok := claims["op"].(string)
if !ok {
return false
}
op := strings.ToLower(strings.TrimSpace(opstr))
status := op == "upload" || (op == "download" && !requireWrite)
return status
}
type authError struct {
error
}
func (e authError) AuthError() bool {
return true
}
func newAuthError() error {
return authError{errors.New("Forbidden")}
}