From 537fb52342f2cc2a32f17a02d77b36e7e2e63716 Mon Sep 17 00:00:00 2001 From: Jiri Otoupal Date: Thu, 7 Mar 2024 13:29:10 +0100 Subject: [PATCH] fixed exceptions --- abst/__version__.py | 2 +- abst/bastion_support/oci_bastion.py | 28 +++++++++++++++++--------- abst/cli_commands/context/commands.py | 2 ++ abst/cli_commands/parallel/commands.py | 10 ++++----- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/abst/__version__.py b/abst/__version__.py index 77c1143..ceb7aa7 100644 --- a/abst/__version__.py +++ b/abst/__version__.py @@ -10,7 +10,7 @@ "CLI Command making OCI Bastion and kubernetes usage simple and fast" ) -__version__ = "2.3.29" +__version__ = "2.3.30" __author__ = "Jiri Otoupal" __author_email__ = "jiri-otoupal@ips-database.eu" __license__ = "MIT" diff --git a/abst/bastion_support/oci_bastion.py b/abst/bastion_support/oci_bastion.py index afa5489..798d3ed 100644 --- a/abst/bastion_support/oci_bastion.py +++ b/abst/bastion_support/oci_bastion.py @@ -409,21 +409,31 @@ def load_config(cls): @classmethod def load_json(cls, path=default_creds_path) -> dict: + if not path.name.endswith(".json"): + logging.error(f"File is not json!") + return {} + if not default_conf_path.exists() and path == default_conf_path: default_conf_path.parent.mkdir(exist_ok=True) with open(str(path), "w") as f: json.dump( {"last-check": datetime.datetime.timestamp(datetime.datetime.now())}, f, indent=3) - - with open(str(path), "r") as f: - creds = json.load(f) - if "delete_this" in creds.keys(): - rich.print( - f"[red]'Delete This' Tag not removed! Please Remove it in {path} " - f"before continuing[/red]") - exit(1) - logging.debug(f"Loaded Credentials {creds}") + try: + with open(str(path), "r") as f: + creds = json.load(f) + if isinstance(creds, str): + logging.error(f"Failed to load creds {path} content is loaded as string") + return {} + if "delete_this" in creds.keys(): + rich.print( + f"[red]'Delete This' Tag not removed! Please Remove it in {path} " + f"before continuing[/red]") + exit(1) + logging.debug(f"Loaded Credentials {creds}") + except JSONDecodeError: + logging.error(f"Failed to load json {path}") + return {} return creds @classmethod diff --git a/abst/cli_commands/context/commands.py b/abst/cli_commands/context/commands.py index 98930ad..425fc65 100644 --- a/abst/cli_commands/context/commands.py +++ b/abst/cli_commands/context/commands.py @@ -34,6 +34,8 @@ def _list(debug=False): tree = Tree("Contexts") for file in Path(default_contexts_location).iterdir(): + if file.name.startswith(".") or not file.name.endswith(".json"): + continue cfg = Bastion.load_json(file) used_time = "" if "last-time-used" not in cfg.keys() else f"| Used: {cfg['last-time-used']}" tree.add(f"{file.name.replace('.json', '')} {used_time}") diff --git a/abst/cli_commands/parallel/commands.py b/abst/cli_commands/parallel/commands.py index fb1c950..7d83b61 100644 --- a/abst/cli_commands/parallel/commands.py +++ b/abst/cli_commands/parallel/commands.py @@ -1,5 +1,3 @@ -from pathlib import Path - import click import rich from InquirerPy import inquirer @@ -7,7 +5,7 @@ from abst.bastion_support.bastion_scheduler import BastionScheduler from abst.bastion_support.oci_bastion import Bastion -from abst.config import default_parallel_sets_location, default_contexts_location +from abst.config import default_parallel_sets_location from abst.tools import display_scheduled from abst.utils.misc_funcs import setup_calls @@ -71,13 +69,15 @@ def _list(debug): tree = Tree("Sets in parallel folder") for _set in default_parallel_sets_location.iterdir(): - if _set.name.startswith(".") and not _set.name.endswith(".json"): + if _set.name.startswith("."): continue leaf = tree.add(f"{_set.name.replace('.json', '')}") for ctx in _set.iterdir(): + if ctx.name.startswith(".") or not ctx.name.endswith(".json"): + continue cfg = Bastion.load_json(ctx) used_time = "" if "last-time-used" not in cfg.keys() else f"| last time used {cfg['last-time-used']}" - if ctx.name.startswith("."): + if ctx.name.startswith(".") or not ctx.name.endswith(".json"): continue leaf.add(f"{ctx.name.replace('.json', '')} {used_time}")