|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
5 | 4 | "strings" |
6 | 5 | "time" |
7 | 6 |
|
@@ -135,94 +134,3 @@ func getActorName(event events.Message) string { |
135 | 134 | return ActorName |
136 | 135 |
|
137 | 136 | } |
138 | | - |
139 | | -func excludeEvent(event events.Message) bool { |
140 | | - // Checks if any of the exclusion criteria matches the event |
141 | | - |
142 | | - ActorID := getActorID(event) |
143 | | - |
144 | | - // Convert the event (struct of type event.Message) to a flattend map |
145 | | - eventMap := structToFlatMap(event) |
146 | | - |
147 | | - // Check for all exclude key -> value combinations if they match the event |
148 | | - for key, values := range config.Exclude { |
149 | | - eventValue, keyExist := eventMap[key] |
150 | | - |
151 | | - // Check if the exclusion key exists in the eventMap |
152 | | - if !keyExist { |
153 | | - log.Debug(). |
154 | | - Str("ActorID", ActorID). |
155 | | - Msgf("Exclusion key \"%s\" did not match", key) |
156 | | - return false |
157 | | - } |
158 | | - |
159 | | - log.Debug(). |
160 | | - Str("ActorID", ActorID). |
161 | | - Msgf("Exclusion key \"%s\" matched, checking values", key) |
162 | | - |
163 | | - log.Debug(). |
164 | | - Str("ActorID", ActorID). |
165 | | - Msgf("Event's value for key \"%s\" is \"%s\"", key, eventValue) |
166 | | - |
167 | | - for _, value := range values { |
168 | | - // comparing the prefix to be able to filter actions like "exec_XXX: YYYY" which use a |
169 | | - // special, dynamic, syntax |
170 | | - // see https://github.com/moby/moby/blob/bf053be997f87af233919a76e6ecbd7d17390e62/api/types/events/events.go#L74-L81 |
171 | | - |
172 | | - if strings.HasPrefix(eventValue, value) { |
173 | | - log.Debug(). |
174 | | - Str("ActorID", ActorID). |
175 | | - Msgf("Event excluded based on exclusion setting \"%s=%s\"", key, value) |
176 | | - return true |
177 | | - } |
178 | | - } |
179 | | - log.Debug(). |
180 | | - Str("ActorID", ActorID). |
181 | | - Msgf("Exclusion key \"%s\" matched, but values did not match", key) |
182 | | - } |
183 | | - |
184 | | - return false |
185 | | -} |
186 | | - |
187 | | -// flatten a nested map, separating nested keys by dots |
188 | | -func flattenMap(prefix string, m map[string]interface{}) map[string]string { |
189 | | - flatMap := make(map[string]string) |
190 | | - for k, v := range m { |
191 | | - newKey := k |
192 | | - // separate nested keys by dot |
193 | | - if prefix != "" { |
194 | | - newKey = prefix + "." + k |
195 | | - } |
196 | | - // if the value is a map/struct itself, transverse it recursivly |
197 | | - switch k { |
198 | | - case "Actor", "Attributes": |
199 | | - nestedMap := v.(map[string]interface{}) |
200 | | - for nk, nv := range flattenMap(newKey, nestedMap) { |
201 | | - flatMap[nk] = nv |
202 | | - } |
203 | | - case "time", "timeNano": |
204 | | - flatMap[newKey] = string(v.(json.Number)) |
205 | | - default: |
206 | | - flatMap[newKey] = v.(string) |
207 | | - } |
208 | | - } |
209 | | - return flatMap |
210 | | -} |
211 | | - |
212 | | -// Convert struct to flat map by first converting it to a map (via JSON) and flatten it afterwards |
213 | | -func structToFlatMap(s interface{}) map[string]string { |
214 | | - m := make(map[string]interface{}) |
215 | | - b, err := json.Marshal(s) |
216 | | - if err != nil { |
217 | | - log.Fatal().Err(err).Msg("Marshaling JSON failed") |
218 | | - } |
219 | | - |
220 | | - // Using a custom decoder to set 'UseNumber' which will preserver a string representation of |
221 | | - // time and timeNano instead of converting it to float64 |
222 | | - decoder := json.NewDecoder(strings.NewReader(string(b))) |
223 | | - decoder.UseNumber() |
224 | | - if err := decoder.Decode(&m); err != nil { |
225 | | - log.Fatal().Err(err).Msg("Unmarshaling JSON failed") |
226 | | - } |
227 | | - return flattenMap("", m) |
228 | | -} |
0 commit comments