def pay_vault_package_order(self): did, app_id, content, err = post_json_param_pre_proc( self.response, "order_id", "pay_txids") if err: return err # if the order is success or have been put txid no more pay again info = get_order_info_by_id(ObjectId(content["order_id"])) if info: if info[VAULT_ORDER_STATE] == VAULT_ORDER_STATE_SUCCESS: return self.response.response_ok( {"message": "order has been effective"}) if info[VAULT_ORDER_TXIDS]: return self.response.response_ok( {"message": "order has been payed no need to pay again"}) # check whether txids have been used by other order which not be canceled for txid in content["pay_txids"]: info_cursor = find_txid(txid) for info_c in info_cursor: if (info_c["_id"] != content["order_id"]) \ and ((info_c[VAULT_ORDER_STATE] != VAULT_ORDER_STATE_CANCELED) \ or (info_c[VAULT_ORDER_DID] != did)): return self.response.response_err( BAD_REQUEST, "txid:" + txid + " has been used") info[VAULT_ORDER_TXIDS] = content["pay_txids"] info[VAULT_ORDER_STATE] = VAULT_ORDER_STATE_WAIT_TX info[VAULT_ORDER_PAY_TIME] = datetime.utcnow().timestamp() update_order_info(info["_id"], info) return self.response.response_ok()
def delete_many(self): did, app_id, content, err = post_json_param_pre_proc( self.response, "collection", "filter", access_vault=VAULT_ACCESS_DEL) if err: return err col = get_collection(did, app_id, content["collection"]) if not col: return self.response.response_err(NOT_FOUND, "collection not exist") try: ret = col.delete_many(convert_oid(content["filter"])) data = { "acknowledged": ret.acknowledged, "deleted_count": ret.deleted_count, } db_size = get_mongo_database_size(did, app_id) update_vault_db_use_storage_byte(did, db_size) return self.response.response_ok(data) except Exception as e: return self.response.response_err(INTERNAL_SERVER_ERROR, "Exception:" + str(e))
def insert_many(self): did, app_id, content, err = post_json_param_pre_proc(self.response, "collection", "document", access_vault=VAULT_ACCESS_WR) if err: return err col = get_collection(did, app_id, content["collection"]) if not col: return self.response.response_err(NOT_FOUND, "collection not exist") options = options_filter(content, ("bypass_document_validation", "ordered")) try: new_document = [] for document in content["document"]: document["created"] = datetime.utcnow() document["modified"] = datetime.utcnow() new_document.append(convert_oid(document)) ret = col.insert_many(new_document, **options) db_size = get_mongo_database_size(did, app_id) update_vault_db_use_storage_byte(did, db_size) data = { "acknowledged": ret.acknowledged, "inserted_ids": [str(_id) for _id in ret.inserted_ids] } return self.response.response_ok(data) except Exception as e: return self.response.response_err(INTERNAL_SERVER_ERROR, "Exception:" + str(e))
def update_many(self): did, app_id, content, err = post_json_param_pre_proc(self.response, "collection", "filter", "update", access_vault=VAULT_ACCESS_WR) if err: return err col = get_collection(did, app_id, content["collection"]) if not col: return self.response.response_err(NOT_FOUND, "collection not exist") options = options_filter(content, ("upsert", "bypass_document_validation")) try: update_set_on_insert = content.get('update').get('$setOnInsert', None) if update_set_on_insert: content["update"]["$setOnInsert"]['created'] = datetime.utcnow() else: content["update"]["$setOnInsert"] = { "created": datetime.utcnow() } if "$set" in content["update"]: content["update"]["$set"]["modified"] = datetime.utcnow() ret = col.update_many(convert_oid(content["filter"]), convert_oid(content["update"], update=True), **options) data = { "acknowledged": ret.acknowledged, "matched_count": ret.matched_count, "modified_count": ret.modified_count, "upserted_id": str(ret.upserted_id) } db_size = get_mongo_database_size(did, app_id) update_vault_db_use_storage_byte(did, db_size) return self.response.response_ok(data) except Exception as e: return self.response.response_err(INTERNAL_SERVER_ERROR, "Exception:" + str(e))
def find_one(self): did, app_id, content, err = post_json_param_pre_proc(self.response, "collection", access_vault=VAULT_ACCESS_R) if err: return err col = get_collection(did, app_id, content["collection"]) if not col: return self.response.response_err(NOT_FOUND, "collection not exist") options = options_filter(content, ("projection", "skip", "sort", "allow_partial_results", "return_key", "show_record_id", "batch_size")) if "sort" in options: sorts = gene_sort(options["sort"]) options["sort"] = sorts try: if "filter" in content: result = col.find_one(convert_oid(content["filter"]), **options) else: result = col.find_one(**options) data = {"items": json.loads(json_util.dumps(result))} return self.response.response_ok(data) except Exception as e: return self.response.response_err(INTERNAL_SERVER_ERROR, "Exception:" + str(e))
def __proc_hive_node_param(self): did, app_id, content, err = post_json_param_pre_proc( self.response, "backup_credential") if err: return None, None, None, err host, backup_token, err = view.h_auth.backup_auth_request(content) if err: return None, None, None, self.response.response_err( UNAUTHORIZED, err) info = get_vault_backup_info(did) if info and info[VAULT_BACKUP_INFO_STATE] != VAULT_BACKUP_STATE_STOP: if info[VAULT_BACKUP_INFO_TIME] < (datetime.utcnow().timestamp() - 60 * 60 * 24): data = dict() data["vault_backup_state"] = info[VAULT_BACKUP_INFO_STATE] return None, None, None, self.response.response_ok(data) upsert_vault_backup_info(did, VAULT_BACKUP_INFO_TYPE_HIVE_NODE, host, backup_token) data, err = self.start_internal_ftp(host + INTER_BACKUP_FTP_START_URL, backup_token) if err: return None, None, None, err backup_service = data["backup_service"] access_token = data["token"] update_vault_backup_info_item(did, VAULT_BACKUP_INFO_FTP, access_token) return did, access_token, backup_service, None
def __proc_google_drive_param(self): did, app_id, content, err = post_json_param_pre_proc( self.response, 'token', 'refresh_token', 'expiry', 'client_id', 'client_secret', access_vault=VAULT_ACCESS_R) if err: return None, None, err info = get_vault_backup_info(did) if info and info[VAULT_BACKUP_INFO_STATE] != VAULT_BACKUP_STATE_STOP: # If sync process more than one day, we think it is failed if info[VAULT_BACKUP_INFO_TIME] < (datetime.utcnow().timestamp() - 60 * 60 * 24): data = dict() data["vault_backup_state"] = info[VAULT_BACKUP_INFO_STATE] return None, None, self.response.response_ok(data) config_data = RcloneTool.get_config_data(content, did) drive_name = HiveBackup.gene_did_google_drive_name(did) RcloneTool.create_rclone_config_file(drive_name, config_data) upsert_vault_backup_info(did, VAULT_BACKUP_INFO_TYPE_GOOGLE_DRIVE, drive_name) return did, drive_name, None
def delete_collection(self): did, app_id, content, err = post_json_param_pre_proc( self.response, "collection", access_vault=VAULT_ACCESS_DEL) if err: return err collection_name = content.get('collection', None) if collection_name is None: return self.response.response_err(BAD_REQUEST, "parameter is null") if hive_setting.MONGO_URI: uri = hive_setting.MONGO_URI connection = MongoClient(uri) else: connection = MongoClient(host=hive_setting.MONGO_HOST, port=hive_setting.MONGO_PORT) db_name = gene_mongo_db_name(did, app_id) db = connection[db_name] try: db.drop_collection(collection_name) db_size = get_mongo_database_size(did, app_id) update_vault_db_use_storage_byte(did, db_size) except CollectionInvalid: pass except Exception as e: return self.response.response_err(INTERNAL_SERVER_ERROR, "Exception:" + str(e)) return self.response.response_ok()
def create_collection(self): did, app_id, content, err = post_json_param_pre_proc( self.response, "collection", access_vault=VAULT_ACCESS_WR) if err: return err collection_name = content.get('collection') if hive_setting.MONGO_URI: uri = hive_setting.MONGO_URI connection = MongoClient(uri) else: connection = MongoClient(host=hive_setting.MONGO_HOST, port=hive_setting.MONGO_PORT) db_name = gene_mongo_db_name(did, app_id) db = connection[db_name] try: col = db.create_collection(collection_name) except CollectionInvalid: data = {"existing": True} return self.response.response_ok(data) except Exception as e: return self.response.response_err(INTERNAL_SERVER_ERROR, "Exception:" + str(e)) return self.response.response_ok()
def create_vault_package_order(self): did, app_id, content, err = post_json_param_pre_proc(self.response) if err: return err if "pricing_name" in content: package_info = PaymentConfig.get_pricing_plan( content["pricing_name"]) if not package_info: return self.response.response_err( NOT_FOUND, "not found pricing_name of:" + content["pricing_name"]) order_id = create_order_info(did, app_id, package_info, order_type=VAULT_ORDER_TYPE_VAULT) return self.response.response_ok({"order_id": str(order_id)}) elif "backup_name" in content: backup_info = PaymentConfig.get_backup_plan(content["backup_name"]) if not backup_info: return self.response.response_err( NOT_FOUND, "not found backup_name of:" + content["backup_name"]) order_id = create_order_info(did, app_id, backup_info, order_type=VAULT_ORDER_TYPE_BACKUP) return self.response.response_ok({"order_id": str(order_id)}) else: return self.response.response_err( BAD_REQUEST, "parameter pricing_name and backup_name is null")
def update_one(self): did, app_id, content, err = post_json_param_pre_proc( self.response, "collection", "filter", "update", access_vault=VAULT_ACCESS_WR) if err: return err options = populate_options_update_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_update_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)
def remove_channel(self): did, app_id, content, err = post_json_param_pre_proc( self.response, "channel_name") if err: return err channel_name = content["channel_name"] pub_remove_channel(did, app_id, channel_name) return self.response.response_ok()
def cancel_vault_package_order(self): did, app_id, content, err = post_json_param_pre_proc(self.response, "order_id") if err: return err order_id = content['order_id'] info = get_order_info_by_id(ObjectId(order_id)) if info[VAULT_ORDER_STATE] == VAULT_ORDER_STATE_WAIT_TX \ or info[VAULT_ORDER_STATE] == VAULT_ORDER_STATE_WAIT_PAY: info[VAULT_ORDER_STATE] = VAULT_ORDER_STATE_CANCELED
def delete(self): did, app_id, content, response = post_json_param_pre_proc(self.response, "path", access_vault=VAULT_ACCESS_DEL) if response is not None: return response _, resp_err = v2_wrapper(self.ipfs_files.delete_file_with_path)(did, app_id, content.get('path')) if resp_err: return resp_err return self.response.response_ok()
def unsubscribe_channel(self): did, app_id, content, err = post_json_param_pre_proc( self.response, "pub_did", "pub_app_id", "channel_name") if err: return err pub_did = content["pub_did"] pub_appid = content["pub_app_id"] channel_name = content["channel_name"] pub_remove_subscribe(pub_did, pub_appid, channel_name, did, app_id) return self.response.response_ok()
def set_script(self): # Request Validation did, app_id, content, err = post_json_param_pre_proc(self.response, "name", "executable", access_vault=VAULT_ACCESS_WR) if err: return err # Anonymity Options content['allowAnonymousUser'] = content.get('allowAnonymousUser', False) content['allowAnonymousApp'] = content.get('allowAnonymousApp', False) logging.debug(f"Registering a script named '{content.get('name')}' with params: DID: '{did}', App DID: '{app_id}', " f"Anonymous User Access: {content['allowAnonymousUser']}, Anonymous App Access: {content['allowAnonymousApp']}") # Anonymity Validation if (content['allowAnonymousUser'] is True) and (content['allowAnonymousApp'] is False): err_message = "Error while validating anonymity options: Cannot set allowAnonymousUser to be True but " \ "allowAnonymousApp to be False as we cannot request an auth to prove an app identity without " \ "proving the user identity" logging.debug(err_message) return self.response.response_err(BAD_REQUEST, err_message) # Data Validation executable = content.get('executable') massage_keys_with_dollar_signs(executable) err_message = self.__executable_validation(executable) if err_message: logging.debug(f"Error while validating executables: {err_message}") return self.response.response_err(BAD_REQUEST, err_message) # Condition Validation condition = content.get('condition', None) if condition: err_message = check_json_param(condition, "condition", args=["type", "name", "body"]) if err_message: logging.debug(f"Error while validating conditions: {err_message}") return self.response.response_err(BAD_REQUEST, err_message) nested_count = self.__count_nested_condition(condition) for count in nested_count.values(): if count >= 5: err_message = "conditions cannot be nested more than 5 times" logging.debug(f"Error while validating conditions: {err_message}") return self.response.response_err(BAD_REQUEST, err_message) is_valid = self.__condition_validation(condition) if not is_valid: err_message = "some of the parameters are not set for 'condition'" logging.debug(f"Error while validating conditions: {err_message}") return self.response.response_err(BAD_REQUEST, err_message) # Create collection "scripts" if it doesn't exist and # create/update script in the database data, err_message = self.__upsert_script_to_db(did, app_id, content) if err_message: return self.response.response_err(INTERNAL_SERVER_ERROR, err_message) return self.response.response_ok(data)
def publish_channel(self): did, app_id, content, err = post_json_param_pre_proc( self.response, "channel_name") if err: return err channel_name = content["channel_name"] channel_id = pub_setup_channel(did, app_id, channel_name) if channel_id: return self.response.response_ok() else: return self.response.response_err( ALREADY_EXIST, f"Channel {channel_name} has exist")
def move(self, is_copy): did, app_id, content, response = post_json_param_pre_proc( self.response, "src_path", "dst_path", access_vault=VAULT_ACCESS_WR) if response is not None: return response src_name = content.get('src_path') src_name = filter_path_root(src_name) dst_name = content.get('dst_path') dst_name = filter_path_root(dst_name) path = get_save_files_path(did, app_id) src_full_path_name = (path / src_name).resolve() dst_full_path_name = (path / dst_name).resolve() if not src_full_path_name.exists(): return self.response.response_err(NOT_FOUND, "src_name not exists") if dst_full_path_name.exists() and dst_full_path_name.is_file(): return self.response.response_err(METHOD_NOT_ALLOWED, "dst_name file exists") dst_parent_folder = dst_full_path_name.parent if not dst_parent_folder.exists(): if not create_full_path_dir(dst_parent_folder): return self.response.response_err( INTERNAL_SERVER_ERROR, "make dst parent path dir error") try: if is_copy: if src_full_path_name.is_file(): shutil.copy2(src_full_path_name.as_posix(), dst_full_path_name.as_posix()) file_size = os.path.getsize(dst_full_path_name.as_posix()) inc_vault_file_use_storage_byte(did, file_size) else: shutil.copytree(src_full_path_name.as_posix(), dst_full_path_name.as_posix()) dir_size = 0.0 get_dir_size(dst_full_path_name.as_posix(), dir_size) inc_vault_file_use_storage_byte(did, dir_size) else: shutil.move(src_full_path_name.as_posix(), dst_full_path_name.as_posix()) except Exception as e: return self.response.response_err(INTERNAL_SERVER_ERROR, "Exception:" + str(e)) return self.response.response_ok()
def move(self, is_copy): did, app_id, content, response = post_json_param_pre_proc(self.response, "src_path", "dst_path", access_vault=VAULT_ACCESS_WR) if response is not None: return response _, resp_err = v2_wrapper(self.ipfs_files.move_copy_file)( did, app_id, content.get('src_path'), content.get('dst_path'), is_copy=is_copy ) if resp_err: return resp_err return self.response.response_ok()
def pop_messages(self): did, app_id, content, err = post_json_param_pre_proc( self.response, "pub_did", "pub_app_id", "channel_name", "message_limit") if err: return err pub_did = content["pub_did"] pub_appid = content["pub_app_id"] channel_name = content["channel_name"] limit = int(content["message_limit"]) message_list = sub_pop_messages(pub_did, pub_appid, channel_name, did, app_id, limit) data = {"messages": message_list} return self.response.response_ok(data)
def push_message(self): did, app_id, content, err = post_json_param_pre_proc( self.response, "channel_name", "message") if err: return err channel_name = content["channel_name"] message = content["message"] info = pub_get_channel(did, app_id, channel_name) if not info: return self.response.response_err( NOT_FOUND, f"There is no channel:{channel_name} published by did:{did}, appid:{app_id}" ) pubsub_push_message(did, app_id, channel_name, message, datetime.utcnow().timestamp()) return self.response.response_ok()
def find_many(self): did, app_id, content, err = post_json_param_pre_proc(self.response, "collection", access_vault=VAULT_ACCESS_R) if err: return err options = populate_options_find_many(content) col = get_collection(did, app_id, content.get('collection')) if not col: return self.response.response_err(NOT_FOUND, "collection not exist") data, err_message = query_find_many(col, content, options) if err_message: return self.response.response_err(INTERNAL_SERVER_ERROR, err_message) return self.response.response_ok(data)
def subscribe_channel(self): did, app_id, content, err = post_json_param_pre_proc( self.response, "pub_did", "pub_app_id", "channel_name") if err: return err pub_did = content["pub_did"] pub_appid = content["pub_app_id"] channel_name = content["channel_name"] info = pub_get_channel(pub_did, pub_appid, channel_name) if not info: return self.response.response_err( NOT_FOUND, f"There is no channel:{channel_name} published by did:{pub_did}, appid:{pub_appid}" ) pub_add_subscriber(pub_did, pub_appid, channel_name, did, app_id) return self.response.response_ok()
def delete(self): did, app_id, content, response = post_json_param_pre_proc( self.response, "path", access_vault=VAULT_ACCESS_DEL) if response is not None: return response filename = content.get('path') filename = filter_path_root(filename) path = get_save_files_path(did, app_id) file_full_name = (path / filename).resolve() if file_full_name.exists(): if file_full_name.is_dir(): dir_size = 0.0 get_dir_size(file_full_name.as_posix(), dir_size) shutil.rmtree(file_full_name) inc_vault_file_use_storage_byte(did, -dir_size) else: file_size = os.path.getsize(file_full_name.as_posix()) file_full_name.unlink() inc_vault_file_use_storage_byte(did, -file_size) return self.response.response_ok()