From 41538e6a027d4194695c7ae7b051954c7ba4dea1 Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Fri, 15 Mar 2024 14:05:26 +0100 Subject: [PATCH] Add validation base_url fixes #588 --- CHANGES/588.bugfix | 1 + pulp_cli/config.py | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 CHANGES/588.bugfix diff --git a/CHANGES/588.bugfix b/CHANGES/588.bugfix new file mode 100644 index 00000000..ca88503e --- /dev/null +++ b/CHANGES/588.bugfix @@ -0,0 +1 @@ +Added validation to see if `base_url` in the config looks useful. diff --git a/pulp_cli/config.py b/pulp_cli/config.py index 10395513..04d141e4 100644 --- a/pulp_cli/config.py +++ b/pulp_cli/config.py @@ -1,6 +1,7 @@ import re import typing as t from pathlib import Path +from urllib.parse import urlparse import click import toml @@ -105,9 +106,23 @@ def config_options(command: FC) -> FC: return command -def validate_config(config: t.Dict[str, t.Any], strict: bool = False) -> bool: +def validate_config(config: t.Dict[str, t.Any], strict: bool = False) -> None: """Validate, that the config provides the proper data types""" errors: t.List[str] = [] + if "base_url" in config: + try: + parsed_url = urlparse(config["base_url"]) + if ( + not parsed_url.scheme + or not parsed_url.netloc + or parsed_url.path + or parsed_url.params + or parsed_url.query + or parsed_url.fragment + ): + errors.append(_("'base_url' should be of the form '://'")) + except ValueError: + errors.append(_("Failed to parse the 'base_path'.")) if "api_root" in config and (config["api_root"][0] != "/" or config["api_root"][-1] != "/"): errors.append(_("'api_root' must begin and end with '/'")) if "format" in config and config["format"].lower() not in FORMAT_CHOICES: @@ -143,12 +158,11 @@ def validate_config(config: t.Dict[str, t.Any], strict: bool = False) -> bool: errors.append(_("Missing settings: '{}'.").format("','".join(missing_settings))) if errors: raise ValueError("\n".join(errors)) - return True def validate_settings( settings: t.MutableMapping[str, t.Dict[str, t.Any]], strict: bool = False -) -> bool: +) -> None: errors: t.List[str] = [] if "cli" not in settings: errors.append(_("Could not locate default profile 'cli' setting")) @@ -165,7 +179,6 @@ def validate_settings( errors.append(str(e)) if errors: raise ValueError("\n".join(errors)) - return True @pulp_group(name="config", help=_("Manage pulp-cli config file"))