Skip to content

Commit

Permalink
INDY-2341: support content field dynamic validation for CredDef
Browse files Browse the repository at this point in the history
Signed-off-by: ashcherbakov <[email protected]>
  • Loading branch information
ashcherbakov committed Mar 17, 2020
1 parent 02ffd7e commit 46d3e58
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from typing import Optional

from common.serializers.json_serializer import JsonSerializer
from indy_common.authorize.auth_request_validator import WriteRequestValidator

from indy_common.constants import RICH_SCHEMA_CRED_DEF, RS_CRED_DEF_SIG_TYPE, RS_CRED_DEF_MAPPING, \
RS_CRED_DEF_SCHEMA, RS_CRED_DEF_PUB_KEY
RS_CRED_DEF_SCHEMA, RS_CRED_DEF_PUB_KEY, RS_CONTENT, RS_TYPE, RS_SCHEMA_TYPE_VALUE, RS_MAPPING_TYPE_VALUE
from indy_common.types import Request

from indy_node.server.request_handlers.domain_req_handlers.rich_schema.abstract_rich_schema_object_handler import \
AbstractRichSchemaObjectHandler
Expand Down Expand Up @@ -32,3 +36,38 @@ def do_static_validation_content(self, content_as_dict, request):
missing_fields_str = ", ".join(missing_fields)
raise InvalidClientRequest(request.identifier, request.reqId,
"{} must be set".format(missing_fields_str))

def dynamic_validation(self, request: Request, req_pp_time: Optional[int]):
super().dynamic_validation(request, req_pp_time)

# iat has been checked on static validation step that the content is a valid JSON.
# and it has schema and mapping fields
content_as_dict = JsonSerializer.loads(request.operation[RS_CONTENT])
schema_id = content_as_dict[RS_CRED_DEF_SCHEMA]
mapping_id = content_as_dict[RS_CRED_DEF_MAPPING]

schema, _, _ = self.get_from_state(schema_id)
if not schema:
raise InvalidClientRequest(request.identifier,
request.reqId,
'Can not find a schema with id={}; please make sure that it has been added to the ledger'.format(
schema_id))

mapping, _, _ = self.get_from_state(mapping_id)
if not mapping:
raise InvalidClientRequest(request.identifier,
request.reqId,
'Can not find a mapping with id={}; please make sure that it has been added to the ledger'.format(
mapping_id))

if schema.get(RS_TYPE) != RS_SCHEMA_TYPE_VALUE:
raise InvalidClientRequest(request.identifier,
request.reqId,
"'{}' field must reference a schema with {}={}".format(
RS_CRED_DEF_SCHEMA, RS_TYPE, RS_SCHEMA_TYPE_VALUE))

if mapping.get(RS_TYPE) != RS_MAPPING_TYPE_VALUE:
raise InvalidClientRequest(request.identifier,
request.reqId,
"'{}' field must reference a mapping with {}={}".format(
RS_CRED_DEF_MAPPING, RS_TYPE, RS_MAPPING_TYPE_VALUE))
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
rich_schema_mapping_request, make_rich_schema_object_exist
from plenum.common.constants import TRUSTEE
from plenum.common.exceptions import InvalidClientRequest
from plenum.common.util import randomString


@pytest.fixture()
Expand Down Expand Up @@ -67,9 +68,9 @@ def test_static_validation_no_field(cred_def_handler, cred_def_req, missing_fiel
cred_def_handler.static_validation(cred_def_req)


def testdynamic_validation_passes(cred_def_handler, cred_def_req,
rich_schema_handler, rich_schema_req,
mapping_handler, mapping_req):
def test_dynamic_validation_passes(cred_def_handler, cred_def_req,
rich_schema_handler, rich_schema_req,
mapping_handler, mapping_req):
add_to_idr(cred_def_handler.database_manager.idr_cache, cred_def_req.identifier, TRUSTEE)
add_to_idr(cred_def_handler.database_manager.idr_cache, cred_def_req.endorser, ENDORSER)

Expand All @@ -79,9 +80,78 @@ def testdynamic_validation_passes(cred_def_handler, cred_def_req,
content = copy.deepcopy(json.loads(cred_def_req.operation[RS_CONTENT]))
content[RS_CRED_DEF_SCHEMA] = rich_schema_req.operation[RS_ID]
content[RS_CRED_DEF_MAPPING] = mapping_req.operation[RS_ID]
cred_def_req.operation[RS_CONTENT] = json.dumps(content)

cred_def_handler.dynamic_validation(cred_def_req, 0)


def test_dynamic_validation_not_existent_schema(cred_def_handler, cred_def_req):
pass
def test_dynamic_validation_not_existent_schema(cred_def_handler, cred_def_req,
mapping_handler, mapping_req):
add_to_idr(cred_def_handler.database_manager.idr_cache, cred_def_req.identifier, TRUSTEE)
add_to_idr(cred_def_handler.database_manager.idr_cache, cred_def_req.endorser, ENDORSER)

make_rich_schema_object_exist(mapping_handler, mapping_req)

content = copy.deepcopy(json.loads(cred_def_req.operation[RS_CONTENT]))
schema_id = randomString()
content[RS_CRED_DEF_SCHEMA] = schema_id
content[RS_CRED_DEF_MAPPING] = mapping_req.operation[RS_ID]
cred_def_req.operation[RS_CONTENT] = json.dumps(content)

with pytest.raises(InvalidClientRequest,
match='Can not find a schema with id={}; please make sure that it has been added to the ledger'.format(
schema_id)):
cred_def_handler.dynamic_validation(cred_def_req, 0)


def test_dynamic_validation_not_existent_mapping(cred_def_handler, cred_def_req,
rich_schema_handler, rich_schema_req):
add_to_idr(cred_def_handler.database_manager.idr_cache, cred_def_req.identifier, TRUSTEE)
add_to_idr(cred_def_handler.database_manager.idr_cache, cred_def_req.endorser, ENDORSER)

make_rich_schema_object_exist(rich_schema_handler, rich_schema_req)

content = copy.deepcopy(json.loads(cred_def_req.operation[RS_CONTENT]))
mapping_id = randomString()
content[RS_CRED_DEF_MAPPING] = mapping_id
content[RS_CRED_DEF_SCHEMA] = rich_schema_req.operation[RS_ID]
cred_def_req.operation[RS_CONTENT] = json.dumps(content)

with pytest.raises(InvalidClientRequest,
match='Can not find a mapping with id={}; please make sure that it has been added to the ledger'.format(
mapping_id)):
cred_def_handler.dynamic_validation(cred_def_req, 0)


def test_dynamic_validation_not_schema_in_schema_field(cred_def_handler, cred_def_req,
mapping_handler, mapping_req):
add_to_idr(cred_def_handler.database_manager.idr_cache, cred_def_req.identifier, TRUSTEE)
add_to_idr(cred_def_handler.database_manager.idr_cache, cred_def_req.endorser, ENDORSER)

make_rich_schema_object_exist(mapping_handler, mapping_req)

content = copy.deepcopy(json.loads(cred_def_req.operation[RS_CONTENT]))
content[RS_CRED_DEF_SCHEMA] = mapping_req.operation[RS_ID]
content[RS_CRED_DEF_MAPPING] = mapping_req.operation[RS_ID]
cred_def_req.operation[RS_CONTENT] = json.dumps(content)
with pytest.raises(InvalidClientRequest,
match="'schema' field must reference a schema with rsType=sch".format(
RS_CRED_DEF_SCHEMA)):
cred_def_handler.dynamic_validation(cred_def_req, 0)

def test_dynamic_validation_not_mapping_in_mapping_field(cred_def_handler, cred_def_req,
rich_schema_handler, rich_schema_req):
add_to_idr(cred_def_handler.database_manager.idr_cache, cred_def_req.identifier, TRUSTEE)
add_to_idr(cred_def_handler.database_manager.idr_cache, cred_def_req.endorser, ENDORSER)

make_rich_schema_object_exist(rich_schema_handler, rich_schema_req)

content = copy.deepcopy(json.loads(cred_def_req.operation[RS_CONTENT]))
content[RS_CRED_DEF_SCHEMA] = rich_schema_req.operation[RS_ID]
content[RS_CRED_DEF_MAPPING] = rich_schema_req.operation[RS_ID]
cred_def_req.operation[RS_CONTENT] = json.dumps(content)

with pytest.raises(InvalidClientRequest,
match="'mapping' field must reference a mapping with rsType=map".format(
RS_CRED_DEF_MAPPING)):
cred_def_handler.dynamic_validation(cred_def_req, 0)

0 comments on commit 46d3e58

Please sign in to comment.