-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode_auth.go
475 lines (387 loc) · 11.9 KB
/
node_auth.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package ctrl
import (
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
)
// nodeAuth is the structure that holds both keys and acl's
// that the running ctrl node shall use for authorization.
// It holds a mutex to use when interacting with the map.
type nodeAuth struct {
// ACL that defines where a node is allowed to recieve from.
nodeAcl *nodeAcl
// All the public keys for nodes a node is allowed to receive from.
publicKeys *publicKeys
// Full path to the signing keys folder
SignKeyFolder string
// Full path to private signing key.
SignKeyPrivateKeyPath string
// Full path to public signing key.
SignKeyPublicKeyPath string
// private key for ed25519 signing.
SignPrivateKey []byte
// public key for ed25519 signing.
SignPublicKey []byte
configuration *Configuration
errorKernel *errorKernel
}
func newNodeAuth(configuration *Configuration, errorKernel *errorKernel) *nodeAuth {
n := nodeAuth{
nodeAcl: newNodeAcl(configuration, errorKernel),
publicKeys: newPublicKeys(configuration, errorKernel),
configuration: configuration,
errorKernel: errorKernel,
}
// Set the signing key paths.
n.SignKeyFolder = filepath.Join(configuration.ConfigFolder, "signing")
n.SignKeyPrivateKeyPath = filepath.Join(n.SignKeyFolder, "private.key")
n.SignKeyPublicKeyPath = filepath.Join(n.SignKeyFolder, "public.key")
err := n.loadSigningKeys()
if err != nil {
errorKernel.logError("newNodeAuth", "error", err)
os.Exit(1)
}
return &n
}
// --------------------- ACL ---------------------
type aclAndHash struct {
Acl map[Node]map[command]struct{}
Hash [32]byte
}
func newAclAndHash() aclAndHash {
a := aclAndHash{
Acl: make(map[Node]map[command]struct{}),
}
return a
}
type nodeAcl struct {
// allowed is a map for holding all the allowed signatures.
aclAndHash aclAndHash
filePath string
mu sync.Mutex
errorKernel *errorKernel
configuration *Configuration
}
func newNodeAcl(c *Configuration, errorKernel *errorKernel) *nodeAcl {
n := nodeAcl{
aclAndHash: newAclAndHash(),
filePath: filepath.Join(c.DatabaseFolder, "node_aclmap.txt"),
errorKernel: errorKernel,
configuration: c,
}
err := n.loadFromFile()
if err != nil {
errorKernel.logError("newNodeAcl: loading acl's from file", "file", err)
}
return &n
}
// loadFromFile will try to load all the currently stored acl's from file,
// and return the error if it fails.
// If no file is found a nil error is returned.
func (n *nodeAcl) loadFromFile() error {
if _, err := os.Stat(n.filePath); os.IsNotExist(err) {
// Just logging the error since it is not crucial that a key file is missing,
// since a new one will be created on the next update.
n.errorKernel.logDebug("nodeAcl:loadFromFile: no acl file found", "file", n.filePath)
return nil
}
fh, err := os.OpenFile(n.filePath, os.O_RDONLY, 0660)
if err != nil {
return fmt.Errorf("error: failed to open acl file: %v", err)
}
defer fh.Close()
b, err := io.ReadAll(fh)
if err != nil {
return err
}
n.mu.Lock()
defer n.mu.Unlock()
err = json.Unmarshal(b, &n.aclAndHash)
if err != nil {
return err
}
n.errorKernel.logDebug("nodeAcl: loadFromFile: Loaded existing acl's from file", "hash", n.aclAndHash.Hash)
return nil
}
// saveToFile will save the acl to file for persistent storage.
// An error is returned if it fails.
func (n *nodeAcl) saveToFile() error {
fh, err := os.OpenFile(n.filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0660)
if err != nil {
return fmt.Errorf("error: failed to acl file: %v", err)
}
defer fh.Close()
n.mu.Lock()
defer n.mu.Unlock()
enc := json.NewEncoder(fh)
enc.SetEscapeHTML(false)
err = enc.Encode(n.aclAndHash)
// HERE
// b, err := json.Marshal(n.aclAndHash)
if err != nil {
return err
}
// _, err = fh.Write(b)
// if err != nil {
// return err
// }
return nil
}
// --------------------- KEYS ---------------------
type keysAndHash struct {
Keys map[Node][]byte
Hash [32]byte
}
func newKeysAndHash() *keysAndHash {
kh := keysAndHash{
Keys: make(map[Node][]byte),
}
return &kh
}
type publicKeys struct {
keysAndHash *keysAndHash
mu sync.Mutex
filePath string
errorKernel *errorKernel
configuration *Configuration
}
func newPublicKeys(c *Configuration, errorKernel *errorKernel) *publicKeys {
p := publicKeys{
keysAndHash: newKeysAndHash(),
filePath: filepath.Join(c.DatabaseFolder, "publickeys.txt"),
errorKernel: errorKernel,
configuration: c,
}
err := p.loadFromFile()
if err != nil {
errorKernel.logError("newPublicKeys: loading public keys from file", "file", err)
}
return &p
}
// loadFromFile will try to load all the currently stored public keys from file,
// and return the error if it fails.
// If no file is found a nil error is returned.
func (p *publicKeys) loadFromFile() error {
if _, err := os.Stat(p.filePath); os.IsNotExist(err) {
// Just logging the error since it is not crucial that a key file is missing,
// since a new one will be created on the next update.
p.errorKernel.logInfo("publicKeys: loadFromFile: no public keys file found, new file will be created", "file", p.filePath)
return nil
}
fh, err := os.OpenFile(p.filePath, os.O_RDONLY, 0660)
if err != nil {
return fmt.Errorf("error: failed to open public keys file: %v", err)
}
defer fh.Close()
b, err := io.ReadAll(fh)
if err != nil {
return err
}
p.mu.Lock()
defer p.mu.Unlock()
err = json.Unmarshal(b, &p.keysAndHash)
if err != nil {
return err
}
p.errorKernel.logDebug("nodeAuth: loadFromFile: Loaded existing keys from file", "hash", p.keysAndHash.Hash)
return nil
}
// saveToFile will save all the public kets to file for persistent storage.
// An error is returned if it fails.
func (p *publicKeys) saveToFile() error {
fh, err := os.OpenFile(p.filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0660)
if err != nil {
return fmt.Errorf("error: failed to open public keys file: %v", err)
}
defer fh.Close()
p.mu.Lock()
defer p.mu.Unlock()
b, err := json.Marshal(p.keysAndHash)
if err != nil {
return err
}
_, err = fh.Write(b)
if err != nil {
return err
}
return nil
}
// loadSigningKeys will try to load the ed25519 signing keys. If the
// files are not found new keys will be generated and written to disk.
func (n *nodeAuth) loadSigningKeys() error {
// Check if folder structure exist, if not create it.
if _, err := os.Stat(n.SignKeyFolder); os.IsNotExist(err) {
err := os.MkdirAll(n.SignKeyFolder, 0770)
if err != nil {
er := fmt.Errorf("error: failed to create directory for signing keys : %v", err)
return er
}
}
// Check if there already are any keys in the etc folder.
foundKey := false
if _, err := os.Stat(n.SignKeyPublicKeyPath); !os.IsNotExist(err) {
foundKey = true
}
if _, err := os.Stat(n.SignKeyPrivateKeyPath); !os.IsNotExist(err) {
foundKey = true
}
// If no keys where found generete a new pair, load them into the
// processes struct fields, and write them to disk.
if !foundKey {
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
er := fmt.Errorf("error: failed to generate ed25519 keys for signing: %v", err)
return er
}
pubB64string := base64.RawStdEncoding.EncodeToString(pub)
privB64string := base64.RawStdEncoding.EncodeToString(priv)
// Write public key to file.
err = n.writeSigningKey(n.SignKeyPublicKeyPath, pubB64string)
if err != nil {
return err
}
// Write private key to file.
err = n.writeSigningKey(n.SignKeyPrivateKeyPath, privB64string)
if err != nil {
return err
}
// Also store the keys in the processes structure so we can
// reference them from there when we need them.
n.SignPublicKey = pub
n.SignPrivateKey = priv
n.errorKernel.logInfo("loadSigningKeys: no signing keys found, generating new keys")
// We got the new generated keys now, so we can return.
return nil
}
// Key files found, load them into the processes struct fields.
pubKey, _, err := n.readKeyFile(n.SignKeyPublicKeyPath)
if err != nil {
return err
}
n.SignPublicKey = pubKey
privKey, _, err := n.readKeyFile(n.SignKeyPrivateKeyPath)
if err != nil {
return err
}
n.SignPublicKey = pubKey
n.SignPrivateKey = privKey
return nil
}
// writeSigningKey will write the base64 encoded signing key to file.
func (n *nodeAuth) writeSigningKey(realPath string, keyB64 string) error {
fh, err := os.OpenFile(realPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0660)
if err != nil {
er := fmt.Errorf("error: failed to open key file for writing: %v", err)
return er
}
defer fh.Close()
_, err = fh.Write([]byte(keyB64))
if err != nil {
er := fmt.Errorf("error: failed to write key to file: %v", err)
return er
}
return nil
}
// readKeyFile will take the path of a key file as input, read the base64
// encoded data, decode the data. It will return the raw data as []byte,
// the base64 encoded data, and any eventual error.
func (n *nodeAuth) readKeyFile(keyFile string) (ed2519key []byte, b64Key []byte, err error) {
fh, err := os.Open(keyFile)
if err != nil {
er := fmt.Errorf("error: failed to open key file: %v", err)
return nil, nil, er
}
defer fh.Close()
b, err := io.ReadAll(fh)
if err != nil {
er := fmt.Errorf("error: failed to read key file: %v", err)
return nil, nil, er
}
key, err := base64.RawStdEncoding.DecodeString(string(b))
if err != nil {
er := fmt.Errorf("error: failed to base64 decode key data: %v", err)
return nil, nil, er
}
return key, b, nil
}
// verifySignature
func (n *nodeAuth) verifySignature(m Message) bool {
signatureCheckMap := map[Method]struct{}{
OpProcessList: {},
OpProcessStart: {},
OpProcessStop: {},
CliCommand: {},
CliCommandCont: {},
TailFile: {},
HttpGet: {},
CopySrc: {},
Console: {},
File: {},
FileAppend: {},
}
// We only want to signature checking on the methods found
// in the map, we return that the signature was verified
// to true to allow the method to be executed.
if _, ok := signatureCheckMap[m.Method]; !ok {
n.errorKernel.logInfo("verifySignature: will not do signature check for method", "method", m.Method)
return true
}
// Verify if the signature matches.
argsStringified := argsToString(m.MethodArgs)
var ok bool
err := func() error {
n.publicKeys.mu.Lock()
defer n.publicKeys.mu.Unlock()
pubKey := n.publicKeys.keysAndHash.Keys[m.FromNode]
if len(pubKey) != 32 {
err := fmt.Errorf("length of publicKey not equal to 32: %v", len(pubKey))
return err
}
ok = ed25519.Verify(pubKey, []byte(argsStringified), m.ArgSignature)
return nil
}()
if err != nil {
n.errorKernel.logError("verifySignature", "error", err)
}
n.errorKernel.logInfo("verifySignature:", "result", ok, "fromNode", m.FromNode, "method", m.Method)
return ok
}
// verifyAcl
func (n *nodeAuth) verifyAcl(m Message) bool {
// NB: Only enable acl checking for REQCliCommand for now.
if m.Method != CliCommand {
n.errorKernel.logInfo("verifyAcl: we shall not do acl check on method", "method", m.Method)
return true
}
argsStringified := argsToString(m.MethodArgs)
// Verify if the command matches the one in the acl map.
n.nodeAcl.mu.Lock()
defer n.nodeAcl.mu.Unlock()
cmdMap, ok := n.nodeAcl.aclAndHash.Acl[m.FromNode]
if !ok {
n.errorKernel.logError("verifyAcl: The fromNode was not found in the acl", "fromNode", m.FromNode)
return false
}
_, ok = cmdMap[command("*")]
if ok {
n.errorKernel.logInfo("verifyAcl: The acl said \"*\", all commands allowed from node", "fromNode", m.FromNode)
return true
}
_, ok = cmdMap[command(argsStringified)]
if !ok {
n.errorKernel.logInfo("verifyAcl: The command was NOT FOUND in the acl", "methodArgs", m.MethodArgs)
return false
}
n.errorKernel.logInfo("verifyAcl: the command was FOUND in the acl", "result", ok, "fromNode", m.FromNode, "method", m.Method)
return true
}
// argsToString takes args in the format of []string and returns a string.
func argsToString(args []string) string {
return strings.Join(args, " ")
}