diff --git a/app/__init__.py b/app/__init__.py index b7d14061..3b8fd49c 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 @@ -245,6 +246,10 @@ def home(): flash("Error getting User info: \n" + account_info.text, 'error') return render_template('home.html', oidc_name=settings.oidcName) + # Force to get the user credentials to cache them + scheduler.add_job(func=utils.get_cache_creds, trigger='date', run_date=datetime.datetime.now(), + misfire_grace_time=20, args=[cred, session['userid'], 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") @@ -491,7 +496,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, session['userid'], get_cred_id()) except Exception as ex: app.logger.exception("Error getting user credentials: %s" % ex) creds = [] @@ -761,7 +766,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, session['userid'], get_cred_id(), 1) except Exception as ex: flash("Error getting user credentials: %s" % ex, "error") creds = [] @@ -1115,7 +1120,7 @@ def manage_creds(): creds = {} try: - creds = utils.get_cache_creds(session, cred, get_cred_id()) + creds = utils.get_cache_creds(cred, session['userid'], get_cred_id()) # Get the project_id in case it has changed utils.get_project_ids(creds) except Exception as e: @@ -1170,8 +1175,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(session['userid']) cred.write_creds(creds["id"], get_cred_id(), creds, cred_id in [None, '']) if val_res == 0: flash("Credentials successfully written!", 'success') @@ -1189,8 +1193,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(session['userid']) cred.delete_cred(cred_id, get_cred_id()) flash("Credentials successfully deleted!", 'success') except Exception as ex: @@ -1209,8 +1212,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(session['userid']) cred.enable_cred(cred_id, get_cred_id(), enable) except Exception as ex: flash("Error updating credentials %s!" % ex, 'error') diff --git a/app/tests/test_app.py b/app/tests/test_app.py index cc9fa0a6..4c682b49 100644 --- a/app/tests/test_app.py +++ b/app/tests/test_app.py @@ -6,7 +6,7 @@ import unittest import json import defusedxml.ElementTree as etree -from app import create_app +from app import create_app, utils from urllib.parse import urlparse from mock import patch, MagicMock @@ -431,6 +431,7 @@ def test_configure(self, get, get_creds, avatar): self.assertEqual(200, res.status_code) self.assertIn(b"Select Optional Features:", res.data) + utils.CREDS_CACHE = {} get_creds.return_value = [{"id": "credid", "type": "fedcloud", "host": "site_url", "vo": "voname", "enabled": True}, {"id": "credid1", "type": "OpenStack", "host": "site_url1", @@ -569,6 +570,7 @@ def test_manage_creds(self, get_project_ids, get_sites, get_creds, avatar): get_sites.return_value = {"SITE_NAME": {"url": "URL", "state": "", "id": ""}, "SITE2": {"url": "URL2", "state": "CRITICAL", "id": ""}} get_creds.return_value = [{"id": "credid", "type": "fedcloud", "host": "site_url", "project_id": "project"}] + utils.CREDS_CACHE = {} res = self.client.get('/manage_creds') self.assertEqual(200, res.status_code) self.assertIn(b'credid', res.data) diff --git a/app/utils.py b/app/utils.py index f6bb7587..75622f52 100644 --- a/app/utils.py +++ b/app/utils.py @@ -46,6 +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): @@ -984,13 +985,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, creduserid, enabled=None): + global CREDS_CACHE + if userid not in CREDS_CACHE: + CREDS_CACHE[userid] = cred.get_creds(creduserid) 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]