Skip to content

Commit f4b7927

Browse files
committed
Finally fix discovery of tokens at OD
1 parent e7e08a6 commit f4b7927

1 file changed

Lines changed: 35 additions & 33 deletions

File tree

coffea_casa/coffea_casa.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""CoffeaCasaCluster class
22
"""
33
import os
4-
import sys
54
from pathlib import Path
5+
import sys
66
import dask
77
from dask_jobqueue.htcondor import HTCondorCluster, HTCondorJob
88
from distributed.security import Security
@@ -30,41 +30,44 @@
3030
else:
3131
CONDA_ENV = HOME_DIR / "environment.yml"
3232

33-
# helper functions from htcondor/htcondor-ce/htcondorce/tools.py
34-
def x509_user_proxy_path():
35-
"""Return the path to the user's X.509 proxy or raise FileNotFoundError if it doesn't exist on disk
36-
"""
37-
try:
38-
path = os.environ['X509_USER_PROXY']
39-
except KeyError:
40-
path = f'/tmp/x509up_u{os.geteuid()}'
41-
if open(path):
42-
return path
43-
return None # we shouldn't get here; failure to open should raise OSError
33+
import os
34+
from pathlib import Path
4435

45-
# helper functions from htcondor/htcondor-ce/htcondorce/tools.py
4636
def bearer_token_path():
47-
"""Return the path to the user's X.509 proxy or raise FileNotFoundError if it doesn't exist on disk
48-
"""
37+
"""Return the path to the user's X.509 proxy or None if not found"""
38+
4939
def check_token_path(path, suffix=''):
5040
token_path = f'{path}{suffix}'
51-
if open(token_path):
41+
if Path(token_path).is_file():
5242
return token_path
53-
return None # we shouldn't get here, failure to open should raise OSError
43+
return None
5444

45+
# 1. Check BEARER_TOKEN_FILE env variable
5546
try:
56-
# 2. BEARER_TOKEN_PATH containing the path to the token
5747
path = check_token_path(os.environ['BEARER_TOKEN_FILE'])
58-
except (KeyError, FileNotFoundError):
59-
try:
60-
# 3. XDG_RUNTIME_DIR containing the path to the folder containing the token at bt_u$UID
61-
path = check_token_path(os.environ['XDG_RUNTIME_DIR'], suffix=f'/bt_u{os.geteuid()}')
62-
except (KeyError, FileNotFoundError):
63-
# 4. Otherwise, the token is expected at /tmp/bt_u$UID
64-
# Raise FileExceptionError if it doesn't exist
65-
path = check_token_path(f'/tmp/bt_u{os.geteuid()}')
48+
if path:
49+
return path
50+
except KeyError:
51+
pass
52+
53+
# 2. Check XDG_RUNTIME_DIR + /bt_u$UID
54+
try:
55+
xdg_runtime_dir = os.environ['XDG_RUNTIME_DIR']
56+
path = check_token_path(xdg_runtime_dir, suffix=f'/bt_u{os.geteuid()}')
57+
if path:
58+
return path
59+
except KeyError:
60+
pass
61+
62+
# 3. Check /tmp/bt_u$UID
63+
try:
64+
path = check_token_path(f'/tmp/bt_u{os.geteuid()}')
65+
if path:
66+
return path
67+
except KeyError:
68+
pass
6669

67-
return path
70+
return None
6871

6972

7073
def merge_dicts(*dict_args):
@@ -176,12 +179,11 @@ def _modify_job_kwargs(cls,
176179
job_config["protocol"] = "tls://"
177180
job_config["security"] = cls.security()
178181
input_files += [CA_FILE, CERT_FILE]
179-
if opendata is None:
180-
XCACHE_SCITOKEN_FILE = bearer_token_path()
181-
if XCACHE_SCITOKEN_FILE:
182-
input_files += [XCACHE_SCITOKEN_FILE]
183-
else:
184-
raise KeyError("Please check with system administarator why you do not have a certificate.")
182+
XCACHE_SCITOKEN_FILE = bearer_token_path()
183+
if XCACHE_SCITOKEN_FILE:
184+
input_files += [XCACHE_SCITOKEN_FILE]
185+
else:
186+
print("Warning: No bearer token found — proceeding without it.")
185187
files = ", ".join(str(path) for path in input_files)
186188
## Networking settings
187189
try:

0 commit comments

Comments
 (0)