forked from cleanshavenalex/pvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiletree.go
More file actions
69 lines (63 loc) · 1.72 KB
/
filetree.go
File metadata and controls
69 lines (63 loc) · 1.72 KB
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
package pvc
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
// Default mapping for this backend
const (
DefaultFileTreeMapping = "{{ .ID }}"
DefaultFileTreeRootPath = "/vault/secrets"
)
// MaxFileTreeSizeBytes indicates the maximum file size we will read
var MaxFileTreeFileSizeBytes int64 = 2_000_000 // 2 MB
type fileTreeBackendGetter struct {
mapper SecretMapper
config *fileTreeBackend
rootPath string
}
func newFileTreeBackendGetter(ft *fileTreeBackend) (*fileTreeBackendGetter, error) {
if ft.mapping == "" {
ft.mapping = DefaultFileTreeMapping
}
sm, err := newSecretMapper(ft.mapping)
if err != nil {
return nil, fmt.Errorf("file tree error with mapping: %v", err)
}
if ft.rootPath == "" {
ft.rootPath = DefaultFileTreeRootPath
}
return &fileTreeBackendGetter{
mapper: sm,
config: ft,
}, nil
}
func (ftg *fileTreeBackendGetter) Get(id string) ([]byte, error) {
key, err := ftg.mapper.MapSecret(id)
if err != nil {
return nil, fmt.Errorf("error mapping secret id to filetree path: %v", err)
}
secretFilePath := filepath.Join(ftg.config.rootPath, key)
if !filepath.IsAbs(secretFilePath) {
return nil, fmt.Errorf("filetree path must be absolute: %v", secretFilePath)
}
f, err := os.Open(secretFilePath)
if err != nil {
return nil, fmt.Errorf("file tree error opening file %v: %v", secretFilePath, err)
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return nil, fmt.Errorf("error getting file stat: %v", err)
}
size := stat.Size()
if size > MaxFileTreeFileSizeBytes {
return nil, fmt.Errorf("file too large (max: %v bytes): %v", MaxFileTreeFileSizeBytes, size)
}
c, err := ioutil.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("error reading file: %v", err)
}
return c, nil
}