-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
385 lines (357 loc) · 8.65 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
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
package main
import (
"bufio"
"compress/gzip"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/tehmaze-labs/secrets/key"
"github.com/tehmaze-labs/secrets/storage"
"github.com/tehmaze-labs/secrets/storage/backend"
)
func errCfgSyntax(msg string, a ...interface{}) error {
return fmt.Errorf("syntax error: "+msg, a...)
}
// Group is the configuration for a named group.
type Group struct {
Name string
ACLs []string
Keys map[string][]byte
Data *storage.Storage
}
func (g *Group) String() string {
return g.Name
}
// NewGroup initialises a new secrets group and its storage backend.
func NewGroup(name string, cfg *Config) (group *Group, err error) {
if cfg.Storage.Path == "" {
return nil, errors.New(`configure Server.Storage`)
}
opt := backend.NewOptions(filepath.Join(cfg.Storage.Path, "group", name))
if cfg.Storage.Compress {
opt.Extra["compress"] = cfg.Storage.Level
}
backend, err := backend.NewFileBackend(opt)
if err != nil {
return nil, err
}
return &Group{
Name: name,
ACLs: []string{},
Keys: map[string][]byte{},
Data: storage.NewJSON(backend),
}, nil
}
// configBlock holds a block of configuration options.
type configBlock interface {
parse([]string) (configBlock, error)
}
// Config is the top-level configuration structure.
type Config struct {
Storage struct {
Path string
Compress bool
Level int
Keys *storage.Storage
}
Server struct {
tls.Certificate
Bind string
Key *key.Key
Root *x509.CertPool
}
ACL ACLs // map[string]*ACL
Group map[string]*Group
}
// NewConfig initialises a new configuration structure.
func NewConfig() *Config {
cfg := &Config{}
cfg.ACL = map[string]*ACL{}
cfg.Group = map[string]*Group{}
cfg.Server.Root = x509.NewCertPool()
cfg.Storage.Keys = &storage.Storage{}
return cfg
}
// Load reads and parses a configuration file.
func (cfg *Config) Load(file string) (err error) {
handle, err := os.Open(file)
if err != nil {
return fmt.Errorf("error opening %q: %v", file, err)
}
defer handle.Close()
var block, b configBlock
var stack *configStack
stack = newConfigStack()
stack.Push(cfg)
read := bufio.NewReader(handle)
scan := bufio.NewScanner(read)
scan.Split(bufio.ScanLines)
block = cfg
lineno := 0
for scan.Scan() {
line := scan.Text()
lineno++
//fmt.Printf("%d: block %T %v\n", lineno, block, block)
part := strings.Fields(line)
if len(part) == 0 || strings.HasPrefix(part[0], "#") {
continue
}
b, err = block.parse(part)
if err != nil {
return fmt.Errorf("%s[%d]: %v", file, lineno, err)
}
if b == nil {
block = stack.Pop()
if block == nil || len(*stack) == 0 {
return fmt.Errorf("%s[%d]: end of stack", file, lineno)
}
} else if b != block {
stack.Push(block)
block = b
}
}
return cfg.Validate()
}
// Validate does post-configuration checks.
func (cfg *Config) Validate() error {
if len(cfg.ACL) == 0 {
return errors.New("no ACL configured")
}
if len(cfg.Group) == 0 {
return errors.New("no Group configured")
}
for name, group := range cfg.Group {
if len(group.ACLs) == 0 {
return fmt.Errorf("group %q has no ACL", name)
}
}
if cfg.Storage.Keys.Backend == nil {
return errors.New("no Server.Storage configured")
}
return nil
}
func (cfg *Config) parse(field []string) (b configBlock, err error) {
if len(field) < 2 {
return nil, errCfgSyntax(`expected block`)
}
switch field[0] {
case "ACL":
if len(field) != 3 || field[2] != "{" {
return nil, errCfgSyntax(`expected: "ACL <name> {"`)
}
return newConfigACL(cfg, field[1]), nil
case "Group":
if len(field) != 3 || field[2] != "{" {
return nil, errCfgSyntax(`expected: "Group <name> {"`)
}
return newConfigGroup(cfg, field[1]), nil
case "Server":
return newConfigServer(cfg), nil
default:
return nil, errCfgSyntax(`unexpected top-level token %q`, field[0])
}
}
type configStack []configBlock
func newConfigStack() *configStack {
s := make(configStack, 0)
return &s
}
func (s *configStack) Push(b configBlock) {
*s = append(*s, b)
}
func (s *configStack) Pop() configBlock {
b := (*s)[len(*s)-1]
*s = (*s)[0 : len(*s)-1]
return b
}
type configACL struct {
Config *Config
Name string
ACL *ACL
}
func newConfigACL(cfg *Config, name string) *configACL {
empty := []string{}
acl, _ := NewACL(empty, empty, empty, empty)
return &configACL{
Config: cfg,
Name: name,
ACL: acl,
}
}
func (block *configACL) parse(field []string) (b configBlock, err error) {
if len(field) == 1 && field[0] == "}" {
block.Config.ACL[block.Name] = block.ACL
return nil, nil
}
if len(field) != 3 {
return nil, errCfgSyntax(`expected key type value`)
}
if field[1] != "cidr" && field[1] != "host" {
return nil, errCfgSyntax(`invalid type %q`, field[1])
}
switch field[0] {
case "Permit":
switch field[1] {
case "cidr":
_, ipnet, err := net.ParseCIDR(field[2])
if err != nil {
return nil, err
}
block.ACL.PermitCIDR(ipnet)
case "host":
block.ACL.PermitHost(field[2])
}
case "Reject":
switch field[1] {
case "cidr":
_, ipnet, err := net.ParseCIDR(field[2])
if err != nil {
return nil, err
}
block.ACL.RejectCIDR(ipnet)
case "host":
block.ACL.RejectHost(field[2])
}
default:
return nil, errCfgSyntax(`unexpected ACL token %q`, field[0])
}
return block, nil
}
type configGroup struct {
Config *Config
Name string
ACLs []string
Include *key.Key
}
func newConfigGroup(cfg *Config, name string) *configGroup {
block := &configGroup{
Config: cfg,
Name: name,
}
block.ACLs = []string{}
return block
}
func (block *configGroup) parse(field []string) (b configBlock, err error) {
if len(field) == 1 && field[0] == "}" {
block.Config.Group[block.Name], err = NewGroup(block.Name, block.Config)
if err != nil {
return nil, err
}
block.Config.Group[block.Name].ACLs = block.ACLs
if block.Include != nil {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
block.Config.Group[block.Name].Keys[hostname] = block.Include.PublicKey
}
return nil, nil
}
if len(field) < 2 {
return nil, errCfgSyntax(`expected key value`)
}
switch field[0] {
case "ACL":
block.ACLs = append(block.ACLs, field[1:]...)
case "Include":
key, err := key.Load(field[1])
if err != nil {
return nil, err
}
block.Include = key.AsPublicKey()
default:
return nil, errCfgSyntax(`unexpected Group token %q`, field[0])
}
return block, nil
}
type configServer struct {
Config *Config
}
func newConfigServer(cfg *Config) *configServer {
return &configServer{
Config: cfg,
}
}
func (block *configServer) parse(field []string) (b configBlock, err error) {
if len(field) == 1 && field[0] == "}" {
if block.Config.Storage.Path == "" {
return nil, errCfgSyntax(`expected Storage option`)
}
opt := backend.NewOptions(filepath.Join(block.Config.Storage.Path, "keys"))
if block.Config.Storage.Compress {
opt.Extra["compress"] = block.Config.Storage.Level
}
backend, err := backend.NewFileBackend(opt)
if err != nil {
return nil, err
}
block.Config.Storage.Keys = storage.NewJSON(backend)
return nil, nil
}
if len(field) < 2 {
return nil, errCfgSyntax(`expected key value`)
}
switch field[0] {
case "Bind":
if len(field) != 2 {
return nil, errCfgSyntax(`expected address`)
}
block.Config.Server.Bind = field[1]
case "Deflate":
if len(field) != 2 {
return nil, errCfgSyntax(`expected level`)
}
level, err := strconv.Atoi(field[1])
if err != nil {
return nil, err
}
if level < gzip.DefaultCompression || level > gzip.BestCompression {
return nil, fmt.Errorf("gzip: invalid compression level: %d", level)
}
block.Config.Storage.Compress = true
block.Config.Storage.Level = level
case "KeyPair":
if len(field) != 3 {
return nil, errCfgSyntax(`expected keyFile certFile`)
}
if block.Config.Server.Certificate, err = tls.LoadX509KeyPair(field[1], field[2]); err != nil {
return nil, err
}
case "Key":
if len(field) != 2 {
return nil, errCfgSyntax(`expected path`)
}
if block.Config.Server.Key, err = key.Load(field[1]); err != nil {
return nil, err
}
if !block.Config.Server.Key.IsPrivate() {
return nil, fmt.Errorf("%s: not a private key", field[1])
}
case "Root":
if len(field) < 2 {
return nil, errCfgSyntax(`expected path`)
}
for _, file := range field[1:] {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
block.Config.Server.Root.AppendCertsFromPEM(data)
}
case "Storage":
if len(field) != 2 {
return nil, errCfgSyntax(`expected path`)
}
block.Config.Storage.Path = field[1]
default:
return nil, errCfgSyntax(`unexpected Server token %q`, field[0])
}
return block, nil
}