Custom Pipeline for Azure SSO Groups #13129
Unanswered
TimoHess
asked this question in
Help Wanted!
Replies: 2 comments 5 replies
-
Notes
Custom Pipeline# configuration/netbox-custom-pipeline.py
# Netbox < 4.0.0
from django.contrib.auth.models import Group
# Netbox >= 4.0.0
#from netbox.authentication import Group
class AuthFailed(Exception):
pass
def set_role(response, user, backend, *args, **kwargs):
'''
Get roles from JWT. Assign user to netbox group matching role name returned
by the OIDC Application. Also set is_superuser or is_staff for special roles
and special user accounts dedicated for ADMIN purposes.
'''
try:
'''
Take the role object from the OIDC response and assign to variable.
'''
roles = response['roles']
'''
Take the email object from the OIDC response and assign to variable.
We use this later to assign superuser permissions in Netbox if group
membership exists for the account and special ADMIN accounts are used.
'''
email = response['upn']
except KeyError:
'''
We throw this error if the application has no roles assigned to it
in Microsoft Entra ID OIDC to this specific application ID we use to
validate out application against Azure AD.
https://learn.microsoft.com/en-us/entra/identity-platform/howto-add-app-roles-in-apps
'''
raise AuthFailed("No role assigned to your user account from the configured Auth Provider")
'''
Ensure we clear group memberships before starting the pipeline. Ensuring
we always assign only current group memberships as retrieved from our auth
provider. This ensures we add and remove group memberships in Netbox that
have been updated in our App Role(s).
'''
user.groups.clear()
try:
'''
Set the superuser and staff role to false by default. Overwrite below if
specific roles are assigned to the user via direct or group membership.
https://demo.netbox.dev/static/docs/installation/6-ldap/#user-groups-for-permissions
Superuser: This permission overrides all permission delegated with groups and permissions
to the user account. And gives full access to everything.
Staff: Enable access to the Admin panel in Netbox
'''
user.is_superuser = False
user.is_staff = False
for role in roles:
'''
We default to enabling the staff permission for users in the Netbox-Admins group.
'''
if role == 'Netbox-Admins':
'''
Only enable the built-in superuser permission for special purpose accounts starting
with ADMIN.
OPTIONAL
Raise an authentication error message if a non-ADMIN user is member of the Netbox-Admins
group and tries to login.
'''
if email.startswith('adm') == True:
user.is_superuser = True
else:
'''
OPTIONAL
Depending on requirements, you CAN throw an error if a user does match the above
criteria were assigned to this group and tries to login into Netbox.
If not required. Remove this else clause.
'''
raise AuthFailed("Superuser group membership incorrectly delegated to non-ADMIN user")
break
user.is_staff = True
group, created = Group.objects.get_or_create(name=role)
# Netbox < 4.0.0
group.user_set.add(user)
# Netbox >= 4.0.0
#group.users.add(user)
'''
Save the permissions we modified. This needs to be after the for-loop. If multiple roles are
returned and we save while looping through the roles. Only the last assigned role to the user
is saved (the rest is overwritten).
'''
user.save()
except Group.DoesNotExist:
pass Extra configuration# configuration/extra-configuration.py
'''
Do not auto-create groups. Configured Netbox groups must match 1:1 the role-names
return from the Auth Provider App we setup for Netbox.
'''
REMOTE_AUTH_AUTO_CREATE_GROUPS = False
'''
Auto create users if validated by our Auth Provider
'''
REMOTE_AUTH_AUTO_CREATE_USER = True
REMOTE_AUTH_BACKEND = 'social_core.backends.azuread.AzureADOAuth2'
'''
Do not assign any permissions by default. We rely on the group permissions and roles
set by our Auth Provider App.
'''
REMOTE_AUTH_DEFAULT_GROUPS = []
REMOTE_AUTH_DEFAULT_PERMISSIONS = {}
REMOTE_AUTH_STAFF_GROUPS = []
REMOTE_AUTH_SUPERUSER_GROUPS = []
REMOTE_AUTH_ENABLED = True
'''
We need to explicitly request information concerning assigned roles to the user. The built in
configured for AzureADOAuth2 in Netbox does not include this scope out of the box.
'''
SOCIAL_AUTH_AZUREAD_OAUTH2_EXTRA_DATA = [('roles','roles')]
SOCIAL_AUTH_AZUREAD_OAUTH2_KEY = '****************************'
SOCIAL_AUTH_AZUREAD_OAUTH2_SECRET = '**************************'
'''
Only allow users with these mail domains to log in. Measure to avoid external
users added to our Azure organization can log into Netbox with non-corp E-Mail address.
'''
SOCIAL_AUTH_AZUREAD_WHITELISTED_DOMAINS = ['example.com','example.org']
'''
Enforce HTTPS redirect.
'''
SOCIAL_AUTH_REDIRECT_IS_HTTPS = True
'''
Set the username the same as the E-mail address. AzureADOAuth2 defaults to username
format FirstLastname by default.
'''
SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL = True
'''
Our pipeline
'''
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',
# Look up existing users by E-mail. As we trust our Corp Azure AD
'social_core.pipeline.social_auth.associate_by_email',
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'netbox.authentication.user_default_groups_handler',
# Enable loading of extra scopes (e.g. roles)
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
# Enable our custom pipeline
'netbox.custom_pipeline.set_role'
# Debug the pipeline - Only do this for a test-instance!!!
# 'social_core.pipeline.debug.debug'
) |
Beta Was this translation helpful? Give feedback.
4 replies
-
I get Authfailed error no role assigned. What am i going wrong? I have updated configuration.py and created custom.pipeline. Group names in azure ad and netbox are same. what am I missing. any help? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello. I tried setting up Azure SSO with oauth2. This is working. Now I try to assign the special groups from Azure groups to Netbox.
I tried already different custom pipelines. For example this one: #9216 (#9216 )
All I get is this error message:
I set up the pipeline as followed:
In configuration.py:
azuread-oauth2-groups.py:
I hope somebody can help me with assigning the special roles. I also tried groups without a dot and I get the same error.
Beta Was this translation helpful? Give feedback.
All reactions