Netbox / SSO Keycloak login ends up in Netbox admin user #19584
Replies: 1 comment
-
|
Spent an entire day on Keycloak group-to-NetBox permission mapping. Here's the fix. NetBox SSO: Keycloak Group-Based Permission Mapping GuideOverviewThis guide explains how to integrate NetBox with Keycloak OIDC for SSO authentication in a Kubernetes environment using the netbox-chart. Key features:
NetBox Permission Model
Why Custom Pipeline?NetBox uses python-social-auth for OAuth/OIDC authentication. NetBox's built-in References:
Prerequisites
Keycloak ConfigurationCreate Client
Add MappersGo to Clients → netbox → Client Scopes → netbox-dedicated → Mappers. Audience Mapper:
Groups Mapper:
Get Public Keycurl -s https://<keycloak>/realms/<realm> | jq -r '.public_key'You can also get the public key from Keycloak Admin Console: Realm settings → Keys → RS256 → Public key button. Values ConfigurationConfigure netbox's helm chart values file to enable Keycloak OIDC authentication with custom pipeline. The custom pipeline script is deployed as a ConfigMap via netbox subchart's netbox-operator:
netbox:
extraConfig:
- values:
SOCIAL_AUTH_PIPELINE:
# ... omitted for brevity ...
- netbox.sso_pipeline.set_superuserThe group names in the script ( netbox-operator:
netbox:
enabled: true
## Keycloak OIDC SSO settings
remoteAuth:
enabled: true
backends:
- social_core.backends.keycloak.KeycloakOAuth2
autoCreateUser: true
autoCreateGroups: true
defaultGroups: []
defaultPermissions: {}
# NOTE: groupSyncEnabled, superuserGroups, staffGroups only work with
# HTTP header-based auth (e.g., Apache mod_auth_openidc).
# For python-social-auth, use custom pipeline instead.
groupSyncEnabled: true
superuserGroups:
- administrator
superusers: []
staffGroups:
- developer
staffUsers: []
extraConfig:
- values:
SOCIAL_AUTH_KEYCLOAK_KEY: "netbox"
SOCIAL_AUTH_KEYCLOAK_SECRET: "<client-secret>"
SOCIAL_AUTH_KEYCLOAK_PUBLIC_KEY: "<public-key>"
SOCIAL_AUTH_KEYCLOAK_AUTHORIZATION_URL: "https://<keycloak>/realms/<realm>/protocol/openid-connect/auth"
SOCIAL_AUTH_KEYCLOAK_ACCESS_TOKEN_URL: "https://<keycloak>/realms/<realm>/protocol/openid-connect/token"
SOCIAL_AUTH_KEYCLOAK_SCOPE:
- openid
- email
- profile
SOCIAL_AUTH_KEYCLOAK_ID_KEY: "email"
LOGIN_REQUIRED: true
# Authentication pipeline - executed in order during login
SOCIAL_AUTH_PIPELINE:
- social_core.pipeline.social_auth.social_details # Extract user details from OAuth response
- social_core.pipeline.social_auth.social_uid # Get user's unique ID
- social_core.pipeline.social_auth.auth_allowed # Check if authentication is allowed
- social_core.pipeline.social_auth.social_user # Find existing social account
- social_core.pipeline.user.get_username # Generate username
- social_core.pipeline.user.create_user # Create new user if needed
- social_core.pipeline.social_auth.associate_user # Link social account to user
- social_core.pipeline.social_auth.load_extra_data # Load extra data (groups, etc.)
- social_core.pipeline.user.user_details # Update user details
- netbox.sso_pipeline.set_superuser # Custom: Map Keycloak groups to NetBox permissions
# Mount custom pipeline to netbox-app pod
extraVolumes:
- name: sso-pipeline
configMap:
name: netbox-sso-pipeline
extraVolumeMounts:
- name: sso-pipeline
mountPath: /opt/netbox/netbox/netbox/sso_pipeline.py
subPath: sso_pipeline.py
# Custom pipeline ConfigMap
extraDeploy:
- apiVersion: v1
kind: ConfigMap
metadata:
name: netbox-sso-pipeline
data:
sso_pipeline.py: |
def set_superuser(backend, user, response, *args, **kwargs):
groups = response.get('groups', [])
normalized = [g.lstrip('/') for g in groups]
if 'administrator' in normalized:
user.is_superuser = True
user.is_staff = True
elif 'developer' in normalized:
user.is_staff = True
else:
user.is_superuser = False
user.is_staff = False
user.save()Note: Group Mapping
TroubleshootingInvalidAudienceErrorMissing audience mapper in Keycloak client configuration. Add Audience mapper in Keycloak: Clients → netbox → Client Scopes → netbox-dedicated → Mappers → Add Audience mapper. sequence item 1: NoneTypeMissing groups claim or user name fields in the token. Add Groups mapper in Keycloak and ensure the user has first/last name set in their Keycloak profile. Login works but no superuserNetBox's built-in The custom pipeline must be mounted at ConclusionAutomatically creating users and mapping Keycloak groups to NetBox permissions (Staff/Superuser) is not straightforward. NetBox's built-in group sync only supports HTTP header-based authentication, not direct OAuth. Solution:
This approach was inspired by NetBox with Okta SSO using OAuth, which demonstrates custom pipeline implementation for similar OAuth providers. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello all,
we have a Netbox Installation, which is empty. We configured SSO using the provider social_core.backends.keycloak.KeycloakOAuth2.
SSO is working, but each login ends up in the superuser account (named "manager") of netbox.
I don't understand it anymore. Also the account is not synched on first login.
Here is our config:
I am thankful for any hint.
Beta Was this translation helpful? Give feedback.
All reactions