forked from crowdsecurity/crowdsec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: container discovery via labels (crowdsecurity#2959)
* wip: attempt to autodiscover via labels * wip: remove labels dep on docker acquistion * wip: remove labels dep on docker acquistion * wip: add debug * wip: try fix parser maps * wip: remove redundant pointer * wip: add debug * wip: cant type assert * wip: reinstate debug * wip: reinstate debug * wip: reinstate debug * wip: oops * wip: add a debug * wip: fix labels * wip: remove redundant paramter * wip: rename config option to be more self declarative * wip: update log wording * wip: the if check was not correct * wip: me lost * fix: add checks to typecast and log useful information * add tests for parseLabels * return nil instead of pointer to empty struct * simplify EvalContainer return value --------- Co-authored-by: Sebastien Blot <[email protected]>
- Loading branch information
1 parent
f06e3e7
commit 9088f31
Showing
4 changed files
with
158 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package dockeracquisition | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func parseLabels(labels map[string]string) map[string]interface{} { | ||
result := make(map[string]interface{}) | ||
for key, value := range labels { | ||
parseKeyToMap(result, key, value) | ||
} | ||
return result | ||
} | ||
|
||
func parseKeyToMap(m map[string]interface{}, key string, value string) { | ||
if !strings.HasPrefix(key, "crowdsec") { | ||
return | ||
} | ||
parts := strings.Split(key, ".") | ||
|
||
if len(parts) < 2 || parts[0] != "crowdsec" { | ||
return | ||
} | ||
|
||
for i := 0; i < len(parts); i++ { | ||
if parts[i] == "" { | ||
return | ||
} | ||
} | ||
|
||
for i := 1; i < len(parts)-1; i++ { | ||
if _, ok := m[parts[i]]; !ok { | ||
m[parts[i]] = make(map[string]interface{}) | ||
} | ||
m = m[parts[i]].(map[string]interface{}) | ||
} | ||
m[parts[len(parts)-1]] = value | ||
} |