Skip to content

Commit

Permalink
Merge pull request #605 from grycap/egi_brand_v2
Browse files Browse the repository at this point in the history
Egi brand v2
  • Loading branch information
micafer authored Dec 13, 2024
2 parents 3316391 + 3425e29 commit 93586a3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
20 changes: 11 additions & 9 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand All @@ -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:
Expand All @@ -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')
Expand Down
4 changes: 3 additions & 1 deletion app/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 12 additions & 4 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]

0 comments on commit 93586a3

Please sign in to comment.