From 3eb790bebd2f1ca768b337cbbd890826609e8f14 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Fri, 13 Dec 2024 13:25:39 +0100 Subject: [PATCH 01/17] update versions --- docker/Dockerfile | 2 +- requirements.txt | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index ba32ecf9..26c3dbcd 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.10-alpine3.15 +FROM python:3.12-alpine3.21 COPY . /app WORKDIR /app/ diff --git a/requirements.txt b/requirements.txt index 56d3b983..cb5c095c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,18 +1,18 @@ Flask_Dance==7.1.0 -Werkzeug==3.0.6 -Flask==3.0.3 -requests==2.32.2 +Werkzeug==3.1.3 +Flask==3.1.0 +requests==2.32.3 PyYAML==6.0.2 -packaging==24.1 -xmltodict==0.13.0 +packaging==24.2 +xmltodict==0.14.2 radl==1.3.4 -cryptography==43.0.1 -apscheduler==3.10.4 # 3.8.1 version fails +cryptography==44.0.0 +apscheduler==3.11.0 Flask-APScheduler==1.13.1 -Flask-WTF==1.2.1 +Flask-WTF==1.2.2 hvac==2.3.0 -tosca-parser==2.11.0 -mysqlclient==2.2.4 +tosca-parser==2.12.0 +mysqlclient==2.2.6 lxml==5.3.0 defusedxml==0.7.1 -paramiko==3.4.1 +paramiko==3.5.0 From 9a89289ca3544318ba1f80b2a161fbc494b986ea Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:31 +0100 Subject: [PATCH 02/17] Revert "Fix error" This reverts commit 6a6b47a43dcbab9323d27231bcca0986bc3ce138. --- app/__init__.py | 14 +++++++------- app/utils.py | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 37f38204..cecd1598 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -254,7 +254,7 @@ def home(): # 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') + 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"]: @@ -502,7 +502,7 @@ def showinfrastructures(): app.logger.exception("Error getting vm info: %s" % ex) radl_json = [] try: - creds = utils.get_cache_creds(cred, session['userid'], 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 = [] @@ -772,7 +772,7 @@ def configure(): app.logger.debug("Template: " + json.dumps(toscaInfo[selected_tosca])) try: - creds = utils.get_cache_creds(cred, session['userid'], 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 = [] @@ -1126,7 +1126,7 @@ def manage_creds(): creds = {} try: - creds = utils.get_cache_creds(cred, session['userid'], 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: @@ -1181,7 +1181,7 @@ def write_creds(): # Get project_id to save it to de DB utils.get_project_ids([creds]) # delete cached credentials - utils.clear_cache_creds(session['userid']) + 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') @@ -1199,7 +1199,7 @@ def delete_creds(): cred_id = request.args.get('cred_id', "") try: # delete cached credentials - utils.clear_cache_creds(session['userid']) + utils.clear_cache_creds(get_cred_id()) cred.delete_cred(cred_id, get_cred_id()) flash("Credentials successfully deleted!", 'success') except Exception as ex: @@ -1218,7 +1218,7 @@ def enable_creds(): if val_res == 2: flash(val_msg, 'warning') # delete cached credentials - utils.clear_cache_creds(session['userid']) + 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 75622f52..bb202afa 100644 --- a/app/utils.py +++ b/app/utils.py @@ -985,10 +985,10 @@ def merge_templates(template, new_template): return template -def get_cache_creds(cred, userid, creduserid, enabled=None): +def get_cache_creds(cred, userid, enabled=None): global CREDS_CACHE if userid not in CREDS_CACHE: - CREDS_CACHE[userid] = cred.get_creds(creduserid) + CREDS_CACHE[userid] = cred.get_creds(userid) res = [] for cred in CREDS_CACHE[userid]: From a71627fc52189bef5e7ee74afe9bd54d96c2d95f Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:34 +0100 Subject: [PATCH 03/17] Revert "Fix test" This reverts commit 19014c423848326fce0cd223325ea5737052e996. --- app/tests/test_app.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/tests/test_app.py b/app/tests/test_app.py index 0df58f6e..1a7158da 100644 --- a/app/tests/test_app.py +++ b/app/tests/test_app.py @@ -577,6 +577,16 @@ def test_manage_creds(self, get_project_ids, get_sites, get_creds, avatar): self.assertIn(b'credid', res.data) self.assertIn(b'site_url', res.data) self.assertIn(b'fedcloudRow.png', res.data) + self.assertEqual(utils.CREDS_CACHE, {"userid": [{"id": "credid", "type": "fedcloud", + "host": "site_url", "project_id": "project"}]}) + self.assertEqual(get_creds.call_count, 2) + + res = self.client.get('/manage_creds') + self.assertEqual(200, res.status_code) + self.assertIn(b'credid', res.data) + self.assertIn(b'site_url', res.data) + self.assertIn(b'fedcloudRow.png', res.data) + self.assertEqual(get_creds.call_count, 2) @patch("app.utils.avatar") @patch("app.db_cred.DBCredentials.get_cred") From 96f160ce2b82747b779da514cba6ac2d1c6c06c4 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:35 +0100 Subject: [PATCH 04/17] Revert "Fix test" This reverts commit 3a3eddde32aa02bf09ba33a027694c826bbd2613. --- app/tests/test_app.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/app/tests/test_app.py b/app/tests/test_app.py index 1a7158da..197c0bf4 100644 --- a/app/tests/test_app.py +++ b/app/tests/test_app.py @@ -234,7 +234,6 @@ def test_settings(self, avatar): def test_infrastructures(self, avatar, get, user_data, get_creds): user_data.return_value = "type = InfrastructureManager; token = access_token" get_creds.return_value = [] - utils.CREDS_CACHE = {} get.side_effect = self.get_response self.login(avatar) res = self.client.get('/infrastructures') @@ -432,7 +431,6 @@ 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", @@ -577,16 +575,6 @@ def test_manage_creds(self, get_project_ids, get_sites, get_creds, avatar): self.assertIn(b'credid', res.data) self.assertIn(b'site_url', res.data) self.assertIn(b'fedcloudRow.png', res.data) - self.assertEqual(utils.CREDS_CACHE, {"userid": [{"id": "credid", "type": "fedcloud", - "host": "site_url", "project_id": "project"}]}) - self.assertEqual(get_creds.call_count, 2) - - res = self.client.get('/manage_creds') - self.assertEqual(200, res.status_code) - self.assertIn(b'credid', res.data) - self.assertIn(b'site_url', res.data) - self.assertIn(b'fedcloudRow.png', res.data) - self.assertEqual(get_creds.call_count, 2) @patch("app.utils.avatar") @patch("app.db_cred.DBCredentials.get_cred") From 74902c05511bcfa0a8c8f9ee3a54b9e1c2b8b525 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:35 +0100 Subject: [PATCH 05/17] =?UTF-8?q?Revert=20"Fix=20sty=C3=B1e"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit df6e4f8780974877c1d34ceb1cb3ecf2b2e52857. --- app/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index cecd1598..17d51cd6 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -73,7 +73,7 @@ def create_app(oidc_blueprint=None): ssh_key = SSHKey(settings.db_url) vault_info = VaultInfo(settings.db_url) ott = OneTimeTokenData(settings.vault_url) - + # To Reload internally the site cache scheduler = APScheduler() scheduler.api_enabled = False From 63b9d403874343fed9092a2c01ac586122f4fab2 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:36 +0100 Subject: [PATCH 06/17] Revert "Fix tests" This reverts commit 19f538e589e4e3d6899e742d0d41903f44de121f. --- .github/workflows/main.yaml | 1 + app/__init__.py | 14 ++++++++------ app/tests/test_app.py | 3 +-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 12822e63..03ec1911 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -37,6 +37,7 @@ jobs: wget -P tosca-templates https://raw.githubusercontent.com/grycap/tosca/main/templates/simple-node-disk.yml sed -i -e 's|/opt|'${GITHUB_WORKSPACE}'|g' app/config.json sed -i -e 's|creds.db|tmp/creds.db|g' app/config.json + sed -i -e 's|"EXTRA_AUTH": {},|"DISABLE_APPSCHEDULER": true,|g' app/config.json - name: Unit tests run: python -m coverage run --source=. -m unittest discover -s app/tests -p 'test*.py' diff --git a/app/__init__.py b/app/__init__.py index 17d51cd6..05229a10 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -73,12 +73,13 @@ def create_app(oidc_blueprint=None): ssh_key = SSHKey(settings.db_url) vault_info = VaultInfo(settings.db_url) ott = OneTimeTokenData(settings.vault_url) - - # To Reload internally the site cache + scheduler = APScheduler() - scheduler.api_enabled = False - scheduler.init_app(app) - scheduler.start() + # To Reload internally the site cache + if 'DISABLE_APPSCHEDULER' not in app.config: + scheduler.api_enabled = False + scheduler.init_app(app) + scheduler.start() toscaTemplates = utils.loadToscaTemplates(settings.toscaDir) toscaInfo = utils.extractToscaInfo(settings.toscaDir, toscaTemplates, settings.hide_tosca_tags) @@ -253,7 +254,8 @@ def home(): 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(), + ndate = datetime.datetime.now() + datetime.timedelta(0, 2) + scheduler.add_job(func=utils.get_cache_creds, trigger='date', run_date=ndate, misfire_grace_time=20, args=[cred, get_cred_id()], id='get_cache_creds') # if there are any next url, redirect to it diff --git a/app/tests/test_app.py b/app/tests/test_app.py index 197c0bf4..087cfb03 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, utils +from app import create_app from urllib.parse import urlparse from mock import patch, MagicMock @@ -569,7 +569,6 @@ 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) From c33e73866bc6e86e0a27b8c33b94668675338671 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:36 +0100 Subject: [PATCH 07/17] Revert "Fix tests" This reverts commit 9e0140bb11bf7e00545207184f07c0d7a46911bf. --- .github/workflows/main.yaml | 1 - app/__init__.py | 9 ++++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 03ec1911..12822e63 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -37,7 +37,6 @@ jobs: wget -P tosca-templates https://raw.githubusercontent.com/grycap/tosca/main/templates/simple-node-disk.yml sed -i -e 's|/opt|'${GITHUB_WORKSPACE}'|g' app/config.json sed -i -e 's|creds.db|tmp/creds.db|g' app/config.json - sed -i -e 's|"EXTRA_AUTH": {},|"DISABLE_APPSCHEDULER": true,|g' app/config.json - name: Unit tests run: python -m coverage run --source=. -m unittest discover -s app/tests -p 'test*.py' diff --git a/app/__init__.py b/app/__init__.py index 05229a10..00339c47 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -74,12 +74,11 @@ def create_app(oidc_blueprint=None): vault_info = VaultInfo(settings.db_url) ott = OneTimeTokenData(settings.vault_url) - scheduler = APScheduler() # To Reload internally the site cache - if 'DISABLE_APPSCHEDULER' not in app.config: - scheduler.api_enabled = False - scheduler.init_app(app) - scheduler.start() + scheduler = APScheduler() + scheduler.api_enabled = False + scheduler.init_app(app) + scheduler.start() toscaTemplates = utils.loadToscaTemplates(settings.toscaDir) toscaInfo = utils.extractToscaInfo(settings.toscaDir, toscaTemplates, settings.hide_tosca_tags) From c0f6d66f501606cecea1dfeadc768299a89ee592 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:37 +0100 Subject: [PATCH 08/17] Revert "Fix style" This reverts commit e014d3fb5bf70f1119650a3ea0ea66731de5c5c0. --- app/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index 00339c47..c1630a3a 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -253,7 +253,7 @@ def home(): return render_template('home.html', oidc_name=settings.oidcName) # Force to get the user credentials to cache them - ndate = datetime.datetime.now() + datetime.timedelta(0, 2) + ndate = datetime.datetime.now() + datetime.timedelta(0,2) scheduler.add_job(func=utils.get_cache_creds, trigger='date', run_date=ndate, misfire_grace_time=20, args=[cred, get_cred_id()], id='get_cache_creds') From 9a639ff3556ca185321d190e60602e8348fd07a4 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:37 +0100 Subject: [PATCH 09/17] Revert "Fix test" This reverts commit 875fbf8fd0739c1bfa70e6e73ae2f841897ca945. --- app/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index c1630a3a..cecd1598 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -253,8 +253,7 @@ def home(): return render_template('home.html', oidc_name=settings.oidcName) # Force to get the user credentials to cache them - ndate = datetime.datetime.now() + datetime.timedelta(0,2) - scheduler.add_job(func=utils.get_cache_creds, trigger='date', run_date=ndate, + 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 From f461a8e920e40af0d72f8b06e7bfb115286e4a08 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:37 +0100 Subject: [PATCH 10/17] Revert "Fix style" This reverts commit aff6eabaa212f9a4178426b93e5e10a1acb61e69. --- app/__init__.py | 1 - app/utils.py | 1 - 2 files changed, 2 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index cecd1598..81e7f60c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -252,7 +252,6 @@ 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, get_cred_id()], id='get_cache_creds') diff --git a/app/utils.py b/app/utils.py index bb202afa..c6472dc5 100644 --- a/app/utils.py +++ b/app/utils.py @@ -48,7 +48,6 @@ 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 if force and g.settings.static_sites_url: From effc72efae19b9971667311412e8364c95784040 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:38 +0100 Subject: [PATCH 11/17] Revert "Improve cache code" This reverts commit 566b004db3109ebdc94a661c4967f7ec13f16a76. --- app/__init__.py | 19 +++++++++---------- app/utils.py | 17 +++++------------ 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 81e7f60c..fc7f908b 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -26,7 +26,6 @@ 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 @@ -252,9 +251,6 @@ 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") @@ -501,7 +497,7 @@ def showinfrastructures(): app.logger.exception("Error getting vm info: %s" % ex) radl_json = [] try: - creds = utils.get_cache_creds(cred, get_cred_id()) + creds = utils.get_cache_creds(session, cred, get_cred_id()) except Exception as ex: app.logger.exception("Error getting user credentials: %s" % ex) creds = [] @@ -771,7 +767,7 @@ def configure(): app.logger.debug("Template: " + json.dumps(toscaInfo[selected_tosca])) try: - creds = utils.get_cache_creds(cred, get_cred_id(), 1) + creds = utils.get_cache_creds(session, cred, get_cred_id(), 1) except Exception as ex: flash("Error getting user credentials: %s" % ex, "error") creds = [] @@ -1125,7 +1121,7 @@ def manage_creds(): creds = {} try: - creds = utils.get_cache_creds(cred, get_cred_id()) + creds = utils.get_cache_creds(session, cred, get_cred_id()) # Get the project_id in case it has changed utils.get_project_ids(creds) except Exception as e: @@ -1180,7 +1176,8 @@ def write_creds(): # Get project_id to save it to de DB utils.get_project_ids([creds]) # delete cached credentials - utils.clear_cache_creds(get_cred_id()) + if 'creds' in session: + del session['creds'] cred.write_creds(creds["id"], get_cred_id(), creds, cred_id in [None, '']) if val_res == 0: flash("Credentials successfully written!", 'success') @@ -1198,7 +1195,8 @@ def delete_creds(): cred_id = request.args.get('cred_id', "") try: # delete cached credentials - utils.clear_cache_creds(get_cred_id()) + if 'creds' in session: + del session['creds'] cred.delete_cred(cred_id, get_cred_id()) flash("Credentials successfully deleted!", 'success') except Exception as ex: @@ -1217,7 +1215,8 @@ def enable_creds(): if val_res == 2: flash(val_msg, 'warning') # delete cached credentials - utils.clear_cache_creds(get_cred_id()) + if 'creds' in session: + del session['creds'] 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 c6472dc5..f6bb7587 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,20 +984,13 @@ def merge_templates(template, new_template): return template -def get_cache_creds(cred, userid, enabled=None): - global CREDS_CACHE - if userid not in CREDS_CACHE: - CREDS_CACHE[userid] = cred.get_creds(userid) +def get_cache_creds(session, cred, userid, enabled=None): + if "creds" not in session: + session["creds"] = cred.get_creds(userid) res = [] - for cred in CREDS_CACHE[userid]: + for cred in session["creds"]: 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] From 2266047014db836cca1dccb55c7ffb0458749321 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:38 +0100 Subject: [PATCH 12/17] Revert "Fix error" This reverts commit 9d9bfbc4606f295d45f2ea582f5e281a0143c9a9. --- app/utils.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/app/utils.py b/app/utils.py index f6bb7587..a2a1416b 100644 --- a/app/utils.py +++ b/app/utils.py @@ -986,11 +986,5 @@ def merge_templates(template, new_template): def get_cache_creds(session, cred, userid, enabled=None): if "creds" not in session: - session["creds"] = cred.get_creds(userid) - - res = [] - for cred in session["creds"]: - if enabled is None or enabled == cred['enabled']: - res.append(cred) - - return res + session["creds"] = cred.get_creds(userid, enabled) + return session["creds"] From dd3b870e650c57c3c6cacf6a43fbb9a358a39e84 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:38 +0100 Subject: [PATCH 13/17] Revert "Fix error" This reverts commit 6a9c29482f78517390d7e39668943ca7e4383365. --- app/__init__.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index fc7f908b..72b3616d 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1176,8 +1176,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'] + del session['creds'] cred.write_creds(creds["id"], get_cred_id(), creds, cred_id in [None, '']) if val_res == 0: flash("Credentials successfully written!", 'success') @@ -1195,8 +1194,7 @@ def delete_creds(): cred_id = request.args.get('cred_id', "") try: # delete cached credentials - if 'creds' in session: - del session['creds'] + del session['creds'] cred.delete_cred(cred_id, get_cred_id()) flash("Credentials successfully deleted!", 'success') except Exception as ex: @@ -1215,8 +1213,7 @@ def enable_creds(): if val_res == 2: flash(val_msg, 'warning') # delete cached credentials - if 'creds' in session: - del session['creds'] + del session['creds'] cred.enable_cred(cred_id, get_cred_id(), enable) except Exception as ex: flash("Error updating credentials %s!" % ex, 'error') From d6307f1380c343f0cd68c5739710cd97423ee6fd Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 16 Dec 2024 16:50:39 +0100 Subject: [PATCH 14/17] Revert "Add creds cache" This reverts commit 62632f31fecc20b2943c697527f315283558da78. --- app/__init__.py | 12 +++--------- app/utils.py | 6 ------ 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 72b3616d..0ffbaa84 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -497,7 +497,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 = cred.get_creds(get_cred_id()) except Exception as ex: app.logger.exception("Error getting user credentials: %s" % ex) creds = [] @@ -767,7 +767,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 = cred.get_creds(get_cred_id(), 1) except Exception as ex: flash("Error getting user credentials: %s" % ex, "error") creds = [] @@ -1121,7 +1121,7 @@ def manage_creds(): creds = {} try: - creds = utils.get_cache_creds(session, cred, get_cred_id()) + creds = cred.get_creds(get_cred_id()) # Get the project_id in case it has changed utils.get_project_ids(creds) except Exception as e: @@ -1175,8 +1175,6 @@ def write_creds(): if val_res != 1: # Get project_id to save it to de DB utils.get_project_ids([creds]) - # delete cached credentials - del session['creds'] cred.write_creds(creds["id"], get_cred_id(), creds, cred_id in [None, '']) if val_res == 0: flash("Credentials successfully written!", 'success') @@ -1193,8 +1191,6 @@ def delete_creds(): cred_id = request.args.get('cred_id', "") try: - # delete cached credentials - del session['creds'] cred.delete_cred(cred_id, get_cred_id()) flash("Credentials successfully deleted!", 'success') except Exception as ex: @@ -1212,8 +1208,6 @@ def enable_creds(): val_res, val_msg = cred.validate_cred(get_cred_id(), cred_id) if val_res == 2: flash(val_msg, 'warning') - # delete cached credentials - del session['creds'] 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 a2a1416b..a4356683 100644 --- a/app/utils.py +++ b/app/utils.py @@ -982,9 +982,3 @@ def merge_templates(template, new_template): template["metadata"]["tabs"].update(tabs) return template - - -def get_cache_creds(session, cred, userid, enabled=None): - if "creds" not in session: - session["creds"] = cred.get_creds(userid, enabled) - return session["creds"] From 30cda01b18be23f521089e7e9e1cb4a4007b7623 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 17 Dec 2024 09:10:26 +0100 Subject: [PATCH 15/17] Load creds ajax --- app/__init__.py | 22 ++++++++++++---------- app/templates/advanced_config.html | 19 +++++-------------- app/templates/config_form.html | 10 +++++----- app/templates/createdep.html | 29 +++++++++++++++++++++++++++++ app/templates/default_form.html | 8 ++++---- app/tests/test_app.py | 6 ------ 6 files changed, 55 insertions(+), 39 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 0ffbaa84..53ad7e93 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -766,13 +766,6 @@ def configure(): else: app.logger.debug("Template: " + json.dumps(toscaInfo[selected_tosca])) - try: - creds = cred.get_creds(get_cred_id(), 1) - except Exception as ex: - flash("Error getting user credentials: %s" % ex, "error") - creds = [] - utils.get_project_ids(creds) - # Enable to get input values from URL parameters for input_name, input_value in selected_template["inputs"].items(): value = request.args.get(input_name, None) @@ -789,7 +782,7 @@ def configure(): return render_template('createdep.html', template=selected_template, selectedTemplate=selected_tosca, - creds=creds, input_values=inputs, + input_values=inputs, infra_name=infra_name, child_templates=child_templates, vos=utils.getVOs(session), utils=utils) @@ -1127,8 +1120,17 @@ def manage_creds(): except Exception as e: flash("Error retrieving credentials: \n" + str(e), 'warning') - return render_template('service_creds.html', creds=creds, - vault=(settings.vault_url and settings.enable_external_vault)) + if request.args.get('json', 0): + json_creds = json.dumps(creds) + to_delete = ['password', 'token', 'proxy', 'private_key', 'client_id', 'secret'] + for cred in json_creds: + for key in to_delete: + if key in cred: + del cred[key] + return json_creds + else: + return render_template('service_creds.html', creds=creds, + vault=(settings.vault_url and settings.enable_external_vault)) @app.route('/write_creds', methods=['GET', 'POST']) @authorized_with_valid_token diff --git a/app/templates/advanced_config.html b/app/templates/advanced_config.html index 10d5375e..1ef7bfde 100644 --- a/app/templates/advanced_config.html +++ b/app/templates/advanced_config.html @@ -247,23 +247,14 @@
Cloud Provider:
- {% if not creds %} - - {% else %} + +
- {% endif %} +