forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
impl.go
419 lines (380 loc) · 11.3 KB
/
impl.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
/*
Copyright 2016-2017 Gravitational, Inc.
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 dir
import (
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"time"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
log "github.com/sirupsen/logrus"
)
const (
defaultDirMode os.FileMode = 0770
defaultFileMode os.FileMode = 0600
// name of this backend type (as seen in 'storage/type' in YAML)
backendName = "dir"
// selfLock is the lock used internally for compare-and-swap
selfLock = ".backend"
// subdirectory where locks are stored
locksBucket = ".locks"
// reservedPrefix is a character which bucket/key names cannot begin with
reservedPrefix = '.'
)
// fs.Backend implements backend.Backend interface using a regular
// POSIX-style filesystem
type Backend struct {
// RootDir is the root (home) directory where the backend
// stores all the data.
RootDir string
// InternalClock is a test-friendly source of current time
InternalClock clockwork.Clock
*log.Entry
}
func (b *Backend) Clock() clockwork.Clock {
return b.InternalClock
}
// GetName
func GetName() string {
return backendName
}
// New creates a new instance of Filesystem backend, it conforms to backend.NewFunc API
func New(params backend.Params) (backend.Backend, error) {
rootDir := params.GetString("path")
if rootDir == "" {
rootDir = params.GetString("data_dir")
}
if rootDir == "" {
return nil, trace.BadParameter("filesystem backend: 'path' is not set")
}
bk := &Backend{
RootDir: rootDir,
InternalClock: clockwork.NewRealClock(),
Entry: log.WithFields(log.Fields{
trace.Component: "backend:dir",
trace.ComponentFields: log.Fields{
"dir": rootDir,
},
}),
}
locksDir := path.Join(bk.RootDir, locksBucket)
if err := os.MkdirAll(locksDir, defaultDirMode); err != nil {
return nil, trace.ConvertSystemError(err)
}
return bk, nil
}
// GetItems is a function that returns keys in batch
func (bk *Backend) GetItems(bucket []string) ([]backend.Item, error) {
keys, err := bk.GetKeys(bucket)
if err != nil {
return nil, trace.Wrap(err)
}
var items []backend.Item
for _, key := range keys {
v, err := bk.GetVal(bucket, key)
if err != nil {
return nil, trace.Wrap(err)
}
items = append(items, backend.Item{Key: key, Value: v})
}
return items, nil
}
// GetKeys returns a list of keys for a given path
func (bk *Backend) GetKeys(bucket []string) ([]string, error) {
files, err := ioutil.ReadDir(path.Join(bk.RootDir, path.Join(bucket...)))
if err != nil {
if os.IsNotExist(err) {
return []string{}, nil
}
return nil, trace.ConvertSystemError(err)
}
// enumerate all directory entries and select only non-hidden files
retval := make([]string, 0)
for _, fi := range files {
name := fi.Name()
// legal keys cannot start with '.' (resrved prefix)
if name[0] != reservedPrefix {
retval = append(retval, name)
}
}
return retval, nil
}
// CreateVal creates value with a given TTL and key in the bucket
// if the value already exists, returns AlreadyExistsError
func (bk *Backend) CreateVal(bucket []string, key string, val []byte, ttl time.Duration) error {
// do not allow keys that start with a dot
if key[0] == reservedPrefix {
return trace.BadParameter("invalid key: '%s'. Key names cannot start with '.'", key)
}
// create the directory:
dirPath := path.Join(bk.RootDir, path.Join(bucket...))
err := os.MkdirAll(dirPath, defaultDirMode)
if err != nil {
return trace.ConvertSystemError(err)
}
// create the file (AKA "key"):
filename := path.Join(dirPath, key)
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, defaultFileMode)
if err != nil {
if os.IsExist(err) {
return trace.AlreadyExists("%s/%s already exists", dirPath, key)
}
return trace.ConvertSystemError(err)
}
defer f.Close()
if err := utils.FSWriteLock(f); err != nil {
return trace.Wrap(err)
}
defer utils.FSUnlock(f)
if err := f.Truncate(0); err != nil {
return trace.ConvertSystemError(err)
}
n, err := f.Write(val)
if err == nil && n < len(val) {
return trace.Wrap(io.ErrShortWrite)
}
return trace.Wrap(bk.applyTTL(dirPath, key, ttl))
}
// UpsertVal updates or inserts value with a given TTL into a bucket
// ForeverTTL for no TTL
func (bk *Backend) UpsertVal(bucket []string, key string, val []byte, ttl time.Duration) error {
// create the directory:
dirPath := path.Join(bk.RootDir, path.Join(bucket...))
err := os.MkdirAll(dirPath, defaultDirMode)
if err != nil {
return trace.Wrap(err)
}
filename := path.Join(dirPath, key)
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, defaultFileMode)
if err != nil {
if os.IsExist(err) {
return trace.AlreadyExists("%s/%s already exists", dirPath, key)
}
return trace.ConvertSystemError(err)
}
defer f.Close()
if err := utils.FSWriteLock(f); err != nil {
return trace.Wrap(err)
}
defer utils.FSUnlock(f)
if err := f.Truncate(0); err != nil {
return trace.ConvertSystemError(err)
}
n, err := f.Write(val)
if err == nil && n < len(val) {
return trace.Wrap(io.ErrShortWrite)
}
return trace.Wrap(bk.applyTTL(dirPath, key, ttl))
}
// GetVal return a value for a given key in the bucket
func (bk *Backend) GetVal(bucket []string, key string) ([]byte, error) {
dirPath := path.Join(path.Join(bk.RootDir, path.Join(bucket...)))
filename := path.Join(dirPath, key)
expired, err := bk.checkTTL(dirPath, key)
if err != nil {
return nil, trace.Wrap(err)
}
if expired {
bk.DeleteKey(bucket, key)
return nil, trace.NotFound("key %q is not found", key)
}
f, err := os.OpenFile(filename, os.O_RDONLY, defaultFileMode)
if err != nil {
// GetVal() on a bucket must return 'BadParameter' error:
if fi, _ := os.Stat(filename); fi != nil && fi.IsDir() {
return nil, trace.BadParameter("%q is not a valid key", key)
}
return nil, trace.ConvertSystemError(err)
}
defer f.Close()
if err := utils.FSReadLock(f); err != nil {
return nil, trace.Wrap(err)
}
defer utils.FSUnlock(f)
bytes, err := ioutil.ReadAll(f)
if err != nil {
return nil, trace.ConvertSystemError(err)
}
// this could happen when CreateKey or UpsertKey created a file
// but, GetVal managed to get readLock right after it,
// so there are no contents there
if len(bytes) == 0 {
return nil, trace.NotFound("key %q is not found", key)
}
return bytes, nil
}
// DeleteKey deletes a key in a bucket
func (bk *Backend) DeleteKey(bucket []string, key string) error {
dirPath := path.Join(bk.RootDir, path.Join(bucket...))
filename := path.Join(dirPath, key)
f, err := os.OpenFile(filename, os.O_RDONLY, defaultFileMode)
if err != nil {
if fi, _ := os.Stat(filename); fi != nil && fi.IsDir() {
return trace.BadParameter("%q is not a valid key", key)
}
return trace.ConvertSystemError(err)
}
defer f.Close()
if err := utils.FSWriteLock(f); err != nil {
return trace.Wrap(err)
}
defer utils.FSUnlock(f)
if err := os.Remove(bk.ttlFile(dirPath, key)); err != nil {
if !os.IsNotExist(err) {
log.Warn(err)
}
}
return trace.ConvertSystemError(os.Remove(filename))
}
// DeleteBucket deletes the bucket by a given path
func (bk *Backend) DeleteBucket(parent []string, bucket string) error {
return removeFiles(path.Join(path.Join(bk.RootDir, path.Join(parent...)), bucket))
}
// removeFiles removes files from the directory non-recursively
// we need this function because os.RemoveAll does not work
// on concurrent requests - can produce directory not empty
// error, because someone could create a new file in the directory
func removeFiles(dir string) error {
d, err := os.Open(dir)
if err != nil {
err = trace.ConvertSystemError(err)
if !trace.IsNotFound(err) {
return err
}
return nil
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
err = trace.ConvertSystemError(err)
if !trace.IsNotFound(err) {
return err
}
return nil
}
for _, name := range names {
path := filepath.Join(dir, name)
fi, err := os.Stat(path)
if err != nil {
err = trace.ConvertSystemError(err)
if !trace.IsNotFound(err) {
return err
}
} else if !fi.IsDir() {
err = removeFile(path)
if err != nil {
return err
}
}
}
return nil
}
func removeFile(path string) error {
f, err := os.OpenFile(path, os.O_RDONLY, defaultFileMode)
err = trace.ConvertSystemError(err)
if err != nil {
if !trace.IsNotFound(err) {
return trace.Wrap(err)
}
return nil
}
defer f.Close()
if err := utils.FSWriteLock(f); err != nil {
return trace.Wrap(err)
}
defer utils.FSUnlock(f)
err = os.Remove(path)
if err != nil {
err = trace.ConvertSystemError(err)
if !trace.IsNotFound(err) {
return err
}
}
return nil
}
// AcquireLock grabs a lock that will be released automatically in TTL
func (bk *Backend) AcquireLock(token string, ttl time.Duration) (err error) {
bk.Debugf("AcquireLock(%s)", token)
if err = backend.ValidateLockTTL(ttl); err != nil {
return trace.Wrap(err)
}
bucket := []string{locksBucket}
for {
// GetVal will clear TTL on a lock
bk.GetVal(bucket, token)
// CreateVal is atomic:
err = bk.CreateVal(bucket, token, []byte{1}, ttl)
if err == nil {
break // success
}
if trace.IsAlreadyExists(err) { // locked? wait and repeat:
bk.Clock().Sleep(time.Millisecond * 250)
continue
}
return trace.ConvertSystemError(err)
}
return nil
}
// ReleaseLock forces lock release before TTL
func (bk *Backend) ReleaseLock(token string) (err error) {
bk.Debugf("ReleaseLock(%s)", token)
if err = bk.DeleteKey([]string{locksBucket}, token); err != nil {
if !os.IsNotExist(err) {
return trace.ConvertSystemError(err)
}
}
return nil
}
// Close releases the resources taken up by a backend
func (bk *Backend) Close() error {
return nil
}
// applyTTL assigns a given TTL to a file with sub-second granularity
func (bk *Backend) applyTTL(dirPath string, key string, ttl time.Duration) error {
if ttl == backend.Forever {
return nil
}
expiryTime := bk.Clock().Now().Add(ttl)
bytes, _ := expiryTime.MarshalText()
return trace.ConvertSystemError(
ioutil.WriteFile(bk.ttlFile(dirPath, key), bytes, defaultFileMode))
}
// checkTTL checks if a given file has TTL and returns 'true' if it's expired
func (bk *Backend) checkTTL(dirPath string, key string) (expired bool, err error) {
bytes, err := ioutil.ReadFile(bk.ttlFile(dirPath, key))
if err != nil {
if os.IsNotExist(err) { // no TTL
return false, nil
}
return false, trace.Wrap(err)
}
// this could happen if file was deleted, we can sometimes read empty contents
if len(bytes) == 0 {
return false, nil
}
var expiryTime time.Time
if err = expiryTime.UnmarshalText(bytes); err != nil {
return false, trace.Wrap(err)
}
return bk.Clock().Now().After(expiryTime), nil
}
// ttlFile returns the full path of the "TTL file" where the TTL is
// stored for a given key, example: /root/bucket/.keyname.ttl
func (bk *Backend) ttlFile(dirPath, key string) string {
return path.Join(dirPath, "."+key+".ttl")
}