forked from goss-org/goss
/
json.go
374 lines (319 loc) · 8.59 KB
/
json.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
package goss
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"
"github.com/aelsabbahy/goss/resource"
"github.com/aelsabbahy/goss/system"
"github.com/aelsabbahy/goss/util"
"github.com/codegangsta/cli"
)
type ConfigJSON struct {
Files resource.FileMap `json:"file,omitempty"`
Packages resource.PackageMap `json:"package,omitempty"`
Addrs resource.AddrMap `json:"addr,omitempty"`
Ports resource.PortMap `json:"port,omitempty"`
Services resource.ServiceMap `json:"service,omitempty"`
Users resource.UserMap `json:"user,omitempty"`
Groups resource.GroupMap `json:"group,omitempty"`
Commands resource.CommandMap `json:"command,omitempty"`
DNS resource.DNSMap `json:"dns,omitempty"`
Processes resource.ProcessMap `json:"process,omitempty"`
Gossfiles resource.GossfileMap `json:"gossfile,omitempty"`
}
func NewConfigJSON() *ConfigJSON {
return &ConfigJSON{
Files: make(resource.FileMap),
Packages: make(resource.PackageMap),
Addrs: make(resource.AddrMap),
Ports: make(resource.PortMap),
Services: make(resource.ServiceMap),
Users: make(resource.UserMap),
Groups: make(resource.GroupMap),
Commands: make(resource.CommandMap),
DNS: make(resource.DNSMap),
Processes: make(resource.ProcessMap),
Gossfiles: make(resource.GossfileMap),
}
}
func (c *ConfigJSON) String() string {
jsonData, err := json.MarshalIndent(c, "", " ")
if err != nil {
log.Fatalf("Error writing: %v\n", err)
}
return string(jsonData)
}
func (c *ConfigJSON) Resources() []resource.Resource {
var tests []resource.Resource
gm := genericConcatMaps(c.Commands, c.Addrs, c.DNS, c.Packages, c.Services, c.Files, c.Processes, c.Users, c.Groups, c.Ports)
for _, m := range gm {
for _, t := range m {
// FIXME: Can this be moved to a safer compile-time check?
tests = append(tests, t.(resource.Resource))
}
}
return tests
}
func genericConcatMaps(maps ...interface{}) (ret []map[string]interface{}) {
for _, slice := range maps {
im := interfaceMap(slice)
ret = append(ret, im)
}
return ret
}
func interfaceMap(slice interface{}) map[string]interface{} {
m := reflect.ValueOf(slice)
if m.Kind() != reflect.Map {
panic("InterfaceSlice() given a non-slice type")
}
ret := make(map[string]interface{})
for _, k := range m.MapKeys() {
ret[k.Interface().(string)] = m.MapIndex(k).Interface()
}
return ret
}
func ReadJSON(filePath string) ConfigJSON {
file, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Printf("File error: %v\n", err)
os.Exit(1)
}
return ReadJSONData(file)
}
func ReadJSONData(data []byte) ConfigJSON {
configJSON := NewConfigJSON()
err := json.Unmarshal(data, configJSON)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
return *configJSON
}
func RenderJSON(filePath string) string {
path := filepath.Dir(filePath)
configJSON := mergeJSONData(ReadJSON(filePath), 0, path)
return configJSON.String()
}
func mergeJSONData(configJSON ConfigJSON, depth int, path string) ConfigJSON {
depth++
if depth >= 50 {
fmt.Println("Error: Max depth of 50 reached, possibly due to dependency loop in goss file")
os.Exit(1)
}
for _, g := range configJSON.Gossfiles {
fpath := filepath.Join(path, g.ID())
fdir := filepath.Dir(fpath)
j := mergeJSONData(ReadJSON(fpath), depth, fdir)
configJSON = mergeGoss(configJSON, j)
}
return configJSON
}
func mergeGoss(g1, g2 ConfigJSON) ConfigJSON {
g1.Gossfiles = nil
for k, v := range g2.Files {
g1.Files[k] = v
}
for k, v := range g2.Packages {
g1.Packages[k] = v
}
for k, v := range g2.Addrs {
g1.Addrs[k] = v
}
for k, v := range g2.Ports {
g1.Ports[k] = v
}
for k, v := range g2.Services {
g1.Services[k] = v
}
for k, v := range g2.Users {
g1.Users[k] = v
}
for k, v := range g2.Groups {
g1.Groups[k] = v
}
for k, v := range g2.Commands {
g1.Commands[k] = v
}
for k, v := range g2.DNS {
g1.DNS[k] = v
}
for k, v := range g2.Processes {
g1.Processes[k] = v
}
return g1
}
func WriteJSON(filePath string, configJSON ConfigJSON) error {
jsonData, err := json.MarshalIndent(configJSON, "", " ")
if err != nil {
log.Fatalf("Error writing: %v\n", err)
}
if err := ioutil.WriteFile(filePath, jsonData, 0644); err != nil {
log.Fatalf("Error writing: %v\n", err)
}
return nil
}
func AppendResource(fileName, resourceName, key string, c *cli.Context) error {
config := util.Config{
IgnoreList: c.GlobalStringSlice("exclude-attr"),
Timeout: int(c.Duration("timeout") / time.Millisecond),
}
var configJSON ConfigJSON
if _, err := os.Stat(fileName); err == nil {
configJSON = ReadJSON(fileName)
} else {
configJSON = *NewConfigJSON()
}
sys := system.New(c)
// Need to figure out a good way to refactor this
switch resourceName {
case "Addr":
res, err := configJSON.Addrs.AppendSysResource(key, sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Command":
res, err := configJSON.Commands.AppendSysResource(key, sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "DNS":
res, err := configJSON.DNS.AppendSysResource(key, sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "File":
res, err := configJSON.Files.AppendSysResource(key, sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Group":
res, err := configJSON.Groups.AppendSysResource(key, sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Package":
res, err := configJSON.Packages.AppendSysResource(key, sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Port":
res, err := configJSON.Ports.AppendSysResource(key, sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Process":
res, err := configJSON.Processes.AppendSysResource(key, sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Service":
res, err := configJSON.Services.AppendSysResource(key, sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "User":
res, err := configJSON.Users.AppendSysResource(key, sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Gossfile":
res, err := configJSON.Gossfiles.AppendSysResource(key, sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
}
WriteJSON(fileName, configJSON)
return nil
}
func AutoAppendResource(fileName, key string, c *cli.Context) error {
var configJSON ConfigJSON
if _, err := os.Stat(fileName); err == nil {
configJSON = ReadJSON(fileName)
} else {
configJSON = *NewConfigJSON()
}
sys := system.New(c)
// file
if strings.Contains(key, "/") {
if res, _, ok := configJSON.Files.AppendSysResourceIfExists(key, sys); ok == true {
resourcePrint(fileName, res)
}
}
// group
if res, _, ok := configJSON.Groups.AppendSysResourceIfExists(key, sys); ok == true {
resourcePrint(fileName, res)
}
// package
if res, _, ok := configJSON.Packages.AppendSysResourceIfExists(key, sys); ok == true {
resourcePrint(fileName, res)
}
// port
if res, _, ok := configJSON.Ports.AppendSysResourceIfExists(key, sys); ok == true {
resourcePrint(fileName, res)
}
// process
if res, sysres, ok := configJSON.Processes.AppendSysResourceIfExists(key, sys); ok == true {
resourcePrint(fileName, res)
ports := system.GetPorts(true)
pids, _ := sysres.Pids()
for _, pid := range pids {
pidS := strconv.Itoa(pid)
for port, entries := range ports {
for _, entry := range entries {
if entry.Pid == pidS {
// port
if res, _, ok := configJSON.Ports.AppendSysResourceIfExists(port, sys); ok == true {
resourcePrint(fileName, res)
}
}
}
}
}
}
// Service
if res, _, ok := configJSON.Services.AppendSysResourceIfExists(key, sys); ok == true {
resourcePrint(fileName, res)
}
// user
if res, _, ok := configJSON.Users.AppendSysResourceIfExists(key, sys); ok == true {
resourcePrint(fileName, res)
}
WriteJSON(fileName, configJSON)
return nil
}
func resourcePrint(fileName string, res resource.IDer) {
resMap := map[string]resource.IDer{res.ID(): res}
oj, _ := json.MarshalIndent(resMap, "", " ")
typ := reflect.TypeOf(res)
typs := strings.Split(typ.String(), ".")[1]
fmt.Printf("Adding %s to '%s':\n\n%s\n\n", typs, fileName, string(oj))
}