예제 #1
0
def run_executable_insert(did, app_did, target_did, target_app_did,
                          executable_body, params):
    r, msg = can_access_vault(target_did, VAULT_ACCESS_WR)
    if not r:
        return None, msg

    executable_body_document = executable_body.get('document', {})
    populate_params_insert_update(did, app_did, executable_body_document,
                                  params)
    created = "created" in executable_body_document.keys()

    options = populate_options_insert_one(executable_body)

    col = get_collection(target_did, target_app_did,
                         executable_body.get('collection'))
    data, err_message = query_insert_one(col,
                                         executable_body,
                                         options,
                                         created=created)
    if err_message:
        return None, err_message
    db_size = get_mongo_database_size(target_did, target_app_did)
    update_vault_db_use_storage_byte(did, db_size)

    return data, None
예제 #2
0
def run_executable_file_upload(did, app_did, target_did, target_app_did,
                               executable_body, params):
    r, msg = can_access_vault(target_did, VAULT_ACCESS_WR)
    if r != SUCCESS:
        return None, msg

    executable_body_path = executable_body.get("path", "")
    if executable_body_path.startswith(f"{SCRIPTING_EXECUTABLE_PARAMS}."):
        v = executable_body_path.replace(f"{SCRIPTING_EXECUTABLE_PARAMS}.", "")
        try:
            v = params[v]
        except Exception as e:
            return None, f"Exception: {str(e)}"
        executable_body_path = v

    if not executable_body_path:
        return None, f"Path cannot be empty"

    full_path_name, err = query_upload_get_filepath(target_did, target_app_did,
                                                    executable_body_path)
    if err:
        return None, f"Exception: Could not upload file. Status={err['status_code']} Error='{err['description']}'"

    content = {
        "document": {
            "file_name": executable_body_path,
            "fileapi_type": "upload"
        }
    }
    col = get_collection(target_did, target_app_did,
                         SCRIPTING_SCRIPT_TEMP_TX_COLLECTION)
    if not col:
        return None, f"collection {SCRIPTING_SCRIPT_TEMP_TX_COLLECTION} does not exist"
    data, err_message = query_insert_one(col, content, {})
    if err_message:
        return None, f"Could not insert data into the database: Err: {err_message}"
    db_size = get_mongo_database_size(target_did, target_app_did)
    update_vault_db_use_storage_byte(target_did, db_size)

    row_id = data.get("inserted_id", None)
    if not row_id:
        return None, f"Could not retrieve the transaction ID. Please try again"

    data = {
        "transaction_id":
        jwt.encode(
            {
                "row_id": row_id,
                "target_did": target_did,
                "target_app_did": target_app_did
            },
            hive_setting.DID_STOREPASS,
            algorithm='HS256')
    }

    return data, None
예제 #3
0
def run_executable_file_download(did, app_did, target_did, target_app_did,
                                 executable_body, params):
    r, msg = can_access_vault(target_did, VAULT_ACCESS_R)
    if not r:
        return None, msg
    executable_body_path = executable_body.get("path", "")
    if executable_body_path.startswith(f"{SCRIPTING_EXECUTABLE_PARAMS}."):
        v = executable_body_path.replace(f"{SCRIPTING_EXECUTABLE_PARAMS}.", "")
        if not (params and params.get(v, None)):
            return None, "Exception: Parameter is not set"
        executable_body_path = params[v]

    if not executable_body_path:
        return None, f"Path cannot be empty"

    content = {
        "document": {
            "file_name": executable_body_path,
            "fileapi_type": "download"
        }
    }
    col = get_collection(target_did, target_app_did,
                         SCRIPTING_SCRIPT_TEMP_TX_COLLECTION)
    if not col:
        return None, f"collection {SCRIPTING_SCRIPT_TEMP_TX_COLLECTION} does not exist"
    data, err_message = query_insert_one(col, content, {})
    if err_message:
        return None, f"Could not insert data into the database: Err: {err_message}"
    db_size = get_mongo_database_size(target_did, target_app_did)
    update_vault_db_use_storage_byte(target_did, db_size)

    row_id = data.get("inserted_id", None)
    if not row_id:
        return None, f"Could not retrieve the transaction ID. Please try again"

    data = {
        "transaction_id":
        jwt.encode(
            {
                "row_id": row_id,
                "target_did": target_did,
                "target_app_did": target_app_did
            },
            hive_setting.DID_STOREPASS,
            algorithm='HS256')
    }

    return data, None
예제 #4
0
    def insert_one(self):
        did, app_id, content, err = post_json_param_pre_proc(self.response, "collection", "document",
                                                             access_vault=VAULT_ACCESS_WR)
        if err:
            return err

        options = populate_options_insert_one(content)

        col = get_collection(did, app_id, content["collection"])
        if not col:
            return self.response.response_err(NOT_FOUND, "collection not exist")

        data, err_message = query_insert_one(col, content, options)
        if err_message:
            return self.response.response_err(INTERNAL_SERVER_ERROR, err_message)

        db_size = get_mongo_database_size(did, app_id)
        update_vault_db_use_storage_byte(did, db_size)
        return self.response.response_ok(data)