-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
456 lines (353 loc) · 8.09 KB
/
file.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
package secfs
import (
"bytes"
"io"
"io/fs"
"os"
"path/filepath"
"sync"
"syscall"
"time"
"github.com/postfinance/secfs/internal/backend"
"github.com/spf13/afero"
)
// File is the corev1.Secret without k8s specific data
type File struct {
name string // absolute name namespace/secret[/key]
spath *secretPath
key string
value []byte
data map[string][]byte
mtime time.Time
mode fs.FileMode
readonly bool
closed bool
delete bool
pos int64
TLS bool // TODO: corev1.SecretTypeTLS
mu sync.RWMutex
backend backend.Backend
}
func newFile(name string) (*File, error) {
p, err := newSecretPath(name)
if err != nil {
return nil, err
}
mode := os.FileMode(0)
if p.IsDir() {
mode = os.ModeDir
}
return &File{
name: name,
spath: p,
key: p.Key(),
data: make(map[string][]byte),
mode: mode,
readonly: true,
pos: 0,
}, nil
}
// Open open a secret or file
// https://pkg.go.dev/os#Open
// returns *File (implements afero.File and os.FileInfo)
func Open(b backend.Backend, name string) (*File, error) {
f, err := newFile(name)
if err != nil {
return nil, wrapPathError("Open", name, err)
}
f.backend = b
f.readonly = true
if err := b.Get(f); err != nil {
return nil, wrapPathError("Open", name, err)
}
if f.IsDir() {
return f, nil
}
v, ok := f.data[f.key]
if !ok {
return nil, wrapPathError("Open", name, syscall.ENOENT)
}
f.value = v
return f, nil
}
// FileCreate create a new or truncated file
// https://pkg.go.dev/os#Create
// returns *File (implements afero.File and os.FileInfo)
func FileCreate(b backend.Backend, name string) (*File, error) {
f, err := newFile(name)
if err != nil {
return nil, wrapPathError("Create", name, err)
}
f.backend = b
f.readonly = false
if f.IsDir() {
return nil, wrapPathError("Create", name, syscall.EISDIR)
}
if err := b.Get(f); err != nil {
return nil, wrapPathError("Create", name, err)
}
f.data[f.key] = make([]byte, 0)
if err := b.Update(f); err != nil {
return nil, wrapPathError("Create", name, err)
}
return f, nil
}
var _ backend.Secret = (*File)(nil) // backend.Secret includes backend.Metadata
// Namespace returns the namespace name (backend.Metadata)
func (f *File) Namespace() string {
return f.spath.Namespace()
}
// Secret returns the name of the secret (backend.Metadata)
func (f *File) Secret() string {
return f.spath.Secret()
}
// Key returns the file name (backend.Metadata)
func (f *File) Key() string {
return f.key
}
// Value returns the file content (backend.Secret)
func (f *File) Value() []byte {
return f.value
}
// Delete key (backend.Secret)
func (f *File) Delete() bool {
return f.delete
}
// SetData sets the secret data map (backend.Secret)
func (f *File) SetData(data map[string][]byte) {
f.data = data
}
// Data returns the underlying secret data map (backend.Secret)
func (f *File) Data() map[string][]byte {
return f.data
}
// SetTime sets the secret mtime (backend.Secret)
func (f *File) SetTime(mtime time.Time) {
f.mtime = mtime
}
var _ afero.File = (*File)(nil) // https://pkg.go.dev/github.com/spf13/afero#File
var _ os.FileInfo = (*File)(nil) // https://pkg.go.dev/io/fs#FileInfo
// Close io.Closer
func (f *File) Close() error {
if f.closed {
return afero.ErrFileClosed
}
if !f.spath.IsDir() {
if err := f.Sync(); err != nil {
return err
}
}
f.mu.Lock()
f.closed = true
f.mu.Unlock()
return nil
}
// Read io.Reader
// https://pkg.go.dev/io#Reader
func (f *File) Read(p []byte) (n int, err error) {
if err := f.validateRO(); err != nil {
return 0, err
}
f.mu.RLock()
defer f.mu.RUnlock()
r := bytes.NewReader(f.value)
// seek pos
_, err = r.Seek(f.pos, io.SeekStart)
if err != nil {
return 0, err
}
n, err = r.Read(p)
// set new pos
f.pos += int64(n)
return n, err
}
// ReadAt io.ReaderAt
// https://pkg.go.dev/io#ReaderAt
func (f *File) ReadAt(p []byte, off int64) (n int, err error) {
if err := f.validateRO(); err != nil {
return 0, err
}
f.mu.RLock()
defer f.mu.RUnlock()
return bytes.NewReader(f.value).ReadAt(p, off)
}
// Seek io.Seeker
// https://pkg.go.dev/io#Seeker
func (f *File) Seek(offset int64, whence int) (int64, error) {
if err := f.validateRO(); err != nil {
return 0, err
}
f.mu.Lock()
defer f.mu.Unlock()
r := bytes.NewReader(f.value)
_, err := r.Seek(f.pos, io.SeekStart)
if err != nil {
return 0, err
}
n, err := r.Seek(offset, whence)
if err != nil {
return 0, err
}
f.pos = n
return n, err
}
// Write io.Writer
// https://pkg.go.dev/io#Writer
func (f *File) Write(p []byte) (n int, err error) {
n, err = f.WriteAt(p, f.pos)
if err != nil {
return 0, err
}
f.pos += int64(n)
return n, err
}
// WriteAt io.WriterAt
// https://pkg.go.dev/io#WriterAt
// Source: https://github.com/aws/aws-sdk-go/blob/e8afe81156c70d5bf7b6d2ed5aeeb609ea3ba3f8/aws/types.go#L183
func (f *File) WriteAt(p []byte, off int64) (n int, err error) {
if err := f.validateRW(); err != nil {
return 0, err
}
f.mu.Lock()
defer f.mu.Unlock()
pLen := len(p)
expLen := off + int64(pLen)
if int64(len(f.value)) < expLen {
if int64(cap(f.value)) < expLen {
buf := make([]byte, expLen)
copy(buf, f.value)
f.value = buf
}
f.value = f.value[:expLen]
}
copy(f.value[off:], p)
return pLen, nil
}
// Name returns the name of the secret or file (afero.File, io.FileInfo)
func (f *File) Name() string {
return f.spath.Name()
}
// Readdir (afero.File)
// TODO: When return io.EOF?
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
if !f.spath.IsDir() {
return nil, syscall.ENOTDIR
}
entries := []os.FileInfo{}
for n := range f.data {
p := &secretPath{
namespace: f.spath.Namespace(),
secret: f.spath.Secret(),
key: n,
isDir: false,
}
entries = append(entries, &File{
name: p.Absolute(),
spath: p,
})
if count > 0 && len(entries) == count {
break
}
}
return entries, nil
}
// Readdirnames (afero.File)
func (f *File) Readdirnames(n int) ([]string, error) {
fi, err := f.Readdir(n)
names := make([]string, len(fi))
for i, f := range fi {
_, names[i] = filepath.Split(f.Name())
}
return names, err
}
// Stat (afero.File)
func (f *File) Stat() (os.FileInfo, error) {
return f, nil
}
// Sync (afero.File)
func (f *File) Sync() error {
if err := f.validateRO(); err != nil {
return err
}
if f.readonly {
return nil
}
return f.backend.Update(f)
}
// Truncate (afero.File)
func (f *File) Truncate(size int64) error {
if err := f.validateRW(); err != nil {
return wrapPathError("Truncate", f.name, err)
}
if int64(len(f.value)) <= size {
return nil
}
f.mu.Lock()
defer f.mu.Unlock()
buf := make([]byte, size)
copy(buf, f.value)
f.value = buf
return nil
}
// WriteString (afero.File)
func (f *File) WriteString(st string) (int, error) {
n, err := f.WriteAt([]byte(st), f.pos)
if err != nil {
return 0, err
}
f.pos += int64(n)
return n, err
}
// Size returns length in bytes for keys (io.FileInfo)
func (f *File) Size() int64 {
if f.spath.IsDir() {
return int64(len(f.data))
}
return int64(len(f.value))
}
// Mode returns file mode bits (io.FileInfo)
func (f *File) Mode() fs.FileMode {
return f.mode
}
// ModTime returns file modification time (io.FileInfo)
func (f *File) ModTime() time.Time {
return f.mtime
}
// IsDir returns true for a secret, false for a key (io.FileInfo)
func (f *File) IsDir() bool {
return f.spath.IsDir()
}
// Sys returns underlying data source (io.FileInfo)
// can return nil
func (f *File) Sys() interface{} {
return f
}
func (f *File) isEmptyDir() bool {
return f.spath.IsDir() && len(f.data) == 0
}
func (f *File) validateRO() error {
if f.closed {
return afero.ErrFileClosed
}
if f.spath.IsDir() {
return syscall.EISDIR
}
return nil
}
func (f *File) validateRW() error {
if err := f.validateRO(); err != nil {
return err
}
if f.readonly {
/*
From the man page of truncate(2):
EINVAL or EBADF
The file descriptor fd is not open for writing. POSIX
permits, and portable applications should handle, either
error for this case. (Linux produces EINVAL.)
We use EBADF which if more consistent here.
*/
return syscall.EBADF
}
return nil
}