-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
81 lines (63 loc) · 1.52 KB
/
config.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
package main
import (
"errors"
"io/ioutil"
"strings"
"gopkg.in/yaml.v2"
)
const defaultCertPath = "/etc/bashrpc/"
type config struct {
Cert string `yaml:"cert"`
Key string `yaml:"key"`
Log string `yaml:"log"`
Port string `yaml:"port"`
Routes []route `yaml:"routes"`
Secret string `yaml:"secret"`
Whitelisted []string `yaml:"whitelisted_clients"`
}
type route struct {
Path string `yaml:"path"`
Cmd string `yaml:"cmd"`
}
func loadConfig(p string) (config, error) {
cfg := config{}
setConfigDefaults(&cfg)
data, err := ioutil.ReadFile(p)
if err != nil {
return cfg, err
}
err = yaml.Unmarshal([]byte(data), &cfg)
return cfg, err
}
func setConfigDefaults(cfg *config) {
defaultPKIPath := "/etc/bashrpc/pki"
cfg.Cert = defaultPKIPath + "/bashrpc.cert"
cfg.Key = defaultPKIPath + "/bashrpc.key"
cfg.Log = "bashrpc.log"
cfg.Port = "8675"
}
func validateConfig(cfg *config) (err error) {
var issues []string
if cfg.Port == "" {
issues = append(issues, "port is missing")
}
if cfg.Secret == "" {
issues = append(issues, "secret is missing")
}
if cfg.Key == "" {
issues = append(issues, "key is missing")
}
if cfg.Cert == "" {
issues = append(issues, "cert is missing")
}
if cfg.Log == "" {
issues = append(issues, "log is missing")
}
if len(cfg.Whitelisted) == 0 {
issues = append(issues, "no whitelisted clients are specified")
}
if len(issues) > 0 {
err = errors.New("config validation errors: " + strings.Join(issues, ", "))
}
return
}