Move getpwnam/grnam/grouplist to LocalAuthenticator

Also fix up an old test of mine
This commit is contained in:
Will Starms
2018-11-13 14:15:47 -06:00
parent f2ae3af90e
commit df829e8927
2 changed files with 38 additions and 39 deletions

View File

@@ -27,30 +27,6 @@ from .utils import maybe_future, url_path_join
from .traitlets import Command
def getgrnam(name):
"""Wrapper function to protect against `grp` not being available
on Windows
"""
import grp
return grp.getgrnam(name)
def getpwnam(name):
"""Wrapper function to protect against `pwd` not being available
on Windows
"""
import pwd
return pwd.getpwnam(name)
def getgrouplist(name, group):
"""Wrapper function to protect against `os.getgrouplist` not being available
on Windows
"""
import os
return os.getgrouplist(name, group)
class Authenticator(LoggingConfigurable):
"""Base class for implementing an authentication provider for JupyterHub"""
@@ -571,7 +547,7 @@ class LocalAuthenticator(Authenticator):
return False
for grnam in self.group_whitelist:
try:
group = getgrnam(grnam)
group = self.getgrnam(grnam)
except KeyError:
self.log.error('No such group: [%s]' % grnam)
continue
@@ -599,10 +575,33 @@ class LocalAuthenticator(Authenticator):
await maybe_future(super().add_user(user))
@staticmethod
def system_user_exists(user):
def getgrnam(name):
"""Wrapper function to protect against `grp` not being available
on Windows
"""
import grp
return grp.getgrnam(name)
@staticmethod
def getpwnam(name):
"""Wrapper function to protect against `pwd` not being available
on Windows
"""
import pwd
return pwd.getpwnam(name)
@staticmethod
def getgrouplist(name, group):
"""Wrapper function to protect against `os.getgrouplist` not being available
on Windows
"""
import os
return os.getgrouplist(name, group)
def system_user_exists(self, user):
"""Check if the user exists on the system"""
try:
getpwnam(user.name)
self.getpwnam(user.name)
except KeyError:
return False
else:
@@ -703,8 +702,8 @@ class PAMAuthenticator(LocalAuthenticator):
# fail to authenticate and raise instead of soft-failing and not changing admin status
# (returning None instead of just the username) as this indicates some sort of system failure
admin_group_gids = {getgrnam(x).gr_gid for x in self.admin_groups}
user_group_gids = {x.gr_gid for x in getgrouplist(username, getpwnam(username).pw_gid)}
admin_group_gids = {self.getgrnam(x).gr_gid for x in self.admin_groups}
user_group_gids = {x.gr_gid for x in self.getgrouplist(username, self.getpwnam(username).pw_gid)}
admin_status = len(admin_group_gids & user_group_gids) != 0
except Exception as e: