Esempio n. 1
0
def document_post(country_name):
    try:
        target_country = Country(country_name)
    except Exception as e:
        raise BadCountryNameError(e)

    object_lake_repo = ObjectLakeRepo(Config.OBJECT_LAKE_CONN)
    object_acl_repo = ObjectACLRepo(Config.OBJECT_ACL_CONN)

    if len(request.files) == 0:
        raise NoInputFileError()
    elif len(request.files) > 1:
        raise TooManyFilesError(len(request.files))

    # get the first file, whatever way it's called
    file = request.files[list(request.files.keys())[0]]

    use_case = StoreObjectUseCase(
        object_acl_repo=object_acl_repo,
        object_lake_repo=object_lake_repo,
    )

    try:
        multihash = use_case.execute(fobj=file, target_country=target_country)
    except Exception as e:
        logger.exception(e)
        raise InternalServerError(e)

    return Response(json.dumps({
        "multihash": multihash,
    }),
                    mimetype='application/json',
                    status=HTTPStatus.OK)
Esempio n. 2
0
def document_fetch(uri):
    if not URI(uri).is_valid_multihash():
        raise InvalidURIError()

    object_lake_repo = ObjectLakeRepo(Config.OBJECT_LAKE_CONN)
    object_acl_repo = ObjectACLRepo(Config.OBJECT_ACL_CONN)

    use_case = AuthenticatedObjectAccessUseCase(
        object_acl_repo=object_acl_repo,
        object_lake_repo=object_lake_repo,
    )

    try:
        auth_country = Country(request.auth['country'])
    except Exception as e:
        raise BadCountryNameError(e)

    try:
        document_body = use_case.execute(uri, auth_country)
    except Exception as e:
        logger.exception(e)
        raise InternalServerError(e)

    if document_body is not None:
        return Response(
            document_body,
            status=HTTPStatus.OK,
            mimetype='binary/octet-stream',  # TODO: correct mimetype?
            # TODO: some information about the file content?
        )
    else:
        raise DocumentNotFoundError(uri, auth_country)
Esempio n. 3
0
 def _prepare_repos(self):
     self.repos = {
         'object_lake_repo':
         ObjectLakeRepo(self.repo_conf['object_lake']),
         'object_retrieval_repo':
         ObjectRetrievalRepo(self.repo_conf['object_retrieval']),
         'object_acl_repo':
         ObjectACLRepo(self.repo_conf['object_acl']),
     }
Esempio n. 4
0
def document_fetch(uri):
    if not URI(uri).is_valid_multihash():
        raise InvalidURIError()

    object_lake_repo = ObjectLakeRepo(Config.OBJECT_LAKE_CONN)
    object_acl_repo = ObjectACLRepo(Config.OBJECT_ACL_CONN)

    use_case = AuthenticatedObjectAccessUseCase(
        object_acl_repo=object_acl_repo,
        object_lake_repo=object_lake_repo,
    )

    request_auth = getattr(request, "auth", None)
    if request_auth and 'country' in request_auth:
        try:
            auth_country = Country(request_auth['country'])
        except Exception as e:
            raise BadCountryNameError(e)
    else:
        # no auth is provided, trust the GET request
        # assuming JWT will handle it
        # TODO: ensure that the auth provided allows access from that country
        try:
            auth_country = Country(request.args["as_country"])
        except Exception as e:
            raise BadCountryNameError(e)

    try:
        document_body = use_case.execute(uri, auth_country)
    except Exception as e:
        logger.exception(e)
        raise InternalServerError(e)

    if document_body is not None:
        return Response(
            document_body,
            status=HTTPStatus.OK,
            mimetype='binary/octet-stream',  # TODO: correct mimetype?
            # TODO: some information about the file content?
        )
    else:
        raise DocumentNotFoundError(uri, auth_country)
Esempio n. 5
0
def test():
    repo = ObjectACLRepo(CONF)
    repo._unsafe_clear_for_test()
    assert repo._unsafe_is_empty_for_test()

    message = _generate_msg_object()
    obj = str(message.obj)
    receiver = str(message.receiver)

    # testing post actions
    assert repo.post(message)
    assert repo.allow_access_to(obj, receiver)
    # no filters
    with pytest.raises(Exception):
        repo.search()

    # invalid filters
    with pytest.raises(Exception):
        repo.search({'abrakadabra': 'value'})

    search_result = repo.search({'object__eq': obj})
    assert search_result
    assert message.receiver in search_result
    assert not repo.search({'object__eq': 'something_strange'})
Esempio n. 6
0
def document_post(jurisdiction_name):
    """
    ---
    post:
      parameters:
      - in: path
        name: jurisdiction_name
        required: true
        schema:
          type: string
      requestBody:
        content:
          application/binary:
            schema:
                format: binary
                type: string
      responses:
        200:
          content:
            application/json:
              schema:
                properties:
                  multihash:
                    format: uuid
                    type: string
                type: object
          description: Returns document id
    """
    try:
        target_jurisdiction = Jurisdiction(jurisdiction_name)
    except Exception as e:
        raise BadJurisdictionNameError(e)

    object_lake_repo = ObjectLakeRepo(Config.OBJECT_LAKE_CONN)
    object_acl_repo = ObjectACLRepo(Config.OBJECT_ACL_CONN)

    if len(request.files) == 0:
        raise NoInputFileError()
    elif len(request.files) > 1:
        raise TooManyFilesError(len(request.files))

    # get the first file, whatever way it's called
    file = request.files[list(request.files.keys())[0]]

    use_case = StoreObjectUseCase(
        object_acl_repo=object_acl_repo,
        object_lake_repo=object_lake_repo,
    )

    try:
        multihash = use_case.execute(fobj=file,
                                     target_jurisdiction=target_jurisdiction)
    except Exception as e:
        logger.exception(e)
        raise InternalServerError(e)

    return Response(json.dumps({
        "multihash": multihash,
    }),
                    mimetype='application/json',
                    status=HTTPStatus.OK)
Esempio n. 7
0
def document_fetch(uri):
    """
    ---
    get:
      parameters:
      - in: path
        name: uri
        required: true
        schema:
          format: uuid
          type: string
      responses:
        200:
          content:
            application/binary:
              schema:
                format: binary
                type: string
          description: Returns document
    """
    if not URI(uri).is_valid_multihash():
        raise InvalidURIError()

    object_lake_repo = ObjectLakeRepo(Config.OBJECT_LAKE_CONN)
    object_acl_repo = ObjectACLRepo(Config.OBJECT_ACL_CONN)

    use_case = AuthenticatedObjectAccessUseCase(
        object_acl_repo=object_acl_repo,
        object_lake_repo=object_lake_repo,
    )

    request_auth = getattr(request, "auth", None)
    if request_auth and 'jurisdiction' in request_auth:
        try:
            auth_jurisdiction = Jurisdiction(request_auth['jurisdiction'])
        except Exception as e:
            raise BadJurisdictionNameError(e)
    else:
        # no auth is provided, trust the GET request
        # assuming JWT will handle it
        # TODO: ensure that the auth provided allows access from that country
        try:
            auth_jurisdiction = Jurisdiction(request.args["as_jurisdiction"])
        except Exception as e:
            raise BadJurisdictionNameError(e)

    try:
        document_body = use_case.execute(uri, auth_jurisdiction)
    except Exception as e:
        logger.exception(e)
        raise InternalServerError(e)

    if document_body is not None:
        return Response(
            document_body,
            status=HTTPStatus.OK,
            mimetype='binary/octet-stream',  # TODO: correct mimetype?
            # TODO: some information about the file content?
        )
    else:
        raise DocumentNotFoundError(uri, auth_jurisdiction)
Esempio n. 8
0
def test():
    # creating testing versions of all required repos
    message_lake_repo = MessageLakeRepo(MESSAGE_LAKE_REPO_CONF)
    object_acl_repo = ObjectACLRepo(OBJECT_ACL_REPO_CONF)

    bc_inbox_repo = BCInboxRepo(BC_INBOX_REPO_CONF)
    object_retrieval_repo = ObjectRetrievalRepo(OBJECT_RETRIEVAL_REPO_CONF)
    notifications_repo = NotificationsRepo(NOTIFICATIONS_REPO_CONF)

    blockchain_outbox_repo = ApiOutboxRepo(BLOCKCHAIN_OUTBOX_REPO_CONF)

    def clear():
        # clearing repos
        message_lake_repo._unsafe_method__clear()
        object_acl_repo._unsafe_method__clear()

        bc_inbox_repo._unsafe_method__clear()
        object_retrieval_repo._unsafe_method__clear()
        notifications_repo._unsafe_method__clear()

        blockchain_outbox_repo._unsafe_method__clear()

        # test repos are empty
        assert message_lake_repo.is_empty()
        assert object_acl_repo.is_empty()

        assert bc_inbox_repo.is_empty()
        assert object_retrieval_repo.is_empty()
        assert notifications_repo.is_empty()

        assert blockchain_outbox_repo.is_empty()

    clear()

    processor = InboundMessageProcessor(
        bc_inbox_repo_conf=BC_INBOX_REPO_CONF,
        message_lake_repo_conf=MESSAGE_LAKE_REPO_CONF,
        object_acl_repo_conf=OBJECT_ACL_REPO_CONF,
        object_retrieval_repo_conf=OBJECT_RETRIEVAL_REPO_CONF,
        notifications_repo_conf=NOTIFICATIONS_REPO_CONF,
        blockchain_outbox_repo_conf=BLOCKCHAIN_OUTBOX_REPO_CONF)

    # test iter processor returns processor
    assert iter(processor) is processor
    # test processor has no jobs
    assert next(processor) is None

    sender_ref = "AU:xxxx-xxxx-xxxx"
    status = 'received'
    message = _generate_msg_object(sender_ref=sender_ref, status=status)
    message.sender = "CN"

    assert bc_inbox_repo.post(message)

    # testing normal execution received message with sender ref
    assert next(processor) is True
    assert next(processor) is None
    # testing that message is deleted
    assert bc_inbox_repo.is_empty()
    # testing message posted to related repos
    assert not message_lake_repo.is_empty()
    assert not object_acl_repo.is_empty()
    # we can't say it's empty because worker gets values from there
    # assert not object_retrieval_repo.is_empty()
    # received status should not be posted to blockchain
    assert blockchain_outbox_repo.is_empty()

    clear()

    sender_ref = "AU:xxxx-xxxx-xxxx"
    # this one should go to blockchain outbox
    status = 'pending'
    message = _generate_msg_object(sender_ref=sender_ref, status=status)
    message.sender = OUR_JRD
    message.receiver = 'CN'

    assert bc_inbox_repo.post(message)

    # testing normal execution received message with sender ref
    assert next(processor) is True
    assert next(processor) is None
    # testing that message is deleted
    assert bc_inbox_repo.is_empty()
    # testing message posted to related repos
    assert not message_lake_repo.is_empty()
    assert not object_acl_repo.is_empty()

    clear()

    # message without sender ref should fail
    message = _generate_msg_object()
    assert bc_inbox_repo.post(message)

    assert next(processor) is False
    assert next(processor) is None
Esempio n. 9
0
 def _prepare_object_acl_repo(self, conf):
     object_acl_repo_conf = env_s3_config('PROC_OBJECT_ACL_REPO')
     if conf:
         object_acl_repo_conf.update(conf)
     self.object_acl_repo = ObjectACLRepo(object_acl_repo_conf)