Skip to content

Commit e704179

Browse files
authored
[HWORKS-2078] python client login on serverless should exclude projects not owner by the user (#506) (#507)
1 parent 449d335 commit e704179

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

python/hopsworks/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,12 @@ def _get_cached_api_key_path():
341341

342342
def _prompt_project(valid_connection, project, is_app):
343343
if project is None:
344-
saas_projects = valid_connection._project_api._get_projects()
344+
if is_app:
345+
# On Serverless we filter out projects owned by other users to make sure automatic login
346+
# without a prompt still happens when users add showcase projects created by other users
347+
saas_projects = valid_connection._project_api._get_owned_projects()
348+
else:
349+
saas_projects = valid_connection._project_api._get_projects()
345350
if len(saas_projects) == 0:
346351
if is_app:
347352
raise ProjectException("Could not find any project")

python/hopsworks_common/core/project_api.py

+35-4
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,50 @@ def _exists(self, name: str):
3737
except RestAPIError:
3838
return False
3939

40-
def _get_projects(self):
41-
"""Get all projects accessible by the user.
40+
def _get_owned_projects(self):
41+
"""Get all projects owned by the current user
4242
4343
# Returns
4444
`List[Project]`: List of Project objects
4545
# Raises
46-
`hopsworks.client.exceptions.RestAPIError`: If unable to get the projects
46+
`hopsworks.client.exceptions.RestAPIError`: If unable to get the project teams
47+
"""
48+
project_team_json = self._get_project_teams()
49+
projects = []
50+
if project_team_json:
51+
# This information can be retrieved calling the /users/profile endpoint but is avoided as that
52+
# requires an API key to have the USER scope which is not guaranteed on serverless
53+
# Until there is a better solution this code is used to get the current user_id to check project ownership
54+
current_user_uid = project_team_json[0]['user']['uid']
55+
for project_team in project_team_json:
56+
if project_team["project"]["owner"]["uid"] == current_user_uid:
57+
projects.append(self._get_project(project_team["project"]["name"]))
58+
return projects
59+
60+
61+
def _get_project_teams(self):
62+
"""Get all project teams for this user.
63+
64+
# Returns
65+
`str`: List of Project teams
66+
# Raises
67+
`hopsworks.client.exceptions.RestAPIError`: If unable to get the project teams
4768
"""
4869
_client = client.get_instance()
4970
path_params = [
5071
"project",
5172
]
52-
project_team_json = _client._send_request("GET", path_params)
73+
return _client._send_request("GET", path_params)
74+
75+
def _get_projects(self):
76+
"""Get all projects accessible by the user.
77+
78+
# Returns
79+
`List[Project]`: List of Project objects
80+
# Raises
81+
`hopsworks.client.exceptions.RestAPIError`: If unable to get the projects
82+
"""
83+
project_team_json = self._get_project_teams()
5384
projects = []
5485
for project_team in project_team_json:
5586
projects.append(self._get_project(project_team["project"]["name"]))

0 commit comments

Comments
 (0)