-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
keystore.go
176 lines (139 loc) · 3.62 KB
/
keystore.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
package keystore
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
logging "gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log"
ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
)
var log = logging.Logger("keystore")
// Keystore provides a key management interface
type Keystore interface {
// Has returns whether or not a key exist in the Keystore
Has(string) (bool, error)
// Put stores a key in the Keystore, if a key with the same name already exists, returns ErrKeyExists
Put(string, ci.PrivKey) error
// Get retrieves a key from the Keystore if it exists, and returns ErrNoSuchKey
// otherwise.
Get(string) (ci.PrivKey, error)
// Delete removes a key from the Keystore
Delete(string) error
// List returns a list of key identifier
List() ([]string, error)
}
var ErrNoSuchKey = fmt.Errorf("no key by the given name was found")
var ErrKeyExists = fmt.Errorf("key by that name already exists, refusing to overwrite")
// FSKeystore is a keystore backed by files in a given directory stored on disk.
type FSKeystore struct {
dir string
}
func validateName(name string) error {
if name == "" {
return fmt.Errorf("key names must be at least one character")
}
if strings.Contains(name, "/") {
return fmt.Errorf("key names may not contain slashes")
}
if strings.HasPrefix(name, ".") {
return fmt.Errorf("key names may not begin with a period")
}
return nil
}
func NewFSKeystore(dir string) (*FSKeystore, error) {
_, err := os.Stat(dir)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
if err := os.Mkdir(dir, 0700); err != nil {
return nil, err
}
}
return &FSKeystore{dir}, nil
}
// Has returns whether or not a key exist in the Keystore
func (ks *FSKeystore) Has(name string) (bool, error) {
kp := filepath.Join(ks.dir, name)
_, err := os.Stat(kp)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
if err := validateName(name); err != nil {
return false, err
}
return true, nil
}
// Put stores a key in the Keystore, if a key with the same name already exists, returns ErrKeyExists
func (ks *FSKeystore) Put(name string, k ci.PrivKey) error {
if err := validateName(name); err != nil {
return err
}
b, err := k.Bytes()
if err != nil {
return err
}
kp := filepath.Join(ks.dir, name)
_, err = os.Stat(kp)
if err == nil {
return ErrKeyExists
} else if !os.IsNotExist(err) {
return err
}
fi, err := os.Create(kp)
if err != nil {
return err
}
defer fi.Close()
_, err = fi.Write(b)
return err
}
// Get retrieves a key from the Keystore if it exists, and returns ErrNoSuchKey
// otherwise.
func (ks *FSKeystore) Get(name string) (ci.PrivKey, error) {
if err := validateName(name); err != nil {
return nil, err
}
kp := filepath.Join(ks.dir, name)
data, err := ioutil.ReadFile(kp)
if err != nil {
if os.IsNotExist(err) {
return nil, ErrNoSuchKey
}
return nil, err
}
return ci.UnmarshalPrivateKey(data)
}
// Delete removes a key from the Keystore
func (ks *FSKeystore) Delete(name string) error {
if err := validateName(name); err != nil {
return err
}
kp := filepath.Join(ks.dir, name)
return os.Remove(kp)
}
// List return a list of key identifier
func (ks *FSKeystore) List() ([]string, error) {
dir, err := os.Open(ks.dir)
if err != nil {
return nil, err
}
dirs, err := dir.Readdirnames(0)
if err != nil {
return nil, err
}
list := make([]string, 0, len(dirs))
for _, name := range dirs {
err := validateName(name)
if err == nil {
list = append(list, name)
} else {
log.Warningf("Ignoring the invalid keyfile: %s", name)
}
}
return list, nil
}