Skip to content

Instantly share code, notes, and snippets.

@sbs2001
Created July 28, 2022 06:33
Show Gist options
  • Select an option

  • Save sbs2001/ec82e7f67bc72d4ef18b7806268c75cc to your computer and use it in GitHub Desktop.

Select an option

Save sbs2001/ec82e7f67bc72d4ef18b7806268c75cc to your computer and use it in GitHub Desktop.

Proposal for enabling LAPI error handling in bouncers

Context

This proposal is for solving crowdsecurity/cs-firewall-bouncer#166 in a generalized manner.

Currently the clients of StreamBouncer have no way to handle the errors on LAPI calls .

We could solve this specific issue by tracking LAPI call errors via prometheus counter.

However we want to keep the code flexible enough to do other types of error handling.

Approach

One way of doing this would be to have add an error handler function as a field to the StreamBouncer struct.

type StreamBouncer struct {
	APIKey             string `yaml:"api_key"`
    ........
    ........
    LAPIErrorHandler func(err error)
    ........
    ........

Then upon encountering an error when making LAPI call, the error would be passed to the LAPIErrorHandler function. The code in Run() method would look like

	data, resp, err := b.APIClient.Decisions.GetStream(context.Background(), b.Opts)
		if err != nil {
			if resp != nil && resp.Response != nil {
				resp.Response.Body.Close()
			}
			b.LAPIErrorHandler(err)
			continue
		}

The clients of StreamBouncer, in this case the firewall bouncer then would pass an error handler like:

var TotalLAPIError prometheus.Counter = promauto.NewCounter(prometheus.CounterOpts{
	Name: "lapi_call_failures",
	Help: "The total number of failed calls to CrowdSec LAPI",
},
)


prometheusErrorTracker := func(err error) {
    TotalLAPIError.Inc()
    log.Errorf(err.Error())
}
bouncer := &csbouncer.StreamBouncer{LAPIErrorHandler: prometheusErrorTracker}
..... // Do config + Init stuff
.....

Default Error Handler

If the clients of StreamBouncer don't pass an error handler, we will set the value ofLAPIErrorHandler to a default error handler, which would just log the error. This would be checked in the Init() method of StreamBouncer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment