Skip to content

Commit

Permalink
Make -u/--username required for user update/reset-token
Browse files Browse the repository at this point in the history
Making it optional and defaulting to the current user could have
inadvertently updated your own user if you forgot to specify the target
user. Requiring a value makes the update or reset token action
more explicit.

Resolves #86
  • Loading branch information
mprpic committed Jul 16, 2024
1 parent bc72a35 commit beed020
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions cvelib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ def quota(ctx: click.Context, print_raw: bool) -> None:
"-u",
"--username",
help="Specify the user to show.",
show_default="Current user specified in -u/--username/CVE_USER",
show_default="Current user specified in top-level -u/--username/$CVE_USER",
)
@click.option("--raw", "print_raw", default=False, is_flag=True, help="Print response JSON.")
@click.pass_context
Expand All @@ -920,15 +920,17 @@ def show_user(ctx: click.Context, username: Optional[str], print_raw: bool) -> N
@click.option(
"-u",
"--username",
required=True,
help="User whose API key should be reset (only ADMIN role users can update other users).",
show_default="Current user specified in global -u/--username/CVE_USER",
)
@click.option("--raw", "print_raw", default=False, is_flag=True, help="Print response JSON.")
@click.pass_context
@handle_cve_api_error
def reset_key(ctx: click.Context, username: Optional[str], print_raw: bool) -> None:
"""Reset a user's personal access token (API key).
You must explicitly specify the user being updated using the `-u` option.
This API key is used to authenticate each request to the CVE API.
"""
cve_api = ctx.obj.cve_api
Expand All @@ -951,8 +953,8 @@ def reset_key(ctx: click.Context, username: Optional[str], print_raw: bool) -> N
@click.option(
"-u",
"--username",
required=True,
help="Username of the user being updated (only ADMIN role users can update other users).",
show_default="Current user specified in global -u/--username/CVE_USER",
)
@click.option(
"--mark-active/--mark-inactive", "active", default=None, help="Mark user as active or inactive."
Expand All @@ -968,6 +970,8 @@ def reset_key(ctx: click.Context, username: Optional[str], print_raw: bool) -> N
def update_user(ctx: click.Context, username: Optional[str], **opts_data: dict) -> None:
"""Update a user.
You must explicitly specify the user being updated using the `-u` option.
To reset a user's API key, use `cve user reset-key`.
"""
print_raw = opts_data.pop("print_raw")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def test_reset_key():
with mock.patch("cvelib.cli.CveApi.reset_api_key") as reset_api_key:
reset_api_key.return_value = api_key
runner = CliRunner()
result = runner.invoke(cli, DEFAULT_OPTS + ["user", "reset-key"])
result = runner.invoke(cli, DEFAULT_OPTS + ["user", "reset-key", "-u", "test_user"])
assert result.exit_code == 0, result.output
assert result.output == (
"New API key for test_user:\n\n"
Expand Down

0 comments on commit beed020

Please sign in to comment.