Skip to content

Commit

Permalink
Return dataclasses from get_info and encrypt (#85)
Browse files Browse the repository at this point in the history
This patch replaces the tuples returned by get_info and encrypt with
dataclasses.

Fixes: #80
Fixes: #79
  • Loading branch information
robin-nitrokey authored Nov 17, 2023
1 parent 40a340f commit b7f730b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
20 changes: 16 additions & 4 deletions nethsm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ def from_string(s: str) -> "TlsKeyType":
raise ValueError(f"Unsupported TLS key type {s}")


@dataclass
class Info:
vendor: str
product: str


@dataclass
class SystemInfo:
firmware_version: str
Expand Down Expand Up @@ -225,6 +231,12 @@ class Key:
data: Optional[str]


@dataclass
class EncryptionResult:
encrypted: str
iv: str


@dataclass
class LoggingConfig:
ip_address: str
Expand Down Expand Up @@ -716,12 +728,12 @@ def delete_key_tag(self, key_id: str, tag: str) -> None:
},
)

def get_info(self) -> tuple[str, str]:
def get_info(self) -> Info:
try:
response = self.get_api().info_get()
except Exception as e:
_handle_exception(e)
return (response.body.vendor, response.body.product)
return Info(vendor=response.body.vendor, product=response.body.product)

def get_state(self) -> State:
try:
Expand Down Expand Up @@ -1436,7 +1448,7 @@ def factory_reset(self) -> None:

def encrypt(
self, key_id: str, data: str, mode: EncryptMode, iv: str
) -> tuple[str, str]:
) -> EncryptionResult:
from .client.components.schema.encrypt_request_data import (
EncryptRequestDataDict,
)
Expand All @@ -1460,7 +1472,7 @@ def encrypt(
404: f"Key {key_id} not found",
},
)
return (response.body.encrypted, response.body.iv)
return EncryptionResult(encrypted=response.body.encrypted, iv=response.body.iv)

def decrypt(
self,
Expand Down
3 changes: 2 additions & 1 deletion tests/test_nethsm_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,10 @@ def test_encrypt_decrypt(nethsm: NetHSM) -> None:
EncryptMode.AES_CBC,
iv_b64,
)
assert encrypted.iv == iv_b64
decrypt = nethsm.decrypt(
C.KEY_ID_AES,
encrypted[0],
encrypted.encrypted,
DecryptMode.AES_CBC,
iv_b64,
)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_nethsm_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ def test_state_provision(nethsm_no_provision: NetHSM) -> None:

def test_info(nethsm_no_provision: NetHSM) -> None:
"""Query the vendor and product information for a NetHSM."""
(vendor, product) = nethsm_no_provision.get_info()
info = nethsm_no_provision.get_info()
assert nethsm_no_provision.host == C.HOST
assert vendor == "Nitrokey GmbH"
assert product == "NetHSM"
assert info.vendor == "Nitrokey GmbH"
assert info.product == "NetHSM"


def test_state_provision_add_user_metrics_get_metrics(
Expand Down

0 comments on commit b7f730b

Please sign in to comment.