-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
81 lines (72 loc) · 1.9 KB
/
config.go
File metadata and controls
81 lines (72 loc) · 1.9 KB
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 warded
import (
"encoding/json"
)
// WardConfig contains the configuration for the ward
type WardConfig struct {
KeyDerivation KeyDerivationConfig `json:"keyDerivation"`
Cipher string `json:"cipher"`
}
// DefaultWardConfig returns the default WardConfig.
// This contains recommended values.
func DefaultWardConfig() WardConfig {
return WardConfig{
Cipher: "chacha20poly1305",
KeyDerivation: KeyDerivationConfig{
Type: TypeScrypt,
Data: &Scrypt{
Iterations: 16384, // 2**14
BlockSize: 8,
Parallel: 1,
},
},
}
}
// Config contains the general and ward-specific configurations.
// Ward is the general configuration and defaults
// to the default ward configuration.
// Wards is a map from ward name to configuration
// and defaults to the general ward configuration.
type Config struct {
Ward WardConfig `json:"ward"`
Wards map[string]WardConfig `json:"wards"`
}
// GetWardConfig returns the ward-specific configuration,
// if one exists. Otherwise, the general config is returned.
func (c Config) GetWardConfig(name string) WardConfig {
if wardConf, ok := c.Wards[name]; ok {
return wardConf
}
return c.Ward
}
// UnmarshalJSON unmarshals the warded configuration.
func (c *Config) UnmarshalJSON(data []byte) error {
config := struct {
Ward WardConfig `json:"ward"`
Wards map[string]*json.RawMessage `json:"wards"`
}{
Ward: DefaultWardConfig(),
}
if err := json.Unmarshal(data, &config); err != nil {
return err
}
c.Ward = config.Ward
if c.Wards == nil {
c.Wards = make(map[string]WardConfig)
}
for name, raw := range config.Wards {
base := struct {
Ward WardConfig `json:"ward"`
}{
Ward: DefaultWardConfig(),
}
if err := json.Unmarshal(data, &base); err != nil {
return err
}
if err := json.Unmarshal(*raw, &base.Ward); err != nil {
return err
}
c.Wards[name] = base.Ward
}
return nil
}