From 8224ca8dbdb7450f1608afc747cadcce9f2909ed Mon Sep 17 00:00:00 2001 From: Luc Didry Date: Mon, 29 Apr 2024 14:17:46 +0200 Subject: [PATCH] =?UTF-8?q?=E2=98=9D=F0=9F=A7=90=20=E2=80=94=20Validate=20?= =?UTF-8?q?config=20file=20readability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + argos/commands.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9380d4d..a2f2014 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased] - 🐛 — Fix --config option for some commands +- ☝️🧐 — Validate config file readability ## 0.1.0 diff --git a/argos/commands.py b/argos/commands.py index 1e5cf7b..2b822b7 100644 --- a/argos/commands.py +++ b/argos/commands.py @@ -31,6 +31,16 @@ def coroutine(f): return wrapper +def validate_config_access(ctx, param, value): + if os.path.isfile(value) and os.access(value, os.R_OK): + return value + + if os.path.isfile(value): + raise click.BadParameter(f"the file {value} is not readabale.") + + raise click.BadParameter(f"the file {value} does not exists or is not reachable.") + + @click.group() def cli(): pass @@ -93,6 +103,7 @@ def agent(server_url, auth, max_tasks, wait_time, log_level): help="Path of the configuration file. " "If ARGOS_YAML_FILE environment variable is set, its value will be used instead.", envvar="ARGOS_YAML_FILE", + callback=validate_config_access, ) @click.option("--reload", is_flag=True, help="Enable hot reloading") def start(host, port, config, reload): @@ -140,6 +151,7 @@ def validate_max_results(ctx, param, value): help="Path of the configuration file. " "If ARGOS_YAML_FILE environment variable is set, its value will be used instead.", envvar="ARGOS_YAML_FILE", + callback=validate_config_access, ) @coroutine async def cleandb(max_results, max_lock_seconds, config): @@ -170,6 +182,7 @@ async def cleandb(max_results, max_lock_seconds, config): help="Path of the configuration file. " "If ARGOS_YAML_FILE environment variable is set, its value will be used instead.", envvar="ARGOS_YAML_FILE", + callback=validate_config_access, ) @coroutine async def reload_config(config): @@ -200,6 +213,7 @@ async def reload_config(config): help="Path of the configuration file. " "If ARGOS_YAML_FILE environment variable is set, its value will be used instead.", envvar="ARGOS_YAML_FILE", + callback=validate_config_access, ) @coroutine async def migrate(config):