Skip to content
Snippets Groups Projects
Commit 5cd6ffb7 authored by Tim Kreuzer's avatar Tim Kreuzer
Browse files

improved flavor handling for user specific flavors

parent d3fd8638
Branches
Tags
No related merge requests found
......@@ -15,12 +15,78 @@ from .misc import Thread
_outpost_flavors_cache = {}
async def get_user_specific_flavors(log, user_specific_flavor_systems={}):
# This function can be used in authenticator.post_auth_hook to add user specific flavors
# Systems may have user specific flavors
# If that's the case we will send a request to the outpost, to
# receive the allowed flavors for this specific user
# user_specific_flavor_systems = {
# "system_name": {
# "url": "http://outpost.svc:8000/flavors",
# "body": ... # dict containing information that should be used by the Outpost
# "headers": ... # dict used for authenticating
# "request_kwargs": ... # optional, default: request_timeout: 2
# }
# }
ret = _outpost_flavors_cache
tasks = []
http_client = AsyncHTTPClient(
force_instance=True, defaults=dict(validate_cert=False)
)
system_names = []
for system_name, system_infos in user_specific_flavor_systems.items():
url = system_infos.get("url", "No Url configured")
headers = system_infos.get("headers", {})
body = system_infos.get("body", {})
log.info(
f"OutpostFlavors user specific - Retrieve flavors from {system_name} / {url}"
)
request_kwargs = {"request_timeout": 2}
request_kwargs.update(system_infos.get("request_kwargs", {}))
req = HTTPRequest(
url,
method="POST",
headers=headers,
body=json.dumps(body),
**request_kwargs,
)
tasks.append(http_client.fetch(req, raise_error=False))
system_names.append(system_name)
try:
results = await asyncio.gather(*tasks, return_exceptions=True)
names_results = list(zip(system_names, results))
for name_result in names_results:
try:
if name_result[1].code == 200:
log.debug(
f"OutpostFlavors user specific - {name_result[0]} successful"
)
result_json = json.loads(name_result[1].body)
ret[name_result[0]] = result_json
else:
log.warning(
f"OutpostFlavors user specific - {name_result[0]} - Answered with {name_result[1].code} ({name_result[1]})"
)
except:
log.exception(
f"OutpostFlavors user specific - {name_result.get(0, 'unknown')} Could not load result into json"
)
except:
log.exception("OutpostFlavors user specific - Could not load get flavors")
return ret
async def async_get_flavors(log, user=None):
global _outpost_flavors_cache
if not _outpost_flavors_cache:
try:
initial_system_names = os.environ.get("OUTPOST_FLAVOR_INITIAL_SYSTEM_NAMES", "")
initial_system_urls = os.environ.get("OUTPOST_FLAVOR_INITIAL_SYSTEM_URLS", "")
initial_system_names = os.environ.get(
"OUTPOST_FLAVOR_INITIAL_SYSTEM_NAMES", ""
)
initial_system_urls = os.environ.get(
"OUTPOST_FLAVOR_INITIAL_SYSTEM_URLS", ""
)
initial_system_tokens = os.environ.get(
"OUTPOST_FLAVOR_INITIAL_SYSTEM_TOKENS", ""
)
......@@ -40,7 +106,9 @@ async def async_get_flavors(log, user=None):
if system_name not in _outpost_flavors_cache.keys():
initial_system_names_list.append(system_name)
initial_system_urls_list.append(initial_system_urls_list_all[i])
initial_system_tokens_list.append(initial_system_tokens_list_all[i])
initial_system_tokens_list.append(
initial_system_tokens_list_all[i]
)
i += 1
# If systems are left without successful initial check, try to reach the Outpost
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment