Merge pull request #328 from NIC619/another_error_handling
Some minor error handlings
This commit is contained in:
commit
73251a0c36
|
@ -21,3 +21,7 @@ class MissingLengthException(MsgioException):
|
||||||
|
|
||||||
class MissingMessageException(MsgioException):
|
class MissingMessageException(MsgioException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DecryptionFailedException(MsgioException):
|
||||||
|
pass
|
||||||
|
|
|
@ -3,7 +3,7 @@ from typing import Any, Dict, List, Optional, Sequence
|
||||||
from multiaddr import Multiaddr
|
from multiaddr import Multiaddr
|
||||||
|
|
||||||
from .id import ID
|
from .id import ID
|
||||||
from .peerdata import PeerData
|
from .peerdata import PeerData, PeerDataError
|
||||||
from .peerinfo import PeerInfo
|
from .peerinfo import PeerInfo
|
||||||
from .peerstore_interface import IPeerStore
|
from .peerstore_interface import IPeerStore
|
||||||
|
|
||||||
|
@ -54,7 +54,10 @@ class PeerStore(IPeerStore):
|
||||||
|
|
||||||
def get(self, peer_id: ID, key: str) -> Any:
|
def get(self, peer_id: ID, key: str) -> Any:
|
||||||
if peer_id in self.peer_map:
|
if peer_id in self.peer_map:
|
||||||
|
try:
|
||||||
val = self.peer_map[peer_id].get_metadata(key)
|
val = self.peer_map[peer_id].get_metadata(key)
|
||||||
|
except PeerDataError as error:
|
||||||
|
raise PeerStoreError(error)
|
||||||
return val
|
return val
|
||||||
raise PeerStoreError("peer ID not found")
|
raise PeerStoreError("peer ID not found")
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
from libp2p.crypto.exceptions import MissingDeserializerError
|
||||||
from libp2p.crypto.keys import PrivateKey, PublicKey
|
from libp2p.crypto.keys import PrivateKey, PublicKey
|
||||||
from libp2p.crypto.pb import crypto_pb2
|
from libp2p.crypto.pb import crypto_pb2
|
||||||
from libp2p.crypto.serialization import deserialize_public_key
|
from libp2p.crypto.serialization import deserialize_public_key
|
||||||
|
@ -82,6 +83,8 @@ class InsecureSession(BaseSession):
|
||||||
raise HandshakeFailure(
|
raise HandshakeFailure(
|
||||||
f"unknown `key_type` of remote_msg.pubkey={remote_msg.pubkey}"
|
f"unknown `key_type` of remote_msg.pubkey={remote_msg.pubkey}"
|
||||||
)
|
)
|
||||||
|
except MissingDeserializerError as error:
|
||||||
|
raise HandshakeFailure(error)
|
||||||
peer_id_from_received_pubkey = ID.from_pubkey(received_pubkey)
|
peer_id_from_received_pubkey = ID.from_pubkey(received_pubkey)
|
||||||
if peer_id_from_received_pubkey != received_peer_id:
|
if peer_id_from_received_pubkey != received_peer_id:
|
||||||
raise HandshakeFailure(
|
raise HandshakeFailure(
|
||||||
|
|
|
@ -28,3 +28,7 @@ class IncompatibleChoices(SecioException):
|
||||||
|
|
||||||
class InconsistentNonce(SecioException):
|
class InconsistentNonce(SecioException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class SedesException(SecioException):
|
||||||
|
pass
|
||||||
|
|
|
@ -11,12 +11,14 @@ from libp2p.crypto.authenticated_encryption import (
|
||||||
from libp2p.crypto.authenticated_encryption import (
|
from libp2p.crypto.authenticated_encryption import (
|
||||||
initialize_pair as initialize_pair_for_encryption,
|
initialize_pair as initialize_pair_for_encryption,
|
||||||
)
|
)
|
||||||
|
from libp2p.crypto.authenticated_encryption import InvalidMACException
|
||||||
from libp2p.crypto.authenticated_encryption import MacAndCipher as Encrypter
|
from libp2p.crypto.authenticated_encryption import MacAndCipher as Encrypter
|
||||||
from libp2p.crypto.ecc import ECCPublicKey
|
from libp2p.crypto.ecc import ECCPublicKey
|
||||||
|
from libp2p.crypto.exceptions import MissingDeserializerError
|
||||||
from libp2p.crypto.key_exchange import create_ephemeral_key_pair
|
from libp2p.crypto.key_exchange import create_ephemeral_key_pair
|
||||||
from libp2p.crypto.keys import PrivateKey, PublicKey
|
from libp2p.crypto.keys import PrivateKey, PublicKey
|
||||||
from libp2p.crypto.serialization import deserialize_public_key
|
from libp2p.crypto.serialization import deserialize_public_key
|
||||||
from libp2p.io.exceptions import IOException
|
from libp2p.io.exceptions import DecryptionFailedException, IOException
|
||||||
from libp2p.io.msgio import MsgIOReadWriter
|
from libp2p.io.msgio import MsgIOReadWriter
|
||||||
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
||||||
from libp2p.peer.id import ID as PeerID
|
from libp2p.peer.id import ID as PeerID
|
||||||
|
@ -30,6 +32,7 @@ from .exceptions import (
|
||||||
InvalidSignatureOnExchange,
|
InvalidSignatureOnExchange,
|
||||||
PeerMismatchException,
|
PeerMismatchException,
|
||||||
SecioException,
|
SecioException,
|
||||||
|
SedesException,
|
||||||
SelfEncryption,
|
SelfEncryption,
|
||||||
)
|
)
|
||||||
from .pb.spipe_pb2 import Exchange, Propose
|
from .pb.spipe_pb2 import Exchange, Propose
|
||||||
|
@ -122,7 +125,11 @@ class SecureSession(BaseSession):
|
||||||
|
|
||||||
async def read_msg(self) -> bytes:
|
async def read_msg(self) -> bytes:
|
||||||
msg = await self.conn.read_msg()
|
msg = await self.conn.read_msg()
|
||||||
return self.remote_encrypter.decrypt_if_valid(msg)
|
try:
|
||||||
|
decrypted_msg = self.remote_encrypter.decrypt_if_valid(msg)
|
||||||
|
except InvalidMACException:
|
||||||
|
raise DecryptionFailedException
|
||||||
|
return decrypted_msg
|
||||||
|
|
||||||
async def write(self, data: bytes) -> int:
|
async def write(self, data: bytes) -> int:
|
||||||
await self.write_msg(data)
|
await self.write_msg(data)
|
||||||
|
@ -163,7 +170,10 @@ class Proposal:
|
||||||
|
|
||||||
nonce = protobuf.rand
|
nonce = protobuf.rand
|
||||||
public_key_protobuf_bytes = protobuf.public_key
|
public_key_protobuf_bytes = protobuf.public_key
|
||||||
|
try:
|
||||||
public_key = deserialize_public_key(public_key_protobuf_bytes)
|
public_key = deserialize_public_key(public_key_protobuf_bytes)
|
||||||
|
except MissingDeserializerError as error:
|
||||||
|
raise SedesException(error)
|
||||||
exchanges = protobuf.exchanges
|
exchanges = protobuf.exchanges
|
||||||
ciphers = protobuf.ciphers
|
ciphers = protobuf.ciphers
|
||||||
hashes = protobuf.hashes
|
hashes = protobuf.hashes
|
||||||
|
|
|
@ -146,7 +146,7 @@ class MplexStream(IMuxedStream):
|
||||||
:return: number of bytes written
|
:return: number of bytes written
|
||||||
"""
|
"""
|
||||||
if self.event_local_closed.is_set():
|
if self.event_local_closed.is_set():
|
||||||
raise MplexStreamClosed(f"cannot write to closed stream: data={data}")
|
raise MplexStreamClosed(f"cannot write to closed stream: data={data!r}")
|
||||||
flag = (
|
flag = (
|
||||||
HeaderTags.MessageInitiator
|
HeaderTags.MessageInitiator
|
||||||
if self.is_initiator
|
if self.is_initiator
|
||||||
|
|
|
@ -77,7 +77,7 @@ async def read_delim(reader: Reader) -> bytes:
|
||||||
raise ParseError(f"`len(msg_bytes)` should not be 0")
|
raise ParseError(f"`len(msg_bytes)` should not be 0")
|
||||||
if msg_bytes[-1:] != b"\n":
|
if msg_bytes[-1:] != b"\n":
|
||||||
raise ParseError(
|
raise ParseError(
|
||||||
f'`msg_bytes` is not delimited by b"\\n": `msg_bytes`={msg_bytes}'
|
f'`msg_bytes` is not delimited by b"\\n": `msg_bytes`={msg_bytes!r}'
|
||||||
)
|
)
|
||||||
return msg_bytes[:-1]
|
return msg_bytes[:-1]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user