Skip to content

Commit

Permalink
Merge pull request #1886 from frostyfrog/fix/RFC-0211-Compliance
Browse files Browse the repository at this point in the history
Use did:key for recipient keys
  • Loading branch information
swcurran authored Aug 25, 2022
2 parents a8101e1 + 465a09b commit 2637941
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 69 deletions.
24 changes: 24 additions & 0 deletions aries_cloudagent/messaging/valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,29 @@ def __init__(self):
)


class RoutingKey(Regexp):
"""
Validate between indy or did key.
Validate value against indy (Ed25519VerificationKey2018)
raw public key or DID key specification.
"""

EXAMPLE = DIDKey.EXAMPLE
PATTERN = re.compile(DIDKey.PATTERN.pattern + "|" + IndyRawPublicKey.PATTERN)

def __init__(self):
"""Initializer."""

super().__init__(
RoutingKey.PATTERN,
error=(
"Value {input} is not in W3C did:key"
" or Ed25519VerificationKey2018 key format"
),
)


class IndyCredDefId(Regexp):
"""Validate value against indy credential definition identifier specification."""

Expand Down Expand Up @@ -788,6 +811,7 @@ def __init__(
JWT = {"validate": JSONWebToken(), "example": JSONWebToken.EXAMPLE}
DID_KEY = {"validate": DIDKey(), "example": DIDKey.EXAMPLE}
DID_POSTURE = {"validate": DIDPosture(), "example": DIDPosture.EXAMPLE}
ROUTING_KEY = {"validate": RoutingKey(), "example": RoutingKey.EXAMPLE}
INDY_DID = {"validate": IndyDID(), "example": IndyDID.EXAMPLE}
GENERIC_DID = {"validate": MaybeIndyDID(), "example": MaybeIndyDID.EXAMPLE}
INDY_RAW_PUBLIC_KEY = {
Expand Down
41 changes: 23 additions & 18 deletions aries_cloudagent/multitenant/tests/test_route_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
from ...storage.error import StorageNotFoundError
from ..route_manager import MultitenantRouteManager

TEST_RECORD_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"
TEST_VERKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL"
TEST_ROUTE_RECORD_VERKEY = "9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"
TEST_ROUTE_VERKEY = "did:key:z6MknxTj6Zj1VrDWc1ofaZtmCVv2zNXpD58Xup4ijDGoQhya"


@pytest.fixture
def wallet_id():
Expand Down Expand Up @@ -68,18 +73,18 @@ async def test_route_for_key_sub_mediator_no_base_mediator(
) as mock_create_route_record:
keylist_update = await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
TEST_VERKEY,
mediation_record,
skip_if_exists=False,
replace_key=None,
)

mock_create_route_record.assert_called_once_with(
recipient_key="test-recipient-key", internal_wallet_id=wallet_id
recipient_key=TEST_VERKEY, internal_wallet_id=wallet_id
)
assert keylist_update
assert keylist_update.serialize()["updates"] == [
{"action": "add", "recipient_key": "test-recipient-key"}
{"action": "add", "recipient_key": TEST_VERKEY}
]
assert mock_responder.messages
assert (
Expand Down Expand Up @@ -112,18 +117,18 @@ async def test_route_for_key_sub_mediator_and_base_mediator(
) as mock_create_route_record:
keylist_update = await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
TEST_VERKEY,
mediation_record,
skip_if_exists=False,
replace_key=None,
)

mock_create_route_record.assert_called_once_with(
recipient_key="test-recipient-key", internal_wallet_id=wallet_id
recipient_key=TEST_VERKEY, internal_wallet_id=wallet_id
)
assert keylist_update
assert keylist_update.serialize()["updates"] == [
{"action": "add", "recipient_key": "test-recipient-key"}
{"action": "add", "recipient_key": TEST_VERKEY}
]
assert mock_responder.messages
assert (
Expand Down Expand Up @@ -153,18 +158,18 @@ async def test_route_for_key_base_mediator_no_sub_mediator(
) as mock_create_route_record:
keylist_update = await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
TEST_VERKEY,
None,
skip_if_exists=False,
replace_key=None,
)

mock_create_route_record.assert_called_once_with(
recipient_key="test-recipient-key", internal_wallet_id=wallet_id
recipient_key=TEST_VERKEY, internal_wallet_id=wallet_id
)
assert keylist_update
assert keylist_update.serialize()["updates"] == [
{"action": "add", "recipient_key": "test-recipient-key"}
{"action": "add", "recipient_key": TEST_VERKEY}
]
assert mock_responder.messages
assert (
Expand All @@ -187,7 +192,7 @@ async def test_route_for_key_skip_if_exists_and_exists(
):
keylist_update = await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
TEST_VERKEY,
mediation_record,
skip_if_exists=True,
replace_key=None,
Expand All @@ -212,14 +217,14 @@ async def test_route_for_key_skip_if_exists_and_absent(
):
keylist_update = await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
TEST_VERKEY,
mediation_record,
skip_if_exists=True,
replace_key=None,
)
assert keylist_update
assert keylist_update.serialize()["updates"] == [
{"action": "add", "recipient_key": "test-recipient-key"}
{"action": "add", "recipient_key": TEST_VERKEY}
]
assert mock_responder.messages
assert (
Expand All @@ -239,15 +244,15 @@ async def test_route_for_key_replace_key(
)
keylist_update = await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
TEST_VERKEY,
mediation_record,
skip_if_exists=False,
replace_key="test-replace-key",
replace_key=TEST_ROUTE_VERKEY,
)
assert keylist_update
assert keylist_update.serialize()["updates"] == [
{"action": "add", "recipient_key": "test-recipient-key"},
{"action": "remove", "recipient_key": "test-replace-key"},
{"action": "add", "recipient_key": TEST_VERKEY},
{"action": "remove", "recipient_key": TEST_ROUTE_VERKEY},
]
assert mock_responder.messages
assert (
Expand All @@ -264,10 +269,10 @@ async def test_route_for_key_no_mediator(
assert (
await route_manager._route_for_key(
sub_profile,
"test-recipient-key",
TEST_VERKEY,
None,
skip_if_exists=True,
replace_key="test-replace-key",
replace_key=TEST_ROUTE_VERKEY,
)
is None
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

TEST_CONN_ID = "conn-id"
TEST_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"
TEST_VERKEY_DIDKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL"


class TestKeylistQueryHandler(AsyncTestCase):
Expand Down Expand Up @@ -77,4 +78,4 @@ async def test_handler(self):
result, _target = responder.messages[0]
assert isinstance(result, Keylist)
assert len(result.keys) == 1
assert result.keys[0].recipient_key == TEST_VERKEY
assert result.keys[0].recipient_key == TEST_VERKEY_DIDKEY
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from .. import mediation_grant_handler as test_module

TEST_CONN_ID = "conn-id"
TEST_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"
TEST_RECORD_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"
TEST_VERKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL"
TEST_ENDPOINT = "https://example.com"


Expand Down Expand Up @@ -58,7 +59,7 @@ async def test_handler(self):
assert record
assert record.state == MediationRecord.STATE_GRANTED
assert record.endpoint == TEST_ENDPOINT
assert record.routing_keys == [TEST_VERKEY]
assert record.routing_keys == [TEST_RECORD_VERKEY]

async def test_handler_connection_has_set_to_default_meta(self):
handler, responder = MediationGrantHandler(), MockResponder()
Expand Down
10 changes: 8 additions & 2 deletions aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .messages.mediate_grant import MediationGrant
from .messages.mediate_request import MediationRequest
from .models.mediation_record import MediationRecord
from .normalization import normalize_from_did_key

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -249,8 +250,9 @@ async def update_keylist(
}

def rule_to_update(rule: KeylistUpdateRule):
recipient_key = normalize_from_did_key(rule.recipient_key)
return RouteUpdate(
recipient_key=rule.recipient_key, action=action_map[rule.action]
recipient_key=recipient_key, action=action_map[rule.action]
)

def updated_to_keylist_updated(updated: RouteUpdated):
Expand Down Expand Up @@ -445,7 +447,11 @@ async def request_granted(self, record: MediationRecord, grant: MediationGrant):
"""
record.state = MediationRecord.STATE_GRANTED
record.endpoint = grant.endpoint
record.routing_keys = grant.routing_keys
# record.routing_keys = grant.routing_keys
routing_keys = []
for key in grant.routing_keys:
routing_keys.append(normalize_from_did_key(key))
record.routing_keys = routing_keys
async with self._profile.session() as session:
await record.save(session, reason="Mediation request granted.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from marshmallow import EXCLUDE, fields

from ......messaging.models.base import BaseModel, BaseModelSchema
from ......messaging.valid import INDY_RAW_PUBLIC_KEY
from ......messaging.valid import DID_KEY
from ...normalization import normalize_from_public_key


class KeylistKey(BaseModel):
Expand Down Expand Up @@ -32,7 +33,7 @@ def __init__(
"""
super().__init__(**kwargs)
self.recipient_key = recipient_key
self.recipient_key = normalize_from_public_key(recipient_key)


class KeylistKeySchema(BaseModelSchema):
Expand All @@ -44,4 +45,4 @@ class Meta:
model_class = KeylistKey
unknown = EXCLUDE

recipient_key = fields.Str(required=True, **INDY_RAW_PUBLIC_KEY)
recipient_key = fields.Str(required=True, **DID_KEY)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from marshmallow.validate import OneOf

from ......messaging.models.base import BaseModel, BaseModelSchema
from ......messaging.valid import INDY_RAW_PUBLIC_KEY
from ......messaging.valid import ROUTING_KEY
from ...normalization import normalize_from_public_key


class KeylistUpdateRule(BaseModel):
Expand All @@ -32,7 +33,7 @@ def __init__(self, recipient_key: str, action: str, **kwargs):
"""
super().__init__(**kwargs)
self.recipient_key = recipient_key
self.recipient_key = normalize_from_public_key(recipient_key)
self.action = action


Expand All @@ -45,7 +46,7 @@ class Meta:
model_class = KeylistUpdateRule

recipient_key = fields.Str(
description="Key to remove or add", required=True, **INDY_RAW_PUBLIC_KEY
description="Key to remove or add", required=True, **ROUTING_KEY
)
action = fields.Str(
required=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from marshmallow import EXCLUDE, fields

from ......messaging.models.base import BaseModel, BaseModelSchema
from ......messaging.valid import INDY_RAW_PUBLIC_KEY
from ......messaging.valid import DID_KEY
from ...normalization import normalize_from_public_key


class KeylistUpdated(BaseModel):
Expand Down Expand Up @@ -40,7 +41,7 @@ def __init__(
"""
super().__init__(**kwargs)
self.recipient_key = recipient_key
self.recipient_key = normalize_from_public_key(recipient_key)
self.action = action
self.result = result

Expand All @@ -54,6 +55,6 @@ class Meta:
model_class = KeylistUpdated
unknown = EXCLUDE

recipient_key = fields.Str(required=True, **INDY_RAW_PUBLIC_KEY)
recipient_key = fields.Str(required=True, **DID_KEY)
action = fields.Str(required=True)
result = fields.Str(required=True)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .....messaging.agent_message import AgentMessage, AgentMessageSchema
from ..message_types import MEDIATE_GRANT, PROTOCOL_PACKAGE
from ..normalization import normalize_from_public_key

HANDLER_CLASS = (
f"{PROTOCOL_PACKAGE}.handlers.mediation_grant_handler.MediationGrantHandler"
Expand Down Expand Up @@ -41,7 +42,11 @@ def __init__(
"""
super(MediationGrant, self).__init__(**kwargs)
self.endpoint = endpoint
self.routing_keys = list(routing_keys) if routing_keys else []
self.routing_keys = (
list(normalize_from_public_key(key) for key in routing_keys)
if routing_keys
else []
)


class MediationGrantSchema(AgentMessageSchema):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TestKeylist(MessageTest, TestCase):
"pagination": KeylistQueryPaginate(10, 10),
"keys": [
KeylistKey(
recipient_key="3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx",
recipient_key="did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL",
action="added",
result="success",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class TestKeylistUpdate(MessageTest, TestCase):
SCHEMA = KeylistUpdateSchema
VALUES = {
"updates": [
KeylistUpdateRule("3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx", "add")
KeylistUpdateRule(
"did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL", "add"
)
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestKeylistUpdateResponse(MessageTest, TestCase):
VALUES = {
"updated": [
KeylistUpdated(
recipient_key="3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx",
recipient_key="did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL",
action="added",
result="success",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ class TestMediateGrant(MessageTest, TestCase):
TYPE = MEDIATE_GRANT
CLASS = MediationGrant
SCHEMA = MediationGrantSchema
VALUES = {"endpoint": "http://localhost:3000", "routing_keys": ["test_routing_key"]}
VALUES = {
"endpoint": "http://localhost:3000",
"routing_keys": ["did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL"],
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .....core.profile import ProfileSession
from .....messaging.models.base_record import BaseRecord, BaseRecordSchema
from .....messaging.valid import INDY_RAW_PUBLIC_KEY
from .....messaging.valid import DID_KEY
from .....storage.base import StorageDuplicateError, StorageNotFoundError


Expand Down Expand Up @@ -172,5 +172,5 @@ class Meta:
connection_id = fields.Str(required=True)
mediator_terms = fields.List(fields.Str(), required=False)
recipient_terms = fields.List(fields.Str(), required=False)
routing_keys = fields.List(fields.Str(**INDY_RAW_PUBLIC_KEY), required=False)
routing_keys = fields.List(fields.Str(**DID_KEY), required=False)
endpoint = fields.Str(required=False)
Loading

0 comments on commit 2637941

Please sign in to comment.