crowdsec_manager icon indicating copy to clipboard operation
crowdsec_manager copied to clipboard

Bouncer status disconnected

Open Floordb opened this issue 9 months ago • 14 comments

The bouncer status shows as "Disconnected" while it is actually working correctly.

cscli bouncers list confirms Valid: ✔️ with a recent Last API pull.

Image

Floordb avatar Feb 28 '26 08:02 Floordb

thanks, you for the bug report,. i am aware of this and it's not an issue with functionality. your bouncer is fine. ui shows it disconnected. i have no way to show validation... if anyone can help me validate bounces status in UI would great.

hhftechnology avatar Feb 28 '26 09:02 hhftechnology

Hello Same issue here. I know it's not well viewed but I ran your code into Claude Code and tried its fix in my setup and it seems to be working. At least the bouncer status is correct. If you don't want to hear about AI fixing bugs that's perfectly fine. Otherwise let me know and I'll share it's findings.

notarobot63 avatar Feb 28 '26 14:02 notarobot63

@notarobot63 i/we am not r/selfhosted. we are open to any good code and optimization AI or not

hhftechnology avatar Feb 28 '26 19:02 hhftechnology

Here is the root cause and a working fix, tested on CrowdSec v1.7.6 + crowdsec-firewall-bouncer v0.0.32.


Root cause

CrowdSec's local API used to include a "valid": true field in its bouncer response. Recent versions replaced it with "revoked": false — same information, inverted polarity, different key.

The manager's Go backend deserializes the response into this struct (internal/models/models.go):

type Bouncer struct {
    Name      string    `json:"name"`
    IPAddress string    `json:"ip_address"`
    Valid     bool      `json:"valid"`      // ← expected, but no longer sent
    LastPull  time.Time `json:"last_pull"`
    // ...
}

Go's json.Unmarshal silently ignores unknown keys. Since "valid" is absent from the response, Valid stays at its zero value (false). Every bouncer then falls into the disconnected branch of the status check in handlers/bouncers.go:

if bouncers[i].Valid && time.Since(bouncers[i].LastPull) <= 60*time.Minute {
    bouncers[i].Status = "connected"
} else if bouncers[i].Valid {
    bouncers[i].Status = "stale"
} else {
    bouncers[i].Status = "disconnected"  // ← always reached
}

Fix — two files

internal/models/models.go — add the Revoked field:

 type Bouncer struct {
 	Name      string    `json:"name"`
 	IPAddress string    `json:"ip_address"`
+	Revoked   bool      `json:"revoked"`
 	Valid     bool      `json:"valid"`
 	LastPull  time.Time `json:"last_pull"`
 	Type      string    `json:"type"`
 	Version   string    `json:"version"`
 	Status    string    `json:"status"`
 }

internal/api/handlers/bouncers.go — derive Valid from !Revoked before the status check:

 		for i := range bouncers {
+			// CrowdSec now emits "revoked: false" instead of the legacy "valid: true".
+			// Derive Valid from !Revoked so both API formats are handled correctly.
+			bouncers[i].Valid = !bouncers[i].Revoked
 			if bouncers[i].Valid && time.Since(bouncers[i].LastPull) <= 60*time.Minute {
 				bouncers[i].Status = "connected"

This is backward compatible: if CrowdSec ever reverts to sending "valid": true, both fields get populated and the derivation from the zero-valued Revoked (false) still produces Valid = true.


Status matrix after fix

Scenario revoked from API last_pull Status
Active bouncer false < 60 min connected
Alive but quiet false > 60 min stale
Revoked bouncer true — disconnected
Old CrowdSec (sends valid: true) (field absent) < 60 min connected

Happy to open a PR if that's easier.

notarobot63 avatar Feb 28 '26 21:02 notarobot63

@notarobot63 | Revoked bouncer | true | — | disconnected | this will never happen. Here is an example

/ # cscli bouncers list -o json
[
  {
    "created_at": "2026-03-01T06:29:03.824174764Z",
    "updated_at": "2026-03-01T06:43:23.742932677Z",
    "name": "new-traefik-bouncer",
    "revoked": false,
    "ip_address": "172.17.1.7",
    "type": "",
    "version": "1.X.X",
    "last_pull": null,
    "auth_type": "api-key",
    "os": "unknown/unknown",
    "auto_created": false
  },
  {
    "created_at": "2026-03-01T06:41:29.75268667Z",
    "updated_at": "2026-03-01T06:44:37.69538176Z",
    "name": "traefik-bouncer",
    "revoked": false,
    "ip_address": "172.17.1.7",
    "type": "",
    "version": "1.X.X",
    "last_pull": null,
    "auth_type": "api-key",
    "os": "unknown/unknown",
    "auto_created": false
  }
]
/ # cscli bouncers list
─────────────────────────────────────────────────────────────────────────────────
 Name                 IP Address  Valid  Last API pull  Type  Version  Auth Type 
─────────────────────────────────────────────────────────────────────────────────
 new-traefik-bouncer  172.17.1.7  ✔️                          1.X.X    api-key   
 traefik-bouncer      172.17.1.7  ✔️                          1.X.X    api-key   
─────────────────────────────────────────────────────────────────────────────────

one is valid one is not. how will tell the difference?

hhftechnology avatar Mar 01 '26 06:03 hhftechnology

Yes that's tricky. Maybe by using the "updated_at" ?

notarobot63 avatar Mar 01 '26 07:03 notarobot63

that's what we are using but it's not falling in line ... let me know if you have any other ideas

you can try "updated_at" and test-- see what it does

hhftechnology avatar Mar 01 '26 07:03 hhftechnology

I need to spin up a test instance I do not want to tinker more on my live instance.

notarobot63 avatar Mar 01 '26 07:03 notarobot63

@notarobot63 yes please dont. i have spent almost 1 month to do this (blank page error also in the bouncer page) no result yielded

hhftechnology avatar Mar 01 '26 07:03 hhftechnology

OK so I spun up a test instance with CrowdSec v1.7.6 and tried to replicate your scenario:

The updated_at approach does work, but it needs a guard. CrowdSec sets updated_at on every pull, but it also sets it (once) when the bouncer first registers. So you need to check whether updated_at is meaningfully newer than created_at, otherwise a bouncer that just enrolled but hasn't pulled yet looks the same as one that's actively pulling.

The fix I tested: if updated_at - created_at > 5 seconds, treat updated_at as the last activity timestamp. If the delta is ≤ 5s, the bouncer has only ever been set at registration time and has never actually pulled.

Two files to change:

internal/models/models.go — add three fields:

 type Bouncer struct {
 	Name      string    `json:"name"`
 	IPAddress string    `json:"ip_address"`
+	Revoked   bool      `json:"revoked"`
+	CreatedAt time.Time `json:"created_at"`
+	UpdatedAt time.Time `json:"updated_at"`
 	Valid     bool      `json:"valid"`
 	LastPull  time.Time `json:"last_pull"`

internal/api/handlers/bouncers.go — replace the status logic:

 		for i := range bouncers {
+			bouncers[i].Valid = !bouncers[i].Revoked
+
+			lastActivity := bouncers[i].LastPull
+			if lastActivity.IsZero() &&
+				bouncers[i].UpdatedAt.After(bouncers[i].CreatedAt.Add(5*time.Second)) {
+				lastActivity = bouncers[i].UpdatedAt
+			}
+
-			if bouncers[i].Valid && time.Since(bouncers[i].LastPull) <= 60*time.Minute {
+			if bouncers[i].Valid && !lastActivity.IsZero() && time.Since(lastActivity) <= 60*time.Minute {
 				bouncers[i].Status = "connected"
+			} else if bouncers[i].Valid && lastActivity.IsZero() {
+				bouncers[i].Status = "registered"
 			} else if bouncers[i].Valid {
 				bouncers[i].Status = "stale"
 			} else {
 				bouncers[i].Status = "disconnected"
 			}

Test results with your exact JSON (both bouncers, same IP, last_pull: null):

Name updated_at - created_at Status
new-traefik-bouncer ~14 min connected
traefik-bouncer ~3 min connected

And a bouncer that just enrolled but hasn't pulled yet shows as registered instead of wrongly appearing connected or disconnected.

Won't be able to do much more than that I'm afraid, my dev skills are limited and I don't wan't to go full AI on this. I tested this on the UI and it seemed ok

notarobot63 avatar Mar 01 '26 08:03 notarobot63

@LaurenceJJones when you get time can you help or have a look into it.

hhftechnology avatar Mar 01 '26 13:03 hhftechnology

So revoked is not changeable at all from crowdsec side it will always be false for every key so dont bother using this data point at all (confirmed internally before I left that there is no plans to utilize this as if a user wishes to revoke a key simply cscli bouncers delete it), from the perspective of creating a key afaik crowdsec only populate createdat field upon creation, I dont see the need to complicate the computation of created - updated vs a delta if the primary aim is to know when a bouncer hasnt contacted the lapi.

its the same of what crowdsec uses in cscli bouncers list to display a warning is just compute if the lastpull is greater than a minute, now yes web UI vs a terminal means there is no realtime refresh so they have to run the command again. But I havent used this, so I got limited experience of how the data is updated in the UI, if manual refresh then little gain from making it overly complicated.

LaurenceJJones avatar Mar 01 '26 14:03 LaurenceJJones

@notarobot63 @Floordb --fixed in v2.2.0

hhftechnology avatar Mar 24 '26 06:03 hhftechnology

Congrats !

notarobot63 avatar Mar 24 '26 06:03 notarobot63