Skip to content

Commit

Permalink
Use Nitrokey Python SDK
Browse files Browse the repository at this point in the history
This patch removes the trussed, nk3 and nkpk modules that have been
extracted into the the Nitrokey Python SDK available at
https://github.com/Nitrokey/nitrokey-sdk-py.

The SDK provides a stable API without unnecessary dependencies that can
be used by pynitrokey, nitrokey-app2 and third-party applications.
  • Loading branch information
robin-nitrokey committed Jul 23, 2024
1 parent a5abc29 commit b736a5c
Show file tree
Hide file tree
Showing 55 changed files with 66 additions and 7,758 deletions.
1 change: 0 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
# E501 (line length) disabled as this is handled by black which takes better care of edge cases
extend-ignore = E203,E501,E701
max-complexity = 18
extend-exclude = pynitrokey/trussed/bootloader/nrf52_upload
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PYTHON3=python3
PYTHON3_VENV=venv/bin/python3

# whitelist of directories for flake8
FLAKE8_DIRS=pynitrokey/cli/nk3 pynitrokey/cli/nkpk.py pynitrokey/cli/trussed pynitrokey/nk3 pynitrokey/nkpk.py pynitrokey/trussed
FLAKE8_DIRS=pynitrokey/cli/nk3 pynitrokey/cli/nkpk.py pynitrokey/cli/trussed

all: init

Expand Down
30 changes: 20 additions & 10 deletions pynitrokey/cli/nk3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
from typing import List, Optional

import click
from nitrokey.nk3 import NK3_DATA
from nitrokey.nk3.bootloader import Nitrokey3Bootloader
from nitrokey.nk3.device import Nitrokey3Device
from nitrokey.trussed.base import NitrokeyTrussedBase
from nitrokey.trussed.bootloader import Device

from pynitrokey.cli import trussed
from pynitrokey.cli.exceptions import CliException
from pynitrokey.cli.trussed.test import TestCase
from pynitrokey.helpers import local_print
from pynitrokey.nk3 import NK3_DATA
from pynitrokey.nk3.bootloader import Nitrokey3Bootloader
from pynitrokey.nk3.device import Nitrokey3Device
from pynitrokey.trussed.base import NitrokeyTrussedBase
from pynitrokey.trussed.bootloader import Device
from pynitrokey.helpers import local_critical, local_print


class Context(trussed.Context[Nitrokey3Bootloader, Nitrokey3Device]):
Expand All @@ -42,12 +42,12 @@ def test_cases(self) -> list[TestCase]:
]

def open(self, path: str) -> Optional[NitrokeyTrussedBase]:
from pynitrokey.nk3 import open
from nitrokey.nk3 import open

return open(path)

def list_all(self) -> List[NitrokeyTrussedBase]:
from pynitrokey.nk3 import list
from nitrokey.nk3 import list

return list()

Expand Down Expand Up @@ -252,7 +252,12 @@ def factory_reset(ctx: Context, experimental: bool) -> None:
)

with ctx.connect_device() as device:
device.admin.factory_reset()
local_print("Please touch the device to confirm the operation", file=sys.stderr)
if not device.admin.factory_reset():
local_critical(
"Factory reset is not supported by the firmware version on the device",
support_hint=False,
)


# We consciously do not allow resetting the admin app
Expand All @@ -278,7 +283,12 @@ def factory_reset_app(ctx: Context, application: str, experimental: bool) -> Non
)

with ctx.connect_device() as device:
device.admin.factory_reset_app(application)
local_print("Please touch the device to confirm the operation", file=sys.stderr)
if not device.admin.factory_reset_app(application):
local_critical(
"Application Factory reset is not supported by the firmware version on the device",
support_hint=False,
)


@nk3.command()
Expand Down
8 changes: 4 additions & 4 deletions pynitrokey/cli/nk3/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

import click
from click_aliases import ClickAliasedGroup

from pynitrokey.cli.nk3 import Context, nk3
from pynitrokey.helpers import AskUser, local_critical, local_print
from pynitrokey.nk3.secrets_app import (
from nitrokey.nk3.secrets_app import (
ALGORITHM_TO_KIND,
STRING_TO_KIND,
SecretsApp,
Expand All @@ -20,6 +17,9 @@
SecretsAppHealthCheckException,
)

from pynitrokey.cli.nk3 import Context, nk3
from pynitrokey.helpers import AskUser, local_critical, local_print


@nk3.group(cls=ClickAliasedGroup)
@click.pass_context
Expand Down
4 changes: 2 additions & 2 deletions pynitrokey/cli/nk3/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from typing import Any, Callable, Iterator, List, Optional

from click import Abort
from nitrokey.nk3.updates import Updater, UpdateUi
from nitrokey.trussed.utils import Version

from pynitrokey.cli.exceptions import CliException
from pynitrokey.cli.nk3 import Context
from pynitrokey.helpers import DownloadProgressBar, ProgressBar, confirm, local_print
from pynitrokey.nk3.updates import Updater, UpdateUi
from pynitrokey.trussed.utils import Version

logger = logging.getLogger(__name__)

Expand Down
10 changes: 5 additions & 5 deletions pynitrokey/cli/nkpk.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
from typing import Optional

import click
from nitrokey.nkpk import NKPK_DATA, NitrokeyPasskeyBootloader, NitrokeyPasskeyDevice
from nitrokey.trussed.base import NitrokeyTrussedBase
from nitrokey.trussed.bootloader import Device

from pynitrokey.cli.trussed.test import TestCase
from pynitrokey.nkpk import NKPK_DATA, NitrokeyPasskeyBootloader, NitrokeyPasskeyDevice
from pynitrokey.trussed.base import NitrokeyTrussedBase
from pynitrokey.trussed.bootloader import Device

from . import trussed

Expand Down Expand Up @@ -47,12 +47,12 @@ def device_name(self) -> str:
return "Nitrokey Passkey"

def open(self, path: str) -> Optional[NitrokeyTrussedBase]:
from pynitrokey.nkpk import open
from nitrokey.nkpk import open

return open(path)

def list_all(self) -> list[NitrokeyTrussedBase]:
from pynitrokey.nkpk import list
from nitrokey.nkpk import list

return list()

Expand Down
26 changes: 13 additions & 13 deletions pynitrokey/cli/trussed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePublicKey
from ecdsa import NIST256p, SigningKey
from nitrokey.trussed import DeviceData
from nitrokey.trussed.admin_app import BootMode
from nitrokey.trussed.base import NitrokeyTrussedBase
from nitrokey.trussed.bootloader import Device as BootloaderDevice
from nitrokey.trussed.bootloader import (
FirmwareContainer,
NitrokeyTrussedBootloader,
parse_firmware_image,
)
from nitrokey.trussed.device import NitrokeyTrussedDevice
from nitrokey.trussed.exceptions import TimeoutException
from nitrokey.trussed.provisioner_app import ProvisionerApp
from nitrokey.updates import OverwriteError

from pynitrokey.cli.exceptions import CliException
from pynitrokey.helpers import (
Expand All @@ -26,19 +39,6 @@
local_print,
require_windows_admin,
)
from pynitrokey.trussed import DeviceData
from pynitrokey.trussed.admin_app import BootMode
from pynitrokey.trussed.base import NitrokeyTrussedBase
from pynitrokey.trussed.bootloader import Device as BootloaderDevice
from pynitrokey.trussed.bootloader import (
FirmwareContainer,
NitrokeyTrussedBootloader,
parse_firmware_image,
)
from pynitrokey.trussed.device import NitrokeyTrussedDevice
from pynitrokey.trussed.exceptions import TimeoutException
from pynitrokey.trussed.provisioner_app import ProvisionerApp
from pynitrokey.updates import OverwriteError

from .test import TestCase

Expand Down
5 changes: 3 additions & 2 deletions pynitrokey/cli/trussed/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
from types import TracebackType
from typing import Callable, Iterable, Optional, Sequence, Tuple, Type, Union

from nitrokey.trussed.base import NitrokeyTrussedBase
from nitrokey.trussed.utils import Version

from pynitrokey.cli.exceptions import CliException
from pynitrokey.fido2 import device_path_to_str
from pynitrokey.helpers import local_print
from pynitrokey.trussed.base import NitrokeyTrussedBase
from pynitrokey.trussed.utils import Version

logger = logging.getLogger(__name__)

Expand Down
6 changes: 3 additions & 3 deletions pynitrokey/cli/trussed/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
from threading import Thread
from typing import Any, Optional

from nitrokey.trussed.base import NitrokeyTrussedBase
from nitrokey.trussed.device import NitrokeyTrussedDevice
from nitrokey.trussed.utils import Fido2Certs, Uuid, Version
from tqdm import tqdm

from pynitrokey.cli.trussed.test import TestContext, TestResult, TestStatus, test_case
from pynitrokey.fido2.client import NKFido2Client
from pynitrokey.helpers import local_print
from pynitrokey.trussed.base import NitrokeyTrussedBase
from pynitrokey.trussed.device import NitrokeyTrussedDevice
from pynitrokey.trussed.utils import Fido2Certs, Uuid, Version

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion pynitrokey/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import pytest
from _pytest.fixtures import FixtureRequest
from nitrokey.nk3.secrets_app import Instruction, SecretsApp

from pynitrokey.cli import CliException
from pynitrokey.cli.nk3 import Context
from pynitrokey.nk3.secrets_app import Instruction, SecretsApp

CORPUS_PATH = "/tmp/corpus"

Expand Down
2 changes: 1 addition & 1 deletion pynitrokey/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from typing import Any, Callable, Dict, List, NoReturn, Optional, Tuple, TypeVar, Union

import click
from nitrokey.updates import Repository
from semver.version import Version
from tqdm import tqdm

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

STDOUT_PRINT = True

Expand Down
61 changes: 0 additions & 61 deletions pynitrokey/nk3/__init__.py

This file was deleted.

89 changes: 0 additions & 89 deletions pynitrokey/nk3/bootloader.py

This file was deleted.

Loading

0 comments on commit b736a5c

Please sign in to comment.