Skip to content

Commit 98cfa03

Browse files
Merge branch 'hyperledger:main' into fix/conn-id-in-keylist-webhook
2 parents 2e8b9b8 + 2637941 commit 98cfa03

18 files changed

Lines changed: 178 additions & 69 deletions

aries_cloudagent/messaging/valid.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,29 @@ def __init__(self):
349349
)
350350

351351

352+
class RoutingKey(Regexp):
353+
"""
354+
Validate between indy or did key.
355+
356+
Validate value against indy (Ed25519VerificationKey2018)
357+
raw public key or DID key specification.
358+
"""
359+
360+
EXAMPLE = DIDKey.EXAMPLE
361+
PATTERN = re.compile(DIDKey.PATTERN.pattern + "|" + IndyRawPublicKey.PATTERN)
362+
363+
def __init__(self):
364+
"""Initializer."""
365+
366+
super().__init__(
367+
RoutingKey.PATTERN,
368+
error=(
369+
"Value {input} is not in W3C did:key"
370+
" or Ed25519VerificationKey2018 key format"
371+
),
372+
)
373+
374+
352375
class IndyCredDefId(Regexp):
353376
"""Validate value against indy credential definition identifier specification."""
354377

@@ -788,6 +811,7 @@ def __init__(
788811
JWT = {"validate": JSONWebToken(), "example": JSONWebToken.EXAMPLE}
789812
DID_KEY = {"validate": DIDKey(), "example": DIDKey.EXAMPLE}
790813
DID_POSTURE = {"validate": DIDPosture(), "example": DIDPosture.EXAMPLE}
814+
ROUTING_KEY = {"validate": RoutingKey(), "example": RoutingKey.EXAMPLE}
791815
INDY_DID = {"validate": IndyDID(), "example": IndyDID.EXAMPLE}
792816
GENERIC_DID = {"validate": MaybeIndyDID(), "example": MaybeIndyDID.EXAMPLE}
793817
INDY_RAW_PUBLIC_KEY = {

aries_cloudagent/multitenant/tests/test_route_manager.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
from ...storage.error import StorageNotFoundError
1414
from ..route_manager import MultitenantRouteManager
1515

16+
TEST_RECORD_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"
17+
TEST_VERKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL"
18+
TEST_ROUTE_RECORD_VERKEY = "9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"
19+
TEST_ROUTE_VERKEY = "did:key:z6MknxTj6Zj1VrDWc1ofaZtmCVv2zNXpD58Xup4ijDGoQhya"
20+
1621

1722
@pytest.fixture
1823
def wallet_id():
@@ -68,18 +73,18 @@ async def test_route_for_key_sub_mediator_no_base_mediator(
6873
) as mock_create_route_record:
6974
keylist_update = await route_manager._route_for_key(
7075
sub_profile,
71-
"test-recipient-key",
76+
TEST_VERKEY,
7277
mediation_record,
7378
skip_if_exists=False,
7479
replace_key=None,
7580
)
7681

7782
mock_create_route_record.assert_called_once_with(
78-
recipient_key="test-recipient-key", internal_wallet_id=wallet_id
83+
recipient_key=TEST_VERKEY, internal_wallet_id=wallet_id
7984
)
8085
assert keylist_update
8186
assert keylist_update.serialize()["updates"] == [
82-
{"action": "add", "recipient_key": "test-recipient-key"}
87+
{"action": "add", "recipient_key": TEST_VERKEY}
8388
]
8489
assert mock_responder.messages
8590
assert (
@@ -112,18 +117,18 @@ async def test_route_for_key_sub_mediator_and_base_mediator(
112117
) as mock_create_route_record:
113118
keylist_update = await route_manager._route_for_key(
114119
sub_profile,
115-
"test-recipient-key",
120+
TEST_VERKEY,
116121
mediation_record,
117122
skip_if_exists=False,
118123
replace_key=None,
119124
)
120125

121126
mock_create_route_record.assert_called_once_with(
122-
recipient_key="test-recipient-key", internal_wallet_id=wallet_id
127+
recipient_key=TEST_VERKEY, internal_wallet_id=wallet_id
123128
)
124129
assert keylist_update
125130
assert keylist_update.serialize()["updates"] == [
126-
{"action": "add", "recipient_key": "test-recipient-key"}
131+
{"action": "add", "recipient_key": TEST_VERKEY}
127132
]
128133
assert mock_responder.messages
129134
assert (
@@ -153,18 +158,18 @@ async def test_route_for_key_base_mediator_no_sub_mediator(
153158
) as mock_create_route_record:
154159
keylist_update = await route_manager._route_for_key(
155160
sub_profile,
156-
"test-recipient-key",
161+
TEST_VERKEY,
157162
None,
158163
skip_if_exists=False,
159164
replace_key=None,
160165
)
161166

162167
mock_create_route_record.assert_called_once_with(
163-
recipient_key="test-recipient-key", internal_wallet_id=wallet_id
168+
recipient_key=TEST_VERKEY, internal_wallet_id=wallet_id
164169
)
165170
assert keylist_update
166171
assert keylist_update.serialize()["updates"] == [
167-
{"action": "add", "recipient_key": "test-recipient-key"}
172+
{"action": "add", "recipient_key": TEST_VERKEY}
168173
]
169174
assert mock_responder.messages
170175
assert (
@@ -187,7 +192,7 @@ async def test_route_for_key_skip_if_exists_and_exists(
187192
):
188193
keylist_update = await route_manager._route_for_key(
189194
sub_profile,
190-
"test-recipient-key",
195+
TEST_VERKEY,
191196
mediation_record,
192197
skip_if_exists=True,
193198
replace_key=None,
@@ -212,14 +217,14 @@ async def test_route_for_key_skip_if_exists_and_absent(
212217
):
213218
keylist_update = await route_manager._route_for_key(
214219
sub_profile,
215-
"test-recipient-key",
220+
TEST_VERKEY,
216221
mediation_record,
217222
skip_if_exists=True,
218223
replace_key=None,
219224
)
220225
assert keylist_update
221226
assert keylist_update.serialize()["updates"] == [
222-
{"action": "add", "recipient_key": "test-recipient-key"}
227+
{"action": "add", "recipient_key": TEST_VERKEY}
223228
]
224229
assert mock_responder.messages
225230
assert (
@@ -239,15 +244,15 @@ async def test_route_for_key_replace_key(
239244
)
240245
keylist_update = await route_manager._route_for_key(
241246
sub_profile,
242-
"test-recipient-key",
247+
TEST_VERKEY,
243248
mediation_record,
244249
skip_if_exists=False,
245-
replace_key="test-replace-key",
250+
replace_key=TEST_ROUTE_VERKEY,
246251
)
247252
assert keylist_update
248253
assert keylist_update.serialize()["updates"] == [
249-
{"action": "add", "recipient_key": "test-recipient-key"},
250-
{"action": "remove", "recipient_key": "test-replace-key"},
254+
{"action": "add", "recipient_key": TEST_VERKEY},
255+
{"action": "remove", "recipient_key": TEST_ROUTE_VERKEY},
251256
]
252257
assert mock_responder.messages
253258
assert (
@@ -264,10 +269,10 @@ async def test_route_for_key_no_mediator(
264269
assert (
265270
await route_manager._route_for_key(
266271
sub_profile,
267-
"test-recipient-key",
272+
TEST_VERKEY,
268273
None,
269274
skip_if_exists=True,
270-
replace_key="test-replace-key",
275+
replace_key=TEST_ROUTE_VERKEY,
271276
)
272277
is None
273278
)

aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_keylist_query_handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
TEST_CONN_ID = "conn-id"
2020
TEST_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"
21+
TEST_VERKEY_DIDKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL"
2122

2223

2324
class TestKeylistQueryHandler(AsyncTestCase):
@@ -77,4 +78,4 @@ async def test_handler(self):
7778
result, _target = responder.messages[0]
7879
assert isinstance(result, Keylist)
7980
assert len(result.keys) == 1
80-
assert result.keys[0].recipient_key == TEST_VERKEY
81+
assert result.keys[0].recipient_key == TEST_VERKEY_DIDKEY

aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_mediation_grant_handler.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from .. import mediation_grant_handler as test_module
1919

2020
TEST_CONN_ID = "conn-id"
21-
TEST_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"
21+
TEST_RECORD_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"
22+
TEST_VERKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL"
2223
TEST_ENDPOINT = "https://example.com"
2324

2425

@@ -58,7 +59,7 @@ async def test_handler(self):
5859
assert record
5960
assert record.state == MediationRecord.STATE_GRANTED
6061
assert record.endpoint == TEST_ENDPOINT
61-
assert record.routing_keys == [TEST_VERKEY]
62+
assert record.routing_keys == [TEST_RECORD_VERKEY]
6263

6364
async def test_handler_connection_has_set_to_default_meta(self):
6465
handler, responder = MediationGrantHandler(), MockResponder()

aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .messages.mediate_grant import MediationGrant
3030
from .messages.mediate_request import MediationRequest
3131
from .models.mediation_record import MediationRecord
32+
from .normalization import normalize_from_did_key
3233

3334
LOGGER = logging.getLogger(__name__)
3435

@@ -249,8 +250,9 @@ async def update_keylist(
249250
}
250251

251252
def rule_to_update(rule: KeylistUpdateRule):
253+
recipient_key = normalize_from_did_key(rule.recipient_key)
252254
return RouteUpdate(
253-
recipient_key=rule.recipient_key, action=action_map[rule.action]
255+
recipient_key=recipient_key, action=action_map[rule.action]
254256
)
255257

256258
def updated_to_keylist_updated(updated: RouteUpdated):
@@ -445,7 +447,11 @@ async def request_granted(self, record: MediationRecord, grant: MediationGrant):
445447
"""
446448
record.state = MediationRecord.STATE_GRANTED
447449
record.endpoint = grant.endpoint
448-
record.routing_keys = grant.routing_keys
450+
# record.routing_keys = grant.routing_keys
451+
routing_keys = []
452+
for key in grant.routing_keys:
453+
routing_keys.append(normalize_from_did_key(key))
454+
record.routing_keys = routing_keys
449455
async with self._profile.session() as session:
450456
await record.save(session, reason="Mediation request granted.")
451457

aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from marshmallow import EXCLUDE, fields
44

55
from ......messaging.models.base import BaseModel, BaseModelSchema
6-
from ......messaging.valid import INDY_RAW_PUBLIC_KEY
6+
from ......messaging.valid import DID_KEY
7+
from ...normalization import normalize_from_public_key
78

89

910
class KeylistKey(BaseModel):
@@ -32,7 +33,7 @@ def __init__(
3233
3334
"""
3435
super().__init__(**kwargs)
35-
self.recipient_key = recipient_key
36+
self.recipient_key = normalize_from_public_key(recipient_key)
3637

3738

3839
class KeylistKeySchema(BaseModelSchema):
@@ -44,4 +45,4 @@ class Meta:
4445
model_class = KeylistKey
4546
unknown = EXCLUDE
4647

47-
recipient_key = fields.Str(required=True, **INDY_RAW_PUBLIC_KEY)
48+
recipient_key = fields.Str(required=True, **DID_KEY)

aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from marshmallow.validate import OneOf
99

1010
from ......messaging.models.base import BaseModel, BaseModelSchema
11-
from ......messaging.valid import INDY_RAW_PUBLIC_KEY
11+
from ......messaging.valid import ROUTING_KEY
12+
from ...normalization import normalize_from_public_key
1213

1314

1415
class KeylistUpdateRule(BaseModel):
@@ -32,7 +33,7 @@ def __init__(self, recipient_key: str, action: str, **kwargs):
3233
3334
"""
3435
super().__init__(**kwargs)
35-
self.recipient_key = recipient_key
36+
self.recipient_key = normalize_from_public_key(recipient_key)
3637
self.action = action
3738

3839

@@ -45,7 +46,7 @@ class Meta:
4546
model_class = KeylistUpdateRule
4647

4748
recipient_key = fields.Str(
48-
description="Key to remove or add", required=True, **INDY_RAW_PUBLIC_KEY
49+
description="Key to remove or add", required=True, **ROUTING_KEY
4950
)
5051
action = fields.Str(
5152
required=True,

aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from marshmallow import EXCLUDE, fields
77

88
from ......messaging.models.base import BaseModel, BaseModelSchema
9-
from ......messaging.valid import INDY_RAW_PUBLIC_KEY
9+
from ......messaging.valid import DID_KEY
10+
from ...normalization import normalize_from_public_key
1011

1112

1213
class KeylistUpdated(BaseModel):
@@ -40,7 +41,7 @@ def __init__(
4041
4142
"""
4243
super().__init__(**kwargs)
43-
self.recipient_key = recipient_key
44+
self.recipient_key = normalize_from_public_key(recipient_key)
4445
self.action = action
4546
self.result = result
4647

@@ -54,6 +55,6 @@ class Meta:
5455
model_class = KeylistUpdated
5556
unknown = EXCLUDE
5657

57-
recipient_key = fields.Str(required=True, **INDY_RAW_PUBLIC_KEY)
58+
recipient_key = fields.Str(required=True, **DID_KEY)
5859
action = fields.Str(required=True)
5960
result = fields.Str(required=True)

aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from .....messaging.agent_message import AgentMessage, AgentMessageSchema
1111
from ..message_types import MEDIATE_GRANT, PROTOCOL_PACKAGE
12+
from ..normalization import normalize_from_public_key
1213

1314
HANDLER_CLASS = (
1415
f"{PROTOCOL_PACKAGE}.handlers.mediation_grant_handler.MediationGrantHandler"
@@ -41,7 +42,11 @@ def __init__(
4142
"""
4243
super(MediationGrant, self).__init__(**kwargs)
4344
self.endpoint = endpoint
44-
self.routing_keys = list(routing_keys) if routing_keys else []
45+
self.routing_keys = (
46+
list(normalize_from_public_key(key) for key in routing_keys)
47+
if routing_keys
48+
else []
49+
)
4550

4651

4752
class MediationGrantSchema(AgentMessageSchema):

aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TestKeylist(MessageTest, TestCase):
1818
"pagination": KeylistQueryPaginate(10, 10),
1919
"keys": [
2020
KeylistKey(
21-
recipient_key="3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx",
21+
recipient_key="did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL",
2222
action="added",
2323
result="success",
2424
)

0 commit comments

Comments
 (0)