-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7222 from chaen/diracx_sandbox
[8.1] Get tokens and sandboxes from DiracX
- Loading branch information
Showing
13 changed files
with
271 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# pylint: disable=import-error | ||
|
||
import requests | ||
|
||
from cachetools import TTLCache, cached | ||
from pathlib import Path | ||
from tempfile import NamedTemporaryFile | ||
from typing import Any | ||
from DIRAC import gConfig | ||
from DIRAC.ConfigurationSystem.Client.Helpers import Registry | ||
|
||
|
||
from diracx.core.preferences import DiracxPreferences | ||
|
||
from diracx.core.utils import write_credentials | ||
|
||
from diracx.core.models import TokenResponse | ||
from diracx.client import DiracClient | ||
|
||
# How long tokens are kept | ||
DEFAULT_TOKEN_CACHE_TTL = 5 * 60 | ||
|
||
# Add a cache not to query the token all the time | ||
_token_cache = TTLCache(maxsize=100, ttl=DEFAULT_TOKEN_CACHE_TTL) | ||
|
||
|
||
@cached(_token_cache, key=lambda x, y: repr(x)) | ||
def _get_token(credDict, diracxUrl, /) -> Path: | ||
""" | ||
Write token to a temporary file and return the path to that file | ||
""" | ||
|
||
apiKey = gConfig.getValue("/DiracX/LegacyExchangeApiKey") | ||
if not apiKey: | ||
raise ValueError("Missing mandatory /DiracX/LegacyExchangeApiKey configuration") | ||
|
||
vo = Registry.getVOForGroup(credDict["group"]) | ||
dirac_properties = list(set(credDict.get("groupProperties", [])) | set(credDict.get("properties", []))) | ||
group = credDict["group"] | ||
|
||
scopes = [f"vo:{vo}", f"group:{group}"] + [f"property:{prop}" for prop in dirac_properties] | ||
|
||
r = requests.get( | ||
f"{diracxUrl}/auth/legacy-exchange", | ||
params={ | ||
"preferred_username": credDict["username"], | ||
"scope": " ".join(scopes), | ||
}, | ||
headers={"Authorization": f"Bearer {apiKey}"}, | ||
timeout=10, | ||
) | ||
|
||
r.raise_for_status() | ||
|
||
token_location = Path(NamedTemporaryFile().name) | ||
|
||
write_credentials(TokenResponse(**r.json()), location=token_location) | ||
|
||
return token_location | ||
|
||
|
||
def TheImpersonator(credDict: dict[str, Any]) -> DiracClient: | ||
""" | ||
Client to be used by DIRAC server needing to impersonate | ||
a user for diracx. | ||
It queries a token, places it in a file, and returns the `DiracClient` | ||
class | ||
Use as a context manager | ||
""" | ||
|
||
diracxUrl = gConfig.getValue("/DiracX/URL") | ||
token_location = _get_token(credDict, diracxUrl) | ||
pref = DiracxPreferences(url=diracxUrl, credentials_path=token_location) | ||
|
||
return DiracClient(diracx_preferences=pref) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.