Skip to content

Commit

Permalink
feat(cobra): add parser for structured config file
Browse files Browse the repository at this point in the history
This commit adds a parser for a structured config file using viper.

Now, the config file can be written in a more structured way, like:

cache:
    type: mem
    size: 1024

The old way (cli flags) is still supported.

cache:
    - cache-type=mem
    - mem-cache-size=556
  • Loading branch information
geyslan committed Oct 26, 2023
1 parent cc534be commit 64f737a
Show file tree
Hide file tree
Showing 4 changed files with 1,707 additions and 13 deletions.
53 changes: 48 additions & 5 deletions examples/config/global_config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
blob-perf-buffer-size: 1024
cache:
- none
# - none # cache-type={none,mem}
- cache-type=none
# - mem-cache-size=556
# cache:
# type: mem
# size: 256
proctree:
- none
capabilities: []
# - source=events
source: events
cache:
process: 8192
thread: 4096
capabilities:
# - bypass=false
# - add=cap_kill,cap_syslog
bypass: true
add:
- cap_sys_admin
- cap_syslog
drop:
- cap_chown
containers: false
crs: []
crs:
- containerd:/var/run/containerd/containerd.sock
- docker:/var/run/docker.sock
- runtime:
name: containerd
socket: /var/run/containerd/containerd.sock
- runtime:
name: docker
socket: /var/run/docker.sock


healthz: false
install-path: /tmp/tracee
listen-addr: :3366
Expand All @@ -14,8 +41,24 @@ log:
metrics: false
output:
- json
# - webhook:HTTP://localhost:8080?timeout=5s
# output-structured:
# - webhook:
# name: webhook1
# host: http://localhost
# port: 8000
# timeout: 5s
# - webhook:
# name: webhook2
# host: http://localhost
# port: 9000
# timeout: 5s
perf-buffer-size: 1024
pprof: false
pyroscope: false
rego: []
rego:
# - partial-eval
- aio
# partial-eval: true
# aio: true
signatures-dir: ""
53 changes: 45 additions & 8 deletions pkg/cmd/cobra/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,27 @@ func GetTraceeRunner(c *cobra.Command, version string) (cmd.Runner, error) {
// Log command line flags

// Logger initialization must be the first thing to be done,
// so all other packages can use it
logCfg, err := flags.PrepareLogger(viper.GetStringSlice("log"), true)
// so all other packages can use

logFlags, err := GetFlagsFromViper("log")
if err != nil {
return runner, err
}

logCfg, err := flags.PrepareLogger(logFlags, true)
if err != nil {
return runner, err
}
logger.Init(logCfg)

// Rego command line flags

rego, err := flags.PrepareRego(viper.GetStringSlice("rego"))
regoFlags, err := GetFlagsFromViper("rego")
if err != nil {
return runner, err
}

rego, err := flags.PrepareRego(regoFlags)
if err != nil {
return runner, err
}
Expand Down Expand Up @@ -85,7 +96,12 @@ func GetTraceeRunner(c *cobra.Command, version string) (cmd.Runner, error) {
// Container Runtime command line flags

if !cfg.NoContainersEnrich {
sockets, err := flags.PrepareContainers(viper.GetStringSlice("crs"))
crsFlags, err := GetFlagsFromViper("crs")
if err != nil {
return runner, err
}

sockets, err := flags.PrepareContainers(crsFlags)
if err != nil {
return runner, err
}
Expand All @@ -94,7 +110,12 @@ func GetTraceeRunner(c *cobra.Command, version string) (cmd.Runner, error) {

// Cache command line flags

cache, err := flags.PrepareCache(viper.GetStringSlice("cache"))
cacheFlags, err := GetFlagsFromViper("cache")
if err != nil {
return runner, err
}

cache, err := flags.PrepareCache(cacheFlags)
if err != nil {
return runner, err
}
Expand All @@ -105,7 +126,12 @@ func GetTraceeRunner(c *cobra.Command, version string) (cmd.Runner, error) {

// Process Tree command line flags

procTree, err := flags.PrepareProcTree(viper.GetStringSlice("proctree"))
procTreeFlags, err := GetFlagsFromViper("proctree")
if err != nil {
return runner, err
}

procTree, err := flags.PrepareProcTree(procTreeFlags)
if err != nil {
return runner, err
}
Expand All @@ -126,7 +152,12 @@ func GetTraceeRunner(c *cobra.Command, version string) (cmd.Runner, error) {

// Capabilities command line flags

capsCfg, err := flags.PrepareCapabilities(viper.GetStringSlice("capabilities"))
capFlags, err := GetFlagsFromViper("capabilities")
if err != nil {
return runner, err
}

capsCfg, err := flags.PrepareCapabilities(capFlags)
if err != nil {
return runner, err
}
Expand Down Expand Up @@ -173,7 +204,13 @@ func GetTraceeRunner(c *cobra.Command, version string) (cmd.Runner, error) {
cfg.Policies = policies

// Output command line flags
output, err := flags.PrepareOutput(viper.GetStringSlice("output"), true)

outputFlags, err := GetFlagsFromViper("output")
if err != nil {
return runner, err
}

output, err := flags.PrepareOutput(outputFlags, true)
if err != nil {
return runner, err
}
Expand Down
Loading

0 comments on commit 64f737a

Please sign in to comment.