-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackend_file.go
80 lines (62 loc) · 1.45 KB
/
backend_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
package main
import (
"encoding/json"
"log"
"os"
"path/filepath"
)
// Ensure FileBackend implement BackendProvider interface
var _ BackendProvider = (*FileBackend)(nil)
type FileBackend struct {
path string
}
type fileEntry struct {
Value []byte
}
func NewFileBackend(path string) (BackendProvider, error) {
if _, err := os.Stat(path); err != nil {
return nil, err
}
be := &FileBackend{
path: path,
}
return be, nil
}
func (b *FileBackend) expandPath(k string) string {
path := filepath.Join(b.path, k)
key := filepath.Base(path)
path = filepath.Dir(path)
return filepath.Join(path, "_"+key)
}
func (b *FileBackend) ReadFile(path string) ([]byte, error) {
var entry fileEntry
if _, err := os.Stat(path); err != nil {
return nil, err
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
dec := json.NewDecoder(f)
err = dec.Decode(&entry)
return entry.Value, err
}
func (b *FileBackend) GetRecoveryKey() ([]byte, error) {
fullPath := b.expandPath(recoveryKeyPath)
return b.ReadFile(fullPath)
}
func (b *FileBackend) RecoveryConfig() (*SealConfig, error) {
fullPath := b.expandPath(recoverySealConfigPlaintextPath)
data, err := b.ReadFile(fullPath)
conf := &SealConfig{}
if err != nil {
log.Print("unable to read recovery config", err)
return nil, err
}
if err := json.Unmarshal(data, conf); err != nil {
log.Print("failed to decode seal configuration", err)
return nil, err
}
return conf, nil
}