Skip to content

Commit

Permalink
allow to set the http method for webhook output
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Labarussias <[email protected]>
  • Loading branch information
Issif committed Feb 28, 2023
1 parent 2ac2f8d commit 345388e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ opsgenie:

webhook:
# address: "" # Webhook address, if not empty, Webhook output is enabled
# customHeaders: # Custom headers to add in POST, useful for Authentication
# method: "POST" # Webhook http method: POST or PUT (default: POST)
# customHeaders: # Custom headers to add in the request, useful for Authentication
# key: value
# minimumpriority: "" # minimum priority of event for using this output, order is emergency|alert|critical|error|warning|notice|informational|debug or "" (default)
# mutualtls: false # if true, checkcert flag will be ignored (server cert will always be checked)
Expand Down Expand Up @@ -814,6 +815,7 @@ care of lower/uppercases**) : `yaml: a.b --> envvar: A_B` :
- **DOGSTATSD_TAGS**: A comma-separated list of tags to add to all metrics
- **WEBHOOK_ADDRESS** : Webhook address, if not empty, Webhook output is
_enabled_
- **WEBHOOK_METHOD** : HTTP method: POST or PUT (default: POST)
- **WEBHOOK_CUSTOMHEADERS** : a list of comma separated custom headers to add,
syntax is "key:value,key:value"
- **WEBHOOK_MINIMUMPRIORITY** : minimum priority of event for using this output,
Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func getConfig() *types.Configuration {
v.SetDefault("Dogstatsd.Tags", []string{})

v.SetDefault("Webhook.Address", "")
v.SetDefault("Webhook.Method", "POST")
v.SetDefault("Webhook.MinimumPriority", "")
v.SetDefault("Webhook.MutualTls", false)
v.SetDefault("Webhook.CheckCert", true)
Expand Down
3 changes: 2 additions & 1 deletion config_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ opsgenie:

webhook:
# address: "" # Webhook address, if not empty, Webhook output is enabled
# customHeaders: # Custom headers to add in POST, useful for Authentication
# method: "POST" # HTTP method: POST or PUT (default: POST)
# customHeaders: # Custom headers to add in the request, useful for Authentication
# key: value
# minimumpriority: "" # minimum priority of event for using this output, order is emergency|alert|critical|error|warning|notice|informational|debug or "" (default)
# mutualtls: false # if true, checkcert flag will be ignored (server cert will always be checked)
Expand Down
18 changes: 16 additions & 2 deletions outputs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ const MutualTLSClientCertFilename = "/client.crt"
const MutualTLSClientKeyFilename = "/client.key"
const MutualTLSCacertFilename = "/ca.crt"

// HTTP Methods
const HttpPost = "POST"
const HttpPut = "PUT"

// Headers to add to the client before sending the request
type Header struct {
Key string
Expand Down Expand Up @@ -138,8 +142,18 @@ func NewClient(outputType string, defaultEndpointURL string, mutualTLSEnabled bo
return &Client{OutputType: outputType, EndpointURL: endpointURL, MutualTLSEnabled: mutualTLSEnabled, CheckCert: checkCert, HeaderList: []Header{}, ContentType: DefaultContentType, Config: config, Stats: stats, PromStats: promStats, StatsdClient: statsdClient, DogstatsdClient: dogstatsdClient}, nil
}

// Post sends event (payload) to Output.
// Post sends event (payload) to Output with POST http method.
func (c *Client) Post(payload interface{}) error {
return c.sendRequest("POST", payload)
}

// Put sends event (payload) to Output with PUT http method.
func (c *Client) Put(payload interface{}) error {
return c.sendRequest("PUT", payload)
}

// Post sends event (payload) to Output.
func (c *Client) sendRequest(method string, payload interface{}) error {
// defer + recover to catch panic if output doesn't respond
defer func() {
if err := recover(); err != nil {
Expand Down Expand Up @@ -209,7 +223,7 @@ func (c *Client) Post(payload interface{}) error {
Transport: customTransport,
}

req, err := http.NewRequest("POST", c.EndpointURL.String(), body)
req, err := http.NewRequest(method, c.EndpointURL.String(), body)
if err != nil {
log.Printf("[ERROR] : %v - %v\n", c.OutputType, err.Error())
}
Expand Down
8 changes: 7 additions & 1 deletion outputs/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package outputs

import (
"log"
"strings"

"github.com/falcosecurity/falcosidekick/types"
)
Expand All @@ -17,8 +18,13 @@ func (c *Client) WebhookPost(falcopayload types.FalcoPayload) {
c.AddHeader(i, j)
}
}
var err error
if strings.ToUpper(c.Config.Webhook.Method) == HttpPut {
err = c.Put(falcopayload)
} else {
err = c.Post(falcopayload)
}

err := c.Post(falcopayload)
if err != nil {
go c.CountMetric(Outputs, 1, []string{"output:webhook", "status:error"})
c.Stats.Webhook.Add(Error, 1)
Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ type opsgenieOutputConfig struct {
// WebhookOutputConfig represents parameters for Webhook
type WebhookOutputConfig struct {
Address string
Method string
CustomHeaders map[string]string
MinimumPriority string
CheckCert bool
Expand Down

0 comments on commit 345388e

Please sign in to comment.