From 21686428f03e19d7c8b4b2c4e21f9225736b62c4 Mon Sep 17 00:00:00 2001 From: terrtia Date: Mon, 16 Sep 2024 15:30:12 +0200 Subject: [PATCH] chg: [flask] use the same cookie name + cache git commits, tags and branch name --- bin/lib/ail_core.py | 5 +++ bin/packages/git_status.py | 63 +++++++++++++++++++++++++++++++++----- update/bin/ail_updater.py | 2 ++ var/www/Flask_server.py | 8 ++++- 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/bin/lib/ail_core.py b/bin/lib/ail_core.py index 5bcb6920..9a86b01b 100755 --- a/bin/lib/ail_core.py +++ b/bin/lib/ail_core.py @@ -38,6 +38,11 @@ def _set_ail_uuid(): r_serv_db.set('ail:uuid', ail_uuid) return ail_uuid +def get_ail_uuid_int(): + ail_uuid = get_ail_uuid() + header_uuid = ail_uuid.replace('-', '') + return uuid.UUID(hex=header_uuid, version=4).int + def is_valid_uuid_v4(header_uuid): try: header_uuid = header_uuid.replace('-', '') diff --git a/bin/packages/git_status.py b/bin/packages/git_status.py index 19bf7769..6794a4c6 100755 --- a/bin/packages/git_status.py +++ b/bin/packages/git_status.py @@ -1,7 +1,19 @@ #!/usr/bin/env python3 # -*-coding:UTF-8 -* +import os import subprocess +import sys + +sys.path.append(os.environ['AIL_BIN']) +################################## +# Import Project packages +################################## +from lib.ConfigLoader import ConfigLoader + +config_loader = ConfigLoader() +r_cache = config_loader.get_redis_conn("Redis_Cache") +config_loader = None TERMINAL_RED = '\033[91m' TERMINAL_YELLOW = '\33[93m' @@ -154,15 +166,50 @@ def get_last_tag_from_remote(verbose=False): print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT)) return '' +def clear_git_meta_cache(): + r_cache.delete('git:meta') + +def _get_git_meta(): + if r_cache.exists('git:meta'): + dict_git = {'current_branch': r_cache.hget('git:meta', 'branch'), + 'is_clone': r_cache.hget('git:meta', 'is_clone') == 'True', + 'is_working_directory_clean': is_working_directory_clean(), + 'current_commit': r_cache.hget('git:meta', 'commit'), + 'last_remote_commit': r_cache.hget('git:meta', 'remote_commit'), + 'last_local_tag': r_cache.hget('git:meta', 'tag'), + 'last_remote_tag': r_cache.hget('git:meta', 'remote_tag')} + for k in dict_git: + if not dict_git[k] and dict_git[k] is not False: + return {} + return dict_git + else: + return {} + def get_git_metadata(): - dict_git = {} - dict_git['current_branch'] = get_current_branch() - dict_git['is_clone'] = is_not_fork(REPO_ORIGIN) - dict_git['is_working_directory_clean'] = is_working_directory_clean() - dict_git['current_commit'] = get_last_commit_id_from_local() - dict_git['last_remote_commit'] = get_last_commit_id_from_remote() - dict_git['last_local_tag'] = get_last_tag_from_local() - dict_git['last_remote_tag'] = get_last_tag_from_remote() + dict_git = _get_git_meta() + if not dict_git: + branch = get_current_branch() + commit = get_last_commit_id_from_local() + remote_commit = get_last_commit_id_from_remote() + is_clone = is_not_fork(REPO_ORIGIN) + tag = get_last_tag_from_local() + remote_tag = get_last_tag_from_remote() + + r_cache.hset('git:meta', 'branch', branch) + r_cache.hset('git:meta', 'commit', commit) + r_cache.hset('git:meta', 'remote_commit', remote_commit) + r_cache.hset('git:meta', 'is_clone', str(is_clone)) + r_cache.hset('git:meta', 'tag', tag) + r_cache.hset('git:meta', 'remote_tag', remote_tag) + r_cache.expire('git:meta', 108000) + + dict_git['current_branch'] = branch + dict_git['is_clone'] = is_clone + dict_git['is_working_directory_clean'] = is_working_directory_clean() + dict_git['current_commit'] = commit + dict_git['last_remote_commit'] = remote_commit + dict_git['last_local_tag'] = tag + dict_git['last_remote_tag'] = remote_tag if dict_git['current_commit'] != dict_git['last_remote_commit']: dict_git['new_git_update_available'] = True diff --git a/update/bin/ail_updater.py b/update/bin/ail_updater.py index af45d3a4..1d718d6c 100755 --- a/update/bin/ail_updater.py +++ b/update/bin/ail_updater.py @@ -11,6 +11,7 @@ sys.path.append(os.environ['AIL_BIN']) ################################## from lib import ail_updates from lib.ConfigLoader import ConfigLoader +from packages.git_status import clear_git_meta_cache class AIL_Updater(object): """docstring for AIL_Updater.""" @@ -24,6 +25,7 @@ class AIL_Updater(object): self.f_version = float(self.version[1:]) self.current_f_version = ail_updates.get_ail_float_version() + clear_git_meta_cache() def update(self): """ diff --git a/var/www/Flask_server.py b/var/www/Flask_server.py index f468d693..7e6aa64e 100755 --- a/var/www/Flask_server.py +++ b/var/www/Flask_server.py @@ -26,8 +26,11 @@ sys.path.append(os.environ['AIL_BIN']) from lib.ConfigLoader import ConfigLoader from lib.ail_users import AILUser, get_session_user from lib import Tag +from lib import ail_core from lib import ail_logger +from packages.git_status import clear_git_meta_cache + # Import config import Flask_config @@ -143,7 +146,7 @@ app.register_blueprint(api_rest, url_prefix=baseUrl) # ========= =========# # ========= Cookie name ======== -app.config.update(SESSION_COOKIE_NAME='ail_framework_{}'.format(uuid.uuid4().int)) +app.config.update(SESSION_COOKIE_NAME='ail_framework_{}'.format(ail_core.get_ail_uuid_int())) # ========= session ======== app.secret_key = str(random.getrandbits(256)) @@ -313,6 +316,9 @@ default_taxonomies = ["infoleak", "gdpr", "fpf", "dark-web"] for taxonomy in default_taxonomies: Tag.enable_taxonomy_tags(taxonomy) +# ========== GIT Cache ============ +clear_git_meta_cache() + # rrrr = [str(p) for p in app.url_map.iter_rules()] # for p in rrrr: # print(p)