-
Notifications
You must be signed in to change notification settings - Fork 29
/
configs.go
264 lines (244 loc) Β· 6.51 KB
/
configs.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
package main
import (
"bytes"
"encoding/hex"
"fmt"
"os"
"reflect"
"github.com/BurntSushi/toml"
)
// parseConfig takes the config content as a string and attempts to parse it
// into the conf struct based on the TOML spec.
func parseConfig(configContent string) {
if configContent == "" {
fail("Configuration is empty!")
os.Exit(1)
}
md, err := toml.Decode(configContent, &conf)
if err != nil {
fail("Error decoding TOML: " + err.Error())
os.Exit(1)
}
if verboseEnabled {
for _, undecoded := range md.Undecoded() {
warn("Undecoded scoring configuration key \"" + undecoded.String() + "\" will not be used.")
}
}
// If there's no remote, local must be enabled.
if conf.Remote == "" {
conf.Local = true
if conf.DisableRemoteEncryption {
fail("Remote encryption cannot be disabled if remote is not enabled!")
os.Exit(1)
}
} else {
if conf.Remote[len(conf.Remote)-1] == '/' {
fail("Your remote URL must not end with a slash: try", conf.Remote[:len(conf.Remote)-1])
os.Exit(1)
}
if conf.Name == "" {
fail("Need image name in config if remote is enabled.")
os.Exit(1)
}
if conf.Password == "" && conf.DisableRemoteEncryption == false {
fail("Need password in config if remote is enabled.")
os.Exit(1)
}
if conf.DisableRemoteEncryption && conf.Password != "" {
warn("Remote encryption is disabled, but a password is still defined!")
}
}
// Check if the config version matches ours.
if conf.Version != version {
warn("Scoring version does not match Aeacus version! Compatibility issues may occur.")
info("Consider updating your config to include:")
info(" version = '" + version + "'")
}
// Print warnings for impossible checks and undefined check types.
for i, check := range conf.Check {
if len(check.Pass) == 0 && len(check.PassOverride) == 0 {
warn("Check " + fmt.Sprintf("%d", i+1) + " does not define any possible ways to pass!")
}
allConditions := append(append(append([]cond{}, check.Pass[:]...), check.Fail[:]...), check.PassOverride[:]...)
for j, cond := range allConditions {
if cond.Type == "" {
warn("Check " + fmt.Sprintf("%d condition %d", i+1, j+1) + " does not have a check type!")
}
}
}
}
// writeConfig writes the in-memory config to disk as the an encrypted
// configuration file.
func writeConfig() {
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(conf); err != nil {
fail(err.Error())
os.Exit(1)
return
}
dataPath := dirPath + scoringData
encryptedConfig, err := encryptConfig(buf.String())
if err != nil {
fail("Encrypting config failed: " + err.Error())
os.Exit(1)
} else if verboseEnabled {
info("Writing data to " + dataPath + "...")
}
writeFile(dataPath, encryptedConfig)
}
// ReadConfig parses the scoring configuration file.
func readConfig() {
fileContent, err := readFile(dirPath + scoringConf)
if err != nil {
fail("Configuration file (" + dirPath + scoringConf + ") not found!")
os.Exit(1)
}
parseConfig(fileContent)
assignPoints()
assignDescriptions()
if verboseEnabled {
printConfig()
}
obfuscateConfig()
}
// PrintConfig offers a printed representation of the config, as parsed
// by readData and parseConfig.
func printConfig() {
pass("Configuration " + dirPath + scoringConf + " validity check passed!")
blue("CONF", scoringConf)
if conf.Version != "" {
pass("Version:", conf.Version)
}
if conf.Title == "" {
red("MISS", "Title:", "N/A")
} else {
pass("Title:", conf.Title)
}
if conf.Name == "" {
red("MISS", "Name:", "N/A")
} else {
pass("Name:", conf.Name)
}
if conf.OS == "" {
red("MISS", "OS:", "N/A")
} else {
pass("OS:", conf.OS)
}
if conf.User == "" {
red("MISS", "User:", "N/A")
} else {
pass("User:", conf.User)
}
if conf.Remote != "" {
pass("Remote:", conf.Remote)
}
if conf.DisableRemoteEncryption {
pass("Remote Encryption:", "Disabled")
} else {
pass("Remote Encryption:", "Enabled")
}
if conf.Local {
pass("Local:", conf.Local)
}
if conf.EndDate != "" {
pass("End Date:", conf.EndDate)
}
for i, check := range conf.Check {
green("CHCK", fmt.Sprintf("Check %d (%d points):", i+1, check.Points))
fmt.Println("Message:", check.Message)
for _, c := range check.Pass {
fmt.Println("Pass Condition:")
fmt.Print(c)
}
for _, c := range check.PassOverride {
fmt.Println("PassOverride Condition:")
fmt.Print(c)
}
for _, c := range check.Fail {
fmt.Println("Fail Condition:")
fmt.Print(c)
}
}
}
func obfuscateConfig() {
if debugEnabled {
debug("Obfuscating configuration...")
}
if err := obfuscateData(&conf.Password); err != nil {
fail(err.Error())
}
for i, check := range conf.Check {
if err := obfuscateData(&conf.Check[i].Message); err != nil {
fail(err.Error())
}
if conf.Check[i].Hint != "" {
if err := obfuscateData(&conf.Check[i].Hint); err != nil {
fail(err.Error())
}
}
for j := range check.Pass {
if err := obfuscateCond(&conf.Check[i].Pass[j]); err != nil {
fail(err.Error())
}
}
for j := range check.PassOverride {
if err := obfuscateCond(&conf.Check[i].PassOverride[j]); err != nil {
fail(err.Error())
}
}
for j := range check.Fail {
if err := obfuscateCond(&conf.Check[i].Fail[j]); err != nil {
fail(err.Error())
}
}
}
}
// obfuscateCond is a convenience function to obfuscate all string fields of a
// struct using reflection. It assumes all struct fields are strings.
func obfuscateCond(c *cond) error {
s := reflect.ValueOf(c).Elem()
for i := 0; i < s.NumField(); i++ {
if s.Type().Field(i).Name == "regex" {
continue
}
datum := s.Field(i).String()
if err := obfuscateData(&datum); err != nil {
return err
}
s.Field(i).SetString(datum)
}
return nil
}
// deobfuscateCond is a convenience function to deobfuscate all string fields
// of a struct using reflection.
func deobfuscateCond(c *cond) error {
s := reflect.ValueOf(c).Elem()
for i := 0; i < s.NumField(); i++ {
if s.Type().Field(i).Name == "regex" {
continue
}
datum := s.Field(i).String()
if err := deobfuscateData(&datum); err != nil {
return err
}
s.Field(i).SetString(datum)
}
return nil
}
func xor(key, plaintext string) string {
ciphertext := make([]byte, len(plaintext))
for i := 0; i < len(plaintext); i++ {
ciphertext[i] = key[i%len(key)] ^ plaintext[i]
}
return string(ciphertext)
}
func hexEncode(inputString string) string {
return hex.EncodeToString([]byte(inputString))
}
func hexDecode(inputString string) (string, error) {
result, err := hex.DecodeString(inputString)
if err != nil {
return "", err
}
return string(result), nil
}