|
10 | 10 |
|
11 | 11 | import base64
|
12 | 12 | import functools
|
| 13 | +import hashlib |
13 | 14 | import importlib
|
14 | 15 | import json
|
15 | 16 | import re
|
16 | 17 | import textwrap
|
17 | 18 | from collections.abc import Iterator
|
18 | 19 | from contextlib import contextmanager
|
19 | 20 | from pathlib import Path
|
20 |
| -from tempfile import NamedTemporaryFile |
| 21 | +from tempfile import NamedTemporaryFile, gettempdir |
21 | 22 | from typing import Any
|
22 | 23 |
|
23 | 24 | try:
|
|
30 | 31 | from diracx.core.utils import serialize_credentials
|
31 | 32 |
|
32 | 33 | from DIRAC import gConfig, gLogger
|
| 34 | +from DIRAC.Core.Utilities.File import secureOpenForWrite |
33 | 35 |
|
34 | 36 | from DIRAC.ConfigurationSystem.Client.Helpers import Registry
|
35 | 37 | from DIRAC.Core.Security.Locations import getDefaultProxyLocation
|
@@ -98,14 +100,16 @@ def DiracXClient() -> Iterator[SyncDiracClient]:
|
98 | 100 | if not diracxToken:
|
99 | 101 | raise ValueError(f"No diracx token in the proxy file {proxyLocation}")
|
100 | 102 |
|
101 |
| - with NamedTemporaryFile(mode="wt") as token_file: |
102 |
| - token_file.write(json.dumps(diracxToken)) |
103 |
| - token_file.flush() |
104 |
| - token_file.seek(0) |
| 103 | + hash = hashlib.sha256(diracxToken["refresh_token"].split(".")[1].encode()) |
| 104 | + token_file = Path(gettempdir()) / f"dx_{hash.hexdigest()}" |
| 105 | + if not token_file.exists(): |
| 106 | + token_file.parent.mkdir(parents=True, exist_ok=True) |
| 107 | + with secureOpenForWrite(token_file) as (fd, _): |
| 108 | + fd.write(json.dumps(diracxToken)) |
105 | 109 |
|
106 |
| - pref = DiracxPreferences(url=diracxUrl, credentials_path=token_file.name) |
107 |
| - with SyncDiracClient(diracx_preferences=pref) as api: |
108 |
| - yield api |
| 110 | + pref = DiracxPreferences(url=diracxUrl, credentials_path=token_file) |
| 111 | + with SyncDiracClient(diracx_preferences=pref) as api: |
| 112 | + yield api |
109 | 113 |
|
110 | 114 |
|
111 | 115 | def addRPCStub(meth):
|
|
0 commit comments