-
Notifications
You must be signed in to change notification settings - Fork 107
/
store.go
190 lines (165 loc) 路 5.64 KB
/
store.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
package key
import (
"errors"
"fmt"
"os"
"path"
"reflect"
"github.com/BurntSushi/toml"
"github.com/dedis/drand/fs"
)
// Store abstracts the loading and saving of any private/public cryptographic
// material to be used by drand. For the moment, only a file based store is
// implemented.
type Store interface {
// SaveKeyPair saves the private key generated by drand as well as the
// public identity key associated
SaveKeyPair(p *Pair) error
// LoadKeyPair loads the private/public key pair associated with the drand
// operator
LoadKeyPair() (*Pair, error)
SaveShare(share *Share) error
LoadShare() (*Share, error)
SaveGroup(*Group) error
LoadGroup() (*Group, error)
SaveDistPublic(d *DistPublic) error
LoadDistPublic() (*DistPublic, error)
Reset(...ResetOption) error
}
// ErrStoreFile returns an error in case the store can not save the requested
// file
var ErrStoreFile = errors.New("store file issues")
// ErrAbsent returns
var ErrAbsent = errors.New("store can't find requested object")
// ConfigFolderFlag holds the name of the flag to set using the CLI to change
// the default configuration folder of drand. It mimicks the gpg flag option.
const ConfigFolderFlag = "homedir"
// KeyFolderName is the name of the folder where drand keeps its keys
const KeyFolderName = "key"
// GroupFolderName is the name of the folder where drand keeps its group files
const GroupFolderName = "groups"
const keyFileName = "drand_id"
const privateExtension = ".private"
const publicExtension = ".public"
const groupFileName = "drand_group.toml"
const shareFileName = "dist_key.private"
const distKeyFileName = "dist_key.public"
// Tomler represents any struct that can be (un)marshalled into/from toml format
type Tomler interface {
TOML() interface{}
FromTOML(i interface{}) error
TOMLValue() interface{}
}
// fileStore is a Store using filesystem to store informations
type fileStore struct {
baseFolder string
privateKeyFile string
publicKeyFile string
shareFile string
distKeyFile string
groupFile string
}
// NewFileStore is used to create the config folder and all the subfolders.
// If a folder alredy exists, we simply check the rights
func NewFileStore(baseFolder string) Store {
//config folder
if fs.CreateSecureFolder(baseFolder) == "" {
fmt.Println("Something went wrong with the config folder. Make sure that you have the appropriate rights.")
os.Exit(1)
}
store := &fileStore{baseFolder: baseFolder}
keyFolder := fs.CreateSecureFolder(path.Join(baseFolder, KeyFolderName))
groupFolder := fs.CreateSecureFolder(path.Join(baseFolder, GroupFolderName))
store.privateKeyFile = path.Join(keyFolder, keyFileName) + privateExtension
store.publicKeyFile = path.Join(keyFolder, keyFileName) + publicExtension
store.groupFile = path.Join(groupFolder, groupFileName)
store.shareFile = path.Join(groupFolder, shareFileName)
store.distKeyFile = path.Join(groupFolder, distKeyFileName)
return store
}
// SaveKeyPair first saves the private key in a file with tight permissions and then
// saves the public part in another file.
func (f *fileStore) SaveKeyPair(p *Pair) error {
if err := Save(f.privateKeyFile, p, true); err != nil {
return err
}
fmt.Printf("Saved the key : %s at %s\n", p.Public.Addr, f.publicKeyFile)
return Save(f.publicKeyFile, p.Public, false)
}
// LoadKeyPair decode private key first then public
func (f *fileStore) LoadKeyPair() (*Pair, error) {
p := new(Pair)
if err := Load(f.privateKeyFile, p); err != nil {
return nil, err
}
return p, Load(f.publicKeyFile, p.Public)
}
func (f *fileStore) LoadGroup() (*Group, error) {
g := new(Group)
return g, Load(f.groupFile, g)
}
func (f *fileStore) SaveGroup(g *Group) error {
return Save(f.groupFile, g, false)
}
func (f *fileStore) SaveShare(share *Share) error {
fmt.Printf("crypto store: saving private share in %s\n", f.shareFile)
return Save(f.shareFile, share, true)
}
func (f *fileStore) LoadShare() (*Share, error) {
s := new(Share)
return s, Load(f.shareFile, s)
}
func (f *fileStore) SaveDistPublic(d *DistPublic) error {
fmt.Printf("crypto store: saving public distributed key in %s\n", f.distKeyFile)
return Save(f.distKeyFile, d, false)
}
func (f *fileStore) LoadDistPublic() (*DistPublic, error) {
d := new(DistPublic)
return d, Load(f.distKeyFile, d)
}
func (f *fileStore) Reset(...ResetOption) error {
if err := Delete(f.distKeyFile); err != nil {
return fmt.Errorf("drand: err deleting dist. key file: %v", err)
}
if err := Delete(f.shareFile); err != nil {
return fmt.Errorf("drand: errd eleting share file: %v", err)
}
if err := Delete(f.groupFile); err != nil {
return fmt.Errorf("drand: err deleting group file: %v", err)
}
return nil
}
// Save the given Tomler interface to the given path. If secure is true, the
// file will have a 0700 security.
func Save(path string, t Tomler, secure bool) error {
var fd *os.File
var err error
if secure {
fd, err = fs.CreateSecureFile(path)
} else {
fd, err = os.Create(path)
}
if err != nil {
fmt.Printf("config: can't save %s to %s: %s\n", reflect.TypeOf(t).String(), path, err)
return err
}
defer fd.Close()
return toml.NewEncoder(fd).Encode(t.TOML())
}
// Load the given Tomler from the given file path.
func Load(path string, t Tomler) error {
tomlValue := t.TOMLValue()
var err error
if _, err = toml.DecodeFile(path, tomlValue); err != nil {
return err
}
return t.FromTOML(tomlValue)
}
// Delete the resource denoted by the given path. If it is a file, it deletes
// the file; if it is a folder it delete the folder and all its content.
func Delete(path string) error {
return os.RemoveAll(path)
}
// ResetOption is an option to allow for fine-grained reset
// operations
type ResetOption int