1313from django .db .models .lookups import GreaterThan
1414from django .urls import reverse
1515from tapipy .tapis import TapisResult
16- from tapipy .errors import InternalServerError , UnauthorizedError
16+ from tapipy .errors import InternalServerError , UnauthorizedError , BaseTapyException
1717from designsafe .apps .api .exceptions import ApiException
1818from designsafe .apps .api .users .utils import get_user_data
1919from 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
148181class 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