Skip to content

Commit

Permalink
Check pynitrokey version on firmware updates
Browse files Browse the repository at this point in the history
Check for pynitrokey updates before update firmware of
pro, storage, start, fido2
  • Loading branch information
Laborratte5 committed Jun 30, 2023
1 parent 2c57d06 commit d7911ea
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
9 changes: 8 additions & 1 deletion pynitrokey/cli/pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import intelhex as ih
import nkdfu

from pynitrokey.helpers import local_critical, local_print, prompt
from pynitrokey.helpers import (
check_pynitrokey_version,
local_critical,
local_print,
prompt,
)
from pynitrokey.libnk import DeviceNotFound, NitrokeyPro, RetCode

print = local_print
Expand Down Expand Up @@ -119,6 +124,8 @@ def update(firmware_path: str):
import nkdfu.dfu as dfu
import usb1

check_pynitrokey_version()

print = local_print
# TODO(szszsz): extract logic to nkdfu, leaving only end-user error handling
assert firmware_path.endswith("bin")
Expand Down
4 changes: 3 additions & 1 deletion pynitrokey/cli/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from tqdm import tqdm
from usb.core import USBError

from pynitrokey.helpers import local_critical, local_print
from pynitrokey.helpers import check_pynitrokey_version, local_critical, local_print
from pynitrokey.start.gnuk_token import OnlyBusyICCError, get_gnuk_device
from pynitrokey.start.threaded_log import ThreadLog
from pynitrokey.start.upgrade_by_passwd import (
Expand Down Expand Up @@ -211,6 +211,8 @@ def update(
green_led,
)

check_pynitrokey_version()

if green_led and (regnual is None or gnuk is None):
local_critical(
"You selected the --green-led option, please provide '--regnual' and "
Expand Down
11 changes: 10 additions & 1 deletion pynitrokey/cli/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@
from tqdm import tqdm

from pynitrokey.cli.exceptions import CliException
from pynitrokey.helpers import AskUser, confirm, local_critical, local_print, prompt
from pynitrokey.helpers import (
AskUser,
check_pynitrokey_version,
confirm,
local_critical,
local_print,
prompt,
)
from pynitrokey.libnk import DeviceNotFound, NitrokeyStorage, RetCode


Expand Down Expand Up @@ -144,6 +151,8 @@ def is_connected() -> ConnectedDevices:
)
def update(firmware: str, experimental):
"""experimental: run assisted update through dfu-programmer tool"""
check_pynitrokey_version()

if platform.system() != "Linux" or not experimental:
local_print(
"This feature is Linux only and experimental, which means it was not tested thoroughly.\n"
Expand Down
9 changes: 8 additions & 1 deletion pynitrokey/cli/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@

import pynitrokey
from pynitrokey.confconsts import LOG_FN
from pynitrokey.helpers import AskUser, local_critical, local_print
from pynitrokey.helpers import (
AskUser,
check_pynitrokey_version,
local_critical,
local_print,
)

logger = logging.getLogger()

Expand All @@ -41,6 +46,8 @@ def update(serial, yes, force):
# print('Please use {} to run the firmware update'.format(update_url))
# return

check_pynitrokey_version()

IS_LINUX = platform.system() == "Linux"

logger.debug(f"Start session {datetime.now()}")
Expand Down
22 changes: 22 additions & 0 deletions pynitrokey/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import sys
import time
from getpass import getpass
from importlib.metadata import version
from itertools import chain
from numbers import Number
from threading import Event, Timer
from typing import Any, Callable, Dict, List, NoReturn, Optional, Tuple, TypeVar, Union

import click
import semver
from tqdm import tqdm

from pynitrokey.confconsts import (
Expand All @@ -32,6 +34,7 @@
VERBOSE,
Verbosity,
)
from pynitrokey.updates import Repository

STDOUT_PRINT = True

Expand Down Expand Up @@ -404,3 +407,22 @@ def require_windows_admin() -> None:
"Warning: It is recommended to execute nitropy with admin privileges "
"to be able to access Nitrokey 3 and Nitrokey FIDO 2 devices."
)


def check_pynitrokey_version() -> None:
"""Checks wether the used pynitrokey version is the latest available version and warns the user if the used version is outdated"""

latest_release = Repository("Nitrokey", "pynitrokey").get_latest_release()
latest_version = semver.Version.parse(latest_release.tag[1:])

current_version = semver.Version.parse(version("pynitrokey"))

if current_version < latest_version:
local_print(
f"You are using an outdated version ({current_version}) of pynitrokey."
)
local_print(f"Latest pynitrokey version is {latest_version}")
local_print("Updating with an outdated version is discouraged.")

if not confirm("Do you still want to continue?", default=False):
raise click.Abort()

0 comments on commit d7911ea

Please sign in to comment.