66import re
77import socket
88import sys
9+ from typing import Dict , List
910
1011from fair_research_login .client import NativeClient
1112from globus_sdk import TransferAPIError , TransferClient , TransferData
1516from .settings import logger
1617from .utils import ts_utc
1718
18- hpss_endpoint_map = {
19+ hpss_endpoint_map : Dict [ str , str ] = {
1920 "ALCF" : "de463ec4-6d04-11e5-ba46-22000b92c6ec" ,
2021 "NERSC" : "9cd89cfd-6d04-11e5-ba46-22000b92c6ec" ,
2122}
2223
2324# This is used if the `globus_endpoint_uuid` is not set in `~/.zstash.ini`
24- regex_endpoint_map = {
25+ regex_endpoint_map : Dict [ str , str ] = {
2526 r"theta.*\.alcf\.anl\.gov" : "08925f04-569f-11e7-bef8-22000b9a448b" ,
2627 r"blueslogin.*\.lcrc\.anl\.gov" : "15288284-7006-4041-ba1a-6b52501e49f1" ,
2728 r"chrlogin.*\.lcrc\.anl\.gov" : "15288284-7006-4041-ba1a-6b52501e49f1" ,
3940archive_directory_listing : IterableTransferResponse = None
4041
4142
43+ def get_all_endpoint_scopes (endpoints : List [str ]) -> str :
44+ inner = " " .join (
45+ [f"*https://auth.globus.org/scopes/{ ep } /data_access" for ep in endpoints ]
46+ )
47+ return f"urn:globus:auth:scope:transfer.api.globus.org:all[{ inner } ]"
48+
49+
50+ # Used exclusively by submit_transfer_with_checks, exclusively when there is a TransferAPIError
51+ # This function is really to diagnose an error: are the endpoints ok?
52+ # That is, we don't *need* to check endpoint versions if everything worked out fine.
4253def check_endpoint_version_5 (ep_id ):
4354 output = transfer_client .get_endpoint (ep_id )
4455 version = output .get ("gcs_version" , "0.0" )
@@ -56,7 +67,7 @@ def submit_transfer_with_checks(transfer_data):
5667 if err .info .consent_required :
5768 scopes = "urn:globus:auth:scope:transfer.api.globus.org:all["
5869 for ep_id in [remote_endpoint , local_endpoint ]:
59- if check_endpoint_version_5 (ep_id ):
70+ if ep_id and check_endpoint_version_5 (ep_id ):
6071 scopes += f" *https://auth.globus.org/scopes/{ ep_id } /data_access"
6172 scopes += " ]"
6273 native_client = NativeClient (
@@ -86,6 +97,16 @@ def globus_activate(hpss: str):
8697 url = urlparse (hpss )
8798 if url .scheme != "globus" :
8899 return
100+ globus_cfg : str = os .path .expanduser ("~/.globus-native-apps.cfg" )
101+ logger .info (f"Checking if { globus_cfg } exists" )
102+ if os .path .exists (globus_cfg ):
103+ logger .info (
104+ f"{ globus_cfg } exists. If this file does not have the proper settings, it may cause a TransferAPIError (e.g., 'Token is not active', 'No credentials supplied')"
105+ )
106+ else :
107+ logger .info (
108+ f"{ globus_cfg } does not exist. zstash will need to prompt for authentications twice, and then you will need to re-run."
109+ )
89110 remote_endpoint = url .netloc
90111
91112 ini_path = os .path .expanduser ("~/.zstash.ini" )
@@ -134,7 +155,13 @@ def globus_activate(hpss: str):
134155 app_name = "Zstash" ,
135156 default_scopes = "openid urn:globus:auth:scope:transfer.api.globus.org:all" ,
136157 )
137- native_client .login (no_local_server = True , refresh_tokens = True )
158+ if local_endpoint and remote_endpoint :
159+ all_scopes : str = get_all_endpoint_scopes ([local_endpoint , remote_endpoint ])
160+ native_client .login (
161+ requested_scopes = all_scopes , no_local_server = True , refresh_tokens = True
162+ )
163+ else :
164+ native_client .login (no_local_server = True , refresh_tokens = True )
138165 transfer_authorizer = native_client .get_authorizers ().get ("transfer.api.globus.org" )
139166 transfer_client = TransferClient (authorizer = transfer_authorizer )
140167
0 commit comments