diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a6616d7d..37f7d0bdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed +- `repos create`/`update`, `upstream create`/`update` and `policy deny|license|vulnerability create`/`update` now report a usage error when the config file isn't valid JSON or doesn't hold a JSON object, for example a file with a trailing comma. Previously the command crashed with a `JSONDecodeError` or `AttributeError` traceback. - Fixed `cloudsmith download` choosing a package that can't be downloaded. The command also found packages that failed to sync or haven't finished, such as a failed duplicate upload of the same version. With `--yes`, it then chose the failed package, because it was uploaded last, and the download failed with HTTP 404. The command now searches only packages that have finished syncing. ## [1.31.3] - 2026-11-17 diff --git a/cloudsmith_cli/cli/commands/policy/deny.py b/cloudsmith_cli/cli/commands/policy/deny.py index a4e6b5dea..a9d59cf76 100644 --- a/cloudsmith_cli/cli/commands/policy/deny.py +++ b/cloudsmith_cli/cli/commands/policy/deny.py @@ -104,10 +104,10 @@ def list_deny_policies(ctx, opts, owner, page, page_size, page_all): @click.pass_context def create_deny_policy(ctx, opts, owner, policy_config_file): """Create a deny policy for an organization.""" - import json - use_stderr = utils.should_use_stderr(opts) - policy_config = json.load(policy_config_file) + policy_config = utils.load_json_config_file( + policy_config_file, param_hint="'POLICY_CONFIG_FILE'" + ) policy_name = policy_config.get("name") if not policy_name: @@ -176,10 +176,10 @@ def get_deny_policy(ctx, opts, owner, identifier): @click.pass_context def update_deny_policy(ctx, opts, owner, identifier, policy_config_file): """Update a deny policy for an organization.""" - import json - use_stderr = utils.should_use_stderr(opts) - policy_config = json.load(policy_config_file) + policy_config = utils.load_json_config_file( + policy_config_file, param_hint="'POLICY_CONFIG_FILE'" + ) click.secho( f"Updating {click.style(identifier, bold=True)} deny policy in the {click.style(owner, bold=True)} namespace ...", diff --git a/cloudsmith_cli/cli/commands/policy/license.py b/cloudsmith_cli/cli/commands/policy/license.py index af0c349ac..6cae24d35 100644 --- a/cloudsmith_cli/cli/commands/policy/license.py +++ b/cloudsmith_cli/cli/commands/policy/license.py @@ -1,7 +1,5 @@ """CLI/Commands - create, retrieve, update or delete license policies.""" -import json - import click from ....core.api import orgs as api @@ -169,7 +167,9 @@ def create(ctx, opts, owner, policy_config_file): """ # Use stderr for messages if the output is something else (e.g. JSON) use_stderr = opts.output != "pretty" - policy_config = json.load(policy_config_file) + policy_config = utils.load_json_config_file( + policy_config_file, param_hint="'POLICY_CONFIG_FILE'" + ) policy_name = policy_config.get("name", None) if policy_name is None: @@ -250,7 +250,9 @@ def update(ctx, opts, owner, identifier, policy_config_file): # Use stderr for message if the output is something else (e.g. JSON) use_stderr = opts.output != "pretty" - policy_config = json.load(policy_config_file) + policy_config = utils.load_json_config_file( + policy_config_file, param_hint="'POLICY_CONFIG_FILE'" + ) click.secho( f"Updating {click.style(identifier, bold=True)} license policy in the {click.style(owner, bold=True)} namespace ...", diff --git a/cloudsmith_cli/cli/commands/policy/vulnerability.py b/cloudsmith_cli/cli/commands/policy/vulnerability.py index a77cec58f..0db833b8a 100644 --- a/cloudsmith_cli/cli/commands/policy/vulnerability.py +++ b/cloudsmith_cli/cli/commands/policy/vulnerability.py @@ -1,7 +1,5 @@ """CLI/Commands - Import all vulnerability policy commands.""" -import json - import click from ....core.api import orgs as api @@ -159,7 +157,9 @@ def create(ctx, opts, owner, policy_config_file): """ # Use stderr for messages if the output is something else (e.g. JSON) use_stderr = opts.output != "pretty" - policy_config = json.load(policy_config_file) + policy_config = utils.load_json_config_file( + policy_config_file, param_hint="'POLICY_CONFIG_FILE'" + ) policy_name = policy_config.get("name", None) if policy_name is None: @@ -233,7 +233,9 @@ def update(ctx, opts, owner, identifier, policy_config_file): # Use stderr for message if the output is something else (e.g. JSON) use_stderr = opts.output != "pretty" - policy_config = json.load(policy_config_file) + policy_config = utils.load_json_config_file( + policy_config_file, param_hint="'POLICY_CONFIG_FILE'" + ) click.secho( f"Updating {click.style(identifier, bold=True)} vulnerability policy in the {click.style(owner, bold=True)} namespace ...", diff --git a/cloudsmith_cli/cli/commands/repos.py b/cloudsmith_cli/cli/commands/repos.py index 1c53bfc2a..166c80870 100644 --- a/cloudsmith_cli/cli/commands/repos.py +++ b/cloudsmith_cli/cli/commands/repos.py @@ -254,7 +254,9 @@ def create(ctx, opts, owner, repo_config_file): """ # Use stderr for messages if the output is something else (e.g. JSON) use_stderr = utils.should_use_stderr(opts) - repo_config = json.load(repo_config_file) + repo_config = utils.load_json_config_file( + repo_config_file, param_hint="'REPO_CONFIG_FILE'" + ) repo_name = repo_config.get("name", None) if repo_name is None: @@ -322,7 +324,9 @@ def update(ctx, opts, owner_repo, repo_config_file): use_stderr = opts.output != "pretty" owner, repo = owner_repo - repo_config = json.load(repo_config_file) + repo_config = utils.load_json_config_file( + repo_config_file, param_hint="'REPO_CONFIG_FILE'" + ) click.secho( f"Updating {click.style(repo, bold=True)} repository in the {click.style(owner, bold=True)} namespace ...", diff --git a/cloudsmith_cli/cli/commands/upstream.py b/cloudsmith_cli/cli/commands/upstream.py index 3178d93f7..3f06c8021 100644 --- a/cloudsmith_cli/cli/commands/upstream.py +++ b/cloudsmith_cli/cli/commands/upstream.py @@ -1,7 +1,5 @@ """CLI/Commands - create, retrieve, update or delete repository upstreams.""" -import json - import click from ...core.api import upstreams as api @@ -234,7 +232,9 @@ def func(ctx, opts, owner_repo, upstream_config_file): owner, repo = owner_repo - upstream_config = json.load(upstream_config_file) + upstream_config = utils.load_json_config_file( + upstream_config_file, param_hint="'UPSTREAM_CONFIG_FILE'" + ) upstream_name = upstream_config.get("name", None) @@ -321,7 +321,9 @@ def func(ctx, opts, owner_repo_slug_perm, upstream_config_file): owner, repo, slug_perm = owner_repo_slug_perm - upstream_config = json.load(upstream_config_file) + upstream_config = utils.load_json_config_file( + upstream_config_file, param_hint="'UPSTREAM_CONFIG_FILE'" + ) if not use_stderr: click.secho( diff --git a/cloudsmith_cli/cli/tests/commands/test_config_file_json.py b/cloudsmith_cli/cli/tests/commands/test_config_file_json.py new file mode 100644 index 000000000..23c604893 --- /dev/null +++ b/cloudsmith_cli/cli/tests/commands/test_config_file_json.py @@ -0,0 +1,104 @@ +"""Commands that read a JSON config file reject invalid files with a usage error.""" + +from unittest.mock import patch + +import pytest + +from ...commands.main import main + +HERMETIC_ARGS = ["--api-key", "fake-api-key"] + +# Each entry: (command path, positional arguments before the config file, +# param hint shown in the error, the core API function the command would call). +CONFIG_FILE_COMMANDS = [ + (["repos", "create"], ["your-org"], "REPO_CONFIG_FILE", "repos.create_repo"), + ( + ["repos", "update"], + ["your-org/your-repo"], + "REPO_CONFIG_FILE", + "repos.update_repo", + ), + ( + ["upstream", "npm", "create"], + ["your-org/your-repo"], + "UPSTREAM_CONFIG_FILE", + "upstreams.create_upstream", + ), + ( + ["upstream", "npm", "update"], + ["your-org/your-repo/abc123"], + "UPSTREAM_CONFIG_FILE", + "upstreams.update_upstream", + ), + ( + ["policy", "deny", "create"], + ["your-org"], + "POLICY_CONFIG_FILE", + "orgs.create_deny_policy", + ), + ( + ["policy", "deny", "update"], + ["your-org", "abc123"], + "POLICY_CONFIG_FILE", + "orgs.update_deny_policy", + ), + ( + ["policy", "license", "create"], + ["your-org"], + "POLICY_CONFIG_FILE", + "orgs.create_license_policy", + ), + ( + ["policy", "license", "update"], + ["your-org", "abc123"], + "POLICY_CONFIG_FILE", + "orgs.update_license_policy", + ), + ( + ["policy", "vulnerability", "create"], + ["your-org"], + "POLICY_CONFIG_FILE", + "orgs.create_vulnerability_policy", + ), + ( + ["policy", "vulnerability", "update"], + ["your-org", "abc123"], + "POLICY_CONFIG_FILE", + "orgs.update_vulnerability_policy", + ), +] + +INVALID_DOCUMENTS = [ + pytest.param( + '{\n "name": "your-repo",\n "slug": "your-repo",\n}\n', + "Invalid JSON:", + id="trailing-comma", + ), + pytest.param("", "Invalid JSON:", id="empty"), + pytest.param(b"\xff\xfe\x00", "Invalid JSON:", id="not-utf8"), + pytest.param('["your-repo"]', "must contain a JSON object", id="not-an-object"), +] + + +@pytest.mark.usefixtures("set_fake_api_host_env_var") +@pytest.mark.parametrize("command,args,param_hint,api_function", CONFIG_FILE_COMMANDS) +@pytest.mark.parametrize("document,message", INVALID_DOCUMENTS) +def test_invalid_config_file_is_a_usage_error( + runner, tmp_path, command, args, param_hint, api_function, document, message +): + config_file = tmp_path / "config.json" + if isinstance(document, bytes): + config_file.write_bytes(document) + else: + config_file.write_text(document) + + with patch(f"cloudsmith_cli.core.api.{api_function}") as mock_api: + result = runner.invoke( + main, [*command, *HERMETIC_ARGS, *args, str(config_file)] + ) + + assert result.exit_code == 2, result.output + assert result.exception is None or isinstance(result.exception, SystemExit) + assert f"Invalid value for '{param_hint}'" in result.output + assert message in result.output + mock_api.assert_not_called() diff --git a/cloudsmith_cli/cli/utils.py b/cloudsmith_cli/cli/utils.py index dfdbc999e..f22eb5637 100644 --- a/cloudsmith_cli/cli/utils.py +++ b/cloudsmith_cli/cli/utils.py @@ -275,3 +275,22 @@ def echo_skipped_error(opts: "Options") -> None: def maybe_print_status_json(opts, status_dict): """Maybe print a status dict as JSON.""" return maybe_print_as_json(opts, status_dict) + + +def load_json_config_file(config_file, param_hint): + """Load a JSON object from a config file argument. + + Raise ``click.BadParameter`` instead of an unhandled exception when the + file isn't valid JSON or doesn't hold a JSON object. + """ + try: + config = json.load(config_file) + except (ValueError, UnicodeDecodeError) as exc: + raise click.BadParameter(f"Invalid JSON: {exc}", param_hint=param_hint) + + if not isinstance(config, dict): + raise click.BadParameter( + "The file must contain a JSON object.", param_hint=param_hint + ) + + return config