forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goss_config.go
180 lines (148 loc) · 4.71 KB
/
goss_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
package goss
import (
"log"
"reflect"
"github.com/goss-org/goss/resource"
)
type GossConfig struct {
Files resource.FileMap `json:"file,omitempty" yaml:"file,omitempty"`
Packages resource.PackageMap `json:"package,omitempty" yaml:"package,omitempty"`
Addrs resource.AddrMap `json:"addr,omitempty" yaml:"addr,omitempty"`
Ports resource.PortMap `json:"port,omitempty" yaml:"port,omitempty"`
Services resource.ServiceMap `json:"service,omitempty" yaml:"service,omitempty"`
Users resource.UserMap `json:"user,omitempty" yaml:"user,omitempty"`
Groups resource.GroupMap `json:"group,omitempty" yaml:"group,omitempty"`
Commands resource.CommandMap `json:"command,omitempty" yaml:"command,omitempty"`
DNS resource.DNSMap `json:"dns,omitempty" yaml:"dns,omitempty"`
Processes resource.ProcessMap `json:"process,omitempty" yaml:"process,omitempty"`
Gossfiles resource.GossfileMap `json:"gossfile,omitempty" yaml:"gossfile,omitempty"`
KernelParams resource.KernelParamMap `json:"kernel-param,omitempty" yaml:"kernel-param,omitempty"`
Mounts resource.MountMap `json:"mount,omitempty" yaml:"mount,omitempty"`
Interfaces resource.InterfaceMap `json:"interface,omitempty" yaml:"interface,omitempty"`
HTTPs resource.HTTPMap `json:"http,omitempty" yaml:"http,omitempty"`
Matchings resource.MatchingMap `json:"matching,omitempty" yaml:"matching,omitempty"`
}
func NewGossConfig() *GossConfig {
return &GossConfig{
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),
KernelParams: make(resource.KernelParamMap),
Mounts: make(resource.MountMap),
Interfaces: make(resource.InterfaceMap),
HTTPs: make(resource.HTTPMap),
Matchings: make(resource.MatchingMap),
}
}
// Merge consumes all the resources in g2 into c, duplicate resources
// will be overwritten with the ones in g2
func (c *GossConfig) Merge(g2 GossConfig) {
for k, v := range g2.Files {
mergeType(c.Files, "file", k, v)
}
for k, v := range g2.Packages {
mergeType(c.Packages, "package", k, v)
}
for k, v := range g2.Addrs {
mergeType(c.Addrs, "addr", k, v)
}
for k, v := range g2.Ports {
mergeType(c.Ports, "port", k, v)
}
for k, v := range g2.Services {
mergeType(c.Services, "service", k, v)
}
for k, v := range g2.Users {
mergeType(c.Users, "user", k, v)
}
for k, v := range g2.Groups {
mergeType(c.Groups, "group", k, v)
}
for k, v := range g2.Commands {
mergeType(c.Commands, "command", k, v)
}
for k, v := range g2.DNS {
mergeType(c.DNS, "dns", k, v)
}
for k, v := range g2.Processes {
mergeType(c.Processes, "process", k, v)
}
for k, v := range g2.KernelParams {
mergeType(c.KernelParams, "kernel-param", k, v)
}
for k, v := range g2.Mounts {
mergeType(c.Mounts, "mount", k, v)
}
for k, v := range g2.Interfaces {
mergeType(c.Interfaces, "interface", k, v)
}
for k, v := range g2.HTTPs {
mergeType(c.HTTPs, "http", k, v)
}
for k, v := range g2.Matchings {
mergeType(c.Matchings, "matching", k, v)
}
}
func mergeType[V any](m map[string]V, t, k string, v V) {
if _, ok := m[k]; ok {
log.Printf("[WARN] Duplicate key detected: '%s: %s'. The value from a later-loaded goss file has overwritten the previous value.", t, k)
}
m[k] = v
}
func (c *GossConfig) Resources() []resource.Resource {
var tests []resource.Resource
gm := genericConcatMaps(c.Commands,
c.HTTPs,
c.Addrs,
c.DNS,
c.Packages,
c.Services,
c.Files,
c.Processes,
c.Users,
c.Groups,
c.Ports,
c.KernelParams,
c.Mounts,
c.Interfaces,
c.Matchings,
)
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 ...any) (ret []map[string]any) {
for _, slice := range maps {
im := interfaceMap(slice)
ret = append(ret, im)
}
return ret
}
func interfaceMap(slice any) map[string]any {
m := reflect.ValueOf(slice)
if m.Kind() != reflect.Map {
panic("InterfaceSlice() given a non-slice type")
}
ret := make(map[string]any)
for _, k := range m.MapKeys() {
ret[k.Interface().(string)] = m.MapIndex(k).Interface()
}
return ret
}
func mergeGoss(g1, g2 GossConfig) GossConfig {
g1.Gossfiles = nil
g1.Merge(g2)
return g1
}