-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
67 lines (60 loc) · 1.98 KB
/
fs.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
package keymgr
import (
"fmt"
"io/ioutil"
"os"
)
// FSManager is a File System key manager
type FSManager struct {
basePath string
}
// NewFSManager is the FSManager constructor
func NewFSManager(path string) (*FSManager, error) {
bPath := fmt.Sprintf("%s/keys", path)
if _, err := os.Stat(bPath); os.IsNotExist(err) {
if err := os.Mkdir(bPath, 755); err != nil {
return nil, fmt.Errorf("could not create new directory %s: %s", path, err)
}
path = fmt.Sprintf("%s/privs", bPath)
if err := os.Mkdir(path, 755); err != nil {
return nil, fmt.Errorf("could not create new directory %s: %s", path, err)
}
path = fmt.Sprintf("%s/pubs", bPath)
if err := os.Mkdir(path, 755); err != nil {
return nil, fmt.Errorf("could not create new directory %s: %s", path, err)
}
}
return &FSManager{
basePath: bPath,
}, nil
}
// PutPriv saves a private key by id
func (m *FSManager) PutPriv(id string, blob string) error {
if err := ioutil.WriteFile(fmt.Sprintf("%s/privs/%s.priv", m.basePath, id), []byte(blob), 0644); err != nil {
return fmt.Errorf("could not write private key to file system: %s", err)
}
return nil
}
// GetPriv gets a private key by id
func (m *FSManager) GetPriv(id string) (string, error) {
dat, err := ioutil.ReadFile(fmt.Sprintf("%s/privs/%s.priv", m.basePath, id))
if err != nil {
return "", fmt.Errorf("could not read private key from file system: %s", err)
}
return string(dat), nil
}
// PutPub saves a public key by id
func (m *FSManager) PutPub(id string, blob string) error {
if err := ioutil.WriteFile(fmt.Sprintf("%s/pubs/%s.pub", m.basePath, id), []byte(blob), 0644); err != nil {
return fmt.Errorf("could not write public key to file system: %s", err)
}
return nil
}
// GetPub gets a public key by id
func (m *FSManager) GetPub(id string) (string, error) {
dat, err := ioutil.ReadFile(fmt.Sprintf("%s/pubs/%s.pub", m.basePath, id))
if err != nil {
return "", fmt.Errorf("could not read public key from file system: %s", err)
}
return string(dat), nil
}