Skip to content

Commit 3068a77

Browse files
committed
database: raise exception on query failure
Raise an exception when querying the device key from the cloud fails. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent 31c0832 commit 3068a77

2 files changed

Lines changed: 31 additions & 12 deletions

File tree

src/infuse_iot/database.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ class DeviceKeyChangedError(KeyError):
5656
"""Device key for the requested device has changed"""
5757

5858

59+
class DeviceKeyQueryFailed(Exception):
60+
"""Querying a device key from the cloud failed"""
61+
62+
def __init__(self, infuse_id: int, status_code: int, content: str):
63+
self.infuse_id = infuse_id
64+
self.status_code = status_code
65+
self.content = content
66+
67+
def __str__(self):
68+
return f"{self.infuse_id:016x}: <{self.status_code}> {self.content}"
69+
70+
5971
class DeviceDatabase:
6072
"""Database of current device state"""
6173

@@ -208,9 +220,11 @@ def observe_security_state(
208220
base64.b64encode(challenge_resp).decode("utf-8"),
209221
)
210222
body = GetDeviceSharedSecretBody(f"{infuse_id:016x}", security_state)
211-
response = get_device_shared_secret.sync(client=client, body=body)
212-
if response is not None:
213-
key = base64.b64decode(response.key)
223+
response = get_device_shared_secret.sync_detailed(client=client, body=body)
224+
if response.parsed is None:
225+
raise DeviceKeyQueryFailed(infuse_id, response.status_code, response.content.decode("utf-8"))
226+
else:
227+
key = base64.b64decode(response.parsed.key)
214228
self.devices[infuse_id].shared_key = key
215229
self._update_cache(infuse_id, device_pub_key, key)
216230

src/infuse_iot/tools/gateway.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from infuse_iot.common import InfuseID, InfuseType
2727
from infuse_iot.database import (
2828
DeviceDatabase,
29+
DeviceKeyQueryFailed,
2930
NoKeyError,
3031
)
3132
from infuse_iot.epacket.packet import (
@@ -80,15 +81,19 @@ def query_device_key(self, infuse_id: int, cb_event: threading.Event | None = No
8081

8182
def security_state_done(pkt: PacketReceived, _rc: int, response: bytes, challenge):
8283
decoded = defs.security_state.response.vla_from_buffer_copy(response)
83-
self.ddb.observe_security_state(
84-
infuse_id,
85-
bytes(decoded.cloud_public_key),
86-
bytes(decoded.device_public_key),
87-
decoded.network_id,
88-
challenge,
89-
decoded.challenge_response_type,
90-
bytes(decoded.challenge_response),
91-
)
84+
try:
85+
self.ddb.observe_security_state(
86+
infuse_id,
87+
bytes(decoded.cloud_public_key),
88+
bytes(decoded.device_public_key),
89+
decoded.network_id,
90+
challenge,
91+
decoded.challenge_response_type,
92+
bytes(decoded.challenge_response),
93+
)
94+
except DeviceKeyQueryFailed as e:
95+
Console.log_error(f"Failed to query key for {e.infuse_id:016x}: {e.content}")
96+
return
9297
if cb_event is not None:
9398
cb_event.set()
9499

0 commit comments

Comments
 (0)