Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add confirm option to allow automated updates on nk3 devices #575

Merged
merged 1 commit into from
Oct 1, 2024
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
16 changes: 12 additions & 4 deletions pynitrokey/cli/nk3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def _list() -> None:
is_flag=True,
help="Allow updates with an outdated pynitrokey version (dangerous)",
)
@click.option(
"--confirm",
default=False,
is_flag=True,
help="Confirm all questions to allow running non-interactively",
)
@click.option(
"--experimental",
default=False,
Expand All @@ -91,15 +97,17 @@ def update(
image: Optional[str],
version: Optional[str],
ignore_pynitrokey_version: bool,
confirm: bool,
experimental: bool,
) -> None:
"""
Update the firmware of the device using the given image.

This command requires that exactly one Nitrokey 3 in bootloader or firmware mode is connected.
The user is asked to confirm the operation before the update is started. The Nitrokey 3 may
not be removed during the update. Also, additional Nitrokey 3 devices may not be connected
during the update.
The user is asked to confirm the operation before the update is started. If the --confirm
option is provided, this is the confirmation. This option may be used to automate an update.
The Nitrokey 3 may not be removed during the update. Also, additional Nitrokey 3 devices may
not be connected during the update.

If no firmware image is given, the latest firmware release is downloaded automatically. If
the --version option is set, the given version is downloaded instead.
Expand All @@ -115,7 +123,7 @@ def update(

from .update import update as exec_update

exec_update(ctx, image, version, ignore_pynitrokey_version)
exec_update(ctx, image, version, ignore_pynitrokey_version, confirm)


@nk3.command()
Expand Down
15 changes: 13 additions & 2 deletions pynitrokey/cli/nk3/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@


class UpdateCli(UpdateUi):
def __init__(self) -> None:
def __init__(self, confirm_continue: bool = False) -> None:
self._version_printed = False
self._confirm_continue = confirm_continue

def error(self, *msgs: Any) -> Exception:
return CliException(*msgs)
Expand All @@ -47,6 +48,9 @@ def abort_pynitrokey_version(
)

def confirm_download(self, current: Optional[Version], new: Version) -> None:
if self._confirm_continue:
return

confirm(
f"Do you want to download the firmware version {new}?",
default=True,
Expand All @@ -71,6 +75,10 @@ def confirm_update(self, current: Optional[Version], new: Version) -> None:
"Please do not remove the Nitrokey 3 or insert any other Nitrokey 3 devices "
"during the update. Doing so may damage the Nitrokey 3."
)

if self._confirm_continue:
return

if not confirm("Do you want to perform the firmware update now?"):
logger.info("Update cancelled by user")
raise Abort()
Expand Down Expand Up @@ -137,7 +145,10 @@ def update(
image: Optional[str],
version: Optional[str],
ignore_pynitrokey_version: bool,
confirm_continue: bool,
) -> Version:
with ctx.connect() as device:
updater = Updater(UpdateCli(), ctx.await_bootloader, ctx.await_device)
updater = Updater(
UpdateCli(confirm_continue), ctx.await_bootloader, ctx.await_device
)
return updater.update(device, image, version, ignore_pynitrokey_version)