Skip to content

Commit ce362f6

Browse files
committed
Move error logging to DSKE exception __init__
1 parent d8a0816 commit ce362f6

8 files changed

Lines changed: 8 additions & 59 deletions

File tree

client/http_client.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ async def get(
8787
try:
8888
response = await self._httpx_client.get(url, params=params, auth=auth)
8989
except httpx.HTTPError as exc:
90-
LOGGER.error(f"Call GET {exc.request.url} exception {str(exc)}")
9190
raise exceptions.HTTPError(
9291
method="GET",
9392
url=url,
@@ -96,7 +95,6 @@ async def get(
9695
exception=str(exc),
9796
) from exc
9897
if response.status_code != 200:
99-
LOGGER.error(f"Call GET {response.request.url} {response.status_code}")
10098
raise exceptions.HTTPError(
10199
method="GET",
102100
url=url,
@@ -169,7 +167,6 @@ async def _put_or_post(
169167
try:
170168
response = await httpx_client.request(method, url, json=json, auth=auth)
171169
except httpx.HTTPError as exc:
172-
LOGGER.error(f"Call {method} {url} exception {str(exc)}")
173170
raise exceptions.HTTPError(
174171
method=method,
175172
url=url,
@@ -178,12 +175,6 @@ async def _put_or_post(
178175
exception=str(exc),
179176
) from exc
180177
if response.status_code != 200:
181-
message = ""
182-
try:
183-
message = " " + response.json().get("message")
184-
except Exception: # pylint: disable=broad-except
185-
pass
186-
LOGGER.error(f"Call {method} {url} {response.status_code}{message}")
187178
raise exceptions.HTTPError(
188179
method=method,
189180
url=url,

common/block.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import pydantic
99
from bitarray import bitarray
1010
from common.fragment import Fragment
11-
from common.logging import LOGGER
1211
from common.utils import bytes_to_str, str_to_bytes
1312
from common.exceptions import (
1413
InvalidBlockUUIDError,
@@ -149,13 +148,10 @@ def take_data(self, start: int, size: int) -> bytes:
149148
"""
150149
end = start + size
151150
if start < 0:
152-
LOGGER.error(f"Take data from blocK: invalid start index {start}")
153151
raise InvalidPSRDIndex(self._block_uuid, start)
154152
if end > self._size:
155-
LOGGER.error(f"Take data from block: invalid end index {end}")
156153
raise InvalidPSRDIndex(self._block_uuid, end)
157154
if self._used[start:end].any():
158-
LOGGER.error(f"Take data from block: already in use {start}:{end}")
159155
raise PSRDDataAlreadyUsedError(self._block_uuid, start, size)
160156
self._used[start:end] = True
161157
data = self._data[start:end]
@@ -191,12 +187,10 @@ def from_api(cls, api_block: APIBlock) -> "Block":
191187
try:
192188
block_uuid = UUID(api_block.block_uuid)
193189
except ValueError as exc:
194-
LOGGER.error(f"Invalid block UUID in API block: {api_block.block_uuid}")
195190
raise InvalidBlockUUIDError(api_block.block_uuid) from exc
196191
try:
197192
data = str_to_bytes(api_block.data)
198193
except Exception as exc:
199-
LOGGER.error(f"Invalid block data in API block: {api_block.data}")
200194
raise InvalidPSRDDataError from exc
201195
return Block(block_uuid, data)
202196

common/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import List
77
from uuid import UUID
88
from fastapi import status
9+
from .logging import LOGGER
910

1011

1112
class DSKEException(Exception):
@@ -18,6 +19,13 @@ def __init__(self, status_code: int, message: str, details: dict | None = None):
1819
self.status_code = status_code
1920
self.message = message
2021
self.details = details
22+
try:
23+
log_message = f"DSKEException raised: {message}"
24+
if details is not None:
25+
log_message += f" Details: {json.dumps(details)}"
26+
LOGGER.error(log_message)
27+
except Exception: # pylint: disable=broad-except
28+
pass
2129

2230

2331
class ClientNotRegisteredError(DSKEException):

common/fragment.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from uuid import UUID
66
import pydantic
77
from common.exceptions import InvalidBlockUUIDError, InvalidEncodedFragmentError
8-
from common.logging import LOGGER
98
from . import utils
109

1110

@@ -107,9 +106,6 @@ def from_api(
107106
try:
108107
block_uuid = UUID(api_fragment.block_uuid)
109108
except ValueError as exc:
110-
LOGGER.error(
111-
"Invalid block UUID in API fragment: %s", api_fragment.block_uuid
112-
)
113109
raise InvalidBlockUUIDError(block_uuid=api_fragment.block_uuid) from exc
114110
block = pool.get_block(block_uuid)
115111
data = block.take_data(api_fragment.start, api_fragment.size)
@@ -139,35 +135,20 @@ def from_enc_str(
139135
"""
140136
parts = enc_str.split(":")
141137
if len(parts) != 3:
142-
LOGGER.error(
143-
f"Invalid encoded fragment {enc_str} "
144-
f"(expected three parts separated by :)"
145-
)
146138
raise InvalidEncodedFragmentError(encoded_fragment=enc_str)
147139
block_uuid_str, start_byte_str, size_str = parts
148140
try:
149141
block_uuid = UUID(block_uuid_str)
150142
except ValueError as exc:
151-
LOGGER.error(
152-
f"Invalid encoded fragment {enc_str} "
153-
f"(invalid block UUID {block_uuid_str})"
154-
)
155143
raise InvalidBlockUUIDError(block_uuid=block_uuid_str) from exc
156144
block = pool.get_block(block_uuid)
157145
try:
158146
start = int(start_byte_str)
159147
except ValueError as exc:
160-
LOGGER.error(
161-
f"Invalid encoded fragment {enc_str} "
162-
f"(invalid start byte {start_byte_str})"
163-
)
164148
raise InvalidEncodedFragmentError(encoded_fragment=enc_str) from exc
165149
try:
166150
size = int(size_str)
167151
except ValueError as exc:
168-
LOGGER.error(
169-
f"Invalid encoded fragment {enc_str} " f"(invalid size {size_str})"
170-
)
171152
raise InvalidEncodedFragmentError(encoded_fragment=enc_str) from exc
172153
data = block.take_data(start, size)
173154
return Fragment(block=block, start=start, size=size, data=data)

common/pool.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from .allocation import Allocation
88
from .block import Block
99
from .exceptions import OutOfPreSharedRandomDataError, InvalidBlockUUIDError
10-
from .logging import LOGGER
1110
from .owner import Owner
1211

1312

@@ -68,7 +67,6 @@ def get_block(self, block_uuid: UUID) -> Block:
6867
for block in self._blocks:
6968
if block.uuid == block_uuid:
7069
return block
71-
LOGGER.error(f"Block UUID not found in {str(self.owner)} pool: {block_uuid}")
7270
raise InvalidBlockUUIDError(block_uuid=str(block_uuid))
7371

7472
def allocate(self, size: PositiveInt, purpose: str) -> Allocation:
@@ -79,10 +77,6 @@ def allocate(self, size: PositiveInt, purpose: str) -> Allocation:
7977
"""
8078
available = self.nr_unused_bytes
8179
if available < size:
82-
LOGGER.error(
83-
f"PSRD allocation failed: pool={self._name} owner={self._owner} purpose={purpose} "
84-
f"size={size} available={available}"
85-
)
8680
raise OutOfPreSharedRandomDataError(
8781
f"{self._name} {self._owner}", purpose, size, available
8882
)

common/share.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from uuid import UUID
66
from .exceptions import WrongMasterSAEIDError, WrongSlaveSAEIDError
7-
from .logging import LOGGER
87
from .utils import bytes_to_str
98

109

@@ -108,10 +107,6 @@ def check_master_sae(self, master_sae_id: str):
108107
"""
109108
if self.master_sae_id != master_sae_id:
110109
key_id_str = str(self.user_key_id)
111-
LOGGER.warning(
112-
f"Requested master SAE ID {master_sae_id} does not match master SAE ID "
113-
f"{self.master_sae_id} for key ID {key_id_str}"
114-
)
115110
raise WrongMasterSAEIDError(master_sae_id, self.master_sae_id, key_id_str)
116111

117112
def check_slave_sae(self, slave_sae_id: str):
@@ -121,8 +116,4 @@ def check_slave_sae(self, slave_sae_id: str):
121116
"""
122117
if self.slave_sae_id != slave_sae_id:
123118
key_id_str = str(self.user_key_id)
124-
LOGGER.warning(
125-
f"Requested slave SAE ID {slave_sae_id} does not match slave SAE ID "
126-
f"{self.slave_sae_id} for key ID {key_id_str}"
127-
)
128119
raise WrongSlaveSAEIDError(slave_sae_id, self.slave_sae_id, key_id_str)

hub/hub.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from common.allocation import Allocation
1414
from common.block import Block
1515
from common.encryption_key import EncryptionKey
16-
from common.logging import LOGGER
1716
from common.owner import Owner
1817
from common.share import Share
1918
from common.share_api import APIGetShareResponse, APIPostShareRequest
@@ -119,7 +118,6 @@ def lookup_peer_client(self, client_name: str) -> PeerClient:
119118
Lookup a peer client by name.
120119
"""
121120
if client_name not in self._peer_clients:
122-
LOGGER.warning(f"Peer client {client_name} not found")
123121
raise exceptions.ClientNotRegisteredError(client_name)
124122
return self._peer_clients[client_name]
125123

@@ -160,7 +158,6 @@ def get_share(self, key_id: UUID) -> Share:
160158
try:
161159
share = self._shares[key_id]
162160
except KeyError as exc:
163-
LOGGER.warning(f"No share for key ID {key_id}")
164161
raise exceptions.UnknownKeyIDError(key_id) from exc
165162
return share
166163

hub/peer_client.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from common.allocation import Allocation
88
from common.block import Block
99
from common.exceptions import InvalidSignatureError, EncryptorNotConnectedToClientError
10-
from common.logging import LOGGER
1110
from common.owner import Owner
1211
from common.pool import Pool
1312
from common.signature import Signature
@@ -96,9 +95,6 @@ def check_sae_is_connected(self, sae_id: str):
9695
Check whether a SAE is connected to a client. If not, raise an exception.
9796
"""
9897
if sae_id not in self._encryptor_names:
99-
LOGGER.warning(
100-
f"Encryptor {sae_id} not connected to client {self._client_name}"
101-
)
10298
raise EncryptorNotConnectedToClientError(self._client_name, sae_id)
10399

104100
async def check_request_signature(self, raw_request: fastapi.Request):
@@ -119,9 +115,6 @@ async def check_request_signature(self, raw_request: fastapi.Request):
119115
# back to the pool. This is to prevent an attacker exhausting the pool (denial of
120116
# service) by sending lots of badly signed messages.
121117
allocation.give_back()
122-
LOGGER.warning(
123-
f"Invalid signature received from peer client '{self._client_name}'"
124-
)
125118
raise InvalidSignatureError()
126119

127120
def delete_fully_used_blocks(self) -> None:

0 commit comments

Comments
 (0)