diff --git a/app/__init__.py b/app/__init__.py index fc7f908b..81e7f60c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -26,6 +26,7 @@ import logging import copy import requests +import datetime from requests.exceptions import Timeout from werkzeug.middleware.proxy_fix import ProxyFix from flask_dance.consumer import OAuth2ConsumerBlueprint @@ -251,6 +252,9 @@ def home(): flash("Error getting User info: \n" + account_info.text, 'error') return render_template('home.html', oidc_name=settings.oidcName) + scheduler.add_job(func=utils.get_cache_creds, trigger='date', run_date=datetime.datetime.now(), + misfire_grace_time=20, args=[cred, get_cred_id()], id='get_cache_creds') + # if there are any next url, redirect to it if "next" in session and session["next"]: next_url = session.pop("next") @@ -497,7 +501,7 @@ def showinfrastructures(): app.logger.exception("Error getting vm info: %s" % ex) radl_json = [] try: - creds = utils.get_cache_creds(session, cred, get_cred_id()) + creds = utils.get_cache_creds(cred, get_cred_id()) except Exception as ex: app.logger.exception("Error getting user credentials: %s" % ex) creds = [] @@ -767,7 +771,7 @@ def configure(): app.logger.debug("Template: " + json.dumps(toscaInfo[selected_tosca])) try: - creds = utils.get_cache_creds(session, cred, get_cred_id(), 1) + creds = utils.get_cache_creds(cred, get_cred_id(), 1) except Exception as ex: flash("Error getting user credentials: %s" % ex, "error") creds = [] @@ -1121,7 +1125,7 @@ def manage_creds(): creds = {} try: - creds = utils.get_cache_creds(session, cred, get_cred_id()) + creds = utils.get_cache_creds(cred, get_cred_id()) # Get the project_id in case it has changed utils.get_project_ids(creds) except Exception as e: @@ -1176,8 +1180,7 @@ def write_creds(): # Get project_id to save it to de DB utils.get_project_ids([creds]) # delete cached credentials - if 'creds' in session: - del session['creds'] + utils.clear_cache_creds(get_cred_id()) cred.write_creds(creds["id"], get_cred_id(), creds, cred_id in [None, '']) if val_res == 0: flash("Credentials successfully written!", 'success') @@ -1195,8 +1198,7 @@ def delete_creds(): cred_id = request.args.get('cred_id', "") try: # delete cached credentials - if 'creds' in session: - del session['creds'] + utils.clear_cache_creds(get_cred_id()) cred.delete_cred(cred_id, get_cred_id()) flash("Credentials successfully deleted!", 'success') except Exception as ex: @@ -1215,8 +1217,7 @@ def enable_creds(): if val_res == 2: flash(val_msg, 'warning') # delete cached credentials - if 'creds' in session: - del session['creds'] + utils.clear_cache_creds(get_cred_id()) cred.enable_cred(cred_id, get_cred_id(), enable) except Exception as ex: flash("Error updating credentials %s!" % ex, 'error') diff --git a/app/utils.py b/app/utils.py index f6bb7587..c6472dc5 100644 --- a/app/utils.py +++ b/app/utils.py @@ -46,7 +46,7 @@ SITE_LIST = {} LAST_UPDATE = 0 PORT_SPECT_TYPES = ["PortSpec", "tosca.datatypes.network.PortSpec", "tosca.datatypes.indigo.network.PortSpec"] - +CREDS_CACHE = {} def _getStaticSitesInfo(force=False): # Remove cache if force is True @@ -984,13 +984,20 @@ def merge_templates(template, new_template): return template -def get_cache_creds(session, cred, userid, enabled=None): - if "creds" not in session: - session["creds"] = cred.get_creds(userid) +def get_cache_creds(cred, userid, enabled=None): + global CREDS_CACHE + if userid not in CREDS_CACHE: + CREDS_CACHE[userid] = cred.get_creds(userid) res = [] - for cred in session["creds"]: + for cred in CREDS_CACHE[userid]: if enabled is None or enabled == cred['enabled']: res.append(cred) return res + + +def clear_cache_creds(userid): + global CREDS_CACHE + if userid in CREDS_CACHE: + del CREDS_CACHE[userid]