def add_supported_app_to_agents(username, customer_name, uri, method, agent_id=None): if agent_id: agent_info = get_agent_info(agent_id) apps_info = ( fetch_apps_data_by_os_code( agent_info[AgentKey.OsCode], customer_name, collection=AppCollections.SupportedApps, ) ) if len(apps_info) > 0: for app_info in apps_info: app_id = app_info.get(SupportedAppsKey.AppId) file_data = fetch_file_data(app_id) add_file_data( app_id, file_data, agent_id ) agent_info_to_insert = ( { SupportedAppsPerAgentKey.AgentId: agent_id, SupportedAppsPerAgentKey.AppId: app_id, SupportedAppsPerAgentKey.Status: AVAILABLE, SupportedAppsPerAgentKey.CustomerName: customer_name, SupportedAppsPerAgentKey.InstallDate: r.epoch_time(0.0) } ) insert_app_data( agent_info_to_insert, collection=AppCollections.SupportedAppsPerAgent )
def get_data(self): try: agent_data = get_agent_info(agent_id) agent_data[AgentKey.LastAgentUpdate] = ( int(agent_data[AgentKey.LastAgentUpdate].strftime('%s')) ) if agent_data: agent_data['tags'] = get_tags_by_agent_id(agent_id=self.agent_id) agent_data[CommonAppKeys.BASIC_RV_STATS] = ( get_all_app_stats_by_agentid(self.agent_id) ) status = ( GenericResults( self.username, uri, method ).information_retrieved(agent_data, 1) ) logger.info(status['message']) else: status = ( GenericResults( self.username, uri, method ).invalid_id(self.agent_id, 'agent_id') ) logger.info(status['message']) except Exception as e: agent_data = None status = ( GenericResults( self.username, uri, method ).something_broke(self.agent_id, 'agents', e) ) logger.error(status['message']) return(status)
def add_supported_app_to_agents(username, customer_name, uri, method, agent_id=None): if agent_id: agent_info = get_agent_info(agent_id) apps_info = (fetch_apps_data_by_os_code( agent_info[AgentKey.OsCode], customer_name, collection=AppCollections.SupportedApps, )) if len(apps_info) > 0: for app_info in apps_info: app_id = app_info.get(SupportedAppsKey.AppId) file_data = fetch_file_data(app_id) add_file_data(app_id, file_data, agent_id) agent_info_to_insert = ({ SupportedAppsPerAgentKey.AgentId: agent_id, SupportedAppsPerAgentKey.AppId: app_id, SupportedAppsPerAgentKey.Status: AVAILABLE, SupportedAppsPerAgentKey.CustomerName: customer_name, SupportedAppsPerAgentKey.InstallDate: r.epoch_time(0.0) }) insert_app_data( agent_info_to_insert, collection=AppCollections.SupportedAppsPerAgent)
def delete_agent(self, uri, method, conn=None): try: agent_info = get_agent_info(self.agent_id) if agent_info: ( r .table(AgentsCollection) .get(self.agent_id) .delete() .run(conn) ) ( r .table(HardwarePerAgentCollection) .get_all(self.agent_id, index=HardwarePerAgentIndexes.AgentId) .delete() .run(conn) ) ( r .table(TagsPerAgentCollection) .get_all(self.agent_id, index=TagsPerAgentIndexes.AgentId) .delete() .run(conn) ) rv_q = Queue('delete_agent', connection=rq_pool) rv_q.enqueue_call( func=remove_all_app_data_for_agent, args=(self.agent_id,), timeout=3600, ) status = ( GenericResults( self.username, uri, method ).object_deleted(self.agent_id, 'agents') ) logger.info(status['message']) else: status = ( GenericResults( self.username, uri, method ).invalid_id(self.agent_id, 'agents') ) logger.info(status['message']) except Exception as e: status = ( GenericResults( self.username, uri, method ).something_broke(self.agent_id, 'agents', e) ) logger.exception(status['message']) return(status)
def query_by_name(self, name, conn=None): try: agent = get_agent_info(self.agent_id) if agent: base = ( r .table(self.CurrentAppsPerAgentCollection) .get_all(self.agent_id, index=self.CurrentAppsPerAgentIndexes.AgentId) .eq_join(self.CurrentAppsPerAgentKey.AppId, r.table(self.CurrentAppsCollection)) .zip() ) if self.show_hidden == CommonKeys.NO: base = base.filter({self.CurrentAppsKey.Hidden: CommonKeys.NO}) packages = list( base .filter(lambda x: x[self.CurrentAppsKey.Name].match("(?i)"+name)) .map(self.map_hash) .order_by(self.sort(self.sort_key)) .skip(self.offset) .limit(self.count) .run(conn) ) pkg_count = ( base .filter(lambda x: x[self.CurrentAppsKey.Name].match("(?i)"+name)) .count() .run(conn) ) return_status = ( GenericResults( self.username, self.uri, self.method ).information_retrieved(packages, pkg_count) ) else: return_status = ( GenericResults( self.username, self.uri, self.method ).invalid_id(self.agent_id, 'agents') ) except Exception as e: return_status = ( GenericResults( self.username, self.uri, self.method ).something_broke( "Package Searching went haywire", 'os_updates', e ) ) logger.exception(e) return(return_status)
def get_all_supported_apps_for_agent(agent_id): agent = get_agent_info(agent_id) apps = fetch_apps_data_by_os_code(agent[AgentKey.OsCode], collection=DownloadCollections.LatestDownloadedSupported) if apps: try: update_apps = IncomingSupportedApps() update_apps.update_agents_with_supported(apps, [agent]) except Exception as e: logger.exception(e)
def _get_agent_data(self, agent_id): #if self.agent_data: # if self.agent_data.get(AgentKey.AgentId) == agent_id: # return self.agent_data # else: # logger.info( # "Agent id: {0} did not match agent id of set agent data: {0}" # .format(agent_id, self.agent_data) # ) return get_agent_info(agent_id)
def add_custom_app_to_agents(username, customer_name, uri, method, file_data, agent_id=None, app_id=None): if app_id and not agent_id: app_info = (fetch_app_data(app_id, collection=AppCollections.CustomApps)) agent_ids = get_all_agent_ids(customer_name, agent_os=app_info[AgentKey.OsCode]) if len(agent_ids) > 0: for agentid in agent_ids: add_file_data(app_id, file_data, agent_id) agent_info_to_insert = ({ CustomAppsPerAgentKey.AgentId: agentid, CustomAppsPerAgentKey.AppId: app_id, CustomAppsPerAgentKey.Status: CommonAppKeys.AVAILABLE, CustomAppsPerAgentKey.CustomerName: customer_name, CustomAppsPerAgentKey.InstallDate: r.epoch_time(0.0) }) insert_app_data(agent_info_to_insert, collection=AppCollections.CustomAppsPerAgent) if agent_id and not app_id: agent_info = get_agent_info(agent_id) apps_info = fetch_apps_data_by_os_code( agent_info[AgentKey.OsCode], customer_name, collection=AppCollections.CustomApps) for app_info in apps_info: app_id = app_info.get(CustomAppsKey.AppId) file_data = fetch_file_data(app_id) add_file_data(app_id, file_data, agent_id) agent_info_to_insert = { CustomAppsPerAgentKey.AgentId: agent_id, CustomAppsPerAgentKey.AppId: app_id, CustomAppsPerAgentKey.Status: CommonAppKeys.AVAILABLE, CustomAppsPerAgentKey.CustomerName: customer_name, CustomAppsPerAgentKey.InstallDate: r.epoch_time(0.0) } insert_app_data(agent_info_to_insert, collection=AppCollections.CustomAppsPerAgent)
def get_all_supported_apps_for_agent(agent_id): agent = get_agent_info(agent_id) apps = fetch_apps_data_by_os_code( agent[AgentKey.OsCode], collection=DownloadCollections.LatestDownloadedSupported) if apps: try: update_apps = IncomingSupportedApps() update_apps.update_agents_with_supported(apps, [agent]) except Exception as e: logger.exception(e)
def add_custom_app_to_agents(username, customer_name, uri, method, file_data, agent_id=None, app_id=None): if app_id and not agent_id: app_info = ( fetch_app_data( app_id, collection=AppCollections.CustomApps ) ) agent_ids = get_all_agent_ids(customer_name, agent_os=app_info[AgentKey.OsCode]) if len(agent_ids) > 0: for agentid in agent_ids: add_file_data(app_id, file_data, agent_id) agent_info_to_insert = ( { CustomAppsPerAgentKey.AgentId: agentid, CustomAppsPerAgentKey.AppId: app_id, CustomAppsPerAgentKey.Status: CommonAppKeys.AVAILABLE, CustomAppsPerAgentKey.CustomerName: customer_name, CustomAppsPerAgentKey.InstallDate: r.epoch_time(0.0) } ) insert_app_data( agent_info_to_insert, collection=AppCollections.CustomAppsPerAgent ) if agent_id and not app_id: agent_info = get_agent_info(agent_id) apps_info = fetch_apps_data_by_os_code( agent_info[AgentKey.OsCode], customer_name, collection=AppCollections.CustomApps ) for app_info in apps_info: app_id = app_info.get(CustomAppsKey.AppId) file_data = fetch_file_data(app_id) add_file_data(app_id, file_data, agent_id) agent_info_to_insert = { CustomAppsPerAgentKey.AgentId: agent_id, CustomAppsPerAgentKey.AppId: app_id, CustomAppsPerAgentKey.Status: CommonAppKeys.AVAILABLE, CustomAppsPerAgentKey.CustomerName: customer_name, CustomAppsPerAgentKey.InstallDate: r.epoch_time(0.0) } insert_app_data( agent_info_to_insert, collection=AppCollections.CustomAppsPerAgent )
def __init__(self, username, agent_id, operation_id, success, error=None, status_code=None, uri=None, method=None): """ Args: username (str): The name of the user who made this api call agent_id (str): 36 character UUID of the agent. operation_id (str): 36 character UUID of the operation. success (str): true or false. Kwargs: error (str): The error message, if the operation failed. status_code (int): The exact status of this operation. uri (str): The uri which was called for the results. method (str): The method used to call this api. Basic Usage: >>> from vFense.operations.results import OperationResults >>> username = '******' >>> operation_id = '8fed3dc7-33d4-4278-9bd4-398a68bf7f22' >>> agent_id = 'db6bf07-c5da-4494-93bb-109db205ca64' >>> success = 'true' >>> results = OperationResults( username, agent_id, operation_id, success ) """ self.agent_id = agent_id self.operation_id = operation_id self.username = username self.uri = uri self.method = method self.agent_data = get_agent_info(self.agent_id) self.operation_data = get_agent_operation(self.operation_id) self.customer_name = self.agent_data[AgentKey.CustomerName] self.date_now = DbTime.time_now() self.begining_of_time = DbTime.begining_of_time() self.error = error self.success = success self.status_code = status_code self.operation = (AgentOperation( self.username, self.customer_name, ))
def __init__( self, username, agent_id, operation_id, success, error=None, status_code=None, uri=None, method=None ): """ Args: username (str): The name of the user who made this api call agent_id (str): 36 character UUID of the agent. operation_id (str): 36 character UUID of the operation. success (str): true or false. Kwargs: error (str): The error message, if the operation failed. status_code (int): The exact status of this operation. uri (str): The uri which was called for the results. method (str): The method used to call this api. Basic Usage: >>> from vFense.operations.results import OperationResults >>> username = '******' >>> operation_id = '8fed3dc7-33d4-4278-9bd4-398a68bf7f22' >>> agent_id = 'db6bf07-c5da-4494-93bb-109db205ca64' >>> success = 'true' >>> results = OperationResults( username, agent_id, operation_id, success ) """ self.agent_id = agent_id self.operation_id = operation_id self.username = username self.uri = uri self.method = method self.agent_data = get_agent_info(self.agent_id) self.operation_data = get_agent_operation(self.operation_id) self.customer_name = self.agent_data[AgentKey.CustomerName] self.date_now = DbTime.time_now() self.begining_of_time = DbTime.begining_of_time() self.error = error self.success = success self.status_code = status_code self.operation = ( AgentOperation( self.username, self.customer_name, ) )
def filter_by_status(self, pkg_status, conn=None): try: agent = get_agent_info(self.agent_id) if agent: if pkg_status in CommonAppKeys.ValidPackageStatuses: base = ( r .table(self.CurrentAppsPerAgentCollection, use_outdated=True) .get_all( [pkg_status, self.agent_id], index=self.CurrentAppsPerAgentIndexes.StatusAndAgentId) .eq_join(self.CurrentAppsPerAgentKey.AppId, r.table(self.CurrentAppsCollection)) .zip() ) if self.show_hidden == CommonKeys.NO: base = base.filter({self.CurrentAppsKey.Hidden: CommonKeys.NO}) packages = list( base .map(self.map_hash) .order_by(self.sort(self.sort_key)) .skip(self.offset) .limit(self.count) .run(conn) ) pkg_count = ( base .count() .run(conn) ) return_status = ( GenericResults( self.username, self.uri, self.method ).information_retrieved(packages, pkg_count) ) else: return_status = ( PackageResults( self.username, self.uri, self.method ).invalid_status(self.agent_id, pkg_status) ) else: return_status = ( GenericResults( self.username, self.uri, self.method ).invalid_id(self.agent_id, 'agents') ) except Exception as e: return_status = ( GenericResults( self.username, self.uri, self.method ).something_broke( "Package Searching went haywire", 'os_updates', e ) ) logger.exception(e) return(return_status)
def get_result_uris(agent_id, user_name=None, uri=None, method=None): """Returns back a dictionary with all of the agent api calls for an agent. Args: agent_id (str): 36 character UUID Kwargs: user_name (str): The name of the user who called this function. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Basic Usage: >>> from vFense.receiver.agent_uris import get_result_uris >>> agent_id = 'd4119b36-fe3c-4973-84c7-e8e3d72a3e02' >>> get_result_uris(agent_id) Returns: Dictionary { "count": 1, "uri": null, "rv_status_code": 1001, "http_method": null, "http_status": 200, "message": "None - data was retrieved", "data": { "check_in": { "response_uri": "rvl/v1/d4119b36-fe3c-4973-84c7-e8e3d72a3e02/core/checkin", "request_method": "GET" }, "startup": { "response_uri": "rvl/v1/d4119b36-fe3c-4973-84c7-e8e3d72a3e02/core/results/startup", "request_method": "PUT" }, "shutdown": { "response_uri": "rvl/v1/d4119b36-fe3c-4973-84c7-e8e3d72a3e02/core/results/shutdown", "request_method": "PUT" }, } } """ status_code = 0 status = get_result_uris.func_name + ' - ' result_uris = _get_result_uris_dict(agent_id) agent_exist = get_agent_info(agent_id, AgentKey.AgentId) if agent_exist: generic_status_code = GenericCodes.InformationRetrieved vfense_status_code = GenericCodes.InformationRetrieved msg = 'response uris retrieved successfully for agent_id %s' % \ (agent_id) count = 1 else: generic_status_code = GenericCodes.InvalidId vfense_status_code = AgentFailureCodes.AgentsDoesNotExist result_uris = {} msg = 'invalid agent_id %s' % (agent_id) count = 0 results = { ApiResultKeys.DB_STATUS_CODE: status_code, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.COUNT: count, ApiResultKeys.DATA: result_uris, ApiResultKeys.USERNAME: user_name, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } return(results)