Esempio n. 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 r != SUCCESS:
        return None, msg

    executable_body_document = executable_body.get('document', {})
    err_message = populate_with_params_values(did, app_did,
                                              executable_body_document, params)
    if err_message:
        return None, err_message
    created = "created" in executable_body_document.keys()

    options = populate_options_insert_one(executable_body)
    err_message = populate_with_params_values(did, app_did, options, params)
    if err_message:
        return None, err_message

    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
Esempio n. 2
0
def run_executable_update(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_filter = executable_body.get('filter', {})
    err_message = populate_with_params_values(did, app_did,
                                              executable_body_filter, params)
    if err_message:
        return None, err_message

    executable_body_update = executable_body.get('update').get('$set')
    err_message = populate_with_params_values(did, app_did,
                                              executable_body_update, params)
    if err_message:
        return None, err_message

    options = populate_options_update_one(executable_body)
    err_message = populate_with_params_values(did, app_did, options, params)
    if err_message:
        return None, err_message

    col = get_collection(target_did, target_app_did,
                         executable_body.get('collection'))
    data, err_message = query_update_one(col, executable_body, options)
    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
Esempio n. 3
0
def run_executable_find(did, app_did, target_did, target_app_did,
                        executable_body, params):
    r, msg = can_access_vault(target_did, VAULT_ACCESS_R)
    if r != SUCCESS:
        return None, msg

    executable_body_filter = executable_body.get('filter', {})
    err_message = populate_with_params_values(did, app_did,
                                              executable_body_filter, params)
    if err_message:
        return None, err_message

    options = populate_options_find_many(executable_body)
    err_message = populate_with_params_values(did, app_did, options, params)
    if err_message:
        return None, err_message

    col = get_collection(target_did, target_app_did,
                         executable_body.get('collection'))
    if not col:
        return None, f'Can not find the collection {executable_body.get("collection")}'
    data, err_message = query_find_many(col, executable_body, options)
    if err_message:
        return None, err_message

    return data, None
Esempio n. 4
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
Esempio n. 5
0
def run_executable_file_hash(did, app_did, target_did, target_app_did,
                             executable_body, params):
    r, msg = can_access_vault(target_did, VAULT_ACCESS_R)
    if r != SUCCESS:
        return None, msg

    executable_body_path = executable_body.get("path", "")
    name = ""
    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)}"
        name = v

    data, err = query_hash(target_did, target_app_did, name)
    if err:
        return None, f"Exception: Could not get hash of file. Status={err['status_code']} Error='{err['description']}'"
    return data, None