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

Fix profile filter #125

Merged
merged 4 commits into from
Aug 20, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions egi_notebooks_hub/egiauthenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ async def jwt_authenticate(self, handler, data=None):
# check_allowed, such as admin status and group memberships
return await self.update_auth_model(auth_model)

def get_primary_group(self, oauth_user):
groups = self.get_user_groups(oauth_user)
def get_primary_group(self, user_info):
groups = user_info.get("groups", [])
# first group as the primary, priority is governed by ordering in
# Authenticator.allowed_groups
first_group = next((v for v in self.allowed_groups if v in groups), None)
Expand All @@ -280,7 +280,7 @@ async def authenticate(self, handler, data=None):
self.log.warning("Missing OAuth info")
return user_info

first_group = self.get_primary_group(oauth_user)
first_group = self.get_primary_group(user_info)
self.log.info("Primary group: %s", first_group)
if first_group:
auth_state["primary_group"] = first_group
Expand Down Expand Up @@ -357,7 +357,12 @@ async def refresh_user(self, user, handler=None):
await user.spawner.set_access_token(
auth_state["access_token"], refresh_info.get("id_token", None)
)
return {"auth_state": auth_state}
auth_model = {
"name": user.name,
"admin": True if user.name in self.admin_users else None,
"auth_state": auth_state,
}
return await self.update_auth_model(auth_model)

def get_handlers(self, app):
handlers = super().get_handlers(app)
Expand All @@ -381,14 +386,14 @@ class EOSCNodeAuthenticator(EGICheckinAuthenticator):
used as the name of the Personal project group""",
)

def get_primary_group(self, oauth_user):
def get_primary_group(self, user_info):
# first group is the personal project, which is different for every user
# if not available call super()
for g in self.get_user_groups(oauth_user):
for g in user_info.get("groups", []):
m = re.match(self.personal_project_re, g)
if m:
if m.groups():
return m.groups()[0]
else:
return g
return super().get_primary_group(oauth_user)
return super().get_primary_group(user_info)
28 changes: 16 additions & 12 deletions egi_notebooks_hub/egispawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class EGISpawner(KubeSpawner):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# change to a method so we can filter
self._profile_config = self.profile_list
self.profile_list = self._profile_filter
self.pvc_name = uuid.uuid4().hex
self.token_secret_name = self._expand_user_properties(
self.token_secret_name_template
Expand Down Expand Up @@ -140,18 +143,6 @@ async def auth_state_hook(self, spawner, auth_state):
await spawner.set_access_token(
auth_state.get("access_token", None), auth_state.get("id_token", None)
)
groups = auth_state.get("groups", [])
if spawner.profile_list:
new_profile_list = []
for profile in spawner.profile_list:
profile_vos = profile.get("vo_claims", [])
if not profile_vos:
new_profile_list.append(profile)
else:
if any(i in groups for i in profile_vos):
new_profile_list.append(profile)
spawner.profile_list = new_profile_list

primary_group = auth_state.get("primary_group", None)
if primary_group:
spawner.extra_annotations["egi.eu/primary_group"] = auth_state[
Expand Down Expand Up @@ -181,3 +172,16 @@ async def pre_spawn_hook(self, spawner):
self.volumes = vols
# ensure we have a secret
await self._update_secret({})

def _profile_filter(self, spawner):
profile_list = []
if spawner._profile_config:
groups = [g.name for g in spawner.user.groups]
for profile in spawner._profile_config:
profile_vos = profile.get("vo_claims", [])
if not profile_vos:
profile_list.append(profile)
else:
if any(i in groups for i in profile_vos):
profile_list.append(profile)
return profile_list
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
jupyterhub>=4.0.2
oauthenticator>=16.1.0
oauthenticator>=16.3.0
jupyterhub-kubespawner>=6.1.0
xmltodict
fastapi
Expand Down