Sentry & Keycloak SAML2 Integration

This guide explains how to integrate Keycloak (as Identity Provider) with Sentry (as Service Provider) using SAML2 authentication, for secure and controlled Single Sign-On (SSO) within the TechFinite infrastructure.


๐Ÿ”ง Prerequisites

  • โœ… Keycloak server running at https://keycloak.techfinite.com

  • โœ… Sentry (self-hosted) running at https://sentry.techfinite.com

  • โœ… Admin access to both systems

  • โœ… Docker & Docker Compose set up for Sentry


๐Ÿ› ๏ธ Step 1: Create SAML Client in Keycloak

  1. Login to Keycloak Admin Console.

  2. Navigate to your Realm โ†’ Clients โ†’ Create.

  3. Set:

    • Client ID: https://<sentry-host>/saml/metadata/<org-slug name>/

    • Client Protocol: saml

  4. Click Save.

Client Settings:

Field

Value

Sign Assertions

OFF

Encrypt Assertions

OFF

Client Signature Required

OFF

Force POST Binding

ON

Name ID Format

email

Valid Redirect URIs

*

Assertion Consumer Service URL

https://<sentry-host>/saml/acs/<org-slug name>/

Logout Service POST URL

https://<sentry-host>/saml/sls/<org-slug name>/


๐Ÿงฉ Step 2: Setup Attribute Mapper in Keycloak

  1. Delete default "role list" mapper.

  2. Go to Mappers โ†’ Add Builtin โ†’ X500 Email.

  3. Edit the new mapper:

    • SAML Attribute Name: user_email


๐ŸŽฏ Step 3: Configure SAML2 in Sentry

  1. Open Sentry web UI โ†’ Organization Settings โ†’ Auth โ†’ SAML2.

  2. Use Metadata URL:

    ruby
    https://<Keycloak-host>/realms/<realm-name>/protocol/saml/descriptor
  3. Set Attribute Mappings:

    • IdP User ID: user_email

    • User Email: user_email


โš™๏ธ Step 4: Configure Sentry Server (.env + sentry.conf.py)

Edit .env in ~/self-hosted/:

env
SENTRY_SSO_DEFAULT_LOGIN=false 
SENTRY_SSO_AUTO_LINK=false

Edit /etc/sentry/sentry.conf.py inside the container:

bash
docker compose exec web bash 
echo "SENTRY_FEATURES['auth:register'] = False" >> /etc/sentry/sentry.conf.py

Optional (for password login backup):

python
SENTRY_FEATURES['auth:login'] = True

Then restart:

bash
docker compose restart web

๐Ÿงช Step 5: Testing

โœ… Expected

Scenario

Result

Invited user logs in via SAML

Login successful

Uninvited user logs in via SAML

Login blocked

Admin uses password

Login successful (if enabled)


๐Ÿงจ Common Errors & Fixes

Problem

Solution

SAML creates user without invite

Set auth:register = False in sentry.conf.py

Only SAML login visible

Add dummy Google provider or enable auth:login = True

Invalid request in Keycloak

Use correct Client ID & Metadata URL

SAML auto-logs into admin

Disable auto-link: SENTRY_SSO_AUTO_LINK=false

SAML login fails silently

Check attribute mapping & Keycloak email presence