Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toggle for automatic creation of remote users #2707

Merged
merged 2 commits into from Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions python/nav/etc/webfront/webfront.conf
Expand Up @@ -44,13 +44,13 @@ server = ldap.example.com
#
basedn = ou=people,dc=example,dc=com

# How to lookup a user object from LDAP.
# How to lookup a user object from LDAP.
# 'direct' binds to <uid_attr>=<login name>,<user_basedn>
# 'search' searches for <uid_attr>=<login name> using basedn as searchbase.
#lookupmethod=direct

# Choose to bind to LDAP as the user with 'suffix' for Active Directory support.
# lookupmethod should be set to search for this option to function.
# lookupmethod should be set to search for this option to function.
#suffix = @ad.example.com

# If the LDAP directory requires an authenticated user to search for a user
Expand Down Expand Up @@ -110,6 +110,10 @@ enabled = no
# authenticated user?
#varname = REMOTE_USER

# Whether a username set in REMOTE_USER should lead to the automatic creation
# of a user in the database if the user does not already exist.
# autocreate = off

# If the supplied remote username value needs modification to become more
# "username-like", specify which workaround to use here. Only `feide-oidc` is
# supported, at the moment.
Expand Down
29 changes: 19 additions & 10 deletions python/nav/web/auth/remote_user.py
Expand Up @@ -52,6 +52,7 @@
logout-url=
varname=REMOTE_USER
workaround=none
autocreate=off
"""


Expand All @@ -78,16 +79,11 @@
try:
account = Account.objects.get(login=username)
except Account.DoesNotExist:
# Store the remote user in the database and return the new account
account = Account(login=username, name=username, ext_sync='REMOTE_USER')
account.set_password(fake_password(32))
account.save()
_logger.info("Created user %s from header REMOTE_USER", account.login)
template = 'Account "{actor}" created due to REMOTE_USER HTTP header'
LogEntry.add_log_entry(
account, 'create-account', template=template, subsystem='auth'
)
return account
if _config.getboolean('remote-user', 'autocreate', fallback=False):
return autocreate_remote_user(username)

Check warning on line 83 in python/nav/web/auth/remote_user.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/auth/remote_user.py#L82-L83

Added lines #L82 - L83 were not covered by tests
# Bail out!
_logger.info('User creation turned off, did not create "%s"', username)
return False

Check warning on line 86 in python/nav/web/auth/remote_user.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/auth/remote_user.py#L85-L86

Added lines #L85 - L86 were not covered by tests

# Bail out! Potentially evil user
if account.locked:
Expand All @@ -101,6 +97,19 @@
return account


def autocreate_remote_user(username):
# Store the remote user in the database and return the new account
account = Account(login=username, name=username, ext_sync='REMOTE_USER')
account.set_password(fake_password(32))
account.save()
_logger.info("Created user %s from header REMOTE_USER", account.login)
template = 'Account "{actor}" created due to REMOTE_USER HTTP header'
LogEntry.add_log_entry(

Check warning on line 107 in python/nav/web/auth/remote_user.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/auth/remote_user.py#L102-L107

Added lines #L102 - L107 were not covered by tests
account, 'create-account', template=template, subsystem='auth'
)
return account

Check warning on line 110 in python/nav/web/auth/remote_user.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/auth/remote_user.py#L110

Added line #L110 was not covered by tests


def login(request):
"""Log in the user in REMOTE_USER, if any and enabled

Expand Down