keep icon indicating copy to clipboard operation
keep copied to clipboard

[🐛 Bug]: Bug: API Key authentication fails with Keycloak auth type — 'AuthenticatedEntity' object has no attribute 'token'

Open haonanan opened this issue 8 months ago • 4 comments

Bug: API Key authentication fails with Keycloak auth type — 'AuthenticatedEntity' object has no attribute 'token'

Version: Keep v0.47.11 Auth Type: AUTH_TYPE=KEYCLOAK (single-tenant, non-multi-org)

Description: When sending alerts via webhook using API Key authentication (X-API-KEY header), the request fails with HTTP 403:

{"detail": "Permission check failed - 'AuthenticatedEntity' object has no attribute 'token'"} Root Cause: In ee/identitymanager/identity_managers/keycloak/keycloak_authverifier.py, the _authorize() method unconditionally calls self.keycloak_uma.permissions_check(token=authenticated_entity.token, ...) for UMA permission validation.

However, when authenticating via API Key, the AuthenticatedEntity is created by AuthVerifierBase._verify_api_key() without a token attribute (only tenant_id, email, api_key_name, role). The token field is only populated during Bearer token (Keycloak JWT) authentication in _verify_bearer_token().

This means all API Key-authenticated requests are broken when AUTH_TYPE=KEYCLOAK in single-tenant mode, including the /alerts/event webhook endpoint.

Expected Behavior: API Key authentication should fall back to role-based (RBAC) authorization via super()._authorize() when no Keycloak token is available, since the API key already carries a role (e.g. webhook) that can be checked against required scopes.

Suggested Fix: Add an early return in KeycloakAuthVerifier._authorize():

def _authorize(self, authenticated_entity: AuthenticatedEntity) -> None: if self.keycloak_multi_org: return super()._authorize(authenticated_entity) # API key auth — no Keycloak token, fall back to RBAC if not hasattr(authenticated_entity, "token") or not authenticated_entity.token: return super()._authorize(authenticated_entity) # ... existing UMA check for bearer token auth ... Steps to Reproduce:

Deploy Keep v0.47.11 with AUTH_TYPE=KEYCLOAK Create a webhook API key (via UI or DB) Send a POST to /alerts/event with X-API-KEY header Observe 403 response

haonanan avatar Mar 12 '26 04:03 haonanan

I think this was fixed, can you use the latest version?

shahargl avatar Mar 12 '26 08:03 shahargl

Hi, thanks for the quick response!

I just upgraded from v0.47.11 to v0.50.0 (the latest release as of today) and tested — the bug is still present. The exact same error occurs: {"detail": "Permission check failed - 'AuthenticatedEntity' object has no attribute 'token'"} Test steps:

Deployed Keep v0.50.0 with AUTH_TYPE=KEYCLOAK (single-tenant, non-multi-org) Sent POST /alerts/event with X-API-KEY header Got HTTP 403 with the error above Our current workaround:

We patched ee/identitymanager/identity_managers/keycloak/keycloak_authverifier.py by adding an early return in _authorize() to fall back to RBAC when no Keycloak token is present (i.e., API key authentication):

Image

We persist this patch by mounting the fixed file into the container via docker-compose volume: `volumes:

  • ./patches/keycloak_authverifier.py:/venv/lib/python3.13/site-packages/keep/ee/identitymanager/identity_managers/keycloak/keycloak_authverifier.py:ro` This works, but it's fragile — it could break if the Python version or file path changes in future releases.

Could you confirm which version will include the fix? Is it already merged to main and just not yet released, or is it still in progress? Image @shahargl

haonanan avatar Mar 12 '26 11:03 haonanan

Yea my mistake, we've solved it for a specific customer and never merged it upstream. Will be happy to merge any PR you'll open.

shahargl avatar Mar 12 '26 11:03 shahargl

Yea my mistake, we've solved it for a specific customer and never merged it upstream. Will be happy to merge any PR you'll open. I've submitted a PR to fix this:

I've submitted a PR to fix this: #6091

The fix adds a guard clause in _authorize() and authorize_resource() to detect API-key-authenticated entities (which lack a Keycloak token) and fall back to base-class RBAC, consistent with the existing keycloak_multi_org guard.

Tested on Keep v0.50.0 with AUTH_TYPE=KEYCLOAK (single-tenant) — API key webhook ingestion now returns 202 as expected.

haonanan avatar Mar 13 '26 02:03 haonanan