Skip to content

Commit

Permalink
Merge pull request #2910 from ff137/fix/find_oob_target
Browse files Browse the repository at this point in the history
🐛 Fix ServiceDecorator parsing in oob record handling
  • Loading branch information
dbluhm authored Apr 24, 2024
2 parents ba127fe + 594f5b4 commit c2b4703
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 43 deletions.
25 changes: 9 additions & 16 deletions aries_cloudagent/core/oob_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import json
import logging
from typing import Any, Callable, Dict, List, Optional, cast
from typing import Any, Callable, Dict, List, Optional

from ..messaging.agent_message import AgentMessage
from ..connections.models.conn_record import ConnRecord
Expand Down Expand Up @@ -91,16 +91,18 @@ async def find_oob_target_for_outbound_message(
oob_record.their_service,
)

their_service = ServiceDecorator.deserialize(oob_record.their_service)
their_service = oob_record.their_service
if not their_service:
raise OobMessageProcessorError("Could not determine their service")

# Attach ~service decorator so other message can respond
message = json.loads(outbound_message.payload)
if not message.get("~service"):
if not message.get("~service") and oob_record.our_service:
LOGGER.debug(
"Setting our service on the message ~service %s",
oob_record.our_service,
)
message["~service"] = oob_record.our_service
message["~service"] = oob_record.our_service.serialize()

message["~thread"] = {
**message.get("~thread", {}),
Expand Down Expand Up @@ -256,24 +258,15 @@ async def find_oob_record_for_inbound_message(
)
return None

their_service = (
cast(
ServiceDecorator,
ServiceDecorator.deserialize(oob_record.their_service),
)
if oob_record.their_service
else None
)

# Verify the sender key is present in their service in our record
# If we don't have the sender verkey stored yet we can allow any key
if their_service and (
if oob_record.their_service and (
(
context.message_receipt.recipient_verkey
and (
not context.message_receipt.sender_verkey
or context.message_receipt.sender_verkey
not in their_service.recipient_keys
not in oob_record.their_service.recipient_keys
)
)
):
Expand Down Expand Up @@ -357,7 +350,7 @@ async def handle_message(
LOGGER.debug(
"Storing their service in oob record %s", their_service
)
oob_record.their_service = their_service.serialize()
oob_record.their_service = their_service

await oob_record.save(session)

Expand Down
54 changes: 27 additions & 27 deletions aries_cloudagent/core/tests/test_oob_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ async def asyncSetUp(self):
self.oob_record = mock.MagicMock(
connection_id="a-connection-id",
attach_thread_id="the-thid",
their_service={
"recipientKeys": ["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"],
"routingKeys": ["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"],
"serviceEndpoint": "http://their-service-endpoint.com",
},
their_service=ServiceDecorator(
recipient_keys=["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"],
routing_keys=["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"],
endpoint="http://their-service-endpoint.com",
),
emit_event=mock.CoroutineMock(),
delete_record=mock.CoroutineMock(),
save=mock.CoroutineMock(),
Expand Down Expand Up @@ -121,16 +121,16 @@ async def test_find_oob_target_for_outbound_message(self):
invitation=mock.MagicMock(requests_attach=[]),
invi_msg_id="the-pthid",
our_recipient_key="3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx",
their_service={
"recipientKeys": ["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"],
"serviceEndpoint": "http://their-service-endpoint.com",
"routingKeys": ["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"],
},
our_service={
"recipientKeys": ["3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"],
"serviceEndpoint": "http://our-service-endpoint.com",
"routingKeys": [],
},
their_service=ServiceDecorator(
recipient_keys=["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"],
endpoint="http://their-service-endpoint.com",
routing_keys=["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"],
),
our_service=ServiceDecorator(
recipient_keys=["3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"],
endpoint="http://our-service-endpoint.com",
routing_keys=[],
),
)

message = json.dumps({"~thread": {"thid": "the-thid"}})
Expand Down Expand Up @@ -196,16 +196,16 @@ async def test_find_oob_target_for_outbound_message_update_service_thread(self):
invitation=mock.MagicMock(requests_attach=[]),
invi_msg_id="the-pthid",
our_recipient_key="3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx",
their_service={
"recipientKeys": ["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"],
"serviceEndpoint": "http://their-service-endpoint.com",
"routingKeys": ["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"],
},
our_service={
"recipientKeys": ["3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"],
"serviceEndpoint": "http://our-service-endpoint.com",
"routingKeys": [],
},
their_service=ServiceDecorator(
recipient_keys=["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"],
endpoint="http://their-service-endpoint.com",
routing_keys=["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"],
),
our_service=ServiceDecorator(
recipient_keys=["3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"],
endpoint="http://our-service-endpoint.com",
routing_keys=[],
),
)

with mock.patch.object(
Expand Down Expand Up @@ -303,7 +303,7 @@ async def test_find_oob_record_for_inbound_message_connectionless_retrieve_oob(
self.context.message_receipt = MessageReceipt(
thread_id="the-thid",
recipient_verkey="our-recipient-key",
sender_verkey=self.oob_record.their_service["recipientKeys"][0],
sender_verkey=self.oob_record.their_service.recipient_keys[0],
)

assert await self.oob_processor.find_oob_record_for_inbound_message(
Expand Down Expand Up @@ -728,7 +728,7 @@ async def test_handle_message_connectionless(self):
)

assert oob_record.attach_thread_id == "4a580490-a9d8-44f5-a3f6-14e0b8a219b0"
assert oob_record.their_service == {
assert oob_record.their_service.serialize() == {
"serviceEndpoint": "http://their-service-endpoint.com",
"recipientKeys": ["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"],
"routingKeys": ["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"],
Expand Down

0 comments on commit c2b4703

Please sign in to comment.