Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Fixed

- `repos create`/`update`, `upstream <format> 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
Expand Down
12 changes: 6 additions & 6 deletions cloudsmith_cli/cli/commands/policy/deny.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 ...",
Expand Down
10 changes: 6 additions & 4 deletions cloudsmith_cli/cli/commands/policy/license.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""CLI/Commands - create, retrieve, update or delete license policies."""

import json

import click

from ....core.api import orgs as api
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 ...",
Expand Down
10 changes: 6 additions & 4 deletions cloudsmith_cli/cli/commands/policy/vulnerability.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""CLI/Commands - Import all vulnerability policy commands."""

import json

import click

from ....core.api import orgs as api
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 ...",
Expand Down
8 changes: 6 additions & 2 deletions cloudsmith_cli/cli/commands/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 ...",
Expand Down
10 changes: 6 additions & 4 deletions cloudsmith_cli/cli/commands/upstream.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""CLI/Commands - create, retrieve, update or delete repository upstreams."""

import json

import click

from ...core.api import upstreams as api
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down
104 changes: 104 additions & 0 deletions cloudsmith_cli/cli/tests/commands/test_config_file_json.py
Original file line number Diff line number Diff line change
@@ -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()
19 changes: 19 additions & 0 deletions cloudsmith_cli/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading