Island: Add docstrings to agent event encryption

This commit is contained in:
Kekoa Kaaikala 2022-09-16 14:00:14 +00:00
parent c19e50b7f1
commit 71110c61a7
1 changed files with 23 additions and 0 deletions

View File

@ -8,6 +8,9 @@ ENCRYPTED_PREFIX = "encrypted_"
def get_fields_to_encrypt(event: AbstractAgentEvent):
"""
Get the fields of the event that are not part of the base AbstractAgentEvent.
"""
return set(vars(AbstractAgentEvent)["__fields__"].keys()) ^ set(event.dict().keys())
@ -16,6 +19,18 @@ def encrypt_event(
event_data: JSONSerializable,
fields: Iterable[str] = [],
) -> JSONSerializable:
"""
Encrypt a serialized AbstractAgentEvent
The data is expected to be a dict. The encrypted fields will be given the
prefix "encrypted_".
:param encrypt: Callable used to encrypt data
:param event_data: Serialized event to encrypt
:param fields: Fields to encrypt
:return: Serialized event with the fields encrypted
:raises TypeError: If the serialized data is not a dict
"""
if not isinstance(event_data, dict):
raise TypeError("Event encryption only supported for dict")
@ -31,6 +46,14 @@ def encrypt_event(
def decrypt_event(
decrypt: Callable[[bytes], bytes], event_data: JSONSerializable
) -> JSONSerializable:
"""
Decrypt a serialized AbstractEventData
:param encrypt: Callable used to decrypt data
:param event_data: Serialized event to decrypt
:return: Serialized event with the fields decrypted
:raises TypeError: If the serialized data is not a dict
"""
if not isinstance(event_data, dict):
raise TypeError("Event decryption only supported for dict")