Ejemplo n.º 1
0
    def update_document(self, collection_name, json_body, is_update_one):
        user_did, app_did, col = self.__get_collection(collection_name,
                                                       VAULT_ACCESS_WR)
        update = json_body["update"]
        if "$set" in update:
            update["$set"]["modified"] = datetime.utcnow()
        if is_update_one:
            ret = col.update_one(
                convert_oid(json_body["filter"]),
                convert_oid(update, update=True),
                **options_filter(json_body,
                                 ("upsert", "bypass_document_validation")))
        else:
            ret = col.update_many(
                convert_oid(json_body["filter"]),
                convert_oid(update, update=True),
                **options_filter(json_body,
                                 ("upsert", "bypass_document_validation")))

        update_used_storage_for_mongodb_data(
            user_did, get_mongo_database_size(user_did, app_did))
        return {
            "acknowledged": ret.acknowledged,
            "matched_count": ret.matched_count,
            "modified_count": ret.modified_count,
            "upserted_id": str(ret.upserted_id) if ret.upserted_id else None
        }
Ejemplo n.º 2
0
 def delete_collection(self, collection_name):
     user_did, app_did = check_auth_and_vault(VAULT_ACCESS_DEL)
     cli.delete_collection(user_did,
                           app_did,
                           collection_name,
                           is_check_exist=False)
     update_used_storage_for_mongodb_data(
         user_did, get_mongo_database_size(user_did, app_did))
Ejemplo n.º 3
0
 def delete_document(self, collection_name, col_filter, is_delete_one):
     user_did, app_did, col = self.__get_collection(collection_name,
                                                    VAULT_ACCESS_WR)
     if is_delete_one:
         col.delete_one(convert_oid(col_filter))
     else:
         col.delete_many(convert_oid(col_filter))
     update_used_storage_for_mongodb_data(
         user_did, get_mongo_database_size(user_did, app_did))
Ejemplo n.º 4
0
    def _create_transaction(self, permission, action_type):
        cli.check_vault_access(self.get_target_did(), permission)

        body = self.get_populated_body()
        anonymous_url = ''
        if self.is_ipfs:
            if action_type == 'download':
                metadata = self.ipfs_files.get_file_metadata(
                    self.get_target_did(), self.get_target_app_did(),
                    body['path'])
                anonymous_url = self.ipfs_files.get_ipfs_file_access_url(
                    metadata)
        else:
            _, err = query_upload_get_filepath(self.get_target_did(),
                                               self.get_target_app_did(),
                                               body['path'])
            if err:
                raise BadRequestException(
                    msg='Cannot get file full path with error message: ' +
                    str(err))

        # INFO: Do not consider run script twice.
        data = cli.insert_one(
            self.get_target_did(),
            self.get_target_app_did(),
            SCRIPTING_SCRIPT_TEMP_TX_COLLECTION, {
                "document": {
                    "file_name": body['path'],
                    "fileapi_type": action_type
                },
                'anonymous':
                self.script.anonymous_app and self.script.anonymous_user
            },
            create_on_absence=True)
        if not data.get('inserted_id', None):
            raise BadRequestException('Cannot retrieve the transaction ID.')

        update_used_storage_for_mongodb_data(
            self.get_target_did(),
            get_mongo_database_size(self.get_target_did(),
                                    self.get_target_app_did()))

        result = {
            "transaction_id":
            jwt.encode(
                {
                    "row_id": data.get('inserted_id', None),
                    "target_did": self.get_target_did(),
                    "target_app_did": self.get_target_app_did()
                },
                hive_setting.DID_STOREPASS,
                algorithm='HS256')
        }
        if action_type == 'download' and self.is_ipfs and self.script.anonymous_app and self.script.anonymous_user:
            result['anonymous_url'] = anonymous_url
        return result
Ejemplo n.º 5
0
    def set_script(self, script_name):
        user_did, app_did = check_auth_and_vault(VAULT_ACCESS_WR)

        json_data = request.get_json(force=True, silent=True)
        Script.validate_script_data(json_data)

        result = self.__upsert_script_to_database(script_name, json_data,
                                                  user_did, app_did)
        update_used_storage_for_mongodb_data(
            user_did, get_mongo_database_size(user_did, app_did))
        return result
Ejemplo n.º 6
0
    def delete_script(self, script_name):
        user_did, app_did = check_auth_and_vault(VAULT_ACCESS_DEL)

        col = cli.get_user_collection(user_did, app_did,
                                      SCRIPTING_SCRIPT_COLLECTION)
        if not col:
            raise NotFoundException(NotFoundException.SCRIPT_NOT_FOUND,
                                    'The script collection does not exist.')

        ret = col.delete_many({'name': script_name})
        if ret.deleted_count > 0:
            update_used_storage_for_mongodb_data(
                user_did, get_mongo_database_size(user_did, app_did))
Ejemplo n.º 7
0
    def execute(self):
        cli.check_vault_access(self.get_target_did(), VAULT_ACCESS_DEL)

        data = cli.delete_one(self.get_target_did(), self.get_target_app_did(),
                              self.get_collection_name(),
                              self.get_populated_filter())

        update_used_storage_for_mongodb_data(
            self.get_did(),
            get_mongo_database_size(self.get_target_did(),
                                    self.get_target_app_did()))

        return self.get_output_data(data)
Ejemplo n.º 8
0
 def insert_document(self, collection_name, json_body):
     user_did, app_did, col = self.__get_collection(collection_name,
                                                    VAULT_ACCESS_WR)
     documents = []
     for document in json_body["document"]:
         document["created"] = datetime.utcnow()
         document["modified"] = datetime.utcnow()
         documents.append(convert_oid(document))
     ret = col.insert_many(
         documents,
         **options_filter(json_body,
                          ("bypass_document_validation", "ordered")))
     update_used_storage_for_mongodb_data(
         user_did, get_mongo_database_size(user_did, app_did))
     return {
         "acknowledged": ret.acknowledged,
         "inserted_ids": [str(_id) for _id in ret.inserted_ids]
     }
Ejemplo n.º 9
0
    def handle_transaction(self, transaction_id, is_download=False):
        check_auth_and_vault(
            VAULT_ACCESS_R if is_download else VAULT_ACCESS_WR)

        # check by transaction id
        row_id, target_did, target_app_did = self.parse_transaction_id(
            transaction_id)
        col_filter = {"_id": ObjectId(row_id)}
        trans = cli.find_one(target_did, target_app_did,
                             SCRIPTING_SCRIPT_TEMP_TX_COLLECTION, col_filter)
        if not trans:
            raise BadRequestException("Cannot find the transaction by id.")

        # executing uploading or downloading
        data = None
        logging.info(
            f'handle transaction by id: is_ipfs={self.is_ipfs}, '
            f'is_download={is_download}, file_name={trans["document"]["file_name"]}'
        )
        if self.is_ipfs:
            if is_download:
                data = self.ipfs_files.download_file_with_path(
                    target_did, target_app_did, trans['document']['file_name'])
            else:
                self.ipfs_files.upload_file_with_path(
                    target_did, target_app_did, trans['document']['file_name'])
        else:
            if is_download:
                data = self.get_files().download_file_by_did(
                    target_did, target_app_did, trans['document']['file_name'])
            else:
                self.get_files().upload_file_by_did(
                    target_did, target_app_did, trans['document']['file_name'])

        # recalculate the storage usage of the database
        cli.delete_one(target_did, target_app_did,
                       SCRIPTING_SCRIPT_TEMP_TX_COLLECTION, col_filter)
        update_used_storage_for_mongodb_data(
            target_did, get_mongo_database_size(target_did, target_app_did))

        # return the content of the file
        return data
Ejemplo n.º 10
0
    def execute(self):
        cli.check_vault_access(self.get_target_did(), VAULT_ACCESS_WR)

        document = self.get_document()
        msg = populate_with_params_values(self.get_did(), self.get_app_id(),
                                          document, self.get_params())
        if msg:
            raise BadRequestException(
                msg='Cannot get parameter value for the executable document: '
                + msg)

        data = cli.insert_one(self.get_target_did(), self.get_target_app_did(),
                              self.get_collection_name(), document,
                              populate_options_insert_one(self.body))

        update_used_storage_for_mongodb_data(
            self.get_did(),
            get_mongo_database_size(self.get_target_did(),
                                    self.get_target_app_did()))

        return self.get_output_data(data)
Ejemplo n.º 11
0
 def update_vault_dbs_usage(self, user_did, size):
     update_used_storage_for_mongodb_data(user_did, size)