Skip to content

Commit

Permalink
chore(config): add gotemplate and content-type
Browse files Browse the repository at this point in the history
Since #3622, webhooks can be configured with a gotemplate and a
content-type. This commit adds these two fields to the config file.
  • Loading branch information
geyslan authored and rafaeldtinoco committed Oct 30, 2023
1 parent 0e40953 commit 5da88d1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
4 changes: 4 additions & 0 deletions examples/config/global_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,15 @@ output:
# host: localhost
# port: 8000
# timeout: 5s
# gotemplate: /path/to/template/test.tmpl
# content-type: application/json
# - webhook2:
# protocol: http
# host: localhost
# port: 9000
# timeout: 3s
# gotemplate: /path/to/template/test.tmpl
# content-type: application/json

# options:
# none: false
Expand Down
19 changes: 15 additions & 4 deletions pkg/cmd/cobra/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func (c *LogConfig) flags() []string {
if c.Filters.LibBPF {
flags = append(flags, "filter:libbpf")
}

flags = append(flags, getLogFilterAttrFlags(false, c.Filters.In)...)
flags = append(flags, getLogFilterAttrFlags(true, c.Filters.Out)...)

Expand Down Expand Up @@ -404,6 +405,7 @@ func (c *OutputConfig) flags() []string {
if len(c.GoTemplate.Files) > 0 {
templateFlag += ":" + strings.Join(c.GoTemplate.Files, ",")
}

flags = append(flags, templateFlag)
}

Expand Down Expand Up @@ -432,6 +434,13 @@ func (c *OutputConfig) flags() []string {
if webhook.Timeout != "" {
url += fmt.Sprintf("?timeout=%s", webhook.Timeout)
}
if webhook.GoTemplate != "" {
url += fmt.Sprintf("?gotemplate=%s", webhook.GoTemplate)
}
if webhook.ContentType != "" {
url += fmt.Sprintf("?contentType=%s", webhook.ContentType)
}

flags = append(flags, fmt.Sprintf("webhook:%s", url))
}

Expand Down Expand Up @@ -468,8 +477,10 @@ type OutputForwardConfig struct {
}

type OutputWebhookConfig struct {
Protocol string `mapstructure:"protocol"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Timeout string `mapstructure:"timeout"`
Protocol string `mapstructure:"protocol"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Timeout string `mapstructure:"timeout"`
GoTemplate string `mapstructure:"gotemplate"`
ContentType string `mapstructure:"content-type"`
}
22 changes: 14 additions & 8 deletions pkg/cmd/cobra/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,15 @@ output:
host: localhost
port: 8000
timeout: 5s
gotemplate: /path/to/template1
content-type: application/json
- webhook2:
protocol: http
host: localhost
port: 9000
timeout: 3s
gotemplate: /path/to/template2
content-type: application/ld+json
`,
key: "output",
expectedFlags: []string{
Expand All @@ -376,8 +380,8 @@ output:
"gotemplate=template1:file3,file4",
"forward:tcp://user:[email protected]:24224?tag=tracee1",
"forward:udp://user:[email protected]:24225?tag=tracee2",
"webhook:http://localhost:8000?timeout=5s",
"webhook:http://localhost:9000?timeout=3s",
"webhook:http://localhost:8000?timeout=5s?gotemplate=/path/to/template1?contentType=application/json",
"webhook:http://localhost:9000?timeout=3s?gotemplate=/path/to/template2?contentType=application/ld+json",
},
},
}
Expand Down Expand Up @@ -1063,19 +1067,21 @@ func TestOutputConfigFlags(t *testing.T) {
},
},
{
name: "test webhook with timeout",
name: "test webhook with all fields",
config: OutputConfig{
Webhooks: map[string]OutputWebhookConfig{
"example3": {
Protocol: "http",
Host: "webhook.com",
Port: 9090,
Timeout: "5s",
Protocol: "http",
Host: "webhook.com",
Port: 9090,
Timeout: "5s",
GoTemplate: "/path/to/template1",
ContentType: "application/json",
},
},
},
expected: []string{
"webhook:http://webhook.com:9090?timeout=5s",
"webhook:http://webhook.com:9090?timeout=5s?gotemplate=/path/to/template1?contentType=application/json",
},
},
{
Expand Down

0 comments on commit 5da88d1

Please sign in to comment.