-
Notifications
You must be signed in to change notification settings - Fork 11
/
publisherUtil.go
57 lines (53 loc) · 1.7 KB
/
publisherUtil.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
package publisher
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"strings"
"time"
log "github.com/inconshreveable/log15"
"github.com/netsec-ethz/rains/internal/pkg/keyManager"
"github.com/netsec-ethz/rains/internal/pkg/keys"
)
//LoadConfig loads configuration information from configPath
func LoadConfig(configPath string) (Config, error) {
var config Config
file, err := ioutil.ReadFile(configPath)
if err != nil {
log.Error("Could not open config file...", "path", configPath, "error", err)
return Config{}, err
}
if err = json.Unmarshal(file, &config); err != nil {
log.Error("Could not unmarshal json format of config", "error", err)
return Config{}, err
}
config.MetaDataConf.SigSigningInterval *= time.Second
return config, nil
}
//LoadPrivateKeys reads private keys from the path provided in the config and returns a map from
//PublicKeyID to the corresponding private key data.
func LoadPrivateKeys(path string) (map[keys.PublicKeyID]interface{}, error) {
output := make(map[keys.PublicKeyID]interface{})
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, fmt.Errorf("Was not able to read directory: %v", err)
}
for _, f := range files {
if strings.HasSuffix(f.Name(), keyManager.SecSuffix) {
keyPem, err := keyManager.DecryptKey(path, f.Name(), "")
if err != nil {
return nil, fmt.Errorf("Was not able to decrypt key: %v", err)
}
keyID, pkey, err := keyManager.PemToKeyID(keyPem)
if err != nil {
return nil, fmt.Errorf("Was not able to decode pem encoded private key: %v", err)
}
if _, ok := output[keyID]; ok {
return nil, errors.New("Two keys for the same key meta data are not allowed")
}
output[keyID] = pkey
}
}
return output, nil
}