Skip to content

Commit 67398db

Browse files
CopilottheCyberTech
andcommitted
Add info-level logging for encryption start and complete indications
Co-authored-by: theCyberTech <84775494+theCyberTech@users.noreply.github.com>
1 parent 203d5fd commit 67398db

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/crewai/security/encrypted_communication.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ def encrypt_message(
119119
ValueError: If encryption fails
120120
"""
121121
try:
122+
logger.info(f"Starting encryption for {message_type} message to recipient {recipient_fingerprint.uuid_str[:8]}...")
123+
122124
# Convert message to JSON string if it's a dict
123125
if isinstance(message, dict):
124126
message_str = json.dumps(message)
@@ -135,6 +137,7 @@ def encrypt_message(
135137
encrypted_bytes = fernet.encrypt(message_str.encode('utf-8'))
136138
encrypted_payload = encrypted_bytes.decode('utf-8')
137139

140+
logger.info(f"Successfully encrypted {message_type} message from {self.agent_fingerprint.uuid_str[:8]}... to {recipient_fingerprint.uuid_str[:8]}...")
138141
logger.debug(f"Encrypted message from {self.agent_fingerprint.uuid_str[:8]}... to {recipient_fingerprint.uuid_str[:8]}...")
139142

140143
return EncryptedMessage(
@@ -162,6 +165,8 @@ def decrypt_message(self, encrypted_message: EncryptedMessage) -> Union[str, Dic
162165
ValueError: If decryption fails or message is not for this agent
163166
"""
164167
try:
168+
logger.info(f"Starting decryption of {encrypted_message.message_type} message from sender {encrypted_message.sender_fingerprint[:8]}...")
169+
165170
# Verify this message is intended for this agent
166171
if encrypted_message.recipient_fingerprint != self.agent_fingerprint.uuid_str:
167172
raise ValueError(f"Message not intended for this agent. Expected {self.agent_fingerprint.uuid_str[:8]}..., got {encrypted_message.recipient_fingerprint[:8]}...")
@@ -178,9 +183,13 @@ def decrypt_message(self, encrypted_message: EncryptedMessage) -> Union[str, Dic
178183

179184
# Try to parse as JSON, fallback to string
180185
try:
181-
return json.loads(decrypted_str)
186+
decrypted_content = json.loads(decrypted_str)
182187
except json.JSONDecodeError:
183-
return decrypted_str
188+
decrypted_content = decrypted_str
189+
190+
logger.info(f"Successfully decrypted {encrypted_message.message_type} message from {encrypted_message.sender_fingerprint[:8]}... to {encrypted_message.recipient_fingerprint[:8]}...")
191+
192+
return decrypted_content
184193

185194
except Exception as e:
186195
logger.error(f"Failed to decrypt message: {e}")

src/crewai/tools/agent_tools/base_agent_tools.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ def _prepare_communication_payload(
8383
encryption_handler = self._get_encryption_handler(sender_agent)
8484
if encryption_handler and hasattr(recipient_agent, 'security_config') and recipient_agent.security_config:
8585
try:
86+
logger.info(f"Starting encrypted communication from '{sender_agent.role}' to '{recipient_agent.role}'")
8687
# Encrypt the message for the recipient
8788
encrypted_msg = encryption_handler.encrypt_message(
8889
message_payload,
8990
recipient_agent.security_config.fingerprint,
9091
message_type="agent_communication"
9192
)
93+
logger.info(f"Encrypted communication established between '{sender_agent.role}' and '{recipient_agent.role}'")
9294
logger.debug(f"Encrypted communication from {sender_agent.role} to {recipient_agent.role}")
9395
return encrypted_msg
9496
except Exception as e:
@@ -118,11 +120,13 @@ def _process_received_communication(
118120
encryption_handler = self._get_encryption_handler(recipient_agent)
119121
if encryption_handler:
120122
try:
123+
logger.info(f"Starting decryption of received communication for '{recipient_agent.role}'")
121124
# Convert dict to EncryptedMessage if needed
122125
if isinstance(message, dict):
123126
message = EncryptedMessage(**message)
124127

125128
decrypted = encryption_handler.decrypt_message(message)
129+
logger.info(f"Successfully decrypted communication for '{recipient_agent.role}'")
126130
logger.debug(f"Decrypted communication for {recipient_agent.role}")
127131
return decrypted
128132
except Exception as e:
@@ -253,6 +257,7 @@ def _execute(
253257

254258
# Execute with processed communication context
255259
if isinstance(communication_payload, EncryptedMessage):
260+
logger.info(f"Executing encrypted communication task for agent '{self.sanitize_agent_name(target_agent.role)}'")
256261
logger.debug(f"Executing encrypted communication task for agent '{self.sanitize_agent_name(target_agent.role)}'")
257262
# For encrypted messages, pass the encrypted payload as additional context
258263
# The target agent will need to handle decryption during execution
@@ -261,6 +266,7 @@ def _execute(
261266
enhanced_context += f"\nADDITIONAL_CONTEXT: {context}"
262267
result = target_agent.execute_task(task_with_assigned_agent, enhanced_context)
263268
else:
269+
logger.info(f"Executing plain communication task for agent '{self.sanitize_agent_name(target_agent.role)}'")
264270
logger.debug(f"Executing plain communication task for agent '{self.sanitize_agent_name(target_agent.role)}'")
265271
result = target_agent.execute_task(task_with_assigned_agent, context)
266272

0 commit comments

Comments
 (0)