-
Notifications
You must be signed in to change notification settings - Fork 22
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 #373 from tmaeno/master
enhancement of proxy cache and getProxy for token exchange flow
- Loading branch information
Showing
5 changed files
with
234 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
""" | ||
download access tokens for OIDC token exchange flow | ||
""" | ||
import datetime | ||
import json | ||
import os.path | ||
import pathlib | ||
|
||
from pandacommon.pandalogger.LogWrapper import LogWrapper | ||
from pandacommon.pandalogger.PandaLogger import PandaLogger | ||
|
||
from pandaserver.config import panda_config | ||
from pandaserver.srvcore.oidc_utils import get_access_token | ||
|
||
# logger | ||
_logger = PandaLogger().getLogger("token_cache") | ||
|
||
|
||
class TokenCache: | ||
""" | ||
A class used to download and give access tokens for OIDC token exchange flow | ||
""" | ||
|
||
# constructor | ||
def __init__(self, target_path=None, file_prefix=None, refresh_interval=60): | ||
""" | ||
Constructs all the necessary attributes for the TokenCache object. | ||
Attributes: | ||
target_path : str | ||
The base path to store the access tokens | ||
file_prefix : str | ||
The prefix of the access token files | ||
refresh_interval : int | ||
The interval to refresh the access tokens (default is 60 minutes) | ||
""" | ||
if target_path: | ||
self.target_path = target_path | ||
else: | ||
self.target_path = "/tmp/proxies" | ||
if file_prefix: | ||
self.file_prefix = file_prefix | ||
else: | ||
self.file_prefix = "access_token_" | ||
self.refresh_interval = refresh_interval | ||
|
||
# construct target path | ||
def construct_target_path(self, client_name) -> str: | ||
""" | ||
Constructs the target path to store an access token | ||
:param client_name : client name | ||
:return: the target path | ||
""" | ||
return os.path.join(self.target_path, f"{self.file_prefix}{client_name}") | ||
|
||
# main | ||
def run(self): | ||
""" " | ||
Main function to download access tokens | ||
""" | ||
tmp_log = LogWrapper(_logger) | ||
tmp_log.debug("================= start ==================") | ||
try: | ||
# check config | ||
if not hasattr(panda_config, "token_cache_config") or not panda_config.token_cache_config: | ||
tmp_log.debug("token_cache_config is not set in panda_config") | ||
# check config path | ||
elif not os.path.exists(panda_config.token_cache_config): | ||
tmp_log.debug(f"config file {panda_config.token_cache_config} not found") | ||
# read config | ||
else: | ||
with open(panda_config.token_cache_config) as f: | ||
token_cache_config = json.load(f) | ||
for client_name, client_config in token_cache_config.items(): | ||
tmp_log.debug(f"client_name={client_name}") | ||
# target path | ||
target_path = self.construct_target_path(client_name) | ||
# check if fresh | ||
if os.path.exists(target_path): | ||
mod_time = datetime.datetime.fromtimestamp(os.stat(target_path).st_mtime, datetime.timezone.utc) | ||
if datetime.datetime.now(datetime.timezone.utc) - mod_time < datetime.timedelta(minutes=self.refresh_interval): | ||
tmp_log.debug(f"skip since {target_path} is fresh") | ||
continue | ||
# get access token | ||
status_code, output = get_access_token( | ||
client_config["endpoint"], client_config["client_id"], client_config["secret"], client_config.get("scope") | ||
) | ||
if status_code: | ||
with open(target_path, "w") as f: | ||
f.write(output) | ||
tmp_log.debug(f"dump access token to {target_path}") | ||
else: | ||
tmp_log.error(output) | ||
# touch file to avoid immediate reattempt | ||
pathlib.Path(target_path).touch() | ||
except Exception as e: | ||
tmp_log.error(f"failed with {str(e)}") | ||
tmp_log.debug("================= end ==================") | ||
tmp_log.debug("done") | ||
return | ||
|
||
# get access token for a client | ||
def get_access_token(self, client_name) -> str | None: | ||
""" | ||
Get an access token string for a client. None is returned if the access token is not found | ||
:param client_name : client name | ||
:return: the access token | ||
""" | ||
target_path = self.construct_target_path(client_name) | ||
token = None | ||
if os.path.exists(target_path): | ||
with open(target_path) as f: | ||
token = f.read() | ||
if not token: | ||
token = None | ||
return token |
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