Skip to content

Commit fcb5512

Browse files
authored
fix credential check for pki systems (#1563)
1 parent fb57c36 commit fcb5512

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

designsafe/apps/api/datafiles/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def datafiles_get_handler(api, client, scheme, system, path, operation, username
4141
try:
4242
return op(client, system, path, username=username, tapis_tracking_id=tapis_tracking_id, **kwargs)
4343
except (InternalServerError, UnauthorizedError):
44-
system_needs_keys = test_system_needs_keys(client, username, system)
44+
system_needs_keys = test_system_needs_keys(client, username, system, path)
4545
if system_needs_keys:
4646
logger.error(
4747
f"Keys for user {username} must be manually pushed to system: {system_needs_keys.id}"

designsafe/apps/workspace/api/views.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from django.db.models.lookups import GreaterThan
1414
from django.urls import reverse
1515
from tapipy.tapis import TapisResult
16-
from tapipy.errors import InternalServerError, UnauthorizedError
16+
from tapipy.errors import InternalServerError, UnauthorizedError, BaseTapyException
1717
from designsafe.apps.api.exceptions import ApiException
1818
from designsafe.apps.api.users.utils import get_user_data
1919
from designsafe.apps.api.views import AuthenticatedApiView
@@ -114,7 +114,27 @@ def _get_app(app_id, app_version, user):
114114
return data
115115

116116

117-
def test_system_needs_keys(tapis, username, system_id):
117+
def test_system_access_ok(
118+
tapis: object, username: str, system_id: str, path: str = "/"
119+
) -> bool:
120+
"""
121+
Test system access by attempting to list files in a given path.
122+
"""
123+
try:
124+
tapis.files.listFiles(systemId=system_id, path=path, limit=1)
125+
return True
126+
except UnauthorizedError:
127+
return False
128+
except BaseTapyException:
129+
logger.exception(
130+
"System access check failed for user: %s on system: %s", username, system_id
131+
)
132+
raise
133+
134+
135+
def test_system_needs_keys(
136+
tapis: object, username: str, system_id: str, path: str = "/"
137+
) -> bool:
118138
"""Tests a Tapis system by making a file listing call.
119139
120140
If the system is TMS_KEYS-based, it attempts to create credentials before listing files.
@@ -130,19 +150,32 @@ def test_system_needs_keys(tapis, username, system_id):
130150
try:
131151
tapis.systems.checkUserCredential(systemId=system_id, userName=username)
132152
return False
133-
except (InternalServerError, UnauthorizedError):
134-
# Check if the system uses TMS_KEYS and create credentials if necessary
153+
except (InternalServerError, UnauthorizedError) as exc:
135154
system_def = tapis.systems.getSystem(systemId=system_id)
155+
156+
# Check if the system uses TMS_KEYS and create credentials if necessary
136157
if system_def.get("defaultAuthnMethod") == "TMS_KEYS":
137158
try:
138159
create_system_credentials(
139160
tapis, username, system_id, createTmsKeys=True
140161
)
141162
return False
142163
except (InternalServerError, UnauthorizedError):
143-
logger.error(f"TMS_KEYS credential generation failed for system: {system_id}, user: {username}")
164+
logger.exception(
165+
f"TMS_KEYS credential generation failed for system: {system_id}, user: {username}"
166+
)
144167
raise
145-
return True
168+
169+
# If the system uses PKI_KEYS, check if the user has access
170+
if (
171+
system_def.get("effectiveUserId") == username
172+
and system_def.get("defaultAuthnMethod") == "PKI_KEYS"
173+
):
174+
return test_system_access_ok(tapis, username, system_id, path)
175+
176+
raise ApiException(
177+
f"User {username} does not have system credentials and cannot push keys or create credentials for system {system_id}."
178+
) from exc
146179

147180

148181
class SystemListingView(AuthenticatedApiView):
@@ -373,8 +406,7 @@ def _get_public_apps(self, user, verbose):
373406
bundle_label=F("bundle__label"),
374407
bundle_license_type=F("bundle__license_type"),
375408
bundle_user_guide_link=F("bundle__user_guide_link"),
376-
)
377-
.values(*values)
409+
).values(*values)
378410
)
379411

380412
html_apps = list(

0 commit comments

Comments
 (0)